blob: fa39b79e3979e49c386d0df8142ab4de3223b788 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Vladimir Markof4f2daa2017-03-20 18:26:59 +000019#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000020#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080022#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010024#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080025#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010026#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080027#include "intrinsics.h"
28#include "intrinsics_arm64.h"
Vladimir Markof4f2daa2017-03-20 18:26:59 +000029#include "linker/arm64/relative_patcher_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010030#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000032#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010033#include "thread.h"
34#include "utils/arm64/assembler_arm64.h"
35#include "utils/assembler.h"
36#include "utils/stack_checks.h"
37
Scott Wakeling97c72b72016-06-24 16:19:36 +010038using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000039using vixl::ExactAssemblyScope;
40using vixl::CodeBufferCheckScope;
41using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010042
43#ifdef __
44#error "ARM64 Codegen VIXL macro-assembler macro already defined."
45#endif
46
Alexandre Rames5319def2014-10-23 10:03:10 +010047namespace art {
48
Roland Levillain22ccc3a2015-11-24 13:10:05 +000049template<class MirrorType>
50class GcRoot;
51
Alexandre Rames5319def2014-10-23 10:03:10 +010052namespace arm64 {
53
Alexandre Ramesbe919d92016-08-23 18:33:36 +010054using helpers::ARM64EncodableConstantOrRegister;
55using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080056using helpers::CPURegisterFrom;
57using helpers::DRegisterFrom;
58using helpers::FPRegisterFrom;
59using helpers::HeapOperand;
60using helpers::HeapOperandFrom;
61using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010062using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080063using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080064using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010065using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080066using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010067using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080068using helpers::LocationFrom;
69using helpers::OperandFromMemOperand;
70using helpers::OutputCPURegister;
71using helpers::OutputFPRegister;
72using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010073using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080074using helpers::RegisterFrom;
75using helpers::StackOperandFrom;
76using helpers::VIXLRegCodeFromART;
77using helpers::WRegisterFrom;
78using helpers::XRegisterFrom;
79
Alexandre Rames5319def2014-10-23 10:03:10 +010080static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000081// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080082// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
83// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000084static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010085
Vladimir Markof4f2daa2017-03-20 18:26:59 +000086// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
87// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
88// For the Baker read barrier implementation using link-generated thunks we need to split
89// the offset explicitly.
90constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
91
92// Flags controlling the use of link-time generated thunks for Baker read barriers.
Vladimir Markod1ef8732017-04-18 13:55:13 +010093constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
Vladimir Marko66d691d2017-04-07 17:53:39 +010094constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
Vladimir Markod1ef8732017-04-18 13:55:13 +010095constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
Vladimir Markof4f2daa2017-03-20 18:26:59 +000096
97// Some instructions have special requirements for a temporary, for example
98// LoadClass/kBssEntry and LoadString/kBssEntry for Baker read barrier require
99// temp that's not an R0 (to avoid an extra move) and Baker read barrier field
100// loads with large offsets need a fixed register to limit the number of link-time
101// thunks we generate. For these and similar cases, we want to reserve a specific
102// register that's neither callee-save nor an argument register. We choose x15.
103inline Location FixedTempLocation() {
104 return Location::RegisterLocation(x15.GetCode());
105}
106
Alexandre Rames5319def2014-10-23 10:03:10 +0100107inline Condition ARM64Condition(IfCondition cond) {
108 switch (cond) {
109 case kCondEQ: return eq;
110 case kCondNE: return ne;
111 case kCondLT: return lt;
112 case kCondLE: return le;
113 case kCondGT: return gt;
114 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700115 case kCondB: return lo;
116 case kCondBE: return ls;
117 case kCondA: return hi;
118 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100119 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000120 LOG(FATAL) << "Unreachable";
121 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100122}
123
Vladimir Markod6e069b2016-01-18 11:11:01 +0000124inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
125 // The ARM64 condition codes can express all the necessary branches, see the
126 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
127 // There is no dex instruction or HIR that would need the missing conditions
128 // "equal or unordered" or "not equal".
129 switch (cond) {
130 case kCondEQ: return eq;
131 case kCondNE: return ne /* unordered */;
132 case kCondLT: return gt_bias ? cc : lt /* unordered */;
133 case kCondLE: return gt_bias ? ls : le /* unordered */;
134 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
135 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
136 default:
137 LOG(FATAL) << "UNREACHABLE";
138 UNREACHABLE();
139 }
140}
141
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000142Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000143 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
144 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
145 // but we use the exact registers for clarity.
146 if (return_type == Primitive::kPrimFloat) {
147 return LocationFrom(s0);
148 } else if (return_type == Primitive::kPrimDouble) {
149 return LocationFrom(d0);
150 } else if (return_type == Primitive::kPrimLong) {
151 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100152 } else if (return_type == Primitive::kPrimVoid) {
153 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000154 } else {
155 return LocationFrom(w0);
156 }
157}
158
Alexandre Rames5319def2014-10-23 10:03:10 +0100159Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000160 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100161}
162
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100163// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
164#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700165#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100166
Zheng Xuda403092015-04-24 17:35:39 +0800167// Calculate memory accessing operand for save/restore live registers.
168static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100169 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800170 int64_t spill_offset,
171 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100172 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
173 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
174 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800175 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100176 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800177 codegen->GetNumberOfFloatingPointRegisters()));
178
Vladimir Marko804b03f2016-09-14 16:26:36 +0100179 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100180 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
181 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800182
183 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
184 UseScratchRegisterScope temps(masm);
185
186 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100187 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
188 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800189 int64_t reg_size = kXRegSizeInBytes;
190 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
191 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100192 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800193 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
194 // If the offset does not fit in the instruction's immediate field, use an alternate register
195 // to compute the base address(float point registers spill base address).
196 Register new_base = temps.AcquireSameSizeAs(base);
197 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
198 base = new_base;
199 spill_offset = -core_spill_size;
200 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
201 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
202 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
203 }
204
205 if (is_save) {
206 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
207 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
208 } else {
209 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
210 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
211 }
212}
213
214void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800215 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100216 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
217 for (uint32_t i : LowToHighBits(core_spills)) {
218 // If the register holds an object, update the stack mask.
219 if (locations->RegisterContainsObject(i)) {
220 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800221 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100222 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
223 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
224 saved_core_stack_offsets_[i] = stack_offset;
225 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800226 }
227
Vladimir Marko804b03f2016-09-14 16:26:36 +0100228 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
229 for (uint32_t i : LowToHighBits(fp_spills)) {
230 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
231 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
232 saved_fpu_stack_offsets_[i] = stack_offset;
233 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800234 }
235
Vladimir Marko804b03f2016-09-14 16:26:36 +0100236 SaveRestoreLiveRegistersHelper(codegen,
237 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800238 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
239}
240
241void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100242 SaveRestoreLiveRegistersHelper(codegen,
243 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800244 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
245}
246
Alexandre Rames5319def2014-10-23 10:03:10 +0100247class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
248 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000249 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100250
Alexandre Rames67555f72014-11-18 10:55:16 +0000251 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100252 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000253 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100254
Alexandre Rames5319def2014-10-23 10:03:10 +0100255 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000256 if (instruction_->CanThrowIntoCatchBlock()) {
257 // Live registers will be restored in the catch block if caught.
258 SaveLiveRegisters(codegen, instruction_->GetLocations());
259 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000260 // We're moving two locations to locations that could overlap, so we need a parallel
261 // move resolver.
262 InvokeRuntimeCallingConvention calling_convention;
263 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100264 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
265 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000266 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
267 ? kQuickThrowStringBounds
268 : kQuickThrowArrayBounds;
269 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100270 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800271 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100272 }
273
Alexandre Rames8158f282015-08-07 10:26:17 +0100274 bool IsFatal() const OVERRIDE { return true; }
275
Alexandre Rames9931f312015-06-19 14:47:01 +0100276 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
277
Alexandre Rames5319def2014-10-23 10:03:10 +0100278 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100279 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
280};
281
Alexandre Rames67555f72014-11-18 10:55:16 +0000282class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
283 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000284 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000285
286 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
287 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
288 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000289 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800290 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000291 }
292
Alexandre Rames8158f282015-08-07 10:26:17 +0100293 bool IsFatal() const OVERRIDE { return true; }
294
Alexandre Rames9931f312015-06-19 14:47:01 +0100295 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
296
Alexandre Rames67555f72014-11-18 10:55:16 +0000297 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
299};
300
301class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
302 public:
303 LoadClassSlowPathARM64(HLoadClass* cls,
304 HInstruction* at,
305 uint32_t dex_pc,
Vladimir Markoea4c1262017-02-06 19:59:33 +0000306 bool do_clinit,
307 vixl::aarch64::Register bss_entry_temp = vixl::aarch64::Register(),
308 vixl::aarch64::Label* bss_entry_adrp_label = nullptr)
309 : SlowPathCodeARM64(at),
310 cls_(cls),
311 dex_pc_(dex_pc),
312 do_clinit_(do_clinit),
313 bss_entry_temp_(bss_entry_temp),
314 bss_entry_adrp_label_(bss_entry_adrp_label) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000315 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
316 }
317
318 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000319 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000320 Location out = locations->Out();
321 constexpr bool call_saves_everything_except_r0_ip0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexandre Rames67555f72014-11-18 10:55:16 +0000322 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
323
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000324 InvokeRuntimeCallingConvention calling_convention;
325 // For HLoadClass/kBssEntry/kSaveEverything, the page address of the entry is in a temp
326 // register, make sure it's not clobbered by the call or by saving/restoring registers.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000327 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
328 bool is_load_class_bss_entry =
329 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000330 if (is_load_class_bss_entry) {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000331 DCHECK(bss_entry_temp_.IsValid());
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000332 DCHECK(!bss_entry_temp_.Is(calling_convention.GetRegisterAt(0)));
333 DCHECK(
334 !UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(bss_entry_temp_));
Vladimir Markoea4c1262017-02-06 19:59:33 +0000335 }
336
Alexandre Rames67555f72014-11-18 10:55:16 +0000337 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000338 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000339
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000340 dex::TypeIndex type_index = cls_->GetTypeIndex();
341 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000342 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
343 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000344 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800345 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100346 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800347 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100348 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800349 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000350
351 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 if (out.IsValid()) {
353 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000354 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000355 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000357 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000358 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000359 if (is_load_class_bss_entry) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000360 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000361 const DexFile& dex_file = cls_->GetDexFile();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000362 if (call_saves_everything_except_r0_ip0) {
363 // The class entry page address was preserved in bss_entry_temp_ thanks to kSaveEverything.
364 } else {
365 // For non-Baker read barrier, we need to re-calculate the address of the class entry page.
366 bss_entry_adrp_label_ = arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
367 arm64_codegen->EmitAdrpPlaceholder(bss_entry_adrp_label_, bss_entry_temp_);
368 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000369 vixl::aarch64::Label* strp_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +0000370 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label_);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000371 {
372 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
373 __ Bind(strp_label);
374 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Markoea4c1262017-02-06 19:59:33 +0000375 MemOperand(bss_entry_temp_, /* offset placeholder */ 0));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000376 }
377 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000378 __ B(GetExitLabel());
379 }
380
Alexandre Rames9931f312015-06-19 14:47:01 +0100381 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
382
Alexandre Rames67555f72014-11-18 10:55:16 +0000383 private:
384 // The class this slow path will load.
385 HLoadClass* const cls_;
386
Alexandre Rames67555f72014-11-18 10:55:16 +0000387 // The dex PC of `at_`.
388 const uint32_t dex_pc_;
389
390 // Whether to initialize the class.
391 const bool do_clinit_;
392
Vladimir Markoea4c1262017-02-06 19:59:33 +0000393 // For HLoadClass/kBssEntry, the temp register and the label of the ADRP where it was loaded.
394 vixl::aarch64::Register bss_entry_temp_;
395 vixl::aarch64::Label* bss_entry_adrp_label_;
396
Alexandre Rames67555f72014-11-18 10:55:16 +0000397 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
398};
399
Vladimir Markoaad75c62016-10-03 08:46:48 +0000400class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
401 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100402 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
403 : SlowPathCodeARM64(instruction),
404 temp_(temp),
405 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000406
407 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
408 LocationSummary* locations = instruction_->GetLocations();
409 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
410 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
411
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000412 InvokeRuntimeCallingConvention calling_convention;
413 // Make sure `temp_` is not clobbered by the call or by saving/restoring registers.
414 DCHECK(temp_.IsValid());
415 DCHECK(!temp_.Is(calling_convention.GetRegisterAt(0)));
416 DCHECK(!UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(temp_));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100417
Vladimir Markoaad75c62016-10-03 08:46:48 +0000418 __ Bind(GetEntryLabel());
419 SaveLiveRegisters(codegen, locations);
420
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000421 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
422 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000423 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
424 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
425 Primitive::Type type = instruction_->GetType();
426 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
427
428 RestoreLiveRegisters(codegen, locations);
429
430 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000431 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100432 if (!kUseReadBarrier || kUseBakerReadBarrier) {
433 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
434 } else {
435 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
436 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
437 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
438 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000439 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100440 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000441 {
442 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
443 __ Bind(strp_label);
444 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100445 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000446 }
447
448 __ B(GetExitLabel());
449 }
450
451 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
452
453 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100454 const Register temp_;
455 vixl::aarch64::Label* adrp_label_;
456
Vladimir Markoaad75c62016-10-03 08:46:48 +0000457 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
458};
459
Alexandre Rames5319def2014-10-23 10:03:10 +0100460class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
461 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000462 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100463
Alexandre Rames67555f72014-11-18 10:55:16 +0000464 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
465 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100466 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000467 if (instruction_->CanThrowIntoCatchBlock()) {
468 // Live registers will be restored in the catch block if caught.
469 SaveLiveRegisters(codegen, instruction_->GetLocations());
470 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000471 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
472 instruction_,
473 instruction_->GetDexPc(),
474 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800475 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100476 }
477
Alexandre Rames8158f282015-08-07 10:26:17 +0100478 bool IsFatal() const OVERRIDE { return true; }
479
Alexandre Rames9931f312015-06-19 14:47:01 +0100480 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
481
Alexandre Rames5319def2014-10-23 10:03:10 +0100482 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100483 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
484};
485
486class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
487 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100488 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000489 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100490
Alexandre Rames67555f72014-11-18 10:55:16 +0000491 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100492 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000493 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100494 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100495 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000496 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800497 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100498 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000499 if (successor_ == nullptr) {
500 __ B(GetReturnLabel());
501 } else {
502 __ B(arm64_codegen->GetLabelOf(successor_));
503 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100504 }
505
Scott Wakeling97c72b72016-06-24 16:19:36 +0100506 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100507 DCHECK(successor_ == nullptr);
508 return &return_label_;
509 }
510
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100511 HBasicBlock* GetSuccessor() const {
512 return successor_;
513 }
514
Alexandre Rames9931f312015-06-19 14:47:01 +0100515 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
516
Alexandre Rames5319def2014-10-23 10:03:10 +0100517 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100518 // If not null, the block to branch to after the suspend check.
519 HBasicBlock* const successor_;
520
521 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100522 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100523
524 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
525};
526
Alexandre Rames67555f72014-11-18 10:55:16 +0000527class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
528 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000529 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000530 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000531
532 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000533 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800534
Alexandre Rames3e69f162014-12-10 10:36:50 +0000535 DCHECK(instruction_->IsCheckCast()
536 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
537 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100538 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000539
Alexandre Rames67555f72014-11-18 10:55:16 +0000540 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000541
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000542 if (!is_fatal_) {
543 SaveLiveRegisters(codegen, locations);
544 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000545
546 // We're moving two locations to locations that could overlap, so we need a parallel
547 // move resolver.
548 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800549 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800550 LocationFrom(calling_convention.GetRegisterAt(0)),
551 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800552 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800553 LocationFrom(calling_convention.GetRegisterAt(1)),
554 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000555 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000556 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800557 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000558 Primitive::Type ret_type = instruction_->GetType();
559 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
560 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
561 } else {
562 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800563 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
564 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000565 }
566
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000567 if (!is_fatal_) {
568 RestoreLiveRegisters(codegen, locations);
569 __ B(GetExitLabel());
570 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000571 }
572
Alexandre Rames9931f312015-06-19 14:47:01 +0100573 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100574 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100575
Alexandre Rames67555f72014-11-18 10:55:16 +0000576 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000577 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000578
Alexandre Rames67555f72014-11-18 10:55:16 +0000579 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
580};
581
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700582class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
583 public:
Aart Bik42249c32016-01-07 15:33:50 -0800584 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000585 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700586
587 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800588 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700589 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100590 LocationSummary* locations = instruction_->GetLocations();
591 SaveLiveRegisters(codegen, locations);
592 InvokeRuntimeCallingConvention calling_convention;
593 __ Mov(calling_convention.GetRegisterAt(0),
594 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000595 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100596 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700597 }
598
Alexandre Rames9931f312015-06-19 14:47:01 +0100599 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
600
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700601 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700602 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
603};
604
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100605class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
606 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000607 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100608
609 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
610 LocationSummary* locations = instruction_->GetLocations();
611 __ Bind(GetEntryLabel());
612 SaveLiveRegisters(codegen, locations);
613
614 InvokeRuntimeCallingConvention calling_convention;
615 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
616 parallel_move.AddMove(
617 locations->InAt(0),
618 LocationFrom(calling_convention.GetRegisterAt(0)),
619 Primitive::kPrimNot,
620 nullptr);
621 parallel_move.AddMove(
622 locations->InAt(1),
623 LocationFrom(calling_convention.GetRegisterAt(1)),
624 Primitive::kPrimInt,
625 nullptr);
626 parallel_move.AddMove(
627 locations->InAt(2),
628 LocationFrom(calling_convention.GetRegisterAt(2)),
629 Primitive::kPrimNot,
630 nullptr);
631 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
632
633 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000634 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100635 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
636 RestoreLiveRegisters(codegen, locations);
637 __ B(GetExitLabel());
638 }
639
640 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
641
642 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100643 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
644};
645
Zheng Xu3927c8b2015-11-18 17:46:25 +0800646void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
647 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000648 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800649
650 // We are about to use the assembler to place literals directly. Make sure we have enough
651 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000652 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
653 num_entries * sizeof(int32_t),
654 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800655
656 __ Bind(&table_start_);
657 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
658 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100659 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800660 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100661 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800662 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
663 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
664 Literal<int32_t> literal(jump_offset);
665 __ place(&literal);
666 }
667}
668
Roland Levillain54f869e2017-03-06 13:54:11 +0000669// Abstract base class for read barrier slow paths marking a reference
670// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000671//
Roland Levillain54f869e2017-03-06 13:54:11 +0000672// Argument `entrypoint` must be a register location holding the read
673// barrier marking runtime entry point to be invoked.
674class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
675 protected:
676 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
677 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000678 DCHECK(kEmitCompilerReadBarrier);
679 }
680
Roland Levillain54f869e2017-03-06 13:54:11 +0000681 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000682
Roland Levillain54f869e2017-03-06 13:54:11 +0000683 // Generate assembly code calling the read barrier marking runtime
684 // entry point (ReadBarrierMarkRegX).
685 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000686 // No need to save live registers; it's taken care of by the
687 // entrypoint. Also, there is no need to update the stack mask,
688 // as this runtime call will not trigger a garbage collection.
689 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
690 DCHECK_NE(ref_.reg(), LR);
691 DCHECK_NE(ref_.reg(), WSP);
692 DCHECK_NE(ref_.reg(), WZR);
693 // IP0 is used internally by the ReadBarrierMarkRegX entry point
694 // as a temporary, it cannot be the entry point's input/output.
695 DCHECK_NE(ref_.reg(), IP0);
696 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
697 // "Compact" slow path, saving two moves.
698 //
699 // Instead of using the standard runtime calling convention (input
700 // and output in W0):
701 //
702 // W0 <- ref
703 // W0 <- ReadBarrierMark(W0)
704 // ref <- W0
705 //
706 // we just use rX (the register containing `ref`) as input and output
707 // of a dedicated entrypoint:
708 //
709 // rX <- ReadBarrierMarkRegX(rX)
710 //
711 if (entrypoint_.IsValid()) {
712 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
713 __ Blr(XRegisterFrom(entrypoint_));
714 } else {
715 // Entrypoint is not already loaded, load from the thread.
716 int32_t entry_point_offset =
717 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
718 // This runtime call does not require a stack map.
719 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
720 }
721 }
722
723 // The location (register) of the marked object reference.
724 const Location ref_;
725
726 // The location of the entrypoint if it is already loaded.
727 const Location entrypoint_;
728
Roland Levillain54f869e2017-03-06 13:54:11 +0000729 private:
730 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
731};
732
Alexandre Rames5319def2014-10-23 10:03:10 +0100733// Slow path marking an object reference `ref` during a read
734// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000735// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100736//
737// This means that after the execution of this slow path, `ref` will
738// always be up-to-date, but `obj.field` may not; i.e., after the
739// flip, `ref` will be a to-space reference, but `obj.field` will
740// probably still be a from-space reference (unless it gets updated by
741// another thread, or if another thread installed another object
742// reference (different from `ref`) in `obj.field`).
743//
744// If `entrypoint` is a valid location it is assumed to already be
745// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000746// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000747class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100748 public:
749 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
750 Location ref,
751 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000752 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100753 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100754 }
755
756 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
757
758 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000759 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100760 DCHECK(locations->CanCall());
761 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000762 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000763 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
764 << "Unexpected instruction in read barrier marking slow path: "
765 << instruction_->DebugName();
766
767 __ Bind(GetEntryLabel());
768 GenerateReadBarrierMarkRuntimeCall(codegen);
769 __ B(GetExitLabel());
770 }
771
772 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000773 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
774};
775
Roland Levillain54f869e2017-03-06 13:54:11 +0000776// Slow path loading `obj`'s lock word, loading a reference from
777// object `*(obj + offset + (index << scale_factor))` into `ref`, and
778// marking `ref` if `obj` is gray according to the lock word (Baker
779// read barrier). The field `obj.field` in the object `obj` holding
780// this reference does not get updated by this slow path after marking
781// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
782// below for that).
783//
784// This means that after the execution of this slow path, `ref` will
785// always be up-to-date, but `obj.field` may not; i.e., after the
786// flip, `ref` will be a to-space reference, but `obj.field` will
787// probably still be a from-space reference (unless it gets updated by
788// another thread, or if another thread installed another object
789// reference (different from `ref`) in `obj.field`).
790//
791// Argument `entrypoint` must be a register location holding the read
792// barrier marking runtime entry point to be invoked.
793class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
794 public:
795 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
796 Location ref,
797 Register obj,
798 uint32_t offset,
799 Location index,
800 size_t scale_factor,
801 bool needs_null_check,
802 bool use_load_acquire,
803 Register temp,
804 Location entrypoint)
805 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
806 obj_(obj),
807 offset_(offset),
808 index_(index),
809 scale_factor_(scale_factor),
810 needs_null_check_(needs_null_check),
811 use_load_acquire_(use_load_acquire),
812 temp_(temp) {
813 DCHECK(kEmitCompilerReadBarrier);
814 DCHECK(kUseBakerReadBarrier);
815 }
816
817 const char* GetDescription() const OVERRIDE {
818 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
819 }
820
821 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
822 LocationSummary* locations = instruction_->GetLocations();
823 DCHECK(locations->CanCall());
824 DCHECK(ref_.IsRegister()) << ref_;
825 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
826 DCHECK(obj_.IsW());
827 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100828 DCHECK(instruction_->IsInstanceFieldGet() ||
829 instruction_->IsStaticFieldGet() ||
830 instruction_->IsArrayGet() ||
831 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100832 instruction_->IsInstanceOf() ||
833 instruction_->IsCheckCast() ||
834 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
835 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
836 << "Unexpected instruction in read barrier marking slow path: "
837 << instruction_->DebugName();
838 // The read barrier instrumentation of object ArrayGet
839 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000840 // instruction.
841 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000842 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
843
Roland Levillain54f869e2017-03-06 13:54:11 +0000844 // Temporary register `temp_`, used to store the lock word, must
845 // not be IP0 nor IP1, as we may use them to emit the reference
846 // load (in the call to GenerateRawReferenceLoad below), and we
847 // need the lock word to still be in `temp_` after the reference
848 // load.
849 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
850 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
851
Alexandre Rames5319def2014-10-23 10:03:10 +0100852 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000853
854 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
855 // inserted after the original load. However, in fast path based
856 // Baker's read barriers, we need to perform the load of
857 // mirror::Object::monitor_ *before* the original reference load.
858 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000859 // The slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100860 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000861 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
862 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
863 // HeapReference<mirror::Object> ref = *src; // Original reference load.
864 // bool is_gray = (rb_state == ReadBarrier::GrayState());
865 // if (is_gray) {
866 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
867 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000868 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000869 // Note: the original implementation in ReadBarrier::Barrier is
870 // slightly more complex as it performs additional checks that we do
871 // not do here for performance reasons.
872
873 // /* int32_t */ monitor = obj->monitor_
874 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
875 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
876 if (needs_null_check_) {
877 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100878 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000879 // /* LockWord */ lock_word = LockWord(monitor)
880 static_assert(sizeof(LockWord) == sizeof(int32_t),
881 "art::LockWord and int32_t have different sizes.");
882
883 // Introduce a dependency on the lock_word including rb_state,
884 // to prevent load-load reordering, and without using
885 // a memory barrier (which would be more expensive).
886 // `obj` is unchanged by this operation, but its value now depends
887 // on `temp`.
888 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
889
890 // The actual reference load.
891 // A possible implicit null check has already been handled above.
892 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
893 arm64_codegen->GenerateRawReferenceLoad(instruction_,
894 ref_,
895 obj_,
896 offset_,
897 index_,
898 scale_factor_,
899 /* needs_null_check */ false,
900 use_load_acquire_);
901
902 // Mark the object `ref` when `obj` is gray.
903 //
904 // if (rb_state == ReadBarrier::GrayState())
905 // ref = ReadBarrier::Mark(ref);
906 //
907 // Given the numeric representation, it's enough to check the low bit of the rb_state.
908 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
909 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
910 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
911 GenerateReadBarrierMarkRuntimeCall(codegen);
912
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000913 __ B(GetExitLabel());
914 }
915
916 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000917 // The register containing the object holding the marked object reference field.
918 Register obj_;
919 // The offset, index and scale factor to access the reference in `obj_`.
920 uint32_t offset_;
921 Location index_;
922 size_t scale_factor_;
923 // Is a null check required?
924 bool needs_null_check_;
925 // Should this reference load use Load-Acquire semantics?
926 bool use_load_acquire_;
927 // A temporary register used to hold the lock word of `obj_`.
928 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000929
Roland Levillain54f869e2017-03-06 13:54:11 +0000930 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000931};
932
Roland Levillain54f869e2017-03-06 13:54:11 +0000933// Slow path loading `obj`'s lock word, loading a reference from
934// object `*(obj + offset + (index << scale_factor))` into `ref`, and
935// marking `ref` if `obj` is gray according to the lock word (Baker
936// read barrier). If needed, this slow path also atomically updates
937// the field `obj.field` in the object `obj` holding this reference
938// after marking (contrary to
939// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
940// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100941//
942// This means that after the execution of this slow path, both `ref`
943// and `obj.field` will be up-to-date; i.e., after the flip, both will
944// hold the same to-space reference (unless another thread installed
945// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000946//
Roland Levillain54f869e2017-03-06 13:54:11 +0000947// Argument `entrypoint` must be a register location holding the read
948// barrier marking runtime entry point to be invoked.
949class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
950 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100951 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000952 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(HInstruction* instruction,
953 Location ref,
954 Register obj,
955 uint32_t offset,
956 Location index,
957 size_t scale_factor,
958 bool needs_null_check,
959 bool use_load_acquire,
960 Register temp,
961 Location entrypoint)
962 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100963 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000964 offset_(offset),
965 index_(index),
966 scale_factor_(scale_factor),
967 needs_null_check_(needs_null_check),
968 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000969 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100970 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000971 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100972 }
973
974 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000975 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100976 }
977
978 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
979 LocationSummary* locations = instruction_->GetLocations();
980 Register ref_reg = WRegisterFrom(ref_);
981 DCHECK(locations->CanCall());
982 DCHECK(ref_.IsRegister()) << ref_;
983 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000984 DCHECK(obj_.IsW());
985 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
986
987 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100988 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
989 << "Unexpected instruction in read barrier marking and field updating slow path: "
990 << instruction_->DebugName();
991 DCHECK(instruction_->GetLocations()->Intrinsified());
992 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000993 DCHECK_EQ(offset_, 0u);
994 DCHECK_EQ(scale_factor_, 0u);
995 DCHECK_EQ(use_load_acquire_, false);
996 // The location of the offset of the marked reference field within `obj_`.
997 Location field_offset = index_;
998 DCHECK(field_offset.IsRegister()) << field_offset;
999
1000 // Temporary register `temp_`, used to store the lock word, must
1001 // not be IP0 nor IP1, as we may use them to emit the reference
1002 // load (in the call to GenerateRawReferenceLoad below), and we
1003 // need the lock word to still be in `temp_` after the reference
1004 // load.
1005 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1006 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001007
1008 __ Bind(GetEntryLabel());
1009
Roland Levillainff487002017-03-07 16:50:01 +00001010 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM64's:
1011 //
1012 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1013 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1014 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1015 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1016 // if (is_gray) {
1017 // old_ref = ref;
1018 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1019 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1020 // }
1021
Roland Levillain54f869e2017-03-06 13:54:11 +00001022 // /* int32_t */ monitor = obj->monitor_
1023 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1024 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
1025 if (needs_null_check_) {
1026 codegen->MaybeRecordImplicitNullCheck(instruction_);
1027 }
1028 // /* LockWord */ lock_word = LockWord(monitor)
1029 static_assert(sizeof(LockWord) == sizeof(int32_t),
1030 "art::LockWord and int32_t have different sizes.");
1031
1032 // Introduce a dependency on the lock_word including rb_state,
1033 // to prevent load-load reordering, and without using
1034 // a memory barrier (which would be more expensive).
1035 // `obj` is unchanged by this operation, but its value now depends
1036 // on `temp`.
1037 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
1038
1039 // The actual reference load.
1040 // A possible implicit null check has already been handled above.
1041 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1042 arm64_codegen->GenerateRawReferenceLoad(instruction_,
1043 ref_,
1044 obj_,
1045 offset_,
1046 index_,
1047 scale_factor_,
1048 /* needs_null_check */ false,
1049 use_load_acquire_);
1050
1051 // Mark the object `ref` when `obj` is gray.
1052 //
1053 // if (rb_state == ReadBarrier::GrayState())
1054 // ref = ReadBarrier::Mark(ref);
1055 //
1056 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1057 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1058 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1059 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1060
1061 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001062 // Note that we cannot use IP to save the old reference, as IP is
1063 // used internally by the ReadBarrierMarkRegX entry point, and we
1064 // need the old reference after the call to that entry point.
1065 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1066 __ Mov(temp_.W(), ref_reg);
1067
Roland Levillain54f869e2017-03-06 13:54:11 +00001068 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001069
1070 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001071 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001072 //
1073 // Note that this field could also hold a different object, if
1074 // another thread had concurrently changed it. In that case, the
1075 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1076 // (CAS) operation below would abort the CAS, leaving the field
1077 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001078 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001079 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001080
1081 // Update the the holder's field atomically. This may fail if
1082 // mutator updates before us, but it's OK. This is achieved
1083 // using a strong compare-and-set (CAS) operation with relaxed
1084 // memory synchronization ordering, where the expected value is
1085 // the old reference and the desired value is the new reference.
1086
1087 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1088 UseScratchRegisterScope temps(masm);
1089
1090 // Convenience aliases.
1091 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001092 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001093 Register expected = temp_.W();
1094 Register value = ref_reg;
1095 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1096 Register tmp_value = temps.AcquireW(); // Value in memory.
1097
1098 __ Add(tmp_ptr, base.X(), Operand(offset));
1099
1100 if (kPoisonHeapReferences) {
1101 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1102 if (value.Is(expected)) {
1103 // Do not poison `value`, as it is the same register as
1104 // `expected`, which has just been poisoned.
1105 } else {
1106 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1107 }
1108 }
1109
1110 // do {
1111 // tmp_value = [tmp_ptr] - expected;
1112 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1113
Roland Levillain24a4d112016-10-26 13:10:46 +01001114 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001115 __ Bind(&loop_head);
1116 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1117 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001118 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001119 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1120 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001121 __ B(&exit_loop);
1122 __ Bind(&comparison_failed);
1123 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001124 __ Bind(&exit_loop);
1125
1126 if (kPoisonHeapReferences) {
1127 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1128 if (value.Is(expected)) {
1129 // Do not unpoison `value`, as it is the same register as
1130 // `expected`, which has just been unpoisoned.
1131 } else {
1132 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1133 }
1134 }
1135
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001136 __ B(GetExitLabel());
1137 }
1138
1139 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001140 // The register containing the object holding the marked object reference field.
1141 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001142 // The offset, index and scale factor to access the reference in `obj_`.
1143 uint32_t offset_;
1144 Location index_;
1145 size_t scale_factor_;
1146 // Is a null check required?
1147 bool needs_null_check_;
1148 // Should this reference load use Load-Acquire semantics?
1149 bool use_load_acquire_;
1150 // A temporary register used to hold the lock word of `obj_`; and
1151 // also to hold the original reference value, when the reference is
1152 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001153 const Register temp_;
1154
Roland Levillain54f869e2017-03-06 13:54:11 +00001155 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001156};
1157
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001158// Slow path generating a read barrier for a heap reference.
1159class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1160 public:
1161 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1162 Location out,
1163 Location ref,
1164 Location obj,
1165 uint32_t offset,
1166 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001167 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001168 out_(out),
1169 ref_(ref),
1170 obj_(obj),
1171 offset_(offset),
1172 index_(index) {
1173 DCHECK(kEmitCompilerReadBarrier);
1174 // If `obj` is equal to `out` or `ref`, it means the initial object
1175 // has been overwritten by (or after) the heap object reference load
1176 // to be instrumented, e.g.:
1177 //
1178 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001179 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001180 //
1181 // In that case, we have lost the information about the original
1182 // object, and the emitted read barrier cannot work properly.
1183 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1184 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1185 }
1186
1187 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1188 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1189 LocationSummary* locations = instruction_->GetLocations();
1190 Primitive::Type type = Primitive::kPrimNot;
1191 DCHECK(locations->CanCall());
1192 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001193 DCHECK(instruction_->IsInstanceFieldGet() ||
1194 instruction_->IsStaticFieldGet() ||
1195 instruction_->IsArrayGet() ||
1196 instruction_->IsInstanceOf() ||
1197 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001198 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001199 << "Unexpected instruction in read barrier for heap reference slow path: "
1200 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001201 // The read barrier instrumentation of object ArrayGet
1202 // instructions does not support the HIntermediateAddress
1203 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001204 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001205 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001206
1207 __ Bind(GetEntryLabel());
1208
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001209 SaveLiveRegisters(codegen, locations);
1210
1211 // We may have to change the index's value, but as `index_` is a
1212 // constant member (like other "inputs" of this slow path),
1213 // introduce a copy of it, `index`.
1214 Location index = index_;
1215 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001216 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001217 if (instruction_->IsArrayGet()) {
1218 // Compute the actual memory offset and store it in `index`.
1219 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
1220 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1221 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1222 // We are about to change the value of `index_reg` (see the
1223 // calls to vixl::MacroAssembler::Lsl and
1224 // vixl::MacroAssembler::Mov below), but it has
1225 // not been saved by the previous call to
1226 // art::SlowPathCode::SaveLiveRegisters, as it is a
1227 // callee-save register --
1228 // art::SlowPathCode::SaveLiveRegisters does not consider
1229 // callee-save registers, as it has been designed with the
1230 // assumption that callee-save registers are supposed to be
1231 // handled by the called function. So, as a callee-save
1232 // register, `index_reg` _would_ eventually be saved onto
1233 // the stack, but it would be too late: we would have
1234 // changed its value earlier. Therefore, we manually save
1235 // it here into another freely available register,
1236 // `free_reg`, chosen of course among the caller-save
1237 // registers (as a callee-save `free_reg` register would
1238 // exhibit the same problem).
1239 //
1240 // Note we could have requested a temporary register from
1241 // the register allocator instead; but we prefer not to, as
1242 // this is a slow path, and we know we can find a
1243 // caller-save register that is available.
1244 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1245 __ Mov(free_reg.W(), index_reg);
1246 index_reg = free_reg;
1247 index = LocationFrom(index_reg);
1248 } else {
1249 // The initial register stored in `index_` has already been
1250 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1251 // (as it is not a callee-save register), so we can freely
1252 // use it.
1253 }
1254 // Shifting the index value contained in `index_reg` by the scale
1255 // factor (2) cannot overflow in practice, as the runtime is
1256 // unable to allocate object arrays with a size larger than
1257 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1258 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1259 static_assert(
1260 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1261 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1262 __ Add(index_reg, index_reg, Operand(offset_));
1263 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001264 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1265 // intrinsics, `index_` is not shifted by a scale factor of 2
1266 // (as in the case of ArrayGet), as it is actually an offset
1267 // to an object field within an object.
1268 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001269 DCHECK(instruction_->GetLocations()->Intrinsified());
1270 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1271 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1272 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001273 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001274 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001275 }
1276 }
1277
1278 // We're moving two or three locations to locations that could
1279 // overlap, so we need a parallel move resolver.
1280 InvokeRuntimeCallingConvention calling_convention;
1281 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1282 parallel_move.AddMove(ref_,
1283 LocationFrom(calling_convention.GetRegisterAt(0)),
1284 type,
1285 nullptr);
1286 parallel_move.AddMove(obj_,
1287 LocationFrom(calling_convention.GetRegisterAt(1)),
1288 type,
1289 nullptr);
1290 if (index.IsValid()) {
1291 parallel_move.AddMove(index,
1292 LocationFrom(calling_convention.GetRegisterAt(2)),
1293 Primitive::kPrimInt,
1294 nullptr);
1295 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1296 } else {
1297 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1298 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1299 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001300 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001301 instruction_,
1302 instruction_->GetDexPc(),
1303 this);
1304 CheckEntrypointTypes<
1305 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1306 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1307
1308 RestoreLiveRegisters(codegen, locations);
1309
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001310 __ B(GetExitLabel());
1311 }
1312
1313 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1314
1315 private:
1316 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001317 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1318 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001319 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1320 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1321 return Register(VIXLRegCodeFromART(i), kXRegSize);
1322 }
1323 }
1324 // We shall never fail to find a free caller-save register, as
1325 // there are more than two core caller-save registers on ARM64
1326 // (meaning it is possible to find one which is different from
1327 // `ref` and `obj`).
1328 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1329 LOG(FATAL) << "Could not find a free register";
1330 UNREACHABLE();
1331 }
1332
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001333 const Location out_;
1334 const Location ref_;
1335 const Location obj_;
1336 const uint32_t offset_;
1337 // An additional location containing an index to an array.
1338 // Only used for HArrayGet and the UnsafeGetObject &
1339 // UnsafeGetObjectVolatile intrinsics.
1340 const Location index_;
1341
1342 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1343};
1344
1345// Slow path generating a read barrier for a GC root.
1346class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1347 public:
1348 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001349 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001350 DCHECK(kEmitCompilerReadBarrier);
1351 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001352
1353 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1354 LocationSummary* locations = instruction_->GetLocations();
1355 Primitive::Type type = Primitive::kPrimNot;
1356 DCHECK(locations->CanCall());
1357 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001358 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1359 << "Unexpected instruction in read barrier for GC root slow path: "
1360 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001361
1362 __ Bind(GetEntryLabel());
1363 SaveLiveRegisters(codegen, locations);
1364
1365 InvokeRuntimeCallingConvention calling_convention;
1366 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1367 // The argument of the ReadBarrierForRootSlow is not a managed
1368 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1369 // thus we need a 64-bit move here, and we cannot use
1370 //
1371 // arm64_codegen->MoveLocation(
1372 // LocationFrom(calling_convention.GetRegisterAt(0)),
1373 // root_,
1374 // type);
1375 //
1376 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1377 // reference type (`Primitive::kPrimNot`).
1378 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001379 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001380 instruction_,
1381 instruction_->GetDexPc(),
1382 this);
1383 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1384 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1385
1386 RestoreLiveRegisters(codegen, locations);
1387 __ B(GetExitLabel());
1388 }
1389
1390 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1391
1392 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001393 const Location out_;
1394 const Location root_;
1395
1396 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1397};
1398
Alexandre Rames5319def2014-10-23 10:03:10 +01001399#undef __
1400
1401Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
1402 Location next_location;
1403 if (type == Primitive::kPrimVoid) {
1404 LOG(FATAL) << "Unreachable type " << type;
1405 }
1406
1407 if (Primitive::IsFloatingPointType(type) &&
1408 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001409 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1410 } else if (!Primitive::IsFloatingPointType(type) &&
1411 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1412 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1413 } else {
1414 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001415 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1416 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001417 }
1418
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001419 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001420 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001421 return next_location;
1422}
1423
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001424Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001425 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001426}
1427
Serban Constantinescu579885a2015-02-22 20:51:33 +00001428CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1429 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001430 const CompilerOptions& compiler_options,
1431 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001432 : CodeGenerator(graph,
1433 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001434 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001435 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001436 callee_saved_core_registers.GetList(),
1437 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001438 compiler_options,
1439 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001440 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001441 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001442 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001443 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001444 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001445 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001446 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001447 uint32_literals_(std::less<uint32_t>(),
1448 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001449 uint64_literals_(std::less<uint64_t>(),
1450 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001451 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1452 boot_image_string_patches_(StringReferenceValueComparator(),
1453 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1454 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001455 boot_image_type_patches_(TypeReferenceValueComparator(),
1456 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1457 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001458 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001459 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001460 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001461 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1462 jit_class_patches_(TypeReferenceValueComparator(),
1463 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001464 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001465 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001466}
Alexandre Rames5319def2014-10-23 10:03:10 +01001467
Alexandre Rames67555f72014-11-18 10:55:16 +00001468#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001469
Zheng Xu3927c8b2015-11-18 17:46:25 +08001470void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001471 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001472 jump_table->EmitTable(this);
1473 }
1474}
1475
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001476void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001477 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001478 // Ensure we emit the literal pool.
1479 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001480
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001481 CodeGenerator::Finalize(allocator);
1482}
1483
Zheng Xuad4450e2015-04-17 18:48:56 +08001484void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1485 // Note: There are 6 kinds of moves:
1486 // 1. constant -> GPR/FPR (non-cycle)
1487 // 2. constant -> stack (non-cycle)
1488 // 3. GPR/FPR -> GPR/FPR
1489 // 4. GPR/FPR -> stack
1490 // 5. stack -> GPR/FPR
1491 // 6. stack -> stack (non-cycle)
1492 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1493 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1494 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1495 // dependency.
1496 vixl_temps_.Open(GetVIXLAssembler());
1497}
1498
1499void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1500 vixl_temps_.Close();
1501}
1502
1503Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001504 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1505 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1506 || kind == Location::kSIMDStackSlot);
1507 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1508 ? Location::kFpuRegister
1509 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001510 Location scratch = GetScratchLocation(kind);
1511 if (!scratch.Equals(Location::NoLocation())) {
1512 return scratch;
1513 }
1514 // Allocate from VIXL temp registers.
1515 if (kind == Location::kRegister) {
1516 scratch = LocationFrom(vixl_temps_.AcquireX());
1517 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001518 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001519 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1520 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1521 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001522 }
1523 AddScratchLocation(scratch);
1524 return scratch;
1525}
1526
1527void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1528 if (loc.IsRegister()) {
1529 vixl_temps_.Release(XRegisterFrom(loc));
1530 } else {
1531 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001532 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001533 }
1534 RemoveScratchLocation(loc);
1535}
1536
Alexandre Rames3e69f162014-12-10 10:36:50 +00001537void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001538 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001539 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001540}
1541
Alexandre Rames5319def2014-10-23 10:03:10 +01001542void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001543 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001544 __ Bind(&frame_entry_label_);
1545
Serban Constantinescu02164b32014-11-13 14:05:07 +00001546 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1547 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001548 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001549 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001550 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001551 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001552 {
1553 // Ensure that between load and RecordPcInfo there are no pools emitted.
1554 ExactAssemblyScope eas(GetVIXLAssembler(),
1555 kInstructionSize,
1556 CodeBufferCheckScope::kExactSize);
1557 __ ldr(wzr, MemOperand(temp, 0));
1558 RecordPcInfo(nullptr, 0);
1559 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001560 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001561
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001562 if (!HasEmptyFrame()) {
1563 int frame_size = GetFrameSize();
1564 // Stack layout:
1565 // sp[frame_size - 8] : lr.
1566 // ... : other preserved core registers.
1567 // ... : other preserved fp registers.
1568 // ... : reserved frame space.
1569 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001570
1571 // Save the current method if we need it. Note that we do not
1572 // do this in HCurrentMethod, as the instruction might have been removed
1573 // in the SSA graph.
1574 if (RequiresCurrentMethod()) {
1575 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001576 } else {
1577 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001578 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001579 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001580 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1581 frame_size - GetCoreSpillSize());
1582 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1583 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001584
1585 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1586 // Initialize should_deoptimize flag to 0.
1587 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1588 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1589 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001590 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001591}
1592
1593void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001594 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001595 if (!HasEmptyFrame()) {
1596 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001597 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1598 frame_size - FrameEntrySpillSize());
1599 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1600 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001601 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001602 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001603 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001604 __ Ret();
1605 GetAssembler()->cfi().RestoreState();
1606 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001607}
1608
Scott Wakeling97c72b72016-06-24 16:19:36 +01001609CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001610 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001611 return CPURegList(CPURegister::kRegister, kXRegSize,
1612 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001613}
1614
Scott Wakeling97c72b72016-06-24 16:19:36 +01001615CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001616 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1617 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001618 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1619 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001620}
1621
Alexandre Rames5319def2014-10-23 10:03:10 +01001622void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1623 __ Bind(GetLabelOf(block));
1624}
1625
Calin Juravle175dc732015-08-25 15:42:32 +01001626void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1627 DCHECK(location.IsRegister());
1628 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1629}
1630
Calin Juravlee460d1d2015-09-29 04:52:17 +01001631void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1632 if (location.IsRegister()) {
1633 locations->AddTemp(location);
1634 } else {
1635 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1636 }
1637}
1638
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001639void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001640 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001641 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001642 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001643 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001644 if (value_can_be_null) {
1645 __ Cbz(value, &done);
1646 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001647 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001648 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001649 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001650 if (value_can_be_null) {
1651 __ Bind(&done);
1652 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001653}
1654
David Brazdil58282f42016-01-14 12:45:10 +00001655void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001656 // Blocked core registers:
1657 // lr : Runtime reserved.
1658 // tr : Runtime reserved.
1659 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1660 // ip1 : VIXL core temp.
1661 // ip0 : VIXL core temp.
1662 //
1663 // Blocked fp registers:
1664 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001665 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1666 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001667 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001668 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001669 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001670
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001671 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001672 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001673 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001674 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001675
David Brazdil58282f42016-01-14 12:45:10 +00001676 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001677 // Stubs do not save callee-save floating point registers. If the graph
1678 // is debuggable, we need to deal with these registers differently. For
1679 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001680 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1681 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001682 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001683 }
1684 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001685}
1686
Alexandre Rames3e69f162014-12-10 10:36:50 +00001687size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1688 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1689 __ Str(reg, MemOperand(sp, stack_index));
1690 return kArm64WordSize;
1691}
1692
1693size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1694 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1695 __ Ldr(reg, MemOperand(sp, stack_index));
1696 return kArm64WordSize;
1697}
1698
1699size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1700 FPRegister reg = FPRegister(reg_id, kDRegSize);
1701 __ Str(reg, MemOperand(sp, stack_index));
1702 return kArm64WordSize;
1703}
1704
1705size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1706 FPRegister reg = FPRegister(reg_id, kDRegSize);
1707 __ Ldr(reg, MemOperand(sp, stack_index));
1708 return kArm64WordSize;
1709}
1710
Alexandre Rames5319def2014-10-23 10:03:10 +01001711void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001712 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001713}
1714
1715void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001716 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001717}
1718
Alexandre Rames67555f72014-11-18 10:55:16 +00001719void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001720 if (constant->IsIntConstant()) {
1721 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1722 } else if (constant->IsLongConstant()) {
1723 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1724 } else if (constant->IsNullConstant()) {
1725 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001726 } else if (constant->IsFloatConstant()) {
1727 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1728 } else {
1729 DCHECK(constant->IsDoubleConstant());
1730 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1731 }
1732}
1733
Alexandre Rames3e69f162014-12-10 10:36:50 +00001734
1735static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1736 DCHECK(constant.IsConstant());
1737 HConstant* cst = constant.GetConstant();
1738 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001739 // Null is mapped to a core W register, which we associate with kPrimInt.
1740 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001741 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1742 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1743 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1744}
1745
Roland Levillain952b2352017-05-03 19:49:14 +01001746// Allocate a scratch register from the VIXL pool, querying first
1747// the floating-point register pool, and then the core register
1748// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001749// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1750// using a different allocation strategy.
1751static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1752 vixl::aarch64::UseScratchRegisterScope* temps,
1753 int size_in_bits) {
1754 return masm->GetScratchFPRegisterList()->IsEmpty()
1755 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1756 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1757}
1758
Calin Juravlee460d1d2015-09-29 04:52:17 +01001759void CodeGeneratorARM64::MoveLocation(Location destination,
1760 Location source,
1761 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001762 if (source.Equals(destination)) {
1763 return;
1764 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001765
1766 // A valid move can always be inferred from the destination and source
1767 // locations. When moving from and to a register, the argument type can be
1768 // used to generate 32bit instead of 64bit moves. In debug mode we also
1769 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001770 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001771
1772 if (destination.IsRegister() || destination.IsFpuRegister()) {
1773 if (unspecified_type) {
1774 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1775 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001776 (src_cst != nullptr && (src_cst->IsIntConstant()
1777 || src_cst->IsFloatConstant()
1778 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001779 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001780 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001781 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001782 // If the source is a double stack slot or a 64bit constant, a 64bit
1783 // type is appropriate. Else the source is a register, and since the
1784 // type has not been specified, we chose a 64bit type to force a 64bit
1785 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001786 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001787 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001788 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001789 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1790 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1791 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001792 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1793 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1794 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001795 } else if (source.IsSIMDStackSlot()) {
1796 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001797 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001798 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001799 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001800 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001801 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001802 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001803 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001804 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001805 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1806 ? Primitive::kPrimLong
1807 : Primitive::kPrimInt;
1808 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1809 }
1810 } else {
1811 DCHECK(source.IsFpuRegister());
1812 if (destination.IsRegister()) {
1813 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1814 ? Primitive::kPrimDouble
1815 : Primitive::kPrimFloat;
1816 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1817 } else {
1818 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001819 if (GetGraph()->HasSIMD()) {
1820 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1821 } else {
1822 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1823 }
1824 }
1825 }
1826 } else if (destination.IsSIMDStackSlot()) {
1827 if (source.IsFpuRegister()) {
1828 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1829 } else {
1830 DCHECK(source.IsSIMDStackSlot());
1831 UseScratchRegisterScope temps(GetVIXLAssembler());
1832 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1833 Register temp = temps.AcquireX();
1834 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1835 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1836 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1837 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1838 } else {
1839 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1840 __ Ldr(temp, StackOperandFrom(source));
1841 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001842 }
1843 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001844 } else { // The destination is not a register. It must be a stack slot.
1845 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1846 if (source.IsRegister() || source.IsFpuRegister()) {
1847 if (unspecified_type) {
1848 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001849 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001850 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001851 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001852 }
1853 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001854 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1855 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1856 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001857 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001858 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1859 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001860 UseScratchRegisterScope temps(GetVIXLAssembler());
1861 HConstant* src_cst = source.GetConstant();
1862 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001863 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001864 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1865 ? Register(xzr)
1866 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001867 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001868 if (src_cst->IsIntConstant()) {
1869 temp = temps.AcquireW();
1870 } else if (src_cst->IsLongConstant()) {
1871 temp = temps.AcquireX();
1872 } else if (src_cst->IsFloatConstant()) {
1873 temp = temps.AcquireS();
1874 } else {
1875 DCHECK(src_cst->IsDoubleConstant());
1876 temp = temps.AcquireD();
1877 }
1878 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001879 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001880 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001881 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001882 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001883 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001884 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001885 // Use any scratch register (a core or a floating-point one)
1886 // from VIXL scratch register pools as a temporary.
1887 //
1888 // We used to only use the FP scratch register pool, but in some
1889 // rare cases the only register from this pool (D31) would
1890 // already be used (e.g. within a ParallelMove instruction, when
1891 // a move is blocked by a another move requiring a scratch FP
1892 // register, which would reserve D31). To prevent this issue, we
1893 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001894 //
1895 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001896 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001897 // use AcquireFPOrCoreCPURegisterOfSize instead of
1898 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1899 // allocates core scratch registers first.
1900 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1901 GetVIXLAssembler(),
1902 &temps,
1903 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001904 __ Ldr(temp, StackOperandFrom(source));
1905 __ Str(temp, StackOperandFrom(destination));
1906 }
1907 }
1908}
1909
1910void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001911 CPURegister dst,
1912 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001913 switch (type) {
1914 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001915 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001916 break;
1917 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001918 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001919 break;
1920 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001921 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001922 break;
1923 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001924 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001925 break;
1926 case Primitive::kPrimInt:
1927 case Primitive::kPrimNot:
1928 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001929 case Primitive::kPrimFloat:
1930 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001931 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001932 __ Ldr(dst, src);
1933 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001934 case Primitive::kPrimVoid:
1935 LOG(FATAL) << "Unreachable type " << type;
1936 }
1937}
1938
Calin Juravle77520bc2015-01-12 18:45:46 +00001939void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001940 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001941 const MemOperand& src,
1942 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001943 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001944 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001945 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001946 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001947
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001948 DCHECK(!src.IsPreIndex());
1949 DCHECK(!src.IsPostIndex());
1950
1951 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001952 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001953 {
1954 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1955 MemOperand base = MemOperand(temp_base);
1956 switch (type) {
1957 case Primitive::kPrimBoolean:
1958 {
1959 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1960 __ ldarb(Register(dst), base);
1961 if (needs_null_check) {
1962 MaybeRecordImplicitNullCheck(instruction);
1963 }
1964 }
1965 break;
1966 case Primitive::kPrimByte:
1967 {
1968 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1969 __ ldarb(Register(dst), base);
1970 if (needs_null_check) {
1971 MaybeRecordImplicitNullCheck(instruction);
1972 }
1973 }
1974 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1975 break;
1976 case Primitive::kPrimChar:
1977 {
1978 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1979 __ ldarh(Register(dst), base);
1980 if (needs_null_check) {
1981 MaybeRecordImplicitNullCheck(instruction);
1982 }
1983 }
1984 break;
1985 case Primitive::kPrimShort:
1986 {
1987 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1988 __ ldarh(Register(dst), base);
1989 if (needs_null_check) {
1990 MaybeRecordImplicitNullCheck(instruction);
1991 }
1992 }
1993 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1994 break;
1995 case Primitive::kPrimInt:
1996 case Primitive::kPrimNot:
1997 case Primitive::kPrimLong:
1998 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
1999 {
2000 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2001 __ ldar(Register(dst), base);
2002 if (needs_null_check) {
2003 MaybeRecordImplicitNullCheck(instruction);
2004 }
2005 }
2006 break;
2007 case Primitive::kPrimFloat:
2008 case Primitive::kPrimDouble: {
2009 DCHECK(dst.IsFPRegister());
2010 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002011
Artem Serov914d7a82017-02-07 14:33:49 +00002012 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2013 {
2014 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2015 __ ldar(temp, base);
2016 if (needs_null_check) {
2017 MaybeRecordImplicitNullCheck(instruction);
2018 }
2019 }
2020 __ Fmov(FPRegister(dst), temp);
2021 break;
Roland Levillain44015862016-01-22 11:47:17 +00002022 }
Artem Serov914d7a82017-02-07 14:33:49 +00002023 case Primitive::kPrimVoid:
2024 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002025 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002026 }
2027}
2028
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002029void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002030 CPURegister src,
2031 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002032 switch (type) {
2033 case Primitive::kPrimBoolean:
2034 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002035 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002036 break;
2037 case Primitive::kPrimChar:
2038 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002039 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002040 break;
2041 case Primitive::kPrimInt:
2042 case Primitive::kPrimNot:
2043 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002044 case Primitive::kPrimFloat:
2045 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00002046 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002047 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002048 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002049 case Primitive::kPrimVoid:
2050 LOG(FATAL) << "Unreachable type " << type;
2051 }
2052}
2053
Artem Serov914d7a82017-02-07 14:33:49 +00002054void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
2055 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002056 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002057 const MemOperand& dst,
2058 bool needs_null_check) {
2059 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002060 UseScratchRegisterScope temps(GetVIXLAssembler());
2061 Register temp_base = temps.AcquireX();
2062
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002063 DCHECK(!dst.IsPreIndex());
2064 DCHECK(!dst.IsPostIndex());
2065
2066 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002067 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002068 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002069 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002070 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002071 switch (type) {
2072 case Primitive::kPrimBoolean:
2073 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00002074 {
2075 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2076 __ stlrb(Register(src), base);
2077 if (needs_null_check) {
2078 MaybeRecordImplicitNullCheck(instruction);
2079 }
2080 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002081 break;
2082 case Primitive::kPrimChar:
2083 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00002084 {
2085 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2086 __ stlrh(Register(src), base);
2087 if (needs_null_check) {
2088 MaybeRecordImplicitNullCheck(instruction);
2089 }
2090 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002091 break;
2092 case Primitive::kPrimInt:
2093 case Primitive::kPrimNot:
2094 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002095 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002096 {
2097 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2098 __ stlr(Register(src), base);
2099 if (needs_null_check) {
2100 MaybeRecordImplicitNullCheck(instruction);
2101 }
2102 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002103 break;
2104 case Primitive::kPrimFloat:
2105 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002106 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002107 Register temp_src;
2108 if (src.IsZero()) {
2109 // The zero register is used to avoid synthesizing zero constants.
2110 temp_src = Register(src);
2111 } else {
2112 DCHECK(src.IsFPRegister());
2113 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2114 __ Fmov(temp_src, FPRegister(src));
2115 }
Artem Serov914d7a82017-02-07 14:33:49 +00002116 {
2117 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2118 __ stlr(temp_src, base);
2119 if (needs_null_check) {
2120 MaybeRecordImplicitNullCheck(instruction);
2121 }
2122 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002123 break;
2124 }
2125 case Primitive::kPrimVoid:
2126 LOG(FATAL) << "Unreachable type " << type;
2127 }
2128}
2129
Calin Juravle175dc732015-08-25 15:42:32 +01002130void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2131 HInstruction* instruction,
2132 uint32_t dex_pc,
2133 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002134 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002135
2136 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2137 {
2138 // Ensure the pc position is recorded immediately after the `blr` instruction.
2139 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2140 __ blr(lr);
2141 if (EntrypointRequiresStackMap(entrypoint)) {
2142 RecordPcInfo(instruction, dex_pc, slow_path);
2143 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002144 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002145}
2146
Roland Levillaindec8f632016-07-22 17:10:06 +01002147void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2148 HInstruction* instruction,
2149 SlowPathCode* slow_path) {
2150 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002151 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2152 __ Blr(lr);
2153}
2154
Alexandre Rames67555f72014-11-18 10:55:16 +00002155void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002156 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002157 UseScratchRegisterScope temps(GetVIXLAssembler());
2158 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002159 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2160
Serban Constantinescu02164b32014-11-13 14:05:07 +00002161 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002162 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2163 __ Add(temp, class_reg, status_offset);
2164 __ Ldar(temp, HeapOperand(temp));
2165 __ Cmp(temp, mirror::Class::kStatusInitialized);
2166 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002167 __ Bind(slow_path->GetExitLabel());
2168}
Alexandre Rames5319def2014-10-23 10:03:10 +01002169
Roland Levillain44015862016-01-22 11:47:17 +00002170void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002171 BarrierType type = BarrierAll;
2172
2173 switch (kind) {
2174 case MemBarrierKind::kAnyAny:
2175 case MemBarrierKind::kAnyStore: {
2176 type = BarrierAll;
2177 break;
2178 }
2179 case MemBarrierKind::kLoadAny: {
2180 type = BarrierReads;
2181 break;
2182 }
2183 case MemBarrierKind::kStoreStore: {
2184 type = BarrierWrites;
2185 break;
2186 }
2187 default:
2188 LOG(FATAL) << "Unexpected memory barrier " << kind;
2189 }
2190 __ Dmb(InnerShareable, type);
2191}
2192
Serban Constantinescu02164b32014-11-13 14:05:07 +00002193void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2194 HBasicBlock* successor) {
2195 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002196 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2197 if (slow_path == nullptr) {
2198 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2199 instruction->SetSlowPath(slow_path);
2200 codegen_->AddSlowPath(slow_path);
2201 if (successor != nullptr) {
2202 DCHECK(successor->IsLoopHeader());
2203 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2204 }
2205 } else {
2206 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2207 }
2208
Serban Constantinescu02164b32014-11-13 14:05:07 +00002209 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2210 Register temp = temps.AcquireW();
2211
Andreas Gampe542451c2016-07-26 09:02:02 -07002212 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002213 if (successor == nullptr) {
2214 __ Cbnz(temp, slow_path->GetEntryLabel());
2215 __ Bind(slow_path->GetReturnLabel());
2216 } else {
2217 __ Cbz(temp, codegen_->GetLabelOf(successor));
2218 __ B(slow_path->GetEntryLabel());
2219 // slow_path will return to GetLabelOf(successor).
2220 }
2221}
2222
Alexandre Rames5319def2014-10-23 10:03:10 +01002223InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2224 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002225 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002226 assembler_(codegen->GetAssembler()),
2227 codegen_(codegen) {}
2228
2229#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002230 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002231
2232#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2233
2234enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002235 // Using a base helps identify when we hit such breakpoints.
2236 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002237#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2238 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2239#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2240};
2241
2242#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002243 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002244 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2245 } \
2246 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2247 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2248 locations->SetOut(Location::Any()); \
2249 }
2250 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2251#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2252
2253#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002254#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002255
Alexandre Rames67555f72014-11-18 10:55:16 +00002256void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002257 DCHECK_EQ(instr->InputCount(), 2U);
2258 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2259 Primitive::Type type = instr->GetResultType();
2260 switch (type) {
2261 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002262 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002263 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002264 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002265 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002266 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002267
2268 case Primitive::kPrimFloat:
2269 case Primitive::kPrimDouble:
2270 locations->SetInAt(0, Location::RequiresFpuRegister());
2271 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002272 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002273 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002274
Alexandre Rames5319def2014-10-23 10:03:10 +01002275 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002276 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002277 }
2278}
2279
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002280void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2281 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002282 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2283
2284 bool object_field_get_with_read_barrier =
2285 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002286 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002287 new (GetGraph()->GetArena()) LocationSummary(instruction,
2288 object_field_get_with_read_barrier ?
2289 LocationSummary::kCallOnSlowPath :
2290 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002291 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002292 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002293 // We need a temporary register for the read barrier marking slow
2294 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002295 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2296 !Runtime::Current()->UseJitCompilation() &&
2297 !field_info.IsVolatile()) {
2298 // If link-time thunks for the Baker read barrier are enabled, for AOT
2299 // non-volatile loads we need a temporary only if the offset is too big.
2300 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2301 locations->AddTemp(FixedTempLocation());
2302 }
2303 } else {
2304 locations->AddTemp(Location::RequiresRegister());
2305 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002306 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002307 locations->SetInAt(0, Location::RequiresRegister());
2308 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2309 locations->SetOut(Location::RequiresFpuRegister());
2310 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002311 // The output overlaps for an object field get when read barriers
2312 // are enabled: we do not want the load to overwrite the object's
2313 // location, as we need it to emit the read barrier.
2314 locations->SetOut(
2315 Location::RequiresRegister(),
2316 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002317 }
2318}
2319
2320void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2321 const FieldInfo& field_info) {
2322 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002323 LocationSummary* locations = instruction->GetLocations();
2324 Location base_loc = locations->InAt(0);
2325 Location out = locations->Out();
2326 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002327 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002328 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002329
Roland Levillain44015862016-01-22 11:47:17 +00002330 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2331 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002332 // /* HeapReference<Object> */ out = *(base + offset)
2333 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002334 Location maybe_temp =
2335 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002336 // Note that potential implicit null checks are handled in this
2337 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2338 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2339 instruction,
2340 out,
2341 base,
2342 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002343 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002344 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002345 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002346 } else {
2347 // General case.
2348 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002349 // Note that a potential implicit null check is handled in this
2350 // CodeGeneratorARM64::LoadAcquire call.
2351 // NB: LoadAcquire will record the pc info if needed.
2352 codegen_->LoadAcquire(
2353 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002354 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002355 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2356 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002357 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002358 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002359 }
Roland Levillain44015862016-01-22 11:47:17 +00002360 if (field_type == Primitive::kPrimNot) {
2361 // If read barriers are enabled, emit read barriers other than
2362 // Baker's using a slow path (and also unpoison the loaded
2363 // reference, if heap poisoning is enabled).
2364 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2365 }
Roland Levillain4d027112015-07-01 15:41:14 +01002366 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002367}
2368
2369void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2370 LocationSummary* locations =
2371 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2372 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002373 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2374 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2375 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002376 locations->SetInAt(1, Location::RequiresFpuRegister());
2377 } else {
2378 locations->SetInAt(1, Location::RequiresRegister());
2379 }
2380}
2381
2382void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002383 const FieldInfo& field_info,
2384 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002385 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2386
2387 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002388 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002389 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002390 Offset offset = field_info.GetFieldOffset();
2391 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002392
Roland Levillain4d027112015-07-01 15:41:14 +01002393 {
2394 // We use a block to end the scratch scope before the write barrier, thus
2395 // freeing the temporary registers so they can be used in `MarkGCCard`.
2396 UseScratchRegisterScope temps(GetVIXLAssembler());
2397
2398 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2399 DCHECK(value.IsW());
2400 Register temp = temps.AcquireW();
2401 __ Mov(temp, value.W());
2402 GetAssembler()->PoisonHeapReference(temp.W());
2403 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002404 }
Roland Levillain4d027112015-07-01 15:41:14 +01002405
2406 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002407 codegen_->StoreRelease(
2408 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002409 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002410 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2411 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002412 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2413 codegen_->MaybeRecordImplicitNullCheck(instruction);
2414 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002415 }
2416
2417 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002418 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002419 }
2420}
2421
Alexandre Rames67555f72014-11-18 10:55:16 +00002422void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002423 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002424
2425 switch (type) {
2426 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002427 case Primitive::kPrimLong: {
2428 Register dst = OutputRegister(instr);
2429 Register lhs = InputRegisterAt(instr, 0);
2430 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002431 if (instr->IsAdd()) {
2432 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002433 } else if (instr->IsAnd()) {
2434 __ And(dst, lhs, rhs);
2435 } else if (instr->IsOr()) {
2436 __ Orr(dst, lhs, rhs);
2437 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002438 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002439 } else if (instr->IsRor()) {
2440 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002441 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002442 __ Ror(dst, lhs, shift);
2443 } else {
2444 // Ensure shift distance is in the same size register as the result. If
2445 // we are rotating a long and the shift comes in a w register originally,
2446 // we don't need to sxtw for use as an x since the shift distances are
2447 // all & reg_bits - 1.
2448 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2449 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002450 } else {
2451 DCHECK(instr->IsXor());
2452 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002453 }
2454 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002455 }
2456 case Primitive::kPrimFloat:
2457 case Primitive::kPrimDouble: {
2458 FPRegister dst = OutputFPRegister(instr);
2459 FPRegister lhs = InputFPRegisterAt(instr, 0);
2460 FPRegister rhs = InputFPRegisterAt(instr, 1);
2461 if (instr->IsAdd()) {
2462 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002463 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002464 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002465 } else {
2466 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002467 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002468 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002469 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002470 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002471 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002472 }
2473}
2474
Serban Constantinescu02164b32014-11-13 14:05:07 +00002475void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2476 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2477
2478 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2479 Primitive::Type type = instr->GetResultType();
2480 switch (type) {
2481 case Primitive::kPrimInt:
2482 case Primitive::kPrimLong: {
2483 locations->SetInAt(0, Location::RequiresRegister());
2484 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002485 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002486 break;
2487 }
2488 default:
2489 LOG(FATAL) << "Unexpected shift type " << type;
2490 }
2491}
2492
2493void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2494 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2495
2496 Primitive::Type type = instr->GetType();
2497 switch (type) {
2498 case Primitive::kPrimInt:
2499 case Primitive::kPrimLong: {
2500 Register dst = OutputRegister(instr);
2501 Register lhs = InputRegisterAt(instr, 0);
2502 Operand rhs = InputOperandAt(instr, 1);
2503 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002504 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002505 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002506 if (instr->IsShl()) {
2507 __ Lsl(dst, lhs, shift_value);
2508 } else if (instr->IsShr()) {
2509 __ Asr(dst, lhs, shift_value);
2510 } else {
2511 __ Lsr(dst, lhs, shift_value);
2512 }
2513 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002514 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002515
2516 if (instr->IsShl()) {
2517 __ Lsl(dst, lhs, rhs_reg);
2518 } else if (instr->IsShr()) {
2519 __ Asr(dst, lhs, rhs_reg);
2520 } else {
2521 __ Lsr(dst, lhs, rhs_reg);
2522 }
2523 }
2524 break;
2525 }
2526 default:
2527 LOG(FATAL) << "Unexpected shift operation type " << type;
2528 }
2529}
2530
Alexandre Rames5319def2014-10-23 10:03:10 +01002531void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002532 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002533}
2534
2535void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002536 HandleBinaryOp(instruction);
2537}
2538
2539void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2540 HandleBinaryOp(instruction);
2541}
2542
2543void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2544 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002545}
2546
Artem Serov7fc63502016-02-09 17:15:29 +00002547void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002548 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2549 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2550 locations->SetInAt(0, Location::RequiresRegister());
2551 // There is no immediate variant of negated bitwise instructions in AArch64.
2552 locations->SetInAt(1, Location::RequiresRegister());
2553 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2554}
2555
Artem Serov7fc63502016-02-09 17:15:29 +00002556void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002557 Register dst = OutputRegister(instr);
2558 Register lhs = InputRegisterAt(instr, 0);
2559 Register rhs = InputRegisterAt(instr, 1);
2560
2561 switch (instr->GetOpKind()) {
2562 case HInstruction::kAnd:
2563 __ Bic(dst, lhs, rhs);
2564 break;
2565 case HInstruction::kOr:
2566 __ Orn(dst, lhs, rhs);
2567 break;
2568 case HInstruction::kXor:
2569 __ Eon(dst, lhs, rhs);
2570 break;
2571 default:
2572 LOG(FATAL) << "Unreachable";
2573 }
2574}
2575
Anton Kirilov74234da2017-01-13 14:42:47 +00002576void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2577 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002578 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2579 instruction->GetType() == Primitive::kPrimLong);
2580 LocationSummary* locations =
2581 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2582 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2583 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2584 } else {
2585 locations->SetInAt(0, Location::RequiresRegister());
2586 }
2587 locations->SetInAt(1, Location::RequiresRegister());
2588 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2589}
2590
Anton Kirilov74234da2017-01-13 14:42:47 +00002591void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2592 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002593 Primitive::Type type = instruction->GetType();
2594 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2595 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2596 Register out = OutputRegister(instruction);
2597 Register left;
2598 if (kind != HInstruction::kNeg) {
2599 left = InputRegisterAt(instruction, 0);
2600 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002601 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002602 // shifter operand operation, the IR generating `right_reg` (input to the type
2603 // conversion) can have a different type from the current instruction's type,
2604 // so we manually indicate the type.
2605 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002606 Operand right_operand(0);
2607
Anton Kirilov74234da2017-01-13 14:42:47 +00002608 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2609 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002610 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2611 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002612 right_operand = Operand(right_reg,
2613 helpers::ShiftFromOpKind(op_kind),
2614 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002615 }
2616
2617 // Logical binary operations do not support extension operations in the
2618 // operand. Note that VIXL would still manage if it was passed by generating
2619 // the extension as a separate instruction.
2620 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2621 DCHECK(!right_operand.IsExtendedRegister() ||
2622 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2623 kind != HInstruction::kNeg));
2624 switch (kind) {
2625 case HInstruction::kAdd:
2626 __ Add(out, left, right_operand);
2627 break;
2628 case HInstruction::kAnd:
2629 __ And(out, left, right_operand);
2630 break;
2631 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002632 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002633 __ Neg(out, right_operand);
2634 break;
2635 case HInstruction::kOr:
2636 __ Orr(out, left, right_operand);
2637 break;
2638 case HInstruction::kSub:
2639 __ Sub(out, left, right_operand);
2640 break;
2641 case HInstruction::kXor:
2642 __ Eor(out, left, right_operand);
2643 break;
2644 default:
2645 LOG(FATAL) << "Unexpected operation kind: " << kind;
2646 UNREACHABLE();
2647 }
2648}
2649
Artem Serov328429f2016-07-06 16:23:04 +01002650void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002651 LocationSummary* locations =
2652 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2653 locations->SetInAt(0, Location::RequiresRegister());
2654 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002655 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002656}
2657
Roland Levillain19c54192016-11-04 13:44:09 +00002658void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002659 __ Add(OutputRegister(instruction),
2660 InputRegisterAt(instruction, 0),
2661 Operand(InputOperandAt(instruction, 1)));
2662}
2663
Artem Serove1811ed2017-04-27 16:50:47 +01002664void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2665 LocationSummary* locations =
2666 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2667
2668 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2669
2670 locations->SetInAt(0, Location::RequiresRegister());
2671 // For byte case we don't need to shift the index variable so we can encode the data offset into
2672 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2673 // data offset constant generation out of the loop and reduce the critical path length in the
2674 // loop.
2675 locations->SetInAt(1, shift->GetValue() == 0
2676 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2677 : Location::RequiresRegister());
2678 locations->SetInAt(2, Location::ConstantLocation(shift));
2679 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2680}
2681
2682void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2683 HIntermediateAddressIndex* instruction) {
2684 Register index_reg = InputRegisterAt(instruction, 0);
2685 uint32_t shift = Int64ConstantFrom(instruction->GetLocations()->InAt(2));
2686 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2687
2688 if (shift == 0) {
2689 __ Add(OutputRegister(instruction), index_reg, offset);
2690 } else {
2691 Register offset_reg = InputRegisterAt(instruction, 1);
2692 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2693 }
2694}
2695
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002696void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002697 LocationSummary* locations =
2698 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002699 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2700 if (instr->GetOpKind() == HInstruction::kSub &&
2701 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002702 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002703 // Don't allocate register for Mneg instruction.
2704 } else {
2705 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2706 Location::RequiresRegister());
2707 }
2708 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2709 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002710 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2711}
2712
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002713void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002714 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002715 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2716 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002717
2718 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2719 // This fixup should be carried out for all multiply-accumulate instructions:
2720 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2721 if (instr->GetType() == Primitive::kPrimLong &&
2722 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2723 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002724 vixl::aarch64::Instruction* prev =
2725 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002726 if (prev->IsLoadOrStore()) {
2727 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002728 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002729 __ nop();
2730 }
2731 }
2732
2733 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002734 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002735 __ Madd(res, mul_left, mul_right, accumulator);
2736 } else {
2737 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002738 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002739 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002740 __ Mneg(res, mul_left, mul_right);
2741 } else {
2742 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2743 __ Msub(res, mul_left, mul_right, accumulator);
2744 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002745 }
2746}
2747
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002748void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002749 bool object_array_get_with_read_barrier =
2750 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002751 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002752 new (GetGraph()->GetArena()) LocationSummary(instruction,
2753 object_array_get_with_read_barrier ?
2754 LocationSummary::kCallOnSlowPath :
2755 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002756 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002757 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002758 // We need a temporary register for the read barrier marking slow
2759 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002760 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2761 !Runtime::Current()->UseJitCompilation() &&
2762 instruction->GetIndex()->IsConstant()) {
2763 // Array loads with constant index are treated as field loads.
2764 // If link-time thunks for the Baker read barrier are enabled, for AOT
2765 // constant index loads we need a temporary only if the offset is too big.
2766 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2767 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
2768 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
2769 if (offset >= kReferenceLoadMinFarOffset) {
2770 locations->AddTemp(FixedTempLocation());
2771 }
2772 } else {
2773 locations->AddTemp(Location::RequiresRegister());
2774 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002775 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002776 locations->SetInAt(0, Location::RequiresRegister());
2777 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002778 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2779 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2780 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002781 // The output overlaps in the case of an object array get with
2782 // read barriers enabled: we do not want the move to overwrite the
2783 // array's location, as we need it to emit the read barrier.
2784 locations->SetOut(
2785 Location::RequiresRegister(),
2786 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002787 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002788}
2789
2790void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002791 Primitive::Type type = instruction->GetType();
2792 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002793 LocationSummary* locations = instruction->GetLocations();
2794 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002795 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002796 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002797 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2798 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002799 MacroAssembler* masm = GetVIXLAssembler();
2800 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002801
Roland Levillain19c54192016-11-04 13:44:09 +00002802 // The read barrier instrumentation of object ArrayGet instructions
2803 // does not support the HIntermediateAddress instruction.
2804 DCHECK(!((type == Primitive::kPrimNot) &&
2805 instruction->GetArray()->IsIntermediateAddress() &&
2806 kEmitCompilerReadBarrier));
2807
Roland Levillain44015862016-01-22 11:47:17 +00002808 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2809 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002810 // Note that a potential implicit null check is handled in the
2811 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002812 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002813 if (index.IsConstant()) {
2814 // Array load with a constant index can be treated as a field load.
2815 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2816 Location maybe_temp =
2817 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2818 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2819 out,
2820 obj.W(),
2821 offset,
2822 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002823 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002824 /* use_load_acquire */ false);
2825 } else {
2826 Register temp = WRegisterFrom(locations->GetTemp(0));
2827 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002828 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002829 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002830 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002831 // General case.
2832 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002833 Register length;
2834 if (maybe_compressed_char_at) {
2835 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2836 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002837 {
2838 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2839 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2840
2841 if (instruction->GetArray()->IsIntermediateAddress()) {
2842 DCHECK_LT(count_offset, offset);
2843 int64_t adjusted_offset =
2844 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2845 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2846 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2847 } else {
2848 __ Ldr(length, HeapOperand(obj, count_offset));
2849 }
2850 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002851 }
jessicahandojo05765752016-09-09 19:01:32 -07002852 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002853 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002854 if (maybe_compressed_char_at) {
2855 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002856 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2857 "Expecting 0=compressed, 1=uncompressed");
2858 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002859 __ Ldrb(Register(OutputCPURegister(instruction)),
2860 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2861 __ B(&done);
2862 __ Bind(&uncompressed_load);
2863 __ Ldrh(Register(OutputCPURegister(instruction)),
2864 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2865 __ Bind(&done);
2866 } else {
2867 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2868 source = HeapOperand(obj, offset);
2869 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002870 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002871 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002872 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002873 // We do not need to compute the intermediate address from the array: the
2874 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002875 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002876 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002877 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002878 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2879 }
2880 temp = obj;
2881 } else {
2882 __ Add(temp, obj, offset);
2883 }
jessicahandojo05765752016-09-09 19:01:32 -07002884 if (maybe_compressed_char_at) {
2885 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002886 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2887 "Expecting 0=compressed, 1=uncompressed");
2888 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002889 __ Ldrb(Register(OutputCPURegister(instruction)),
2890 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2891 __ B(&done);
2892 __ Bind(&uncompressed_load);
2893 __ Ldrh(Register(OutputCPURegister(instruction)),
2894 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2895 __ Bind(&done);
2896 } else {
2897 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2898 }
Roland Levillain44015862016-01-22 11:47:17 +00002899 }
jessicahandojo05765752016-09-09 19:01:32 -07002900 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002901 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2902 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002903 codegen_->Load(type, OutputCPURegister(instruction), source);
2904 codegen_->MaybeRecordImplicitNullCheck(instruction);
2905 }
Roland Levillain44015862016-01-22 11:47:17 +00002906
2907 if (type == Primitive::kPrimNot) {
2908 static_assert(
2909 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2910 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2911 Location obj_loc = locations->InAt(0);
2912 if (index.IsConstant()) {
2913 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2914 } else {
2915 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2916 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002917 }
Roland Levillain4d027112015-07-01 15:41:14 +01002918 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002919}
2920
Alexandre Rames5319def2014-10-23 10:03:10 +01002921void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2922 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2923 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002924 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002925}
2926
2927void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002928 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002929 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002930 {
2931 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2932 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2933 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2934 codegen_->MaybeRecordImplicitNullCheck(instruction);
2935 }
jessicahandojo05765752016-09-09 19:01:32 -07002936 // Mask out compression flag from String's array length.
2937 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002938 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002939 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002940}
2941
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002942void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002943 Primitive::Type value_type = instruction->GetComponentType();
2944
2945 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002946 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2947 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002948 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002949 LocationSummary::kCallOnSlowPath :
2950 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002951 locations->SetInAt(0, Location::RequiresRegister());
2952 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002953 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2954 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2955 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002956 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002957 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002958 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002959 }
2960}
2961
2962void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2963 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002964 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002965 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002966 bool needs_write_barrier =
2967 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002968
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002969 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002970 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002971 CPURegister source = value;
2972 Location index = locations->InAt(1);
2973 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2974 MemOperand destination = HeapOperand(array);
2975 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002976
2977 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002978 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002979 if (index.IsConstant()) {
2980 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2981 destination = HeapOperand(array, offset);
2982 } else {
2983 UseScratchRegisterScope temps(masm);
2984 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002985 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002986 // We do not need to compute the intermediate address from the array: the
2987 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002988 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002989 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002990 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002991 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2992 }
2993 temp = array;
2994 } else {
2995 __ Add(temp, array, offset);
2996 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002997 destination = HeapOperand(temp,
2998 XRegisterFrom(index),
2999 LSL,
3000 Primitive::ComponentSizeShift(value_type));
3001 }
Artem Serov914d7a82017-02-07 14:33:49 +00003002 {
3003 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3004 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3005 codegen_->Store(value_type, value, destination);
3006 codegen_->MaybeRecordImplicitNullCheck(instruction);
3007 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003008 } else {
Artem Serov328429f2016-07-06 16:23:04 +01003009 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003010 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003011 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01003012 {
3013 // We use a block to end the scratch scope before the write barrier, thus
3014 // freeing the temporary registers so they can be used in `MarkGCCard`.
3015 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003016 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01003017 if (index.IsConstant()) {
3018 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003019 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01003020 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01003021 destination = HeapOperand(temp,
3022 XRegisterFrom(index),
3023 LSL,
3024 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01003025 }
3026
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003027 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3028 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3029 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3030
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003031 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003032 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
3033 codegen_->AddSlowPath(slow_path);
3034 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003035 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003036 __ Cbnz(Register(value), &non_zero);
3037 if (!index.IsConstant()) {
3038 __ Add(temp, array, offset);
3039 }
Artem Serov914d7a82017-02-07 14:33:49 +00003040 {
3041 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
3042 // emitted.
3043 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3044 __ Str(wzr, destination);
3045 codegen_->MaybeRecordImplicitNullCheck(instruction);
3046 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003047 __ B(&done);
3048 __ Bind(&non_zero);
3049 }
3050
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003051 // Note that when Baker read barriers are enabled, the type
3052 // checks are performed without read barriers. This is fine,
3053 // even in the case where a class object is in the from-space
3054 // after the flip, as a comparison involving such a type would
3055 // not produce a false positive; it may of course produce a
3056 // false negative, in which case we would take the ArraySet
3057 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003058
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003059 Register temp2 = temps.AcquireSameSizeAs(array);
3060 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003061 {
3062 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3063 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3064 __ Ldr(temp, HeapOperand(array, class_offset));
3065 codegen_->MaybeRecordImplicitNullCheck(instruction);
3066 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003067 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003068
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003069 // /* HeapReference<Class> */ temp = temp->component_type_
3070 __ Ldr(temp, HeapOperand(temp, component_offset));
3071 // /* HeapReference<Class> */ temp2 = value->klass_
3072 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3073 // If heap poisoning is enabled, no need to unpoison `temp`
3074 // nor `temp2`, as we are comparing two poisoned references.
3075 __ Cmp(temp, temp2);
3076 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003077
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003078 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3079 vixl::aarch64::Label do_put;
3080 __ B(eq, &do_put);
3081 // If heap poisoning is enabled, the `temp` reference has
3082 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003083 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3084
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003085 // /* HeapReference<Class> */ temp = temp->super_class_
3086 __ Ldr(temp, HeapOperand(temp, super_offset));
3087 // If heap poisoning is enabled, no need to unpoison
3088 // `temp`, as we are comparing against null below.
3089 __ Cbnz(temp, slow_path->GetEntryLabel());
3090 __ Bind(&do_put);
3091 } else {
3092 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003093 }
3094 }
3095
3096 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003097 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003098 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003099 __ Mov(temp2, value.W());
3100 GetAssembler()->PoisonHeapReference(temp2);
3101 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003102 }
3103
3104 if (!index.IsConstant()) {
3105 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003106 } else {
3107 // We no longer need the `temp` here so release it as the store below may
3108 // need a scratch register (if the constant index makes the offset too large)
3109 // and the poisoned `source` could be using the other scratch register.
3110 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003111 }
Artem Serov914d7a82017-02-07 14:33:49 +00003112 {
3113 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3114 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3115 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003116
Artem Serov914d7a82017-02-07 14:33:49 +00003117 if (!may_need_runtime_call_for_type_check) {
3118 codegen_->MaybeRecordImplicitNullCheck(instruction);
3119 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003120 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003121 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003122
3123 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3124
3125 if (done.IsLinked()) {
3126 __ Bind(&done);
3127 }
3128
3129 if (slow_path != nullptr) {
3130 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003131 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003132 }
3133}
3134
Alexandre Rames67555f72014-11-18 10:55:16 +00003135void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003136 RegisterSet caller_saves = RegisterSet::Empty();
3137 InvokeRuntimeCallingConvention calling_convention;
3138 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3139 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3140 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003141 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003142 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003143}
3144
3145void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003146 BoundsCheckSlowPathARM64* slow_path =
3147 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003148 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003149 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3150 __ B(slow_path->GetEntryLabel(), hs);
3151}
3152
Alexandre Rames67555f72014-11-18 10:55:16 +00003153void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3154 LocationSummary* locations =
3155 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3156 locations->SetInAt(0, Location::RequiresRegister());
3157 if (check->HasUses()) {
3158 locations->SetOut(Location::SameAsFirstInput());
3159 }
3160}
3161
3162void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3163 // We assume the class is not null.
3164 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3165 check->GetLoadClass(), check, check->GetDexPc(), true);
3166 codegen_->AddSlowPath(slow_path);
3167 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3168}
3169
Roland Levillain1a653882016-03-18 18:05:57 +00003170static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3171 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3172 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3173}
3174
3175void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3176 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3177 Location rhs_loc = instruction->GetLocations()->InAt(1);
3178 if (rhs_loc.IsConstant()) {
3179 // 0.0 is the only immediate that can be encoded directly in
3180 // an FCMP instruction.
3181 //
3182 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3183 // specify that in a floating-point comparison, positive zero
3184 // and negative zero are considered equal, so we can use the
3185 // literal 0.0 for both cases here.
3186 //
3187 // Note however that some methods (Float.equal, Float.compare,
3188 // Float.compareTo, Double.equal, Double.compare,
3189 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3190 // StrictMath.min) consider 0.0 to be (strictly) greater than
3191 // -0.0. So if we ever translate calls to these methods into a
3192 // HCompare instruction, we must handle the -0.0 case with
3193 // care here.
3194 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3195 __ Fcmp(lhs_reg, 0.0);
3196 } else {
3197 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3198 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003199}
3200
Serban Constantinescu02164b32014-11-13 14:05:07 +00003201void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003202 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003203 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3204 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003205 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003206 case Primitive::kPrimBoolean:
3207 case Primitive::kPrimByte:
3208 case Primitive::kPrimShort:
3209 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003210 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003211 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003212 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003213 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003214 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3215 break;
3216 }
3217 case Primitive::kPrimFloat:
3218 case Primitive::kPrimDouble: {
3219 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003220 locations->SetInAt(1,
3221 IsFloatingPointZeroConstant(compare->InputAt(1))
3222 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3223 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003224 locations->SetOut(Location::RequiresRegister());
3225 break;
3226 }
3227 default:
3228 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3229 }
3230}
3231
3232void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3233 Primitive::Type in_type = compare->InputAt(0)->GetType();
3234
3235 // 0 if: left == right
3236 // 1 if: left > right
3237 // -1 if: left < right
3238 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003239 case Primitive::kPrimBoolean:
3240 case Primitive::kPrimByte:
3241 case Primitive::kPrimShort:
3242 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003243 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003244 case Primitive::kPrimLong: {
3245 Register result = OutputRegister(compare);
3246 Register left = InputRegisterAt(compare, 0);
3247 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003248 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003249 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3250 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003251 break;
3252 }
3253 case Primitive::kPrimFloat:
3254 case Primitive::kPrimDouble: {
3255 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003256 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003257 __ Cset(result, ne);
3258 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003259 break;
3260 }
3261 default:
3262 LOG(FATAL) << "Unimplemented compare type " << in_type;
3263 }
3264}
3265
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003266void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003267 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003268
3269 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3270 locations->SetInAt(0, Location::RequiresFpuRegister());
3271 locations->SetInAt(1,
3272 IsFloatingPointZeroConstant(instruction->InputAt(1))
3273 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3274 : Location::RequiresFpuRegister());
3275 } else {
3276 // Integer cases.
3277 locations->SetInAt(0, Location::RequiresRegister());
3278 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3279 }
3280
David Brazdilb3e773e2016-01-26 11:28:37 +00003281 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003282 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003283 }
3284}
3285
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003286void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003287 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003288 return;
3289 }
3290
3291 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003292 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003293 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003294
Roland Levillain7f63c522015-07-13 15:54:55 +00003295 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003296 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003297 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003298 } else {
3299 // Integer cases.
3300 Register lhs = InputRegisterAt(instruction, 0);
3301 Operand rhs = InputOperandAt(instruction, 1);
3302 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003303 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003304 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003305}
3306
3307#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3308 M(Equal) \
3309 M(NotEqual) \
3310 M(LessThan) \
3311 M(LessThanOrEqual) \
3312 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003313 M(GreaterThanOrEqual) \
3314 M(Below) \
3315 M(BelowOrEqual) \
3316 M(Above) \
3317 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003318#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003319void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3320void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003321FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003322#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003323#undef FOR_EACH_CONDITION_INSTRUCTION
3324
Zheng Xuc6667102015-05-15 16:08:45 +08003325void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3326 DCHECK(instruction->IsDiv() || instruction->IsRem());
3327
3328 LocationSummary* locations = instruction->GetLocations();
3329 Location second = locations->InAt(1);
3330 DCHECK(second.IsConstant());
3331
3332 Register out = OutputRegister(instruction);
3333 Register dividend = InputRegisterAt(instruction, 0);
3334 int64_t imm = Int64FromConstant(second.GetConstant());
3335 DCHECK(imm == 1 || imm == -1);
3336
3337 if (instruction->IsRem()) {
3338 __ Mov(out, 0);
3339 } else {
3340 if (imm == 1) {
3341 __ Mov(out, dividend);
3342 } else {
3343 __ Neg(out, dividend);
3344 }
3345 }
3346}
3347
3348void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3349 DCHECK(instruction->IsDiv() || instruction->IsRem());
3350
3351 LocationSummary* locations = instruction->GetLocations();
3352 Location second = locations->InAt(1);
3353 DCHECK(second.IsConstant());
3354
3355 Register out = OutputRegister(instruction);
3356 Register dividend = InputRegisterAt(instruction, 0);
3357 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003358 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003359 int ctz_imm = CTZ(abs_imm);
3360
3361 UseScratchRegisterScope temps(GetVIXLAssembler());
3362 Register temp = temps.AcquireSameSizeAs(out);
3363
3364 if (instruction->IsDiv()) {
3365 __ Add(temp, dividend, abs_imm - 1);
3366 __ Cmp(dividend, 0);
3367 __ Csel(out, temp, dividend, lt);
3368 if (imm > 0) {
3369 __ Asr(out, out, ctz_imm);
3370 } else {
3371 __ Neg(out, Operand(out, ASR, ctz_imm));
3372 }
3373 } else {
3374 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3375 __ Asr(temp, dividend, bits - 1);
3376 __ Lsr(temp, temp, bits - ctz_imm);
3377 __ Add(out, dividend, temp);
3378 __ And(out, out, abs_imm - 1);
3379 __ Sub(out, out, temp);
3380 }
3381}
3382
3383void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3384 DCHECK(instruction->IsDiv() || instruction->IsRem());
3385
3386 LocationSummary* locations = instruction->GetLocations();
3387 Location second = locations->InAt(1);
3388 DCHECK(second.IsConstant());
3389
3390 Register out = OutputRegister(instruction);
3391 Register dividend = InputRegisterAt(instruction, 0);
3392 int64_t imm = Int64FromConstant(second.GetConstant());
3393
3394 Primitive::Type type = instruction->GetResultType();
3395 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3396
3397 int64_t magic;
3398 int shift;
3399 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3400
3401 UseScratchRegisterScope temps(GetVIXLAssembler());
3402 Register temp = temps.AcquireSameSizeAs(out);
3403
3404 // temp = get_high(dividend * magic)
3405 __ Mov(temp, magic);
3406 if (type == Primitive::kPrimLong) {
3407 __ Smulh(temp, dividend, temp);
3408 } else {
3409 __ Smull(temp.X(), dividend, temp);
3410 __ Lsr(temp.X(), temp.X(), 32);
3411 }
3412
3413 if (imm > 0 && magic < 0) {
3414 __ Add(temp, temp, dividend);
3415 } else if (imm < 0 && magic > 0) {
3416 __ Sub(temp, temp, dividend);
3417 }
3418
3419 if (shift != 0) {
3420 __ Asr(temp, temp, shift);
3421 }
3422
3423 if (instruction->IsDiv()) {
3424 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3425 } else {
3426 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3427 // TODO: Strength reduction for msub.
3428 Register temp_imm = temps.AcquireSameSizeAs(out);
3429 __ Mov(temp_imm, imm);
3430 __ Msub(out, temp, temp_imm, dividend);
3431 }
3432}
3433
3434void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3435 DCHECK(instruction->IsDiv() || instruction->IsRem());
3436 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003437 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003438
3439 LocationSummary* locations = instruction->GetLocations();
3440 Register out = OutputRegister(instruction);
3441 Location second = locations->InAt(1);
3442
3443 if (second.IsConstant()) {
3444 int64_t imm = Int64FromConstant(second.GetConstant());
3445
3446 if (imm == 0) {
3447 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3448 } else if (imm == 1 || imm == -1) {
3449 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003450 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003451 DivRemByPowerOfTwo(instruction);
3452 } else {
3453 DCHECK(imm <= -2 || imm >= 2);
3454 GenerateDivRemWithAnyConstant(instruction);
3455 }
3456 } else {
3457 Register dividend = InputRegisterAt(instruction, 0);
3458 Register divisor = InputRegisterAt(instruction, 1);
3459 if (instruction->IsDiv()) {
3460 __ Sdiv(out, dividend, divisor);
3461 } else {
3462 UseScratchRegisterScope temps(GetVIXLAssembler());
3463 Register temp = temps.AcquireSameSizeAs(out);
3464 __ Sdiv(temp, dividend, divisor);
3465 __ Msub(out, temp, divisor, dividend);
3466 }
3467 }
3468}
3469
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003470void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3471 LocationSummary* locations =
3472 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3473 switch (div->GetResultType()) {
3474 case Primitive::kPrimInt:
3475 case Primitive::kPrimLong:
3476 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003477 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003478 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3479 break;
3480
3481 case Primitive::kPrimFloat:
3482 case Primitive::kPrimDouble:
3483 locations->SetInAt(0, Location::RequiresFpuRegister());
3484 locations->SetInAt(1, Location::RequiresFpuRegister());
3485 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3486 break;
3487
3488 default:
3489 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3490 }
3491}
3492
3493void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3494 Primitive::Type type = div->GetResultType();
3495 switch (type) {
3496 case Primitive::kPrimInt:
3497 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003498 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003499 break;
3500
3501 case Primitive::kPrimFloat:
3502 case Primitive::kPrimDouble:
3503 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3504 break;
3505
3506 default:
3507 LOG(FATAL) << "Unexpected div type " << type;
3508 }
3509}
3510
Alexandre Rames67555f72014-11-18 10:55:16 +00003511void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003512 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003513 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003514}
3515
3516void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3517 SlowPathCodeARM64* slow_path =
3518 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3519 codegen_->AddSlowPath(slow_path);
3520 Location value = instruction->GetLocations()->InAt(0);
3521
Alexandre Rames3e69f162014-12-10 10:36:50 +00003522 Primitive::Type type = instruction->GetType();
3523
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003524 if (!Primitive::IsIntegralType(type)) {
3525 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003526 return;
3527 }
3528
Alexandre Rames67555f72014-11-18 10:55:16 +00003529 if (value.IsConstant()) {
3530 int64_t divisor = Int64ConstantFrom(value);
3531 if (divisor == 0) {
3532 __ B(slow_path->GetEntryLabel());
3533 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003534 // A division by a non-null constant is valid. We don't need to perform
3535 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003536 }
3537 } else {
3538 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3539 }
3540}
3541
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003542void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3543 LocationSummary* locations =
3544 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3545 locations->SetOut(Location::ConstantLocation(constant));
3546}
3547
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003548void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3549 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003550 // Will be generated at use site.
3551}
3552
Alexandre Rames5319def2014-10-23 10:03:10 +01003553void LocationsBuilderARM64::VisitExit(HExit* exit) {
3554 exit->SetLocations(nullptr);
3555}
3556
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003557void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003558}
3559
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003560void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3561 LocationSummary* locations =
3562 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3563 locations->SetOut(Location::ConstantLocation(constant));
3564}
3565
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003566void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003567 // Will be generated at use site.
3568}
3569
David Brazdilfc6a86a2015-06-26 10:33:45 +00003570void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003571 DCHECK(!successor->IsExitBlock());
3572 HBasicBlock* block = got->GetBlock();
3573 HInstruction* previous = got->GetPrevious();
3574 HLoopInformation* info = block->GetLoopInformation();
3575
David Brazdil46e2a392015-03-16 17:31:52 +00003576 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003577 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3578 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3579 return;
3580 }
3581 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3582 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3583 }
3584 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003585 __ B(codegen_->GetLabelOf(successor));
3586 }
3587}
3588
David Brazdilfc6a86a2015-06-26 10:33:45 +00003589void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3590 got->SetLocations(nullptr);
3591}
3592
3593void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3594 HandleGoto(got, got->GetSuccessor());
3595}
3596
3597void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3598 try_boundary->SetLocations(nullptr);
3599}
3600
3601void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3602 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3603 if (!successor->IsExitBlock()) {
3604 HandleGoto(try_boundary, successor);
3605 }
3606}
3607
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003608void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003609 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003610 vixl::aarch64::Label* true_target,
3611 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003612 // FP branching requires both targets to be explicit. If either of the targets
3613 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003614 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003615 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003616
David Brazdil0debae72015-11-12 18:37:00 +00003617 if (true_target == nullptr && false_target == nullptr) {
3618 // Nothing to do. The code always falls through.
3619 return;
3620 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003621 // Constant condition, statically compared against "true" (integer value 1).
3622 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003623 if (true_target != nullptr) {
3624 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003625 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003626 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003627 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003628 if (false_target != nullptr) {
3629 __ B(false_target);
3630 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003631 }
David Brazdil0debae72015-11-12 18:37:00 +00003632 return;
3633 }
3634
3635 // The following code generates these patterns:
3636 // (1) true_target == nullptr && false_target != nullptr
3637 // - opposite condition true => branch to false_target
3638 // (2) true_target != nullptr && false_target == nullptr
3639 // - condition true => branch to true_target
3640 // (3) true_target != nullptr && false_target != nullptr
3641 // - condition true => branch to true_target
3642 // - branch to false_target
3643 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003644 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003645 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003646 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003647 if (true_target == nullptr) {
3648 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3649 } else {
3650 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3651 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003652 } else {
3653 // The condition instruction has not been materialized, use its inputs as
3654 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003655 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003656
David Brazdil0debae72015-11-12 18:37:00 +00003657 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003658 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003659 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003660 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003661 IfCondition opposite_condition = condition->GetOppositeCondition();
3662 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003663 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003664 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003665 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003666 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003667 // Integer cases.
3668 Register lhs = InputRegisterAt(condition, 0);
3669 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003670
3671 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003672 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003673 if (true_target == nullptr) {
3674 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3675 non_fallthrough_target = false_target;
3676 } else {
3677 arm64_cond = ARM64Condition(condition->GetCondition());
3678 non_fallthrough_target = true_target;
3679 }
3680
Aart Bik086d27e2016-01-20 17:02:00 -08003681 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003682 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003683 switch (arm64_cond) {
3684 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003685 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003686 break;
3687 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003688 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003689 break;
3690 case lt:
3691 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003692 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003693 break;
3694 case ge:
3695 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003696 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003697 break;
3698 default:
3699 // Without the `static_cast` the compiler throws an error for
3700 // `-Werror=sign-promo`.
3701 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3702 }
3703 } else {
3704 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003705 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003706 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003707 }
3708 }
David Brazdil0debae72015-11-12 18:37:00 +00003709
3710 // If neither branch falls through (case 3), the conditional branch to `true_target`
3711 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3712 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003713 __ B(false_target);
3714 }
David Brazdil0debae72015-11-12 18:37:00 +00003715
3716 if (fallthrough_target.IsLinked()) {
3717 __ Bind(&fallthrough_target);
3718 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003719}
3720
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003721void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3722 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003723 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003724 locations->SetInAt(0, Location::RequiresRegister());
3725 }
3726}
3727
3728void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003729 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3730 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003731 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3732 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3733 true_target = nullptr;
3734 }
3735 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3736 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3737 false_target = nullptr;
3738 }
David Brazdil0debae72015-11-12 18:37:00 +00003739 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003740}
3741
3742void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3743 LocationSummary* locations = new (GetGraph()->GetArena())
3744 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003745 InvokeRuntimeCallingConvention calling_convention;
3746 RegisterSet caller_saves = RegisterSet::Empty();
3747 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3748 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003749 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003750 locations->SetInAt(0, Location::RequiresRegister());
3751 }
3752}
3753
3754void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003755 SlowPathCodeARM64* slow_path =
3756 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003757 GenerateTestAndBranch(deoptimize,
3758 /* condition_input_index */ 0,
3759 slow_path->GetEntryLabel(),
3760 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003761}
3762
Mingyao Yang063fc772016-08-02 11:02:54 -07003763void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3764 LocationSummary* locations = new (GetGraph()->GetArena())
3765 LocationSummary(flag, LocationSummary::kNoCall);
3766 locations->SetOut(Location::RequiresRegister());
3767}
3768
3769void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3770 __ Ldr(OutputRegister(flag),
3771 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3772}
3773
David Brazdilc0b601b2016-02-08 14:20:45 +00003774static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3775 return condition->IsCondition() &&
3776 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3777}
3778
Alexandre Rames880f1192016-06-13 16:04:50 +01003779static inline Condition GetConditionForSelect(HCondition* condition) {
3780 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003781 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3782 : ARM64Condition(cond);
3783}
3784
David Brazdil74eb1b22015-12-14 11:44:01 +00003785void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3786 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003787 if (Primitive::IsFloatingPointType(select->GetType())) {
3788 locations->SetInAt(0, Location::RequiresFpuRegister());
3789 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003790 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003791 } else {
3792 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3793 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3794 bool is_true_value_constant = cst_true_value != nullptr;
3795 bool is_false_value_constant = cst_false_value != nullptr;
3796 // Ask VIXL whether we should synthesize constants in registers.
3797 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3798 Operand true_op = is_true_value_constant ?
3799 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3800 Operand false_op = is_false_value_constant ?
3801 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3802 bool true_value_in_register = false;
3803 bool false_value_in_register = false;
3804 MacroAssembler::GetCselSynthesisInformation(
3805 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3806 true_value_in_register |= !is_true_value_constant;
3807 false_value_in_register |= !is_false_value_constant;
3808
3809 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3810 : Location::ConstantLocation(cst_true_value));
3811 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3812 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003813 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003814 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003815
David Brazdil74eb1b22015-12-14 11:44:01 +00003816 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3817 locations->SetInAt(2, Location::RequiresRegister());
3818 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003819}
3820
3821void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003822 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003823 Condition csel_cond;
3824
3825 if (IsBooleanValueOrMaterializedCondition(cond)) {
3826 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003827 // Use the condition flags set by the previous instruction.
3828 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003829 } else {
3830 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003831 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003832 }
3833 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003834 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003835 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003836 } else {
3837 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003838 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003839 }
3840
Alexandre Rames880f1192016-06-13 16:04:50 +01003841 if (Primitive::IsFloatingPointType(select->GetType())) {
3842 __ Fcsel(OutputFPRegister(select),
3843 InputFPRegisterAt(select, 1),
3844 InputFPRegisterAt(select, 0),
3845 csel_cond);
3846 } else {
3847 __ Csel(OutputRegister(select),
3848 InputOperandAt(select, 1),
3849 InputOperandAt(select, 0),
3850 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003851 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003852}
3853
David Srbecky0cf44932015-12-09 14:09:59 +00003854void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3855 new (GetGraph()->GetArena()) LocationSummary(info);
3856}
3857
David Srbeckyd28f4a02016-03-14 17:14:24 +00003858void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3859 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003860}
3861
3862void CodeGeneratorARM64::GenerateNop() {
3863 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003864}
3865
Alexandre Rames5319def2014-10-23 10:03:10 +01003866void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003867 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003868}
3869
3870void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003871 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003872}
3873
3874void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003875 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003876}
3877
3878void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003879 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003880}
3881
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003882// Temp is used for read barrier.
3883static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3884 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003885 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003886 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3887 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3888 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3889 return 1;
3890 }
3891 return 0;
3892}
3893
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003894// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003895// interface pointer, one for loading the current interface.
3896// The other checks have one temp for loading the object's class.
3897static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3898 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3899 return 3;
3900 }
3901 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003902}
3903
Alexandre Rames67555f72014-11-18 10:55:16 +00003904void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003905 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003906 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003907 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003908 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003909 case TypeCheckKind::kExactCheck:
3910 case TypeCheckKind::kAbstractClassCheck:
3911 case TypeCheckKind::kClassHierarchyCheck:
3912 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003913 call_kind =
3914 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003915 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003916 break;
3917 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003918 case TypeCheckKind::kUnresolvedCheck:
3919 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003920 call_kind = LocationSummary::kCallOnSlowPath;
3921 break;
3922 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003923
Alexandre Rames67555f72014-11-18 10:55:16 +00003924 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003925 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003926 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003927 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003928 locations->SetInAt(0, Location::RequiresRegister());
3929 locations->SetInAt(1, Location::RequiresRegister());
3930 // The "out" register is used as a temporary, so it overlaps with the inputs.
3931 // Note that TypeCheckSlowPathARM64 uses this register too.
3932 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003933 // Add temps if necessary for read barriers.
3934 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003935}
3936
3937void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003938 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003939 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003940 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003941 Register obj = InputRegisterAt(instruction, 0);
3942 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003943 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003944 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003945 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3946 DCHECK_LE(num_temps, 1u);
3947 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003948 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3949 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3950 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3951 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003952
Scott Wakeling97c72b72016-06-24 16:19:36 +01003953 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003954 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003955
3956 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003957 // Avoid null check if we know `obj` is not null.
3958 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003959 __ Cbz(obj, &zero);
3960 }
3961
Roland Levillain44015862016-01-22 11:47:17 +00003962 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003963 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003964 // /* HeapReference<Class> */ out = obj->klass_
3965 GenerateReferenceLoadTwoRegisters(instruction,
3966 out_loc,
3967 obj_loc,
3968 class_offset,
3969 maybe_temp_loc,
3970 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003971 __ Cmp(out, cls);
3972 __ Cset(out, eq);
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::kAbstractClassCheck: {
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 Geoffray85c7bab2015-09-18 13:40:46 +00003987 // If the class is abstract, we eagerly fetch the super class of the
3988 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003989 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003990 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003991 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003992 GenerateReferenceLoadOneRegister(instruction,
3993 out_loc,
3994 super_offset,
3995 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003996 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003997 // If `out` is null, we use it for the result, and jump to `done`.
3998 __ Cbz(out, &done);
3999 __ Cmp(out, cls);
4000 __ B(ne, &loop);
4001 __ Mov(out, 1);
4002 if (zero.IsLinked()) {
4003 __ B(&done);
4004 }
4005 break;
4006 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004007
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004008 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004009 // /* HeapReference<Class> */ out = obj->klass_
4010 GenerateReferenceLoadTwoRegisters(instruction,
4011 out_loc,
4012 obj_loc,
4013 class_offset,
4014 maybe_temp_loc,
4015 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004016 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004017 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004018 __ Bind(&loop);
4019 __ Cmp(out, cls);
4020 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004021 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004022 GenerateReferenceLoadOneRegister(instruction,
4023 out_loc,
4024 super_offset,
4025 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004026 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004027 __ Cbnz(out, &loop);
4028 // If `out` is null, we use it for the result, and jump to `done`.
4029 __ B(&done);
4030 __ Bind(&success);
4031 __ Mov(out, 1);
4032 if (zero.IsLinked()) {
4033 __ B(&done);
4034 }
4035 break;
4036 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004037
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004038 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004039 // /* HeapReference<Class> */ out = obj->klass_
4040 GenerateReferenceLoadTwoRegisters(instruction,
4041 out_loc,
4042 obj_loc,
4043 class_offset,
4044 maybe_temp_loc,
4045 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004046 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004047 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004048 __ Cmp(out, cls);
4049 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004050 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004051 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004052 GenerateReferenceLoadOneRegister(instruction,
4053 out_loc,
4054 component_offset,
4055 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004056 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004057 // If `out` is null, we use it for the result, and jump to `done`.
4058 __ Cbz(out, &done);
4059 __ Ldrh(out, HeapOperand(out, primitive_offset));
4060 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4061 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004062 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004063 __ Mov(out, 1);
4064 __ B(&done);
4065 break;
4066 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004067
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004068 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004069 // No read barrier since the slow path will retry upon failure.
4070 // /* HeapReference<Class> */ out = obj->klass_
4071 GenerateReferenceLoadTwoRegisters(instruction,
4072 out_loc,
4073 obj_loc,
4074 class_offset,
4075 maybe_temp_loc,
4076 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004077 __ Cmp(out, cls);
4078 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004079 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4080 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004081 codegen_->AddSlowPath(slow_path);
4082 __ B(ne, slow_path->GetEntryLabel());
4083 __ Mov(out, 1);
4084 if (zero.IsLinked()) {
4085 __ B(&done);
4086 }
4087 break;
4088 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004089
Calin Juravle98893e12015-10-02 21:05:03 +01004090 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004091 case TypeCheckKind::kInterfaceCheck: {
4092 // Note that we indeed only call on slow path, but we always go
4093 // into the slow path for the unresolved and interface check
4094 // cases.
4095 //
4096 // We cannot directly call the InstanceofNonTrivial runtime
4097 // entry point without resorting to a type checking slow path
4098 // here (i.e. by calling InvokeRuntime directly), as it would
4099 // require to assign fixed registers for the inputs of this
4100 // HInstanceOf instruction (following the runtime calling
4101 // convention), which might be cluttered by the potential first
4102 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004103 //
4104 // TODO: Introduce a new runtime entry point taking the object
4105 // to test (instead of its class) as argument, and let it deal
4106 // with the read barrier issues. This will let us refactor this
4107 // case of the `switch` code as it was previously (with a direct
4108 // call to the runtime not using a type checking slow path).
4109 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004110 DCHECK(locations->OnlyCallsOnSlowPath());
4111 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4112 /* is_fatal */ false);
4113 codegen_->AddSlowPath(slow_path);
4114 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004115 if (zero.IsLinked()) {
4116 __ B(&done);
4117 }
4118 break;
4119 }
4120 }
4121
4122 if (zero.IsLinked()) {
4123 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004124 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004125 }
4126
4127 if (done.IsLinked()) {
4128 __ Bind(&done);
4129 }
4130
4131 if (slow_path != nullptr) {
4132 __ Bind(slow_path->GetExitLabel());
4133 }
4134}
4135
4136void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4137 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4138 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4139
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004140 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4141 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004142 case TypeCheckKind::kExactCheck:
4143 case TypeCheckKind::kAbstractClassCheck:
4144 case TypeCheckKind::kClassHierarchyCheck:
4145 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004146 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4147 LocationSummary::kCallOnSlowPath :
4148 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004149 break;
4150 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004151 case TypeCheckKind::kUnresolvedCheck:
4152 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004153 call_kind = LocationSummary::kCallOnSlowPath;
4154 break;
4155 }
4156
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004157 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4158 locations->SetInAt(0, Location::RequiresRegister());
4159 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004160 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4161 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004162}
4163
4164void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004165 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004166 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004167 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004168 Register obj = InputRegisterAt(instruction, 0);
4169 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004170 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4171 DCHECK_GE(num_temps, 1u);
4172 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004173 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004174 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4175 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004176 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004177 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4178 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4179 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4180 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4181 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4182 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4183 const uint32_t object_array_data_offset =
4184 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004185
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004186 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004187 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4188 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4189 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004190 if (!kEmitCompilerReadBarrier) {
4191 is_type_check_slow_path_fatal =
4192 (type_check_kind == TypeCheckKind::kExactCheck ||
4193 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4194 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4195 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4196 !instruction->CanThrowIntoCatchBlock();
4197 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004198 SlowPathCodeARM64* type_check_slow_path =
4199 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4200 is_type_check_slow_path_fatal);
4201 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004202
Scott Wakeling97c72b72016-06-24 16:19:36 +01004203 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004204 // Avoid null check if we know obj is not null.
4205 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004206 __ Cbz(obj, &done);
4207 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004208
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004209 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004210 case TypeCheckKind::kExactCheck:
4211 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004212 // /* HeapReference<Class> */ temp = obj->klass_
4213 GenerateReferenceLoadTwoRegisters(instruction,
4214 temp_loc,
4215 obj_loc,
4216 class_offset,
4217 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004218 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004219
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004220 __ Cmp(temp, cls);
4221 // Jump to slow path for throwing the exception or doing a
4222 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004223 __ B(ne, 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::kAbstractClassCheck: {
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 Geoffray85c7bab2015-09-18 13:40:46 +00004236 // If the class is abstract, we eagerly fetch the super class of the
4237 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004238 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004239 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004240 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004241 GenerateReferenceLoadOneRegister(instruction,
4242 temp_loc,
4243 super_offset,
4244 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004245 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004246
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004247 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4248 // exception.
4249 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4250 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004251 __ Cmp(temp, cls);
4252 __ B(ne, &loop);
4253 break;
4254 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004255
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004256 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004257 // /* HeapReference<Class> */ temp = obj->klass_
4258 GenerateReferenceLoadTwoRegisters(instruction,
4259 temp_loc,
4260 obj_loc,
4261 class_offset,
4262 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004263 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004264
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004265 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004266 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004267 __ Bind(&loop);
4268 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004269 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004270
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004271 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004272 GenerateReferenceLoadOneRegister(instruction,
4273 temp_loc,
4274 super_offset,
4275 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004276 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004277
4278 // If the class reference currently in `temp` is not null, jump
4279 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004280 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004281 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004282 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004283 break;
4284 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004285
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004286 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004287 // /* HeapReference<Class> */ temp = obj->klass_
4288 GenerateReferenceLoadTwoRegisters(instruction,
4289 temp_loc,
4290 obj_loc,
4291 class_offset,
4292 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004293 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004294
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004295 // Do an exact check.
4296 __ Cmp(temp, cls);
4297 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004298
4299 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004300 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004301 GenerateReferenceLoadOneRegister(instruction,
4302 temp_loc,
4303 component_offset,
4304 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004305 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004306
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004307 // If the component type is null, jump to the slow path to throw the exception.
4308 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4309 // Otherwise, the object is indeed an array. Further check that this component type is not a
4310 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004311 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4312 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004313 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004314 break;
4315 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004316
Calin Juravle98893e12015-10-02 21:05:03 +01004317 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004318 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004319 //
4320 // We cannot directly call the CheckCast runtime entry point
4321 // without resorting to a type checking slow path here (i.e. by
4322 // calling InvokeRuntime directly), as it would require to
4323 // assign fixed registers for the inputs of this HInstanceOf
4324 // instruction (following the runtime calling convention), which
4325 // might be cluttered by the potential first read barrier
4326 // emission at the beginning of this method.
4327 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004328 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004329 case TypeCheckKind::kInterfaceCheck: {
4330 // /* HeapReference<Class> */ temp = obj->klass_
4331 GenerateReferenceLoadTwoRegisters(instruction,
4332 temp_loc,
4333 obj_loc,
4334 class_offset,
4335 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004336 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004337
4338 // /* HeapReference<Class> */ temp = temp->iftable_
4339 GenerateReferenceLoadTwoRegisters(instruction,
4340 temp_loc,
4341 temp_loc,
4342 iftable_offset,
4343 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004344 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004345 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004346 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004347 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004348 vixl::aarch64::Label start_loop;
4349 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004350 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004351 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4352 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004353 // Go to next interface.
4354 __ Add(temp, temp, 2 * kHeapReferenceSize);
4355 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004356 // Compare the classes and continue the loop if they do not match.
4357 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4358 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004359 break;
4360 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004361 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004362 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004363
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004364 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004365}
4366
Alexandre Rames5319def2014-10-23 10:03:10 +01004367void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4368 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4369 locations->SetOut(Location::ConstantLocation(constant));
4370}
4371
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004372void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004373 // Will be generated at use site.
4374}
4375
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004376void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4377 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4378 locations->SetOut(Location::ConstantLocation(constant));
4379}
4380
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004381void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004382 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004383}
4384
Calin Juravle175dc732015-08-25 15:42:32 +01004385void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4386 // The trampoline uses the same calling convention as dex calling conventions,
4387 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4388 // the method_idx.
4389 HandleInvoke(invoke);
4390}
4391
4392void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4393 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4394}
4395
Alexandre Rames5319def2014-10-23 10:03:10 +01004396void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004397 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004398 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004399}
4400
Alexandre Rames67555f72014-11-18 10:55:16 +00004401void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4402 HandleInvoke(invoke);
4403}
4404
4405void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4406 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004407 LocationSummary* locations = invoke->GetLocations();
4408 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004409 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004410 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004411 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004412
4413 // The register ip1 is required to be used for the hidden argument in
4414 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004415 MacroAssembler* masm = GetVIXLAssembler();
4416 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004417 scratch_scope.Exclude(ip1);
4418 __ Mov(ip1, invoke->GetDexMethodIndex());
4419
Artem Serov914d7a82017-02-07 14:33:49 +00004420 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004421 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004422 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004423 {
4424 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4425 // /* HeapReference<Class> */ temp = temp->klass_
4426 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4427 codegen_->MaybeRecordImplicitNullCheck(invoke);
4428 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004429 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004430 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004431 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004432 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004433 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004434 }
Artem Serov914d7a82017-02-07 14:33:49 +00004435
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004436 // Instead of simply (possibly) unpoisoning `temp` here, we should
4437 // emit a read barrier for the previous class reference load.
4438 // However this is not required in practice, as this is an
4439 // intermediate/temporary reference and because the current
4440 // concurrent copying collector keeps the from-space memory
4441 // intact/accessible until the end of the marking phase (the
4442 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004443 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004444 __ Ldr(temp,
4445 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4446 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004447 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004448 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004449 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004450 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004451 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004452
4453 {
4454 // Ensure the pc position is recorded immediately after the `blr` instruction.
4455 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4456
4457 // lr();
4458 __ blr(lr);
4459 DCHECK(!codegen_->IsLeafMethod());
4460 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4461 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004462}
4463
4464void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004465 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004466 if (intrinsic.TryDispatch(invoke)) {
4467 return;
4468 }
4469
Alexandre Rames67555f72014-11-18 10:55:16 +00004470 HandleInvoke(invoke);
4471}
4472
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004473void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004474 // Explicit clinit checks triggered by static invokes must have been pruned by
4475 // art::PrepareForRegisterAllocation.
4476 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004477
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004478 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004479 if (intrinsic.TryDispatch(invoke)) {
4480 return;
4481 }
4482
Alexandre Rames67555f72014-11-18 10:55:16 +00004483 HandleInvoke(invoke);
4484}
4485
Andreas Gampe878d58c2015-01-15 23:24:00 -08004486static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4487 if (invoke->GetLocations()->Intrinsified()) {
4488 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4489 intrinsic.Dispatch(invoke);
4490 return true;
4491 }
4492 return false;
4493}
4494
Vladimir Markodc151b22015-10-15 18:02:30 +01004495HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4496 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004497 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004498 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004499 return desired_dispatch_info;
4500}
4501
TatWai Chongd8c052a2016-11-02 16:12:48 +08004502Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4503 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004504 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004505 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4506 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004507 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4508 uint32_t offset =
4509 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004510 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004511 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004512 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004513 }
Vladimir Marko58155012015-08-19 12:49:41 +00004514 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004515 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004516 break;
4517 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4518 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004519 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004520 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004521 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4522 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004523 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004524 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004525 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004526 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004527 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004528 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004529 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004530 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004531 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004532 }
Vladimir Marko58155012015-08-19 12:49:41 +00004533 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004534 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004535 Register reg = XRegisterFrom(temp);
4536 Register method_reg;
4537 if (current_method.IsRegister()) {
4538 method_reg = XRegisterFrom(current_method);
4539 } else {
4540 DCHECK(invoke->GetLocations()->Intrinsified());
4541 DCHECK(!current_method.IsValid());
4542 method_reg = reg;
4543 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4544 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004545
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004546 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004547 __ Ldr(reg.X(),
4548 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004549 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004550 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004551 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4552 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004553 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4554 break;
4555 }
4556 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004557 return callee_method;
4558}
4559
4560void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4561 // All registers are assumed to be correctly set up.
4562 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004563
4564 switch (invoke->GetCodePtrLocation()) {
4565 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4566 __ Bl(&frame_entry_label_);
4567 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004568 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4569 // LR = callee_method->entry_point_from_quick_compiled_code_;
4570 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004571 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004572 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004573 {
4574 // To ensure that the pc position is recorded immediately after the `blr` instruction
4575 // BLR must be the last instruction emitted in this function.
4576 // Recording the pc will occur right after returning from this function.
4577 ExactAssemblyScope eas(GetVIXLAssembler(),
4578 kInstructionSize,
4579 CodeBufferCheckScope::kExactSize);
4580 // lr()
4581 __ blr(lr);
4582 }
Vladimir Marko58155012015-08-19 12:49:41 +00004583 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004584 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004585
Andreas Gampe878d58c2015-01-15 23:24:00 -08004586 DCHECK(!IsLeafMethod());
4587}
4588
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004589void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004590 // Use the calling convention instead of the location of the receiver, as
4591 // intrinsics may have put the receiver in a different register. In the intrinsics
4592 // slow path, the arguments have been moved to the right place, so here we are
4593 // guaranteed that the receiver is the first register of the calling convention.
4594 InvokeDexCallingConvention calling_convention;
4595 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004596 Register temp = XRegisterFrom(temp_in);
4597 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4598 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4599 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004600 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004601
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004602 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004603
4604 {
4605 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4606 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4607 // /* HeapReference<Class> */ temp = receiver->klass_
4608 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4609 MaybeRecordImplicitNullCheck(invoke);
4610 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004611 // Instead of simply (possibly) unpoisoning `temp` here, we should
4612 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004613 // intermediate/temporary reference and because the current
4614 // concurrent copying collector keeps the from-space memory
4615 // intact/accessible until the end of the marking phase (the
4616 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004617 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4618 // temp = temp->GetMethodAt(method_offset);
4619 __ Ldr(temp, MemOperand(temp, method_offset));
4620 // lr = temp->GetEntryPoint();
4621 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004622 {
4623 // To ensure that the pc position is recorded immediately after the `blr` instruction
4624 // BLR should be the last instruction emitted in this function.
4625 // Recording the pc will occur right after returning from this function.
4626 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4627 // lr();
4628 __ blr(lr);
4629 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004630}
4631
Orion Hodsonac141392017-01-13 11:53:47 +00004632void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4633 HandleInvoke(invoke);
4634}
4635
4636void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4637 codegen_->GenerateInvokePolymorphicCall(invoke);
4638}
4639
Scott Wakeling97c72b72016-06-24 16:19:36 +01004640vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4641 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004642 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004643 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004644 return
4645 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004646}
4647
Scott Wakeling97c72b72016-06-24 16:19:36 +01004648vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4649 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004650 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004651 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004652 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004653}
4654
Vladimir Marko1998cd02017-01-13 13:02:58 +00004655vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4656 const DexFile& dex_file,
4657 dex::TypeIndex type_index,
4658 vixl::aarch64::Label* adrp_label) {
4659 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4660}
4661
Scott Wakeling97c72b72016-06-24 16:19:36 +01004662vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4663 const DexFile& dex_file,
4664 uint32_t element_offset,
4665 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004666 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4667}
4668
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004669vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4670 baker_read_barrier_patches_.emplace_back(custom_data);
4671 return &baker_read_barrier_patches_.back().label;
4672}
4673
Scott Wakeling97c72b72016-06-24 16:19:36 +01004674vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4675 const DexFile& dex_file,
4676 uint32_t offset_or_index,
4677 vixl::aarch64::Label* adrp_label,
4678 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004679 // Add a patch entry and return the label.
4680 patches->emplace_back(dex_file, offset_or_index);
4681 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004682 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004683 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4684 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4685 return label;
4686}
4687
Scott Wakeling97c72b72016-06-24 16:19:36 +01004688vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004689 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004690 return boot_image_string_patches_.GetOrCreate(
4691 StringReference(&dex_file, string_index),
4692 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4693}
4694
Scott Wakeling97c72b72016-06-24 16:19:36 +01004695vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004696 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004697 return boot_image_type_patches_.GetOrCreate(
4698 TypeReference(&dex_file, type_index),
4699 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4700}
4701
Scott Wakeling97c72b72016-06-24 16:19:36 +01004702vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4703 uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00004704 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004705}
4706
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004707vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004708 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4709 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4710 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004711 return jit_string_patches_.GetOrCreate(
4712 StringReference(&dex_file, string_index),
4713 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4714}
4715
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004716vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004717 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4718 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4719 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004720 return jit_class_patches_.GetOrCreate(
4721 TypeReference(&dex_file, type_index),
4722 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4723}
4724
Vladimir Markoaad75c62016-10-03 08:46:48 +00004725void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4726 vixl::aarch64::Register reg) {
4727 DCHECK(reg.IsX());
4728 SingleEmissionCheckScope guard(GetVIXLAssembler());
4729 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004730 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004731}
4732
4733void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4734 vixl::aarch64::Register out,
4735 vixl::aarch64::Register base) {
4736 DCHECK(out.IsX());
4737 DCHECK(base.IsX());
4738 SingleEmissionCheckScope guard(GetVIXLAssembler());
4739 __ Bind(fixup_label);
4740 __ add(out, base, Operand(/* offset placeholder */ 0));
4741}
4742
4743void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4744 vixl::aarch64::Register out,
4745 vixl::aarch64::Register base) {
4746 DCHECK(base.IsX());
4747 SingleEmissionCheckScope guard(GetVIXLAssembler());
4748 __ Bind(fixup_label);
4749 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4750}
4751
4752template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4753inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4754 const ArenaDeque<PcRelativePatchInfo>& infos,
4755 ArenaVector<LinkerPatch>* linker_patches) {
4756 for (const PcRelativePatchInfo& info : infos) {
4757 linker_patches->push_back(Factory(info.label.GetLocation(),
4758 &info.target_dex_file,
4759 info.pc_insn_label->GetLocation(),
4760 info.offset_or_index));
4761 }
4762}
4763
Vladimir Marko58155012015-08-19 12:49:41 +00004764void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4765 DCHECK(linker_patches->empty());
4766 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004767 pc_relative_dex_cache_patches_.size() +
4768 boot_image_string_patches_.size() +
4769 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004770 boot_image_type_patches_.size() +
4771 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004772 type_bss_entry_patches_.size() +
4773 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004774 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004775 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004776 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004777 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004778 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004779 info.offset_or_index));
4780 }
4781 for (const auto& entry : boot_image_string_patches_) {
4782 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004783 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4784 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004785 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004786 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004787 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004788 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004789 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004790 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4791 linker_patches);
4792 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004793 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4794 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004795 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4796 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004797 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004798 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4799 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004800 for (const auto& entry : boot_image_type_patches_) {
4801 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004802 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4803 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004804 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004805 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004806 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004807 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
4808 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
4809 info.custom_data));
4810 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004811 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004812}
4813
Scott Wakeling97c72b72016-06-24 16:19:36 +01004814vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004815 Uint32ToLiteralMap* map) {
4816 return map->GetOrCreate(
4817 value,
4818 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4819}
4820
Scott Wakeling97c72b72016-06-24 16:19:36 +01004821vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004822 return uint64_literals_.GetOrCreate(
4823 value,
4824 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004825}
4826
Scott Wakeling97c72b72016-06-24 16:19:36 +01004827vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004828 MethodReference target_method,
4829 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004830 return map->GetOrCreate(
4831 target_method,
4832 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004833}
4834
Andreas Gampe878d58c2015-01-15 23:24:00 -08004835void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004836 // Explicit clinit checks triggered by static invokes must have been pruned by
4837 // art::PrepareForRegisterAllocation.
4838 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004839
Andreas Gampe878d58c2015-01-15 23:24:00 -08004840 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4841 return;
4842 }
4843
Artem Serov914d7a82017-02-07 14:33:49 +00004844 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4845 // are no pools emitted.
4846 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004847 LocationSummary* locations = invoke->GetLocations();
4848 codegen_->GenerateStaticOrDirectCall(
4849 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004850 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004851}
4852
4853void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004854 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4855 return;
4856 }
4857
Artem Serov914d7a82017-02-07 14:33:49 +00004858 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4859 // are no pools emitted.
4860 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004861 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004862 DCHECK(!codegen_->IsLeafMethod());
4863 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4864}
4865
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004866HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4867 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004868 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004869 case HLoadClass::LoadKind::kInvalid:
4870 LOG(FATAL) << "UNREACHABLE";
4871 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004872 case HLoadClass::LoadKind::kReferrersClass:
4873 break;
4874 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4875 DCHECK(!GetCompilerOptions().GetCompilePic());
4876 break;
4877 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4878 DCHECK(GetCompilerOptions().GetCompilePic());
4879 break;
4880 case HLoadClass::LoadKind::kBootImageAddress:
4881 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004882 case HLoadClass::LoadKind::kBssEntry:
4883 DCHECK(!Runtime::Current()->UseJitCompilation());
4884 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004885 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004886 DCHECK(Runtime::Current()->UseJitCompilation());
4887 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004888 case HLoadClass::LoadKind::kDexCacheViaMethod:
4889 break;
4890 }
4891 return desired_class_load_kind;
4892}
4893
Alexandre Rames67555f72014-11-18 10:55:16 +00004894void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004895 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4896 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004897 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004898 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004899 cls,
4900 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004901 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004902 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004903 return;
4904 }
Vladimir Marko41559982017-01-06 14:04:23 +00004905 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004906
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004907 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4908 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004909 ? LocationSummary::kCallOnSlowPath
4910 : LocationSummary::kNoCall;
4911 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004912 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004913 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004914 }
4915
Vladimir Marko41559982017-01-06 14:04:23 +00004916 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004917 locations->SetInAt(0, Location::RequiresRegister());
4918 }
4919 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004920 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4921 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4922 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004923 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004924 RegisterSet caller_saves = RegisterSet::Empty();
4925 InvokeRuntimeCallingConvention calling_convention;
4926 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4927 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4928 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4929 Primitive::kPrimNot).GetCode());
4930 locations->SetCustomSlowPathCallerSaves(caller_saves);
4931 } else {
4932 // For non-Baker read barrier we have a temp-clobbering call.
4933 }
4934 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004935}
4936
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004937// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4938// move.
4939void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004940 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4941 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4942 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004943 return;
4944 }
Vladimir Marko41559982017-01-06 14:04:23 +00004945 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004946
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004947 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004948 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004949 Register bss_entry_temp;
4950 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004951
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004952 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4953 ? kWithoutReadBarrier
4954 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004955 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004956 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004957 case HLoadClass::LoadKind::kReferrersClass: {
4958 DCHECK(!cls->CanCallRuntime());
4959 DCHECK(!cls->MustGenerateClinitCheck());
4960 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4961 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004962 GenerateGcRootFieldLoad(cls,
4963 out_loc,
4964 current_method,
4965 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004966 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004967 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004968 break;
4969 }
4970 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004971 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004972 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4973 cls->GetTypeIndex()));
4974 break;
4975 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004976 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004977 // Add ADRP with its PC-relative type patch.
4978 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004979 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004980 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004981 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004982 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004983 vixl::aarch64::Label* add_label =
4984 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004985 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004986 break;
4987 }
4988 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004989 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004990 uint32_t address = dchecked_integral_cast<uint32_t>(
4991 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4992 DCHECK_NE(address, 0u);
4993 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004994 break;
4995 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004996 case HLoadClass::LoadKind::kBssEntry: {
4997 // Add ADRP with its PC-relative Class .bss entry patch.
4998 const DexFile& dex_file = cls->GetDexFile();
4999 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005000 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005001 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
5002 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005003 // Add LDR with its PC-relative Class patch.
5004 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00005005 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005006 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
5007 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005008 out_loc,
5009 bss_entry_temp,
5010 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005011 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005012 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005013 generate_null_check = true;
5014 break;
5015 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005016 case HLoadClass::LoadKind::kJitTableAddress: {
5017 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5018 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005019 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005020 GenerateGcRootFieldLoad(cls,
5021 out_loc,
5022 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005023 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01005024 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005025 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005026 break;
5027 }
Vladimir Marko41559982017-01-06 14:04:23 +00005028 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005029 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005030 LOG(FATAL) << "UNREACHABLE";
5031 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005032 }
5033
Vladimir Markoea4c1262017-02-06 19:59:33 +00005034 bool do_clinit = cls->MustGenerateClinitCheck();
5035 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005036 DCHECK(cls->CanCallRuntime());
5037 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00005038 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005039 codegen_->AddSlowPath(slow_path);
5040 if (generate_null_check) {
5041 __ Cbz(out, slow_path->GetEntryLabel());
5042 }
5043 if (cls->MustGenerateClinitCheck()) {
5044 GenerateClassInitializationCheck(slow_path, out);
5045 } else {
5046 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005047 }
5048 }
5049}
5050
David Brazdilcb1c0552015-08-04 16:22:25 +01005051static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005052 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005053}
5054
Alexandre Rames67555f72014-11-18 10:55:16 +00005055void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5056 LocationSummary* locations =
5057 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5058 locations->SetOut(Location::RequiresRegister());
5059}
5060
5061void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005062 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5063}
5064
5065void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
5066 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5067}
5068
5069void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5070 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005071}
5072
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005073HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5074 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005075 switch (desired_string_load_kind) {
5076 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5077 DCHECK(!GetCompilerOptions().GetCompilePic());
5078 break;
5079 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5080 DCHECK(GetCompilerOptions().GetCompilePic());
5081 break;
5082 case HLoadString::LoadKind::kBootImageAddress:
5083 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005084 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005085 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005086 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005087 case HLoadString::LoadKind::kJitTableAddress:
5088 DCHECK(Runtime::Current()->UseJitCompilation());
5089 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005090 case HLoadString::LoadKind::kDexCacheViaMethod:
5091 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005092 }
5093 return desired_string_load_kind;
5094}
5095
Alexandre Rames67555f72014-11-18 10:55:16 +00005096void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005097 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005098 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005099 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005100 InvokeRuntimeCallingConvention calling_convention;
5101 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5102 } else {
5103 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005104 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5105 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005106 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005107 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005108 RegisterSet caller_saves = RegisterSet::Empty();
5109 InvokeRuntimeCallingConvention calling_convention;
5110 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5111 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
5112 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
5113 Primitive::kPrimNot).GetCode());
5114 locations->SetCustomSlowPathCallerSaves(caller_saves);
5115 } else {
5116 // For non-Baker read barrier we have a temp-clobbering call.
5117 }
5118 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005119 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005120}
5121
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005122// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5123// move.
5124void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005125 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005126 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005127
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005128 switch (load->GetLoadKind()) {
5129 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005130 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5131 load->GetStringIndex()));
5132 return; // No dex cache slow path.
5133 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005134 // Add ADRP with its PC-relative String patch.
5135 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005136 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005137 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01005138 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005139 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005140 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005141 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005142 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005143 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005144 return; // No dex cache slow path.
5145 }
5146 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005147 uint32_t address = dchecked_integral_cast<uint32_t>(
5148 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5149 DCHECK_NE(address, 0u);
5150 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005151 return; // No dex cache slow path.
5152 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005153 case HLoadString::LoadKind::kBssEntry: {
5154 // Add ADRP with its PC-relative String .bss entry patch.
5155 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005156 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005157 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005158 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005159 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005160 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005161 // Add LDR with its PC-relative String patch.
5162 vixl::aarch64::Label* ldr_label =
5163 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005164 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005165 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005166 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005167 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005168 /* offset placeholder */ 0u,
5169 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005170 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005171 SlowPathCodeARM64* slow_path =
5172 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005173 codegen_->AddSlowPath(slow_path);
5174 __ Cbz(out.X(), slow_path->GetEntryLabel());
5175 __ Bind(slow_path->GetExitLabel());
5176 return;
5177 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005178 case HLoadString::LoadKind::kJitTableAddress: {
5179 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005180 load->GetStringIndex(),
5181 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005182 GenerateGcRootFieldLoad(load,
5183 out_loc,
5184 out.X(),
5185 /* offset */ 0,
5186 /* fixup_label */ nullptr,
5187 kCompilerReadBarrierOption);
5188 return;
5189 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005190 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005191 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005192 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005193
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005194 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005195 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005196 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005197 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005198 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5199 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005200}
5201
Alexandre Rames5319def2014-10-23 10:03:10 +01005202void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5203 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5204 locations->SetOut(Location::ConstantLocation(constant));
5205}
5206
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005207void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005208 // Will be generated at use site.
5209}
5210
Alexandre Rames67555f72014-11-18 10:55:16 +00005211void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5212 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005213 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005214 InvokeRuntimeCallingConvention calling_convention;
5215 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5216}
5217
5218void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005219 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005220 instruction,
5221 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005222 if (instruction->IsEnter()) {
5223 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5224 } else {
5225 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5226 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005227}
5228
Alexandre Rames42d641b2014-10-27 14:00:51 +00005229void LocationsBuilderARM64::VisitMul(HMul* mul) {
5230 LocationSummary* locations =
5231 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5232 switch (mul->GetResultType()) {
5233 case Primitive::kPrimInt:
5234 case Primitive::kPrimLong:
5235 locations->SetInAt(0, Location::RequiresRegister());
5236 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005237 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005238 break;
5239
5240 case Primitive::kPrimFloat:
5241 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005242 locations->SetInAt(0, Location::RequiresFpuRegister());
5243 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005244 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005245 break;
5246
5247 default:
5248 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5249 }
5250}
5251
5252void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5253 switch (mul->GetResultType()) {
5254 case Primitive::kPrimInt:
5255 case Primitive::kPrimLong:
5256 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5257 break;
5258
5259 case Primitive::kPrimFloat:
5260 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005261 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005262 break;
5263
5264 default:
5265 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5266 }
5267}
5268
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005269void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5270 LocationSummary* locations =
5271 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5272 switch (neg->GetResultType()) {
5273 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005274 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005275 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005277 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005278
5279 case Primitive::kPrimFloat:
5280 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005281 locations->SetInAt(0, Location::RequiresFpuRegister());
5282 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005283 break;
5284
5285 default:
5286 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5287 }
5288}
5289
5290void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5291 switch (neg->GetResultType()) {
5292 case Primitive::kPrimInt:
5293 case Primitive::kPrimLong:
5294 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5295 break;
5296
5297 case Primitive::kPrimFloat:
5298 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005299 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005300 break;
5301
5302 default:
5303 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5304 }
5305}
5306
5307void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5308 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005309 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005310 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005311 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005312 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5313 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005314}
5315
5316void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005317 // Note: if heap poisoning is enabled, the entry point takes cares
5318 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005319 QuickEntrypointEnum entrypoint =
5320 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5321 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005322 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005323}
5324
Alexandre Rames5319def2014-10-23 10:03:10 +01005325void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5326 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005327 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005328 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005329 if (instruction->IsStringAlloc()) {
5330 locations->AddTemp(LocationFrom(kArtMethodRegister));
5331 } else {
5332 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005333 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005334 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5335}
5336
5337void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005338 // Note: if heap poisoning is enabled, the entry point takes cares
5339 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005340 if (instruction->IsStringAlloc()) {
5341 // String is allocated through StringFactory. Call NewEmptyString entry point.
5342 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005343 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005344 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5345 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005346
5347 {
5348 // Ensure the pc position is recorded immediately after the `blr` instruction.
5349 ExactAssemblyScope eas(GetVIXLAssembler(),
5350 kInstructionSize,
5351 CodeBufferCheckScope::kExactSize);
5352 __ blr(lr);
5353 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5354 }
David Brazdil6de19382016-01-08 17:37:10 +00005355 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005356 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005357 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005358 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005359}
5360
5361void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5362 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005363 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005364 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005365}
5366
5367void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005368 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005369 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005370 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005371 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005372 break;
5373
5374 default:
5375 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5376 }
5377}
5378
David Brazdil66d126e2015-04-03 16:02:44 +01005379void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5380 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5381 locations->SetInAt(0, Location::RequiresRegister());
5382 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5383}
5384
5385void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005386 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005387}
5388
Alexandre Rames5319def2014-10-23 10:03:10 +01005389void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005390 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5391 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005392}
5393
Calin Juravle2ae48182016-03-16 14:05:09 +00005394void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5395 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005396 return;
5397 }
Artem Serov914d7a82017-02-07 14:33:49 +00005398 {
5399 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5400 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5401 Location obj = instruction->GetLocations()->InAt(0);
5402 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5403 RecordPcInfo(instruction, instruction->GetDexPc());
5404 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005405}
5406
Calin Juravle2ae48182016-03-16 14:05:09 +00005407void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005408 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005409 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005410
5411 LocationSummary* locations = instruction->GetLocations();
5412 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005413
5414 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005415}
5416
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005417void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005418 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005419}
5420
Alexandre Rames67555f72014-11-18 10:55:16 +00005421void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5422 HandleBinaryOp(instruction);
5423}
5424
5425void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5426 HandleBinaryOp(instruction);
5427}
5428
Alexandre Rames3e69f162014-12-10 10:36:50 +00005429void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5430 LOG(FATAL) << "Unreachable";
5431}
5432
5433void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5434 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5435}
5436
Alexandre Rames5319def2014-10-23 10:03:10 +01005437void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5438 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5439 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5440 if (location.IsStackSlot()) {
5441 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5442 } else if (location.IsDoubleStackSlot()) {
5443 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5444 }
5445 locations->SetOut(location);
5446}
5447
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005448void InstructionCodeGeneratorARM64::VisitParameterValue(
5449 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005450 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005451}
5452
5453void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5454 LocationSummary* locations =
5455 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005456 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005457}
5458
5459void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5460 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5461 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005462}
5463
5464void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5465 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005466 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005467 locations->SetInAt(i, Location::Any());
5468 }
5469 locations->SetOut(Location::Any());
5470}
5471
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005472void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005473 LOG(FATAL) << "Unreachable";
5474}
5475
Serban Constantinescu02164b32014-11-13 14:05:07 +00005476void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005477 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005478 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005479 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5480 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005481 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5482
5483 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005484 case Primitive::kPrimInt:
5485 case Primitive::kPrimLong:
5486 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005487 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005488 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5489 break;
5490
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005491 case Primitive::kPrimFloat:
5492 case Primitive::kPrimDouble: {
5493 InvokeRuntimeCallingConvention calling_convention;
5494 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5495 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5496 locations->SetOut(calling_convention.GetReturnLocation(type));
5497
5498 break;
5499 }
5500
Serban Constantinescu02164b32014-11-13 14:05:07 +00005501 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005502 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005503 }
5504}
5505
5506void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5507 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005508
Serban Constantinescu02164b32014-11-13 14:05:07 +00005509 switch (type) {
5510 case Primitive::kPrimInt:
5511 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005512 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005513 break;
5514 }
5515
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005516 case Primitive::kPrimFloat:
5517 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005518 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5519 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005520 if (type == Primitive::kPrimFloat) {
5521 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5522 } else {
5523 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5524 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005525 break;
5526 }
5527
Serban Constantinescu02164b32014-11-13 14:05:07 +00005528 default:
5529 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005530 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005531 }
5532}
5533
Igor Murashkind01745e2017-04-05 16:40:31 -07005534void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5535 constructor_fence->SetLocations(nullptr);
5536}
5537
5538void InstructionCodeGeneratorARM64::VisitConstructorFence(
5539 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5540 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5541}
5542
Calin Juravle27df7582015-04-17 19:12:31 +01005543void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5544 memory_barrier->SetLocations(nullptr);
5545}
5546
5547void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005548 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005549}
5550
Alexandre Rames5319def2014-10-23 10:03:10 +01005551void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5552 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5553 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005554 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005555}
5556
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005557void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005558 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005559}
5560
5561void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5562 instruction->SetLocations(nullptr);
5563}
5564
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005565void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005566 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005567}
5568
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005569void LocationsBuilderARM64::VisitRor(HRor* ror) {
5570 HandleBinaryOp(ror);
5571}
5572
5573void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5574 HandleBinaryOp(ror);
5575}
5576
Serban Constantinescu02164b32014-11-13 14:05:07 +00005577void LocationsBuilderARM64::VisitShl(HShl* shl) {
5578 HandleShift(shl);
5579}
5580
5581void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5582 HandleShift(shl);
5583}
5584
5585void LocationsBuilderARM64::VisitShr(HShr* shr) {
5586 HandleShift(shr);
5587}
5588
5589void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5590 HandleShift(shr);
5591}
5592
Alexandre Rames5319def2014-10-23 10:03:10 +01005593void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005594 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005595}
5596
5597void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005598 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005599}
5600
Alexandre Rames67555f72014-11-18 10:55:16 +00005601void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005602 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005603}
5604
5605void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005606 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005607}
5608
5609void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005610 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005611}
5612
Alexandre Rames67555f72014-11-18 10:55:16 +00005613void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005614 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005615}
5616
Calin Juravlee460d1d2015-09-29 04:52:17 +01005617void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5618 HUnresolvedInstanceFieldGet* instruction) {
5619 FieldAccessCallingConventionARM64 calling_convention;
5620 codegen_->CreateUnresolvedFieldLocationSummary(
5621 instruction, instruction->GetFieldType(), calling_convention);
5622}
5623
5624void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5625 HUnresolvedInstanceFieldGet* instruction) {
5626 FieldAccessCallingConventionARM64 calling_convention;
5627 codegen_->GenerateUnresolvedFieldAccess(instruction,
5628 instruction->GetFieldType(),
5629 instruction->GetFieldIndex(),
5630 instruction->GetDexPc(),
5631 calling_convention);
5632}
5633
5634void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5635 HUnresolvedInstanceFieldSet* instruction) {
5636 FieldAccessCallingConventionARM64 calling_convention;
5637 codegen_->CreateUnresolvedFieldLocationSummary(
5638 instruction, instruction->GetFieldType(), calling_convention);
5639}
5640
5641void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5642 HUnresolvedInstanceFieldSet* instruction) {
5643 FieldAccessCallingConventionARM64 calling_convention;
5644 codegen_->GenerateUnresolvedFieldAccess(instruction,
5645 instruction->GetFieldType(),
5646 instruction->GetFieldIndex(),
5647 instruction->GetDexPc(),
5648 calling_convention);
5649}
5650
5651void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5652 HUnresolvedStaticFieldGet* instruction) {
5653 FieldAccessCallingConventionARM64 calling_convention;
5654 codegen_->CreateUnresolvedFieldLocationSummary(
5655 instruction, instruction->GetFieldType(), calling_convention);
5656}
5657
5658void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5659 HUnresolvedStaticFieldGet* instruction) {
5660 FieldAccessCallingConventionARM64 calling_convention;
5661 codegen_->GenerateUnresolvedFieldAccess(instruction,
5662 instruction->GetFieldType(),
5663 instruction->GetFieldIndex(),
5664 instruction->GetDexPc(),
5665 calling_convention);
5666}
5667
5668void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5669 HUnresolvedStaticFieldSet* instruction) {
5670 FieldAccessCallingConventionARM64 calling_convention;
5671 codegen_->CreateUnresolvedFieldLocationSummary(
5672 instruction, instruction->GetFieldType(), calling_convention);
5673}
5674
5675void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5676 HUnresolvedStaticFieldSet* instruction) {
5677 FieldAccessCallingConventionARM64 calling_convention;
5678 codegen_->GenerateUnresolvedFieldAccess(instruction,
5679 instruction->GetFieldType(),
5680 instruction->GetFieldIndex(),
5681 instruction->GetDexPc(),
5682 calling_convention);
5683}
5684
Alexandre Rames5319def2014-10-23 10:03:10 +01005685void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005686 LocationSummary* locations =
5687 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005688 // In suspend check slow path, usually there are no caller-save registers at all.
5689 // If SIMD instructions are present, however, we force spilling all live SIMD
5690 // registers in full width (since the runtime only saves/restores lower part).
5691 locations->SetCustomSlowPathCallerSaves(
5692 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005693}
5694
5695void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005696 HBasicBlock* block = instruction->GetBlock();
5697 if (block->GetLoopInformation() != nullptr) {
5698 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5699 // The back edge will generate the suspend check.
5700 return;
5701 }
5702 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5703 // The goto will generate the suspend check.
5704 return;
5705 }
5706 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005707}
5708
Alexandre Rames67555f72014-11-18 10:55:16 +00005709void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5710 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005711 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005712 InvokeRuntimeCallingConvention calling_convention;
5713 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5714}
5715
5716void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005717 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005718 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005719}
5720
5721void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5722 LocationSummary* locations =
5723 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5724 Primitive::Type input_type = conversion->GetInputType();
5725 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005726 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005727 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5728 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5729 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5730 }
5731
Alexandre Rames542361f2015-01-29 16:57:31 +00005732 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005733 locations->SetInAt(0, Location::RequiresFpuRegister());
5734 } else {
5735 locations->SetInAt(0, Location::RequiresRegister());
5736 }
5737
Alexandre Rames542361f2015-01-29 16:57:31 +00005738 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005739 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5740 } else {
5741 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5742 }
5743}
5744
5745void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5746 Primitive::Type result_type = conversion->GetResultType();
5747 Primitive::Type input_type = conversion->GetInputType();
5748
5749 DCHECK_NE(input_type, result_type);
5750
Alexandre Rames542361f2015-01-29 16:57:31 +00005751 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005752 int result_size = Primitive::ComponentSize(result_type);
5753 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005754 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005755 Register output = OutputRegister(conversion);
5756 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005757 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005758 // 'int' values are used directly as W registers, discarding the top
5759 // bits, so we don't need to sign-extend and can just perform a move.
5760 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5761 // top 32 bits of the target register. We theoretically could leave those
5762 // bits unchanged, but we would have to make sure that no code uses a
5763 // 32bit input value as a 64bit value assuming that the top 32 bits are
5764 // zero.
5765 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005766 } else if (result_type == Primitive::kPrimChar ||
5767 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5768 __ Ubfx(output,
5769 output.IsX() ? source.X() : source.W(),
5770 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005771 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005772 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005773 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005774 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005775 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005776 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005777 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5778 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005779 } else if (Primitive::IsFloatingPointType(result_type) &&
5780 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005781 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5782 } else {
5783 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5784 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005785 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005786}
Alexandre Rames67555f72014-11-18 10:55:16 +00005787
Serban Constantinescu02164b32014-11-13 14:05:07 +00005788void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5789 HandleShift(ushr);
5790}
5791
5792void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5793 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005794}
5795
5796void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5797 HandleBinaryOp(instruction);
5798}
5799
5800void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5801 HandleBinaryOp(instruction);
5802}
5803
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005804void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005805 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005806 LOG(FATAL) << "Unreachable";
5807}
5808
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005809void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005810 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005811 LOG(FATAL) << "Unreachable";
5812}
5813
Mark Mendellfe57faa2015-09-18 09:26:15 -04005814// Simple implementation of packed switch - generate cascaded compare/jumps.
5815void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5816 LocationSummary* locations =
5817 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5818 locations->SetInAt(0, Location::RequiresRegister());
5819}
5820
5821void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5822 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005823 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005824 Register value_reg = InputRegisterAt(switch_instr, 0);
5825 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5826
Zheng Xu3927c8b2015-11-18 17:46:25 +08005827 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005828 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005829 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5830 // make sure we don't emit it if the target may run out of range.
5831 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5832 // ranges and emit the tables only as required.
5833 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005834
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005835 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005836 // Current instruction id is an upper bound of the number of HIRs in the graph.
5837 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5838 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005839 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5840 Register temp = temps.AcquireW();
5841 __ Subs(temp, value_reg, Operand(lower_bound));
5842
Zheng Xu3927c8b2015-11-18 17:46:25 +08005843 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005844 // Jump to successors[0] if value == lower_bound.
5845 __ B(eq, codegen_->GetLabelOf(successors[0]));
5846 int32_t last_index = 0;
5847 for (; num_entries - last_index > 2; last_index += 2) {
5848 __ Subs(temp, temp, Operand(2));
5849 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5850 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5851 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5852 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5853 }
5854 if (num_entries - last_index == 2) {
5855 // The last missing case_value.
5856 __ Cmp(temp, Operand(1));
5857 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005858 }
5859
5860 // And the default for any other value.
5861 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5862 __ B(codegen_->GetLabelOf(default_block));
5863 }
5864 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005865 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005866
5867 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5868
5869 // Below instructions should use at most one blocked register. Since there are two blocked
5870 // registers, we are free to block one.
5871 Register temp_w = temps.AcquireW();
5872 Register index;
5873 // Remove the bias.
5874 if (lower_bound != 0) {
5875 index = temp_w;
5876 __ Sub(index, value_reg, Operand(lower_bound));
5877 } else {
5878 index = value_reg;
5879 }
5880
5881 // Jump to default block if index is out of the range.
5882 __ Cmp(index, Operand(num_entries));
5883 __ B(hs, codegen_->GetLabelOf(default_block));
5884
5885 // In current VIXL implementation, it won't require any blocked registers to encode the
5886 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5887 // register pressure.
5888 Register table_base = temps.AcquireX();
5889 // Load jump offset from the table.
5890 __ Adr(table_base, jump_table->GetTableStartLabel());
5891 Register jump_offset = temp_w;
5892 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5893
5894 // Jump to target block by branching to table_base(pc related) + offset.
5895 Register target_address = table_base;
5896 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5897 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005898 }
5899}
5900
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005901void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5902 HInstruction* instruction,
5903 Location out,
5904 uint32_t offset,
5905 Location maybe_temp,
5906 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005907 Primitive::Type type = Primitive::kPrimNot;
5908 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005909 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005910 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005911 if (kUseBakerReadBarrier) {
5912 // Load with fast path based Baker's read barrier.
5913 // /* HeapReference<Object> */ out = *(out + offset)
5914 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5915 out,
5916 out_reg,
5917 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005918 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005919 /* needs_null_check */ false,
5920 /* use_load_acquire */ false);
5921 } else {
5922 // Load with slow path based read barrier.
5923 // Save the value of `out` into `maybe_temp` before overwriting it
5924 // in the following move operation, as we will need it for the
5925 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005926 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005927 __ Mov(temp_reg, out_reg);
5928 // /* HeapReference<Object> */ out = *(out + offset)
5929 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5930 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5931 }
5932 } else {
5933 // Plain load with no read barrier.
5934 // /* HeapReference<Object> */ out = *(out + offset)
5935 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5936 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5937 }
5938}
5939
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005940void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5941 HInstruction* instruction,
5942 Location out,
5943 Location obj,
5944 uint32_t offset,
5945 Location maybe_temp,
5946 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005947 Primitive::Type type = Primitive::kPrimNot;
5948 Register out_reg = RegisterFrom(out, type);
5949 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005950 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005951 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005952 if (kUseBakerReadBarrier) {
5953 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005954 // /* HeapReference<Object> */ out = *(obj + offset)
5955 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5956 out,
5957 obj_reg,
5958 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005959 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005960 /* needs_null_check */ false,
5961 /* use_load_acquire */ false);
5962 } else {
5963 // Load with slow path based read barrier.
5964 // /* HeapReference<Object> */ out = *(obj + offset)
5965 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5966 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5967 }
5968 } else {
5969 // Plain load with no read barrier.
5970 // /* HeapReference<Object> */ out = *(obj + offset)
5971 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5972 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5973 }
5974}
5975
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005976void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5977 HInstruction* instruction,
5978 Location root,
5979 Register obj,
5980 uint32_t offset,
5981 vixl::aarch64::Label* fixup_label,
5982 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005983 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005984 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005985 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005986 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005987 if (kUseBakerReadBarrier) {
5988 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005989 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005990 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5991 !Runtime::Current()->UseJitCompilation()) {
5992 // Note that we do not actually check the value of `GetIsGcMarking()`
5993 // to decide whether to mark the loaded GC root or not. Instead, we
Vladimir Marko66d691d2017-04-07 17:53:39 +01005994 // load into `temp` (actually IP1) the read barrier mark introspection
5995 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
5996 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005997 //
5998 // We use link-time generated thunks for the slow path. That thunk
5999 // checks the reference and jumps to the entrypoint if needed.
6000 //
6001 // temp = Thread::Current()->pReadBarrierMarkIntrospection
6002 // lr = &return_address;
6003 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6004 // if (temp != nullptr) {
6005 // goto gc_root_thunk<root_reg>(lr)
6006 // }
6007 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00006008
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006009 UseScratchRegisterScope temps(GetVIXLAssembler());
6010 DCHECK(temps.IsAvailable(ip0));
6011 DCHECK(temps.IsAvailable(ip1));
6012 temps.Exclude(ip0, ip1);
6013 uint32_t custom_data =
6014 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
6015 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00006016
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006017 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6018 DCHECK_EQ(ip0.GetCode(), 16u);
6019 const int32_t entry_point_offset =
6020 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6021 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
6022 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6023 vixl::aarch64::Label return_address;
6024 __ adr(lr, &return_address);
6025 if (fixup_label != nullptr) {
6026 __ Bind(fixup_label);
6027 }
6028 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6029 "GC root LDR must be 2 instruction (8B) before the return address label.");
6030 __ ldr(root_reg, MemOperand(obj.X(), offset));
6031 __ Bind(cbnz_label);
6032 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6033 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006034 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006035 // Note that we do not actually check the value of
6036 // `GetIsGcMarking()` to decide whether to mark the loaded GC
6037 // root or not. Instead, we load into `temp` the read barrier
6038 // mark entry point corresponding to register `root`. If `temp`
6039 // is null, it means that `GetIsGcMarking()` is false, and vice
6040 // versa.
6041 //
6042 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6043 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6044 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6045 // // Slow path.
6046 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
6047 // }
Roland Levillain44015862016-01-22 11:47:17 +00006048
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006049 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
6050 Register temp = lr;
6051 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
6052 instruction, root, /* entrypoint */ LocationFrom(temp));
6053 codegen_->AddSlowPath(slow_path);
6054
6055 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6056 const int32_t entry_point_offset =
6057 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
6058 // Loading the entrypoint does not require a load acquire since it is only changed when
6059 // threads are suspended or running a checkpoint.
6060 __ Ldr(temp, MemOperand(tr, entry_point_offset));
6061
6062 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6063 if (fixup_label == nullptr) {
6064 __ Ldr(root_reg, MemOperand(obj, offset));
6065 } else {
6066 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
6067 }
6068 static_assert(
6069 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6070 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6071 "have different sizes.");
6072 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6073 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6074 "have different sizes.");
6075
6076 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6077 // checking GetIsGcMarking.
6078 __ Cbnz(temp, slow_path->GetEntryLabel());
6079 __ Bind(slow_path->GetExitLabel());
6080 }
Roland Levillain44015862016-01-22 11:47:17 +00006081 } else {
6082 // GC root loaded through a slow path for read barriers other
6083 // than Baker's.
6084 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006085 if (fixup_label == nullptr) {
6086 __ Add(root_reg.X(), obj.X(), offset);
6087 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006088 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006089 }
Roland Levillain44015862016-01-22 11:47:17 +00006090 // /* mirror::Object* */ root = root->Read()
6091 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6092 }
6093 } else {
6094 // Plain GC root load with no read barrier.
6095 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006096 if (fixup_label == nullptr) {
6097 __ Ldr(root_reg, MemOperand(obj, offset));
6098 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006099 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006100 }
Roland Levillain44015862016-01-22 11:47:17 +00006101 // Note that GC roots are not affected by heap poisoning, thus we
6102 // do not have to unpoison `root_reg` here.
6103 }
6104}
6105
6106void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6107 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006108 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006109 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006110 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006111 bool needs_null_check,
6112 bool use_load_acquire) {
6113 DCHECK(kEmitCompilerReadBarrier);
6114 DCHECK(kUseBakerReadBarrier);
6115
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006116 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6117 !use_load_acquire &&
6118 !Runtime::Current()->UseJitCompilation()) {
6119 // Note that we do not actually check the value of `GetIsGcMarking()`
Vladimir Marko66d691d2017-04-07 17:53:39 +01006120 // to decide whether to mark the loaded reference or not. Instead, we
6121 // load into `temp` (actually IP1) the read barrier mark introspection
6122 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6123 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006124 //
6125 // We use link-time generated thunks for the slow path. That thunk checks
6126 // the holder and jumps to the entrypoint if needed. If the holder is not
6127 // gray, it creates a fake dependency and returns to the LDR instruction.
6128 //
6129 // temp = Thread::Current()->pReadBarrierMarkIntrospection
Vladimir Marko66d691d2017-04-07 17:53:39 +01006130 // lr = &gray_return_address;
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006131 // if (temp != nullptr) {
6132 // goto field_thunk<holder_reg, base_reg>(lr)
6133 // }
6134 // not_gray_return_address:
6135 // // Original reference load. If the offset is too large to fit
6136 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006137 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006138 // gray_return_address:
6139
6140 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6141 Register base = obj;
6142 if (offset >= kReferenceLoadMinFarOffset) {
6143 DCHECK(maybe_temp.IsRegister());
6144 base = WRegisterFrom(maybe_temp);
6145 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6146 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6147 offset &= (kReferenceLoadMinFarOffset - 1u);
6148 }
6149 UseScratchRegisterScope temps(GetVIXLAssembler());
6150 DCHECK(temps.IsAvailable(ip0));
6151 DCHECK(temps.IsAvailable(ip1));
6152 temps.Exclude(ip0, ip1);
6153 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6154 base.GetCode(),
6155 obj.GetCode());
6156 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6157
6158 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6159 DCHECK_EQ(ip0.GetCode(), 16u);
6160 const int32_t entry_point_offset =
6161 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6162 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
Vladimir Markod1ef8732017-04-18 13:55:13 +01006163 EmissionCheckScope guard(GetVIXLAssembler(),
6164 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006165 vixl::aarch64::Label return_address;
6166 __ adr(lr, &return_address);
6167 __ Bind(cbnz_label);
6168 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markod1ef8732017-04-18 13:55:13 +01006169 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6170 "Field LDR must be 1 instruction (4B) before the return address label; "
6171 " 2 instructions (8B) for heap poisoning.");
6172 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6173 __ ldr(ref_reg, MemOperand(base.X(), offset));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006174 if (needs_null_check) {
6175 MaybeRecordImplicitNullCheck(instruction);
6176 }
Vladimir Markod1ef8732017-04-18 13:55:13 +01006177 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006178 __ Bind(&return_address);
6179 return;
6180 }
6181
Roland Levillain44015862016-01-22 11:47:17 +00006182 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006183 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006184 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006185 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006186 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6187 ref,
6188 obj,
6189 offset,
6190 no_index,
6191 no_scale_factor,
6192 temp,
6193 needs_null_check,
6194 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006195}
6196
6197void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6198 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006199 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006200 uint32_t data_offset,
6201 Location index,
6202 Register temp,
6203 bool needs_null_check) {
6204 DCHECK(kEmitCompilerReadBarrier);
6205 DCHECK(kUseBakerReadBarrier);
6206
Vladimir Marko66d691d2017-04-07 17:53:39 +01006207 static_assert(
6208 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6209 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6210 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
6211
6212 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6213 !Runtime::Current()->UseJitCompilation()) {
6214 // Note that we do not actually check the value of `GetIsGcMarking()`
6215 // to decide whether to mark the loaded reference or not. Instead, we
6216 // load into `temp` (actually IP1) the read barrier mark introspection
6217 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6218 // false, and vice versa.
6219 //
6220 // We use link-time generated thunks for the slow path. That thunk checks
6221 // the holder and jumps to the entrypoint if needed. If the holder is not
6222 // gray, it creates a fake dependency and returns to the LDR instruction.
6223 //
6224 // temp = Thread::Current()->pReadBarrierMarkIntrospection
6225 // lr = &gray_return_address;
6226 // if (temp != nullptr) {
6227 // goto field_thunk<holder_reg, base_reg>(lr)
6228 // }
6229 // not_gray_return_address:
6230 // // Original reference load. If the offset is too large to fit
6231 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006232 // HeapReference<mirror::Object> reference = data[index];
Vladimir Marko66d691d2017-04-07 17:53:39 +01006233 // gray_return_address:
6234
6235 DCHECK(index.IsValid());
6236 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
6237 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6238
6239 UseScratchRegisterScope temps(GetVIXLAssembler());
6240 DCHECK(temps.IsAvailable(ip0));
6241 DCHECK(temps.IsAvailable(ip1));
6242 temps.Exclude(ip0, ip1);
6243 uint32_t custom_data =
6244 linker::Arm64RelativePatcher::EncodeBakerReadBarrierArrayData(temp.GetCode());
6245 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6246
6247 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6248 DCHECK_EQ(ip0.GetCode(), 16u);
6249 const int32_t entry_point_offset =
6250 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6251 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
6252 __ Add(temp.X(), obj.X(), Operand(data_offset));
6253 EmissionCheckScope guard(GetVIXLAssembler(),
6254 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6255 vixl::aarch64::Label return_address;
6256 __ adr(lr, &return_address);
6257 __ Bind(cbnz_label);
6258 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6259 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6260 "Array LDR must be 1 instruction (4B) before the return address label; "
6261 " 2 instructions (8B) for heap poisoning.");
6262 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6263 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6264 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6265 __ Bind(&return_address);
6266 return;
6267 }
6268
Roland Levillain44015862016-01-22 11:47:17 +00006269 // Array cells are never volatile variables, therefore array loads
6270 // never use Load-Acquire instructions on ARM64.
6271 const bool use_load_acquire = false;
6272
6273 // /* HeapReference<Object> */ ref =
6274 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006275 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6276 ref,
6277 obj,
6278 data_offset,
6279 index,
6280 scale_factor,
6281 temp,
6282 needs_null_check,
6283 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006284}
6285
6286void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6287 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006288 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006289 uint32_t offset,
6290 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006291 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006292 Register temp,
6293 bool needs_null_check,
Roland Levillainff487002017-03-07 16:50:01 +00006294 bool use_load_acquire) {
Roland Levillain44015862016-01-22 11:47:17 +00006295 DCHECK(kEmitCompilerReadBarrier);
6296 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006297 // If we are emitting an array load, we should not be using a
6298 // Load Acquire instruction. In other words:
6299 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6300 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006301
Roland Levillain54f869e2017-03-06 13:54:11 +00006302 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6303 // whether we need to enter the slow path to mark the reference.
6304 // Then, in the slow path, check the gray bit in the lock word of
6305 // the reference's holder (`obj`) to decide whether to mark `ref` or
6306 // not.
Roland Levillain44015862016-01-22 11:47:17 +00006307 //
Roland Levillainba650a42017-03-06 13:52:32 +00006308 // Note that we do not actually check the value of `GetIsGcMarking()`;
6309 // instead, we load into `temp2` the read barrier mark entry point
6310 // corresponding to register `ref`. If `temp2` is null, it means
6311 // that `GetIsGcMarking()` is false, and vice versa.
6312 //
6313 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00006314 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6315 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006316 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6317 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6318 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6319 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6320 // if (is_gray) {
6321 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6322 // }
6323 // } else {
6324 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006325 // }
Roland Levillain44015862016-01-22 11:47:17 +00006326
Roland Levillainba650a42017-03-06 13:52:32 +00006327 // Slow path marking the object `ref` when the GC is marking. The
6328 // entrypoint will already be loaded in `temp2`.
6329 Register temp2 = lr;
6330 Location temp2_loc = LocationFrom(temp2);
Roland Levillainff487002017-03-07 16:50:01 +00006331 SlowPathCodeARM64* slow_path =
6332 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6333 instruction,
6334 ref,
6335 obj,
6336 offset,
6337 index,
6338 scale_factor,
6339 needs_null_check,
6340 use_load_acquire,
6341 temp,
6342 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006343 AddSlowPath(slow_path);
6344
6345 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6346 const int32_t entry_point_offset =
6347 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6348 // Loading the entrypoint does not require a load acquire since it is only changed when
6349 // threads are suspended or running a checkpoint.
6350 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006351 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6352 // checking GetIsGcMarking.
6353 __ Cbnz(temp2, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006354 // Fast path: the GC is not marking: just load the reference.
Roland Levillain54f869e2017-03-06 13:54:11 +00006355 GenerateRawReferenceLoad(
6356 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006357 __ Bind(slow_path->GetExitLabel());
6358}
6359
Roland Levillainff487002017-03-07 16:50:01 +00006360void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6361 Location ref,
6362 Register obj,
6363 Location field_offset,
6364 Register temp,
6365 bool needs_null_check,
6366 bool use_load_acquire) {
6367 DCHECK(kEmitCompilerReadBarrier);
6368 DCHECK(kUseBakerReadBarrier);
6369 // If we are emitting an array load, we should not be using a
6370 // Load Acquire instruction. In other words:
6371 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6372 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6373
6374 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6375 // whether we need to enter the slow path to update the reference
6376 // field within `obj`. Then, in the slow path, check the gray bit
6377 // in the lock word of the reference's holder (`obj`) to decide
6378 // whether to mark `ref` and update the field or not.
6379 //
6380 // Note that we do not actually check the value of `GetIsGcMarking()`;
6381 // instead, we load into `temp2` the read barrier mark entry point
6382 // corresponding to register `ref`. If `temp2` is null, it means
6383 // that `GetIsGcMarking()` is false, and vice versa.
6384 //
6385 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6386 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6387 // // Slow path.
6388 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6389 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6390 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6391 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6392 // if (is_gray) {
6393 // old_ref = ref;
6394 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6395 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6396 // }
6397 // }
6398
6399 // Slow path updating the object reference at address `obj + field_offset`
6400 // when the GC is marking. The entrypoint will already be loaded in `temp2`.
6401 Register temp2 = lr;
6402 Location temp2_loc = LocationFrom(temp2);
6403 SlowPathCodeARM64* slow_path =
6404 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6405 instruction,
6406 ref,
6407 obj,
6408 /* offset */ 0u,
6409 /* index */ field_offset,
6410 /* scale_factor */ 0u /* "times 1" */,
6411 needs_null_check,
6412 use_load_acquire,
6413 temp,
6414 /* entrypoint */ temp2_loc);
6415 AddSlowPath(slow_path);
6416
6417 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6418 const int32_t entry_point_offset =
6419 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6420 // Loading the entrypoint does not require a load acquire since it is only changed when
6421 // threads are suspended or running a checkpoint.
6422 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
6423 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6424 // checking GetIsGcMarking.
6425 __ Cbnz(temp2, slow_path->GetEntryLabel());
6426 // Fast path: the GC is not marking: nothing to do (the field is
6427 // up-to-date, and we don't need to load the reference).
6428 __ Bind(slow_path->GetExitLabel());
6429}
6430
Roland Levillainba650a42017-03-06 13:52:32 +00006431void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6432 Location ref,
6433 Register obj,
6434 uint32_t offset,
6435 Location index,
6436 size_t scale_factor,
6437 bool needs_null_check,
6438 bool use_load_acquire) {
6439 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006440 Primitive::Type type = Primitive::kPrimNot;
6441 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006442
Roland Levillainba650a42017-03-06 13:52:32 +00006443 // If needed, vixl::EmissionCheckScope guards are used to ensure
6444 // that no pools are emitted between the load (macro) instruction
6445 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006446
Roland Levillain44015862016-01-22 11:47:17 +00006447 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006448 // Load types involving an "index": ArrayGet,
6449 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6450 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006451 if (use_load_acquire) {
6452 // UnsafeGetObjectVolatile intrinsic case.
6453 // Register `index` is not an index in an object array, but an
6454 // offset to an object reference field within object `obj`.
6455 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6456 DCHECK(instruction->GetLocations()->Intrinsified());
6457 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6458 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006459 DCHECK_EQ(offset, 0u);
6460 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006461 DCHECK_EQ(needs_null_check, false);
6462 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006463 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6464 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006465 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006466 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6467 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006468 if (index.IsConstant()) {
6469 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006470 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006471 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006472 if (needs_null_check) {
6473 MaybeRecordImplicitNullCheck(instruction);
6474 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006475 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006476 UseScratchRegisterScope temps(GetVIXLAssembler());
6477 Register temp = temps.AcquireW();
6478 __ Add(temp, obj, offset);
6479 {
6480 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6481 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6482 if (needs_null_check) {
6483 MaybeRecordImplicitNullCheck(instruction);
6484 }
6485 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006486 }
Roland Levillain44015862016-01-22 11:47:17 +00006487 }
Roland Levillain44015862016-01-22 11:47:17 +00006488 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006489 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006490 MemOperand field = HeapOperand(obj, offset);
6491 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006492 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6493 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006494 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006495 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006496 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006497 if (needs_null_check) {
6498 MaybeRecordImplicitNullCheck(instruction);
6499 }
Roland Levillain44015862016-01-22 11:47:17 +00006500 }
6501 }
6502
6503 // Object* ref = ref_addr->AsMirrorPtr()
6504 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006505}
6506
6507void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6508 Location out,
6509 Location ref,
6510 Location obj,
6511 uint32_t offset,
6512 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006513 DCHECK(kEmitCompilerReadBarrier);
6514
Roland Levillain44015862016-01-22 11:47:17 +00006515 // Insert a slow path based read barrier *after* the reference load.
6516 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006517 // If heap poisoning is enabled, the unpoisoning of the loaded
6518 // reference will be carried out by the runtime within the slow
6519 // path.
6520 //
6521 // Note that `ref` currently does not get unpoisoned (when heap
6522 // poisoning is enabled), which is alright as the `ref` argument is
6523 // not used by the artReadBarrierSlow entry point.
6524 //
6525 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6526 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6527 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6528 AddSlowPath(slow_path);
6529
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006530 __ B(slow_path->GetEntryLabel());
6531 __ Bind(slow_path->GetExitLabel());
6532}
6533
Roland Levillain44015862016-01-22 11:47:17 +00006534void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6535 Location out,
6536 Location ref,
6537 Location obj,
6538 uint32_t offset,
6539 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006540 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006541 // Baker's read barriers shall be handled by the fast path
6542 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6543 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006544 // If heap poisoning is enabled, unpoisoning will be taken care of
6545 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006546 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006547 } else if (kPoisonHeapReferences) {
6548 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6549 }
6550}
6551
Roland Levillain44015862016-01-22 11:47:17 +00006552void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6553 Location out,
6554 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006555 DCHECK(kEmitCompilerReadBarrier);
6556
Roland Levillain44015862016-01-22 11:47:17 +00006557 // Insert a slow path based read barrier *after* the GC root load.
6558 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006559 // Note that GC roots are not affected by heap poisoning, so we do
6560 // not need to do anything special for this here.
6561 SlowPathCodeARM64* slow_path =
6562 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6563 AddSlowPath(slow_path);
6564
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006565 __ B(slow_path->GetEntryLabel());
6566 __ Bind(slow_path->GetExitLabel());
6567}
6568
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006569void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6570 LocationSummary* locations =
6571 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6572 locations->SetInAt(0, Location::RequiresRegister());
6573 locations->SetOut(Location::RequiresRegister());
6574}
6575
6576void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6577 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006578 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006579 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006580 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006581 __ Ldr(XRegisterFrom(locations->Out()),
6582 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006583 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006584 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006585 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006586 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6587 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006588 __ Ldr(XRegisterFrom(locations->Out()),
6589 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006590 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006591}
6592
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006593static void PatchJitRootUse(uint8_t* code,
6594 const uint8_t* roots_data,
6595 vixl::aarch64::Literal<uint32_t>* literal,
6596 uint64_t index_in_table) {
6597 uint32_t literal_offset = literal->GetOffset();
6598 uintptr_t address =
6599 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6600 uint8_t* data = code + literal_offset;
6601 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6602}
6603
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006604void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6605 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006606 const StringReference& string_reference = entry.first;
6607 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
6608 const auto it = jit_string_roots_.find(string_reference);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006609 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006610 uint64_t index_in_table = it->second;
6611 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006612 }
6613 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006614 const TypeReference& type_reference = entry.first;
6615 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
6616 const auto it = jit_class_roots_.find(type_reference);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006617 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006618 uint64_t index_in_table = it->second;
6619 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006620 }
6621}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006622
Alexandre Rames67555f72014-11-18 10:55:16 +00006623#undef __
6624#undef QUICK_ENTRY_POINT
6625
Alexandre Rames5319def2014-10-23 10:03:10 +01006626} // namespace arm64
6627} // namespace art