blob: 4955562f82978003569ec4c50bf32d392beae460 [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.
Mathieu Chartierdda73602017-04-17 12:50:45 -070093// Not yet implemented for heap poisoning.
94constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = !kPoisonHeapReferences;
95constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = !kPoisonHeapReferences;
Vladimir Markof4f2daa2017-03-20 18:26:59 +000096
97// Some instructions have special requirements for a temporary, for example
98// LoadClass/kBssEntry and LoadString/kBssEntry for Baker read barrier require
99// temp that's not an R0 (to avoid an extra move) and Baker read barrier field
100// loads with large offsets need a fixed register to limit the number of link-time
101// thunks we generate. For these and similar cases, we want to reserve a specific
102// register that's neither callee-save nor an argument register. We choose x15.
103inline Location FixedTempLocation() {
104 return Location::RegisterLocation(x15.GetCode());
105}
106
Alexandre Rames5319def2014-10-23 10:03:10 +0100107inline Condition ARM64Condition(IfCondition cond) {
108 switch (cond) {
109 case kCondEQ: return eq;
110 case kCondNE: return ne;
111 case kCondLT: return lt;
112 case kCondLE: return le;
113 case kCondGT: return gt;
114 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700115 case kCondB: return lo;
116 case kCondBE: return ls;
117 case kCondA: return hi;
118 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100119 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000120 LOG(FATAL) << "Unreachable";
121 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100122}
123
Vladimir Markod6e069b2016-01-18 11:11:01 +0000124inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
125 // The ARM64 condition codes can express all the necessary branches, see the
126 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
127 // There is no dex instruction or HIR that would need the missing conditions
128 // "equal or unordered" or "not equal".
129 switch (cond) {
130 case kCondEQ: return eq;
131 case kCondNE: return ne /* unordered */;
132 case kCondLT: return gt_bias ? cc : lt /* unordered */;
133 case kCondLE: return gt_bias ? ls : le /* unordered */;
134 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
135 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
136 default:
137 LOG(FATAL) << "UNREACHABLE";
138 UNREACHABLE();
139 }
140}
141
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000142Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000143 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
144 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
145 // but we use the exact registers for clarity.
146 if (return_type == Primitive::kPrimFloat) {
147 return LocationFrom(s0);
148 } else if (return_type == Primitive::kPrimDouble) {
149 return LocationFrom(d0);
150 } else if (return_type == Primitive::kPrimLong) {
151 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100152 } else if (return_type == Primitive::kPrimVoid) {
153 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000154 } else {
155 return LocationFrom(w0);
156 }
157}
158
Alexandre Rames5319def2014-10-23 10:03:10 +0100159Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000160 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100161}
162
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100163// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
164#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700165#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100166
Zheng Xuda403092015-04-24 17:35:39 +0800167// Calculate memory accessing operand for save/restore live registers.
168static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100169 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800170 int64_t spill_offset,
171 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100172 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
173 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
174 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800175 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100176 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800177 codegen->GetNumberOfFloatingPointRegisters()));
178
Vladimir Marko804b03f2016-09-14 16:26:36 +0100179 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100180 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
181 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800182
183 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
184 UseScratchRegisterScope temps(masm);
185
186 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100187 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
188 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800189 int64_t reg_size = kXRegSizeInBytes;
190 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
191 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100192 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800193 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
194 // If the offset does not fit in the instruction's immediate field, use an alternate register
195 // to compute the base address(float point registers spill base address).
196 Register new_base = temps.AcquireSameSizeAs(base);
197 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
198 base = new_base;
199 spill_offset = -core_spill_size;
200 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
201 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
202 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
203 }
204
205 if (is_save) {
206 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
207 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
208 } else {
209 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
210 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
211 }
212}
213
214void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800215 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100216 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
217 for (uint32_t i : LowToHighBits(core_spills)) {
218 // If the register holds an object, update the stack mask.
219 if (locations->RegisterContainsObject(i)) {
220 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800221 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100222 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
223 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
224 saved_core_stack_offsets_[i] = stack_offset;
225 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800226 }
227
Vladimir Marko804b03f2016-09-14 16:26:36 +0100228 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
229 for (uint32_t i : LowToHighBits(fp_spills)) {
230 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
231 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
232 saved_fpu_stack_offsets_[i] = stack_offset;
233 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800234 }
235
Vladimir Marko804b03f2016-09-14 16:26:36 +0100236 SaveRestoreLiveRegistersHelper(codegen,
237 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800238 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
239}
240
241void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100242 SaveRestoreLiveRegistersHelper(codegen,
243 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800244 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
245}
246
Alexandre Rames5319def2014-10-23 10:03:10 +0100247class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
248 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000249 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100250
Alexandre Rames67555f72014-11-18 10:55:16 +0000251 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100252 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000253 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100254
Alexandre Rames5319def2014-10-23 10:03:10 +0100255 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000256 if (instruction_->CanThrowIntoCatchBlock()) {
257 // Live registers will be restored in the catch block if caught.
258 SaveLiveRegisters(codegen, instruction_->GetLocations());
259 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000260 // We're moving two locations to locations that could overlap, so we need a parallel
261 // move resolver.
262 InvokeRuntimeCallingConvention calling_convention;
263 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100264 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
265 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000266 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
267 ? kQuickThrowStringBounds
268 : kQuickThrowArrayBounds;
269 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100270 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800271 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100272 }
273
Alexandre Rames8158f282015-08-07 10:26:17 +0100274 bool IsFatal() const OVERRIDE { return true; }
275
Alexandre Rames9931f312015-06-19 14:47:01 +0100276 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
277
Alexandre Rames5319def2014-10-23 10:03:10 +0100278 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100279 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
280};
281
Alexandre Rames67555f72014-11-18 10:55:16 +0000282class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
283 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000284 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000285
286 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
287 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
288 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000289 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800290 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000291 }
292
Alexandre Rames8158f282015-08-07 10:26:17 +0100293 bool IsFatal() const OVERRIDE { return true; }
294
Alexandre Rames9931f312015-06-19 14:47:01 +0100295 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
296
Alexandre Rames67555f72014-11-18 10:55:16 +0000297 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
299};
300
301class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
302 public:
303 LoadClassSlowPathARM64(HLoadClass* cls,
304 HInstruction* at,
305 uint32_t dex_pc,
Vladimir Markoea4c1262017-02-06 19:59:33 +0000306 bool do_clinit,
307 vixl::aarch64::Register bss_entry_temp = vixl::aarch64::Register(),
308 vixl::aarch64::Label* bss_entry_adrp_label = nullptr)
309 : SlowPathCodeARM64(at),
310 cls_(cls),
311 dex_pc_(dex_pc),
312 do_clinit_(do_clinit),
313 bss_entry_temp_(bss_entry_temp),
314 bss_entry_adrp_label_(bss_entry_adrp_label) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000315 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
316 }
317
318 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000319 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000320 Location out = locations->Out();
321 constexpr bool call_saves_everything_except_r0_ip0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexandre Rames67555f72014-11-18 10:55:16 +0000322 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
323
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000324 InvokeRuntimeCallingConvention calling_convention;
325 // For HLoadClass/kBssEntry/kSaveEverything, the page address of the entry is in a temp
326 // register, make sure it's not clobbered by the call or by saving/restoring registers.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000327 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
328 bool is_load_class_bss_entry =
329 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000330 if (is_load_class_bss_entry) {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000331 DCHECK(bss_entry_temp_.IsValid());
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000332 DCHECK(!bss_entry_temp_.Is(calling_convention.GetRegisterAt(0)));
333 DCHECK(
334 !UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(bss_entry_temp_));
Vladimir Markoea4c1262017-02-06 19:59:33 +0000335 }
336
Alexandre Rames67555f72014-11-18 10:55:16 +0000337 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000338 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000339
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000340 dex::TypeIndex type_index = cls_->GetTypeIndex();
341 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000342 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
343 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000344 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800345 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100346 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800347 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100348 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800349 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000350
351 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 if (out.IsValid()) {
353 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000354 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000355 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000357 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000358 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000359 if (is_load_class_bss_entry) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000360 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000361 const DexFile& dex_file = cls_->GetDexFile();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000362 if (call_saves_everything_except_r0_ip0) {
363 // The class entry page address was preserved in bss_entry_temp_ thanks to kSaveEverything.
364 } else {
365 // For non-Baker read barrier, we need to re-calculate the address of the class entry page.
366 bss_entry_adrp_label_ = arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
367 arm64_codegen->EmitAdrpPlaceholder(bss_entry_adrp_label_, bss_entry_temp_);
368 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000369 vixl::aarch64::Label* strp_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +0000370 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label_);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000371 {
372 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
373 __ Bind(strp_label);
374 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Markoea4c1262017-02-06 19:59:33 +0000375 MemOperand(bss_entry_temp_, /* offset placeholder */ 0));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000376 }
377 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000378 __ B(GetExitLabel());
379 }
380
Alexandre Rames9931f312015-06-19 14:47:01 +0100381 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
382
Alexandre Rames67555f72014-11-18 10:55:16 +0000383 private:
384 // The class this slow path will load.
385 HLoadClass* const cls_;
386
Alexandre Rames67555f72014-11-18 10:55:16 +0000387 // The dex PC of `at_`.
388 const uint32_t dex_pc_;
389
390 // Whether to initialize the class.
391 const bool do_clinit_;
392
Vladimir Markoea4c1262017-02-06 19:59:33 +0000393 // For HLoadClass/kBssEntry, the temp register and the label of the ADRP where it was loaded.
394 vixl::aarch64::Register bss_entry_temp_;
395 vixl::aarch64::Label* bss_entry_adrp_label_;
396
Alexandre Rames67555f72014-11-18 10:55:16 +0000397 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
398};
399
Vladimir Markoaad75c62016-10-03 08:46:48 +0000400class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
401 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100402 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
403 : SlowPathCodeARM64(instruction),
404 temp_(temp),
405 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000406
407 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
408 LocationSummary* locations = instruction_->GetLocations();
409 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
410 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
411
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000412 InvokeRuntimeCallingConvention calling_convention;
413 // Make sure `temp_` is not clobbered by the call or by saving/restoring registers.
414 DCHECK(temp_.IsValid());
415 DCHECK(!temp_.Is(calling_convention.GetRegisterAt(0)));
416 DCHECK(!UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(temp_));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100417
Vladimir Markoaad75c62016-10-03 08:46:48 +0000418 __ Bind(GetEntryLabel());
419 SaveLiveRegisters(codegen, locations);
420
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000421 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
422 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000423 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
424 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
425 Primitive::Type type = instruction_->GetType();
426 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
427
428 RestoreLiveRegisters(codegen, locations);
429
430 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000431 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100432 if (!kUseReadBarrier || kUseBakerReadBarrier) {
433 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
434 } else {
435 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
436 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
437 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
438 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000439 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100440 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000441 {
442 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
443 __ Bind(strp_label);
444 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100445 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000446 }
447
448 __ B(GetExitLabel());
449 }
450
451 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
452
453 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100454 const Register temp_;
455 vixl::aarch64::Label* adrp_label_;
456
Vladimir Markoaad75c62016-10-03 08:46:48 +0000457 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
458};
459
Alexandre Rames5319def2014-10-23 10:03:10 +0100460class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
461 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000462 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100463
Alexandre Rames67555f72014-11-18 10:55:16 +0000464 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
465 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100466 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000467 if (instruction_->CanThrowIntoCatchBlock()) {
468 // Live registers will be restored in the catch block if caught.
469 SaveLiveRegisters(codegen, instruction_->GetLocations());
470 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000471 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
472 instruction_,
473 instruction_->GetDexPc(),
474 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800475 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100476 }
477
Alexandre Rames8158f282015-08-07 10:26:17 +0100478 bool IsFatal() const OVERRIDE { return true; }
479
Alexandre Rames9931f312015-06-19 14:47:01 +0100480 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
481
Alexandre Rames5319def2014-10-23 10:03:10 +0100482 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100483 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
484};
485
486class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
487 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100488 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000489 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100490
Alexandre Rames67555f72014-11-18 10:55:16 +0000491 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100492 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000493 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100494 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100495 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000496 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800497 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100498 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000499 if (successor_ == nullptr) {
500 __ B(GetReturnLabel());
501 } else {
502 __ B(arm64_codegen->GetLabelOf(successor_));
503 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100504 }
505
Scott Wakeling97c72b72016-06-24 16:19:36 +0100506 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100507 DCHECK(successor_ == nullptr);
508 return &return_label_;
509 }
510
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100511 HBasicBlock* GetSuccessor() const {
512 return successor_;
513 }
514
Alexandre Rames9931f312015-06-19 14:47:01 +0100515 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
516
Alexandre Rames5319def2014-10-23 10:03:10 +0100517 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100518 // If not null, the block to branch to after the suspend check.
519 HBasicBlock* const successor_;
520
521 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100522 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100523
524 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
525};
526
Alexandre Rames67555f72014-11-18 10:55:16 +0000527class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
528 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000529 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000530 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000531
532 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000533 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800534
Alexandre Rames3e69f162014-12-10 10:36:50 +0000535 DCHECK(instruction_->IsCheckCast()
536 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
537 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100538 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000539
Alexandre Rames67555f72014-11-18 10:55:16 +0000540 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000541
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000542 if (!is_fatal_) {
543 SaveLiveRegisters(codegen, locations);
544 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000545
546 // We're moving two locations to locations that could overlap, so we need a parallel
547 // move resolver.
548 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800549 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800550 LocationFrom(calling_convention.GetRegisterAt(0)),
551 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800552 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800553 LocationFrom(calling_convention.GetRegisterAt(1)),
554 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000555 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000556 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800557 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000558 Primitive::Type ret_type = instruction_->GetType();
559 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
560 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
561 } else {
562 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800563 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
564 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000565 }
566
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000567 if (!is_fatal_) {
568 RestoreLiveRegisters(codegen, locations);
569 __ B(GetExitLabel());
570 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000571 }
572
Alexandre Rames9931f312015-06-19 14:47:01 +0100573 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100574 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100575
Alexandre Rames67555f72014-11-18 10:55:16 +0000576 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000577 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000578
Alexandre Rames67555f72014-11-18 10:55:16 +0000579 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
580};
581
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700582class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
583 public:
Aart Bik42249c32016-01-07 15:33:50 -0800584 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000585 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700586
587 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800588 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700589 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000590 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000591 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700592 }
593
Alexandre Rames9931f312015-06-19 14:47:01 +0100594 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
595
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700596 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700597 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
598};
599
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100600class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
601 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000602 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100603
604 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
605 LocationSummary* locations = instruction_->GetLocations();
606 __ Bind(GetEntryLabel());
607 SaveLiveRegisters(codegen, locations);
608
609 InvokeRuntimeCallingConvention calling_convention;
610 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
611 parallel_move.AddMove(
612 locations->InAt(0),
613 LocationFrom(calling_convention.GetRegisterAt(0)),
614 Primitive::kPrimNot,
615 nullptr);
616 parallel_move.AddMove(
617 locations->InAt(1),
618 LocationFrom(calling_convention.GetRegisterAt(1)),
619 Primitive::kPrimInt,
620 nullptr);
621 parallel_move.AddMove(
622 locations->InAt(2),
623 LocationFrom(calling_convention.GetRegisterAt(2)),
624 Primitive::kPrimNot,
625 nullptr);
626 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
627
628 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000629 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100630 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
631 RestoreLiveRegisters(codegen, locations);
632 __ B(GetExitLabel());
633 }
634
635 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
636
637 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100638 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
639};
640
Zheng Xu3927c8b2015-11-18 17:46:25 +0800641void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
642 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000643 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800644
645 // We are about to use the assembler to place literals directly. Make sure we have enough
646 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000647 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
648 num_entries * sizeof(int32_t),
649 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800650
651 __ Bind(&table_start_);
652 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
653 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100654 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800655 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100656 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800657 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
658 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
659 Literal<int32_t> literal(jump_offset);
660 __ place(&literal);
661 }
662}
663
Roland Levillain54f869e2017-03-06 13:54:11 +0000664// Abstract base class for read barrier slow paths marking a reference
665// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000666//
Roland Levillain54f869e2017-03-06 13:54:11 +0000667// Argument `entrypoint` must be a register location holding the read
668// barrier marking runtime entry point to be invoked.
669class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
670 protected:
671 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
672 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000673 DCHECK(kEmitCompilerReadBarrier);
674 }
675
Roland Levillain54f869e2017-03-06 13:54:11 +0000676 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000677
Roland Levillain54f869e2017-03-06 13:54:11 +0000678 // Generate assembly code calling the read barrier marking runtime
679 // entry point (ReadBarrierMarkRegX).
680 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000681 // No need to save live registers; it's taken care of by the
682 // entrypoint. Also, there is no need to update the stack mask,
683 // as this runtime call will not trigger a garbage collection.
684 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
685 DCHECK_NE(ref_.reg(), LR);
686 DCHECK_NE(ref_.reg(), WSP);
687 DCHECK_NE(ref_.reg(), WZR);
688 // IP0 is used internally by the ReadBarrierMarkRegX entry point
689 // as a temporary, it cannot be the entry point's input/output.
690 DCHECK_NE(ref_.reg(), IP0);
691 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
692 // "Compact" slow path, saving two moves.
693 //
694 // Instead of using the standard runtime calling convention (input
695 // and output in W0):
696 //
697 // W0 <- ref
698 // W0 <- ReadBarrierMark(W0)
699 // ref <- W0
700 //
701 // we just use rX (the register containing `ref`) as input and output
702 // of a dedicated entrypoint:
703 //
704 // rX <- ReadBarrierMarkRegX(rX)
705 //
706 if (entrypoint_.IsValid()) {
707 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
708 __ Blr(XRegisterFrom(entrypoint_));
709 } else {
710 // Entrypoint is not already loaded, load from the thread.
711 int32_t entry_point_offset =
712 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
713 // This runtime call does not require a stack map.
714 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
715 }
716 }
717
718 // The location (register) of the marked object reference.
719 const Location ref_;
720
721 // The location of the entrypoint if it is already loaded.
722 const Location entrypoint_;
723
Roland Levillain54f869e2017-03-06 13:54:11 +0000724 private:
725 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
726};
727
Alexandre Rames5319def2014-10-23 10:03:10 +0100728// Slow path marking an object reference `ref` during a read
729// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000730// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100731//
732// This means that after the execution of this slow path, `ref` will
733// always be up-to-date, but `obj.field` may not; i.e., after the
734// flip, `ref` will be a to-space reference, but `obj.field` will
735// probably still be a from-space reference (unless it gets updated by
736// another thread, or if another thread installed another object
737// reference (different from `ref`) in `obj.field`).
738//
739// If `entrypoint` is a valid location it is assumed to already be
740// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000741// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000742class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100743 public:
744 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
745 Location ref,
746 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000747 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100748 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100749 }
750
751 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
752
753 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000754 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100755 DCHECK(locations->CanCall());
756 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000757 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000758 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
759 << "Unexpected instruction in read barrier marking slow path: "
760 << instruction_->DebugName();
761
762 __ Bind(GetEntryLabel());
763 GenerateReadBarrierMarkRuntimeCall(codegen);
764 __ B(GetExitLabel());
765 }
766
767 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000768 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
769};
770
Roland Levillain54f869e2017-03-06 13:54:11 +0000771// Slow path loading `obj`'s lock word, loading a reference from
772// object `*(obj + offset + (index << scale_factor))` into `ref`, and
773// marking `ref` if `obj` is gray according to the lock word (Baker
774// read barrier). The field `obj.field` in the object `obj` holding
775// this reference does not get updated by this slow path after marking
776// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
777// below for that).
778//
779// This means that after the execution of this slow path, `ref` will
780// always be up-to-date, but `obj.field` may not; i.e., after the
781// flip, `ref` will be a to-space reference, but `obj.field` will
782// probably still be a from-space reference (unless it gets updated by
783// another thread, or if another thread installed another object
784// reference (different from `ref`) in `obj.field`).
785//
786// Argument `entrypoint` must be a register location holding the read
787// barrier marking runtime entry point to be invoked.
788class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
789 public:
790 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
791 Location ref,
792 Register obj,
793 uint32_t offset,
794 Location index,
795 size_t scale_factor,
796 bool needs_null_check,
797 bool use_load_acquire,
798 Register temp,
799 Location entrypoint)
800 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
801 obj_(obj),
802 offset_(offset),
803 index_(index),
804 scale_factor_(scale_factor),
805 needs_null_check_(needs_null_check),
806 use_load_acquire_(use_load_acquire),
807 temp_(temp) {
808 DCHECK(kEmitCompilerReadBarrier);
809 DCHECK(kUseBakerReadBarrier);
810 }
811
812 const char* GetDescription() const OVERRIDE {
813 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
814 }
815
816 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
817 LocationSummary* locations = instruction_->GetLocations();
818 DCHECK(locations->CanCall());
819 DCHECK(ref_.IsRegister()) << ref_;
820 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
821 DCHECK(obj_.IsW());
822 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100823 DCHECK(instruction_->IsInstanceFieldGet() ||
824 instruction_->IsStaticFieldGet() ||
825 instruction_->IsArrayGet() ||
826 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100827 instruction_->IsInstanceOf() ||
828 instruction_->IsCheckCast() ||
829 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
830 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
831 << "Unexpected instruction in read barrier marking slow path: "
832 << instruction_->DebugName();
833 // The read barrier instrumentation of object ArrayGet
834 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000835 // instruction.
836 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000837 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
838
Roland Levillain54f869e2017-03-06 13:54:11 +0000839 // Temporary register `temp_`, used to store the lock word, must
840 // not be IP0 nor IP1, as we may use them to emit the reference
841 // load (in the call to GenerateRawReferenceLoad below), and we
842 // need the lock word to still be in `temp_` after the reference
843 // load.
844 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
845 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
846
Alexandre Rames5319def2014-10-23 10:03:10 +0100847 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000848
849 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
850 // inserted after the original load. However, in fast path based
851 // Baker's read barriers, we need to perform the load of
852 // mirror::Object::monitor_ *before* the original reference load.
853 // This load-load ordering is required by the read barrier.
854 // The fast path/slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100855 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000856 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
857 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
858 // HeapReference<mirror::Object> ref = *src; // Original reference load.
859 // bool is_gray = (rb_state == ReadBarrier::GrayState());
860 // if (is_gray) {
861 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
862 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000863 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000864 // Note: the original implementation in ReadBarrier::Barrier is
865 // slightly more complex as it performs additional checks that we do
866 // not do here for performance reasons.
867
868 // /* int32_t */ monitor = obj->monitor_
869 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
870 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
871 if (needs_null_check_) {
872 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100873 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000874 // /* LockWord */ lock_word = LockWord(monitor)
875 static_assert(sizeof(LockWord) == sizeof(int32_t),
876 "art::LockWord and int32_t have different sizes.");
877
878 // Introduce a dependency on the lock_word including rb_state,
879 // to prevent load-load reordering, and without using
880 // a memory barrier (which would be more expensive).
881 // `obj` is unchanged by this operation, but its value now depends
882 // on `temp`.
883 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
884
885 // The actual reference load.
886 // A possible implicit null check has already been handled above.
887 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
888 arm64_codegen->GenerateRawReferenceLoad(instruction_,
889 ref_,
890 obj_,
891 offset_,
892 index_,
893 scale_factor_,
894 /* needs_null_check */ false,
895 use_load_acquire_);
896
897 // Mark the object `ref` when `obj` is gray.
898 //
899 // if (rb_state == ReadBarrier::GrayState())
900 // ref = ReadBarrier::Mark(ref);
901 //
902 // Given the numeric representation, it's enough to check the low bit of the rb_state.
903 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
904 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
905 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
906 GenerateReadBarrierMarkRuntimeCall(codegen);
907
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000908 __ B(GetExitLabel());
909 }
910
911 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000912 // The register containing the object holding the marked object reference field.
913 Register obj_;
914 // The offset, index and scale factor to access the reference in `obj_`.
915 uint32_t offset_;
916 Location index_;
917 size_t scale_factor_;
918 // Is a null check required?
919 bool needs_null_check_;
920 // Should this reference load use Load-Acquire semantics?
921 bool use_load_acquire_;
922 // A temporary register used to hold the lock word of `obj_`.
923 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000924
Roland Levillain54f869e2017-03-06 13:54:11 +0000925 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000926};
927
Roland Levillain54f869e2017-03-06 13:54:11 +0000928// Slow path loading `obj`'s lock word, loading a reference from
929// object `*(obj + offset + (index << scale_factor))` into `ref`, and
930// marking `ref` if `obj` is gray according to the lock word (Baker
931// read barrier). If needed, this slow path also atomically updates
932// the field `obj.field` in the object `obj` holding this reference
933// after marking (contrary to
934// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
935// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100936//
937// This means that after the execution of this slow path, both `ref`
938// and `obj.field` will be up-to-date; i.e., after the flip, both will
939// hold the same to-space reference (unless another thread installed
940// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000941//
Roland Levillain54f869e2017-03-06 13:54:11 +0000942// Argument `entrypoint` must be a register location holding the read
943// barrier marking runtime entry point to be invoked.
944class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
945 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100946 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000947 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(HInstruction* instruction,
948 Location ref,
949 Register obj,
950 uint32_t offset,
951 Location index,
952 size_t scale_factor,
953 bool needs_null_check,
954 bool use_load_acquire,
955 Register temp,
956 Location entrypoint)
957 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100958 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000959 offset_(offset),
960 index_(index),
961 scale_factor_(scale_factor),
962 needs_null_check_(needs_null_check),
963 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000964 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100965 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000966 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100967 }
968
969 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000970 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100971 }
972
973 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
974 LocationSummary* locations = instruction_->GetLocations();
975 Register ref_reg = WRegisterFrom(ref_);
976 DCHECK(locations->CanCall());
977 DCHECK(ref_.IsRegister()) << ref_;
978 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000979 DCHECK(obj_.IsW());
980 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
981
982 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100983 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
984 << "Unexpected instruction in read barrier marking and field updating slow path: "
985 << instruction_->DebugName();
986 DCHECK(instruction_->GetLocations()->Intrinsified());
987 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000988 DCHECK_EQ(offset_, 0u);
989 DCHECK_EQ(scale_factor_, 0u);
990 DCHECK_EQ(use_load_acquire_, false);
991 // The location of the offset of the marked reference field within `obj_`.
992 Location field_offset = index_;
993 DCHECK(field_offset.IsRegister()) << field_offset;
994
995 // Temporary register `temp_`, used to store the lock word, must
996 // not be IP0 nor IP1, as we may use them to emit the reference
997 // load (in the call to GenerateRawReferenceLoad below), and we
998 // need the lock word to still be in `temp_` after the reference
999 // load.
1000 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1001 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001002
1003 __ Bind(GetEntryLabel());
1004
Roland Levillain54f869e2017-03-06 13:54:11 +00001005 // /* int32_t */ monitor = obj->monitor_
1006 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1007 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
1008 if (needs_null_check_) {
1009 codegen->MaybeRecordImplicitNullCheck(instruction_);
1010 }
1011 // /* LockWord */ lock_word = LockWord(monitor)
1012 static_assert(sizeof(LockWord) == sizeof(int32_t),
1013 "art::LockWord and int32_t have different sizes.");
1014
1015 // Introduce a dependency on the lock_word including rb_state,
1016 // to prevent load-load reordering, and without using
1017 // a memory barrier (which would be more expensive).
1018 // `obj` is unchanged by this operation, but its value now depends
1019 // on `temp`.
1020 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
1021
1022 // The actual reference load.
1023 // A possible implicit null check has already been handled above.
1024 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1025 arm64_codegen->GenerateRawReferenceLoad(instruction_,
1026 ref_,
1027 obj_,
1028 offset_,
1029 index_,
1030 scale_factor_,
1031 /* needs_null_check */ false,
1032 use_load_acquire_);
1033
1034 // Mark the object `ref` when `obj` is gray.
1035 //
1036 // if (rb_state == ReadBarrier::GrayState())
1037 // ref = ReadBarrier::Mark(ref);
1038 //
1039 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1040 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1041 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1042 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1043
1044 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001045 // Note that we cannot use IP to save the old reference, as IP is
1046 // used internally by the ReadBarrierMarkRegX entry point, and we
1047 // need the old reference after the call to that entry point.
1048 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1049 __ Mov(temp_.W(), ref_reg);
1050
Roland Levillain54f869e2017-03-06 13:54:11 +00001051 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001052
1053 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001054 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001055 //
1056 // Note that this field could also hold a different object, if
1057 // another thread had concurrently changed it. In that case, the
1058 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1059 // (CAS) operation below would abort the CAS, leaving the field
1060 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001061 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001062 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001063
1064 // Update the the holder's field atomically. This may fail if
1065 // mutator updates before us, but it's OK. This is achieved
1066 // using a strong compare-and-set (CAS) operation with relaxed
1067 // memory synchronization ordering, where the expected value is
1068 // the old reference and the desired value is the new reference.
1069
1070 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1071 UseScratchRegisterScope temps(masm);
1072
1073 // Convenience aliases.
1074 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001075 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001076 Register expected = temp_.W();
1077 Register value = ref_reg;
1078 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1079 Register tmp_value = temps.AcquireW(); // Value in memory.
1080
1081 __ Add(tmp_ptr, base.X(), Operand(offset));
1082
1083 if (kPoisonHeapReferences) {
1084 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1085 if (value.Is(expected)) {
1086 // Do not poison `value`, as it is the same register as
1087 // `expected`, which has just been poisoned.
1088 } else {
1089 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1090 }
1091 }
1092
1093 // do {
1094 // tmp_value = [tmp_ptr] - expected;
1095 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1096
Roland Levillain24a4d112016-10-26 13:10:46 +01001097 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001098 __ Bind(&loop_head);
1099 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1100 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001101 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001102 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1103 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001104 __ B(&exit_loop);
1105 __ Bind(&comparison_failed);
1106 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001107 __ Bind(&exit_loop);
1108
1109 if (kPoisonHeapReferences) {
1110 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1111 if (value.Is(expected)) {
1112 // Do not unpoison `value`, as it is the same register as
1113 // `expected`, which has just been unpoisoned.
1114 } else {
1115 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1116 }
1117 }
1118
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001119 __ B(GetExitLabel());
1120 }
1121
1122 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001123 // The register containing the object holding the marked object reference field.
1124 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001125 // The offset, index and scale factor to access the reference in `obj_`.
1126 uint32_t offset_;
1127 Location index_;
1128 size_t scale_factor_;
1129 // Is a null check required?
1130 bool needs_null_check_;
1131 // Should this reference load use Load-Acquire semantics?
1132 bool use_load_acquire_;
1133 // A temporary register used to hold the lock word of `obj_`; and
1134 // also to hold the original reference value, when the reference is
1135 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001136 const Register temp_;
1137
Roland Levillain54f869e2017-03-06 13:54:11 +00001138 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001139};
1140
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001141// Slow path generating a read barrier for a heap reference.
1142class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1143 public:
1144 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1145 Location out,
1146 Location ref,
1147 Location obj,
1148 uint32_t offset,
1149 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001150 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001151 out_(out),
1152 ref_(ref),
1153 obj_(obj),
1154 offset_(offset),
1155 index_(index) {
1156 DCHECK(kEmitCompilerReadBarrier);
1157 // If `obj` is equal to `out` or `ref`, it means the initial object
1158 // has been overwritten by (or after) the heap object reference load
1159 // to be instrumented, e.g.:
1160 //
1161 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001162 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001163 //
1164 // In that case, we have lost the information about the original
1165 // object, and the emitted read barrier cannot work properly.
1166 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1167 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1168 }
1169
1170 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1171 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1172 LocationSummary* locations = instruction_->GetLocations();
1173 Primitive::Type type = Primitive::kPrimNot;
1174 DCHECK(locations->CanCall());
1175 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001176 DCHECK(instruction_->IsInstanceFieldGet() ||
1177 instruction_->IsStaticFieldGet() ||
1178 instruction_->IsArrayGet() ||
1179 instruction_->IsInstanceOf() ||
1180 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001181 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001182 << "Unexpected instruction in read barrier for heap reference slow path: "
1183 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001184 // The read barrier instrumentation of object ArrayGet
1185 // instructions does not support the HIntermediateAddress
1186 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001187 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001188 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001189
1190 __ Bind(GetEntryLabel());
1191
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001192 SaveLiveRegisters(codegen, locations);
1193
1194 // We may have to change the index's value, but as `index_` is a
1195 // constant member (like other "inputs" of this slow path),
1196 // introduce a copy of it, `index`.
1197 Location index = index_;
1198 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001199 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001200 if (instruction_->IsArrayGet()) {
1201 // Compute the actual memory offset and store it in `index`.
1202 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
1203 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1204 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1205 // We are about to change the value of `index_reg` (see the
1206 // calls to vixl::MacroAssembler::Lsl and
1207 // vixl::MacroAssembler::Mov below), but it has
1208 // not been saved by the previous call to
1209 // art::SlowPathCode::SaveLiveRegisters, as it is a
1210 // callee-save register --
1211 // art::SlowPathCode::SaveLiveRegisters does not consider
1212 // callee-save registers, as it has been designed with the
1213 // assumption that callee-save registers are supposed to be
1214 // handled by the called function. So, as a callee-save
1215 // register, `index_reg` _would_ eventually be saved onto
1216 // the stack, but it would be too late: we would have
1217 // changed its value earlier. Therefore, we manually save
1218 // it here into another freely available register,
1219 // `free_reg`, chosen of course among the caller-save
1220 // registers (as a callee-save `free_reg` register would
1221 // exhibit the same problem).
1222 //
1223 // Note we could have requested a temporary register from
1224 // the register allocator instead; but we prefer not to, as
1225 // this is a slow path, and we know we can find a
1226 // caller-save register that is available.
1227 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1228 __ Mov(free_reg.W(), index_reg);
1229 index_reg = free_reg;
1230 index = LocationFrom(index_reg);
1231 } else {
1232 // The initial register stored in `index_` has already been
1233 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1234 // (as it is not a callee-save register), so we can freely
1235 // use it.
1236 }
1237 // Shifting the index value contained in `index_reg` by the scale
1238 // factor (2) cannot overflow in practice, as the runtime is
1239 // unable to allocate object arrays with a size larger than
1240 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1241 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1242 static_assert(
1243 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1244 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1245 __ Add(index_reg, index_reg, Operand(offset_));
1246 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001247 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1248 // intrinsics, `index_` is not shifted by a scale factor of 2
1249 // (as in the case of ArrayGet), as it is actually an offset
1250 // to an object field within an object.
1251 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001252 DCHECK(instruction_->GetLocations()->Intrinsified());
1253 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1254 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1255 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001256 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001257 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001258 }
1259 }
1260
1261 // We're moving two or three locations to locations that could
1262 // overlap, so we need a parallel move resolver.
1263 InvokeRuntimeCallingConvention calling_convention;
1264 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1265 parallel_move.AddMove(ref_,
1266 LocationFrom(calling_convention.GetRegisterAt(0)),
1267 type,
1268 nullptr);
1269 parallel_move.AddMove(obj_,
1270 LocationFrom(calling_convention.GetRegisterAt(1)),
1271 type,
1272 nullptr);
1273 if (index.IsValid()) {
1274 parallel_move.AddMove(index,
1275 LocationFrom(calling_convention.GetRegisterAt(2)),
1276 Primitive::kPrimInt,
1277 nullptr);
1278 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1279 } else {
1280 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1281 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1282 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001283 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001284 instruction_,
1285 instruction_->GetDexPc(),
1286 this);
1287 CheckEntrypointTypes<
1288 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1289 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1290
1291 RestoreLiveRegisters(codegen, locations);
1292
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001293 __ B(GetExitLabel());
1294 }
1295
1296 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1297
1298 private:
1299 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001300 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1301 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001302 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1303 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1304 return Register(VIXLRegCodeFromART(i), kXRegSize);
1305 }
1306 }
1307 // We shall never fail to find a free caller-save register, as
1308 // there are more than two core caller-save registers on ARM64
1309 // (meaning it is possible to find one which is different from
1310 // `ref` and `obj`).
1311 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1312 LOG(FATAL) << "Could not find a free register";
1313 UNREACHABLE();
1314 }
1315
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001316 const Location out_;
1317 const Location ref_;
1318 const Location obj_;
1319 const uint32_t offset_;
1320 // An additional location containing an index to an array.
1321 // Only used for HArrayGet and the UnsafeGetObject &
1322 // UnsafeGetObjectVolatile intrinsics.
1323 const Location index_;
1324
1325 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1326};
1327
1328// Slow path generating a read barrier for a GC root.
1329class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1330 public:
1331 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001332 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001333 DCHECK(kEmitCompilerReadBarrier);
1334 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001335
1336 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1337 LocationSummary* locations = instruction_->GetLocations();
1338 Primitive::Type type = Primitive::kPrimNot;
1339 DCHECK(locations->CanCall());
1340 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001341 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1342 << "Unexpected instruction in read barrier for GC root slow path: "
1343 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001344
1345 __ Bind(GetEntryLabel());
1346 SaveLiveRegisters(codegen, locations);
1347
1348 InvokeRuntimeCallingConvention calling_convention;
1349 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1350 // The argument of the ReadBarrierForRootSlow is not a managed
1351 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1352 // thus we need a 64-bit move here, and we cannot use
1353 //
1354 // arm64_codegen->MoveLocation(
1355 // LocationFrom(calling_convention.GetRegisterAt(0)),
1356 // root_,
1357 // type);
1358 //
1359 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1360 // reference type (`Primitive::kPrimNot`).
1361 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001362 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001363 instruction_,
1364 instruction_->GetDexPc(),
1365 this);
1366 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1367 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1368
1369 RestoreLiveRegisters(codegen, locations);
1370 __ B(GetExitLabel());
1371 }
1372
1373 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1374
1375 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001376 const Location out_;
1377 const Location root_;
1378
1379 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1380};
1381
Alexandre Rames5319def2014-10-23 10:03:10 +01001382#undef __
1383
1384Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
1385 Location next_location;
1386 if (type == Primitive::kPrimVoid) {
1387 LOG(FATAL) << "Unreachable type " << type;
1388 }
1389
1390 if (Primitive::IsFloatingPointType(type) &&
1391 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001392 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1393 } else if (!Primitive::IsFloatingPointType(type) &&
1394 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1395 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1396 } else {
1397 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001398 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1399 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001400 }
1401
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001402 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001403 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001404 return next_location;
1405}
1406
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001407Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001408 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001409}
1410
Serban Constantinescu579885a2015-02-22 20:51:33 +00001411CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1412 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001413 const CompilerOptions& compiler_options,
1414 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001415 : CodeGenerator(graph,
1416 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001417 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001418 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001419 callee_saved_core_registers.GetList(),
1420 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001421 compiler_options,
1422 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001423 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001424 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001425 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001426 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001427 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001428 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001429 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001430 uint32_literals_(std::less<uint32_t>(),
1431 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001432 uint64_literals_(std::less<uint64_t>(),
1433 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001434 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1435 boot_image_string_patches_(StringReferenceValueComparator(),
1436 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1437 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001438 boot_image_type_patches_(TypeReferenceValueComparator(),
1439 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1440 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001441 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001442 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001443 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001444 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1445 jit_class_patches_(TypeReferenceValueComparator(),
1446 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001447 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001448 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001449}
Alexandre Rames5319def2014-10-23 10:03:10 +01001450
Alexandre Rames67555f72014-11-18 10:55:16 +00001451#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001452
Zheng Xu3927c8b2015-11-18 17:46:25 +08001453void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001454 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001455 jump_table->EmitTable(this);
1456 }
1457}
1458
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001459void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001460 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001461 // Ensure we emit the literal pool.
1462 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001463
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001464 CodeGenerator::Finalize(allocator);
1465}
1466
Zheng Xuad4450e2015-04-17 18:48:56 +08001467void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1468 // Note: There are 6 kinds of moves:
1469 // 1. constant -> GPR/FPR (non-cycle)
1470 // 2. constant -> stack (non-cycle)
1471 // 3. GPR/FPR -> GPR/FPR
1472 // 4. GPR/FPR -> stack
1473 // 5. stack -> GPR/FPR
1474 // 6. stack -> stack (non-cycle)
1475 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1476 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1477 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1478 // dependency.
1479 vixl_temps_.Open(GetVIXLAssembler());
1480}
1481
1482void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1483 vixl_temps_.Close();
1484}
1485
1486Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001487 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1488 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1489 || kind == Location::kSIMDStackSlot);
1490 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1491 ? Location::kFpuRegister
1492 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001493 Location scratch = GetScratchLocation(kind);
1494 if (!scratch.Equals(Location::NoLocation())) {
1495 return scratch;
1496 }
1497 // Allocate from VIXL temp registers.
1498 if (kind == Location::kRegister) {
1499 scratch = LocationFrom(vixl_temps_.AcquireX());
1500 } else {
1501 DCHECK(kind == Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001502 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1503 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1504 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001505 }
1506 AddScratchLocation(scratch);
1507 return scratch;
1508}
1509
1510void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1511 if (loc.IsRegister()) {
1512 vixl_temps_.Release(XRegisterFrom(loc));
1513 } else {
1514 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001515 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001516 }
1517 RemoveScratchLocation(loc);
1518}
1519
Alexandre Rames3e69f162014-12-10 10:36:50 +00001520void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001521 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001522 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001523}
1524
Alexandre Rames5319def2014-10-23 10:03:10 +01001525void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001526 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001527 __ Bind(&frame_entry_label_);
1528
Serban Constantinescu02164b32014-11-13 14:05:07 +00001529 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1530 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001531 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001532 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001533 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001534 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001535 {
1536 // Ensure that between load and RecordPcInfo there are no pools emitted.
1537 ExactAssemblyScope eas(GetVIXLAssembler(),
1538 kInstructionSize,
1539 CodeBufferCheckScope::kExactSize);
1540 __ ldr(wzr, MemOperand(temp, 0));
1541 RecordPcInfo(nullptr, 0);
1542 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001543 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001544
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001545 if (!HasEmptyFrame()) {
1546 int frame_size = GetFrameSize();
1547 // Stack layout:
1548 // sp[frame_size - 8] : lr.
1549 // ... : other preserved core registers.
1550 // ... : other preserved fp registers.
1551 // ... : reserved frame space.
1552 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001553
1554 // Save the current method if we need it. Note that we do not
1555 // do this in HCurrentMethod, as the instruction might have been removed
1556 // in the SSA graph.
1557 if (RequiresCurrentMethod()) {
1558 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001559 } else {
1560 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001561 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001562 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001563 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1564 frame_size - GetCoreSpillSize());
1565 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1566 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001567
1568 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1569 // Initialize should_deoptimize flag to 0.
1570 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1571 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1572 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001573 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001574}
1575
1576void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001577 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001578 if (!HasEmptyFrame()) {
1579 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001580 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1581 frame_size - FrameEntrySpillSize());
1582 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1583 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001584 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001585 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001586 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001587 __ Ret();
1588 GetAssembler()->cfi().RestoreState();
1589 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001590}
1591
Scott Wakeling97c72b72016-06-24 16:19:36 +01001592CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001593 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001594 return CPURegList(CPURegister::kRegister, kXRegSize,
1595 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001596}
1597
Scott Wakeling97c72b72016-06-24 16:19:36 +01001598CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001599 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1600 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001601 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1602 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001603}
1604
Alexandre Rames5319def2014-10-23 10:03:10 +01001605void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1606 __ Bind(GetLabelOf(block));
1607}
1608
Calin Juravle175dc732015-08-25 15:42:32 +01001609void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1610 DCHECK(location.IsRegister());
1611 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1612}
1613
Calin Juravlee460d1d2015-09-29 04:52:17 +01001614void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1615 if (location.IsRegister()) {
1616 locations->AddTemp(location);
1617 } else {
1618 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1619 }
1620}
1621
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001622void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001623 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001624 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001625 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001626 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001627 if (value_can_be_null) {
1628 __ Cbz(value, &done);
1629 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001630 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001631 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001632 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001633 if (value_can_be_null) {
1634 __ Bind(&done);
1635 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001636}
1637
David Brazdil58282f42016-01-14 12:45:10 +00001638void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001639 // Blocked core registers:
1640 // lr : Runtime reserved.
1641 // tr : Runtime reserved.
1642 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1643 // ip1 : VIXL core temp.
1644 // ip0 : VIXL core temp.
1645 //
1646 // Blocked fp registers:
1647 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001648 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1649 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001650 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001651 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001652 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001653
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001654 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001655 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001656 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001657 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001658
David Brazdil58282f42016-01-14 12:45:10 +00001659 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001660 // Stubs do not save callee-save floating point registers. If the graph
1661 // is debuggable, we need to deal with these registers differently. For
1662 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001663 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1664 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001665 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001666 }
1667 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001668}
1669
Alexandre Rames3e69f162014-12-10 10:36:50 +00001670size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1671 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1672 __ Str(reg, MemOperand(sp, stack_index));
1673 return kArm64WordSize;
1674}
1675
1676size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1677 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1678 __ Ldr(reg, MemOperand(sp, stack_index));
1679 return kArm64WordSize;
1680}
1681
1682size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1683 FPRegister reg = FPRegister(reg_id, kDRegSize);
1684 __ Str(reg, MemOperand(sp, stack_index));
1685 return kArm64WordSize;
1686}
1687
1688size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1689 FPRegister reg = FPRegister(reg_id, kDRegSize);
1690 __ Ldr(reg, MemOperand(sp, stack_index));
1691 return kArm64WordSize;
1692}
1693
Alexandre Rames5319def2014-10-23 10:03:10 +01001694void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001695 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001696}
1697
1698void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001699 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001700}
1701
Alexandre Rames67555f72014-11-18 10:55:16 +00001702void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001703 if (constant->IsIntConstant()) {
1704 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1705 } else if (constant->IsLongConstant()) {
1706 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1707 } else if (constant->IsNullConstant()) {
1708 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001709 } else if (constant->IsFloatConstant()) {
1710 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1711 } else {
1712 DCHECK(constant->IsDoubleConstant());
1713 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1714 }
1715}
1716
Alexandre Rames3e69f162014-12-10 10:36:50 +00001717
1718static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1719 DCHECK(constant.IsConstant());
1720 HConstant* cst = constant.GetConstant();
1721 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001722 // Null is mapped to a core W register, which we associate with kPrimInt.
1723 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001724 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1725 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1726 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1727}
1728
Roland Levillain558dea12017-01-27 19:40:44 +00001729// Allocate a scratch register from the VIXL pool, querying first into
1730// the floating-point register pool, and then the the core register
1731// pool. This is essentially a reimplementation of
1732// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1733// using a different allocation strategy.
1734static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1735 vixl::aarch64::UseScratchRegisterScope* temps,
1736 int size_in_bits) {
1737 return masm->GetScratchFPRegisterList()->IsEmpty()
1738 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1739 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1740}
1741
Calin Juravlee460d1d2015-09-29 04:52:17 +01001742void CodeGeneratorARM64::MoveLocation(Location destination,
1743 Location source,
1744 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001745 if (source.Equals(destination)) {
1746 return;
1747 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001748
1749 // A valid move can always be inferred from the destination and source
1750 // locations. When moving from and to a register, the argument type can be
1751 // used to generate 32bit instead of 64bit moves. In debug mode we also
1752 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001753 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001754
1755 if (destination.IsRegister() || destination.IsFpuRegister()) {
1756 if (unspecified_type) {
1757 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1758 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001759 (src_cst != nullptr && (src_cst->IsIntConstant()
1760 || src_cst->IsFloatConstant()
1761 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001762 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001763 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001764 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001765 // If the source is a double stack slot or a 64bit constant, a 64bit
1766 // type is appropriate. Else the source is a register, and since the
1767 // type has not been specified, we chose a 64bit type to force a 64bit
1768 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001769 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001770 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001771 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001772 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1773 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1774 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001775 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1776 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1777 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001778 } else if (source.IsSIMDStackSlot()) {
1779 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001780 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001781 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001782 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001783 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001784 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001785 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001786 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001787 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001788 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1789 ? Primitive::kPrimLong
1790 : Primitive::kPrimInt;
1791 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1792 }
1793 } else {
1794 DCHECK(source.IsFpuRegister());
1795 if (destination.IsRegister()) {
1796 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1797 ? Primitive::kPrimDouble
1798 : Primitive::kPrimFloat;
1799 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1800 } else {
1801 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001802 if (GetGraph()->HasSIMD()) {
1803 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1804 } else {
1805 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1806 }
1807 }
1808 }
1809 } else if (destination.IsSIMDStackSlot()) {
1810 if (source.IsFpuRegister()) {
1811 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1812 } else {
1813 DCHECK(source.IsSIMDStackSlot());
1814 UseScratchRegisterScope temps(GetVIXLAssembler());
1815 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1816 Register temp = temps.AcquireX();
1817 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1818 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1819 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1820 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1821 } else {
1822 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1823 __ Ldr(temp, StackOperandFrom(source));
1824 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001825 }
1826 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001827 } else { // The destination is not a register. It must be a stack slot.
1828 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1829 if (source.IsRegister() || source.IsFpuRegister()) {
1830 if (unspecified_type) {
1831 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001832 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001833 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001834 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001835 }
1836 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001837 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1838 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1839 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001840 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001841 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1842 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001843 UseScratchRegisterScope temps(GetVIXLAssembler());
1844 HConstant* src_cst = source.GetConstant();
1845 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001846 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001847 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1848 ? Register(xzr)
1849 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001850 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001851 if (src_cst->IsIntConstant()) {
1852 temp = temps.AcquireW();
1853 } else if (src_cst->IsLongConstant()) {
1854 temp = temps.AcquireX();
1855 } else if (src_cst->IsFloatConstant()) {
1856 temp = temps.AcquireS();
1857 } else {
1858 DCHECK(src_cst->IsDoubleConstant());
1859 temp = temps.AcquireD();
1860 }
1861 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001862 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001863 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001864 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001865 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001866 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001867 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001868 // Use any scratch register (a core or a floating-point one)
1869 // from VIXL scratch register pools as a temporary.
1870 //
1871 // We used to only use the FP scratch register pool, but in some
1872 // rare cases the only register from this pool (D31) would
1873 // already be used (e.g. within a ParallelMove instruction, when
1874 // a move is blocked by a another move requiring a scratch FP
1875 // register, which would reserve D31). To prevent this issue, we
1876 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001877 //
1878 // Also, we start by asking for a FP scratch register first, as the
1879 // demand of scratch core registers is higher. This is why we
1880 // use AcquireFPOrCoreCPURegisterOfSize instead of
1881 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1882 // allocates core scratch registers first.
1883 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1884 GetVIXLAssembler(),
1885 &temps,
1886 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001887 __ Ldr(temp, StackOperandFrom(source));
1888 __ Str(temp, StackOperandFrom(destination));
1889 }
1890 }
1891}
1892
1893void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001894 CPURegister dst,
1895 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001896 switch (type) {
1897 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001898 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001899 break;
1900 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001901 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001902 break;
1903 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001904 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001905 break;
1906 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001907 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001908 break;
1909 case Primitive::kPrimInt:
1910 case Primitive::kPrimNot:
1911 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001912 case Primitive::kPrimFloat:
1913 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001914 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001915 __ Ldr(dst, src);
1916 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001917 case Primitive::kPrimVoid:
1918 LOG(FATAL) << "Unreachable type " << type;
1919 }
1920}
1921
Calin Juravle77520bc2015-01-12 18:45:46 +00001922void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001923 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001924 const MemOperand& src,
1925 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001926 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001927 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001928 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001929 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001930
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001931 DCHECK(!src.IsPreIndex());
1932 DCHECK(!src.IsPostIndex());
1933
1934 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001935 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001936 {
1937 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1938 MemOperand base = MemOperand(temp_base);
1939 switch (type) {
1940 case Primitive::kPrimBoolean:
1941 {
1942 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1943 __ ldarb(Register(dst), base);
1944 if (needs_null_check) {
1945 MaybeRecordImplicitNullCheck(instruction);
1946 }
1947 }
1948 break;
1949 case Primitive::kPrimByte:
1950 {
1951 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1952 __ ldarb(Register(dst), base);
1953 if (needs_null_check) {
1954 MaybeRecordImplicitNullCheck(instruction);
1955 }
1956 }
1957 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1958 break;
1959 case Primitive::kPrimChar:
1960 {
1961 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1962 __ ldarh(Register(dst), base);
1963 if (needs_null_check) {
1964 MaybeRecordImplicitNullCheck(instruction);
1965 }
1966 }
1967 break;
1968 case Primitive::kPrimShort:
1969 {
1970 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1971 __ ldarh(Register(dst), base);
1972 if (needs_null_check) {
1973 MaybeRecordImplicitNullCheck(instruction);
1974 }
1975 }
1976 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1977 break;
1978 case Primitive::kPrimInt:
1979 case Primitive::kPrimNot:
1980 case Primitive::kPrimLong:
1981 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
1982 {
1983 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1984 __ ldar(Register(dst), base);
1985 if (needs_null_check) {
1986 MaybeRecordImplicitNullCheck(instruction);
1987 }
1988 }
1989 break;
1990 case Primitive::kPrimFloat:
1991 case Primitive::kPrimDouble: {
1992 DCHECK(dst.IsFPRegister());
1993 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001994
Artem Serov914d7a82017-02-07 14:33:49 +00001995 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1996 {
1997 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1998 __ ldar(temp, base);
1999 if (needs_null_check) {
2000 MaybeRecordImplicitNullCheck(instruction);
2001 }
2002 }
2003 __ Fmov(FPRegister(dst), temp);
2004 break;
Roland Levillain44015862016-01-22 11:47:17 +00002005 }
Artem Serov914d7a82017-02-07 14:33:49 +00002006 case Primitive::kPrimVoid:
2007 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002008 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002009 }
2010}
2011
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002012void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002013 CPURegister src,
2014 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002015 switch (type) {
2016 case Primitive::kPrimBoolean:
2017 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002018 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002019 break;
2020 case Primitive::kPrimChar:
2021 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002022 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002023 break;
2024 case Primitive::kPrimInt:
2025 case Primitive::kPrimNot:
2026 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002027 case Primitive::kPrimFloat:
2028 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00002029 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002030 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002031 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002032 case Primitive::kPrimVoid:
2033 LOG(FATAL) << "Unreachable type " << type;
2034 }
2035}
2036
Artem Serov914d7a82017-02-07 14:33:49 +00002037void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
2038 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002039 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002040 const MemOperand& dst,
2041 bool needs_null_check) {
2042 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002043 UseScratchRegisterScope temps(GetVIXLAssembler());
2044 Register temp_base = temps.AcquireX();
2045
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002046 DCHECK(!dst.IsPreIndex());
2047 DCHECK(!dst.IsPostIndex());
2048
2049 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002050 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002051 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002052 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002053 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002054 switch (type) {
2055 case Primitive::kPrimBoolean:
2056 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00002057 {
2058 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2059 __ stlrb(Register(src), base);
2060 if (needs_null_check) {
2061 MaybeRecordImplicitNullCheck(instruction);
2062 }
2063 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002064 break;
2065 case Primitive::kPrimChar:
2066 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00002067 {
2068 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2069 __ stlrh(Register(src), base);
2070 if (needs_null_check) {
2071 MaybeRecordImplicitNullCheck(instruction);
2072 }
2073 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002074 break;
2075 case Primitive::kPrimInt:
2076 case Primitive::kPrimNot:
2077 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002078 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002079 {
2080 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2081 __ stlr(Register(src), base);
2082 if (needs_null_check) {
2083 MaybeRecordImplicitNullCheck(instruction);
2084 }
2085 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002086 break;
2087 case Primitive::kPrimFloat:
2088 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002089 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002090 Register temp_src;
2091 if (src.IsZero()) {
2092 // The zero register is used to avoid synthesizing zero constants.
2093 temp_src = Register(src);
2094 } else {
2095 DCHECK(src.IsFPRegister());
2096 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2097 __ Fmov(temp_src, FPRegister(src));
2098 }
Artem Serov914d7a82017-02-07 14:33:49 +00002099 {
2100 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2101 __ stlr(temp_src, base);
2102 if (needs_null_check) {
2103 MaybeRecordImplicitNullCheck(instruction);
2104 }
2105 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002106 break;
2107 }
2108 case Primitive::kPrimVoid:
2109 LOG(FATAL) << "Unreachable type " << type;
2110 }
2111}
2112
Calin Juravle175dc732015-08-25 15:42:32 +01002113void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2114 HInstruction* instruction,
2115 uint32_t dex_pc,
2116 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002117 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002118
2119 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2120 {
2121 // Ensure the pc position is recorded immediately after the `blr` instruction.
2122 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2123 __ blr(lr);
2124 if (EntrypointRequiresStackMap(entrypoint)) {
2125 RecordPcInfo(instruction, dex_pc, slow_path);
2126 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002127 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002128}
2129
Roland Levillaindec8f632016-07-22 17:10:06 +01002130void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2131 HInstruction* instruction,
2132 SlowPathCode* slow_path) {
2133 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002134 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2135 __ Blr(lr);
2136}
2137
Alexandre Rames67555f72014-11-18 10:55:16 +00002138void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002139 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002140 UseScratchRegisterScope temps(GetVIXLAssembler());
2141 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002142 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2143
Serban Constantinescu02164b32014-11-13 14:05:07 +00002144 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002145 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2146 __ Add(temp, class_reg, status_offset);
2147 __ Ldar(temp, HeapOperand(temp));
2148 __ Cmp(temp, mirror::Class::kStatusInitialized);
2149 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002150 __ Bind(slow_path->GetExitLabel());
2151}
Alexandre Rames5319def2014-10-23 10:03:10 +01002152
Roland Levillain44015862016-01-22 11:47:17 +00002153void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002154 BarrierType type = BarrierAll;
2155
2156 switch (kind) {
2157 case MemBarrierKind::kAnyAny:
2158 case MemBarrierKind::kAnyStore: {
2159 type = BarrierAll;
2160 break;
2161 }
2162 case MemBarrierKind::kLoadAny: {
2163 type = BarrierReads;
2164 break;
2165 }
2166 case MemBarrierKind::kStoreStore: {
2167 type = BarrierWrites;
2168 break;
2169 }
2170 default:
2171 LOG(FATAL) << "Unexpected memory barrier " << kind;
2172 }
2173 __ Dmb(InnerShareable, type);
2174}
2175
Serban Constantinescu02164b32014-11-13 14:05:07 +00002176void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2177 HBasicBlock* successor) {
2178 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002179 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2180 if (slow_path == nullptr) {
2181 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2182 instruction->SetSlowPath(slow_path);
2183 codegen_->AddSlowPath(slow_path);
2184 if (successor != nullptr) {
2185 DCHECK(successor->IsLoopHeader());
2186 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2187 }
2188 } else {
2189 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2190 }
2191
Serban Constantinescu02164b32014-11-13 14:05:07 +00002192 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2193 Register temp = temps.AcquireW();
2194
Andreas Gampe542451c2016-07-26 09:02:02 -07002195 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002196 if (successor == nullptr) {
2197 __ Cbnz(temp, slow_path->GetEntryLabel());
2198 __ Bind(slow_path->GetReturnLabel());
2199 } else {
2200 __ Cbz(temp, codegen_->GetLabelOf(successor));
2201 __ B(slow_path->GetEntryLabel());
2202 // slow_path will return to GetLabelOf(successor).
2203 }
2204}
2205
Alexandre Rames5319def2014-10-23 10:03:10 +01002206InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2207 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002208 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002209 assembler_(codegen->GetAssembler()),
2210 codegen_(codegen) {}
2211
2212#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002213 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002214
2215#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2216
2217enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002218 // Using a base helps identify when we hit such breakpoints.
2219 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002220#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2221 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2222#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2223};
2224
2225#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002226 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002227 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2228 } \
2229 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2230 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2231 locations->SetOut(Location::Any()); \
2232 }
2233 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2234#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2235
2236#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002237#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002238
Alexandre Rames67555f72014-11-18 10:55:16 +00002239void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002240 DCHECK_EQ(instr->InputCount(), 2U);
2241 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2242 Primitive::Type type = instr->GetResultType();
2243 switch (type) {
2244 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002245 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002246 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002247 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002248 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002249 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002250
2251 case Primitive::kPrimFloat:
2252 case Primitive::kPrimDouble:
2253 locations->SetInAt(0, Location::RequiresFpuRegister());
2254 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002255 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002256 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002257
Alexandre Rames5319def2014-10-23 10:03:10 +01002258 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002259 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002260 }
2261}
2262
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002263void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2264 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002265 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2266
2267 bool object_field_get_with_read_barrier =
2268 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002269 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002270 new (GetGraph()->GetArena()) LocationSummary(instruction,
2271 object_field_get_with_read_barrier ?
2272 LocationSummary::kCallOnSlowPath :
2273 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002274 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002275 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002276 // We need a temporary register for the read barrier marking slow
2277 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002278 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2279 !Runtime::Current()->UseJitCompilation() &&
2280 !field_info.IsVolatile()) {
2281 // If link-time thunks for the Baker read barrier are enabled, for AOT
2282 // non-volatile loads we need a temporary only if the offset is too big.
2283 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2284 locations->AddTemp(FixedTempLocation());
2285 }
2286 } else {
2287 locations->AddTemp(Location::RequiresRegister());
2288 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002289 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002290 locations->SetInAt(0, Location::RequiresRegister());
2291 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2292 locations->SetOut(Location::RequiresFpuRegister());
2293 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002294 // The output overlaps for an object field get when read barriers
2295 // are enabled: we do not want the load to overwrite the object's
2296 // location, as we need it to emit the read barrier.
2297 locations->SetOut(
2298 Location::RequiresRegister(),
2299 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002300 }
2301}
2302
2303void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2304 const FieldInfo& field_info) {
2305 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002306 LocationSummary* locations = instruction->GetLocations();
2307 Location base_loc = locations->InAt(0);
2308 Location out = locations->Out();
2309 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002310 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002311 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002312
Roland Levillain44015862016-01-22 11:47:17 +00002313 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2314 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002315 // /* HeapReference<Object> */ out = *(base + offset)
2316 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002317 Location maybe_temp =
2318 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002319 // Note that potential implicit null checks are handled in this
2320 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2321 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2322 instruction,
2323 out,
2324 base,
2325 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002326 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002327 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002328 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002329 } else {
2330 // General case.
2331 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002332 // Note that a potential implicit null check is handled in this
2333 // CodeGeneratorARM64::LoadAcquire call.
2334 // NB: LoadAcquire will record the pc info if needed.
2335 codegen_->LoadAcquire(
2336 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002337 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002338 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2339 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002340 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002341 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002342 }
Roland Levillain44015862016-01-22 11:47:17 +00002343 if (field_type == Primitive::kPrimNot) {
2344 // If read barriers are enabled, emit read barriers other than
2345 // Baker's using a slow path (and also unpoison the loaded
2346 // reference, if heap poisoning is enabled).
2347 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2348 }
Roland Levillain4d027112015-07-01 15:41:14 +01002349 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002350}
2351
2352void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2353 LocationSummary* locations =
2354 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2355 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002356 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2357 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2358 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002359 locations->SetInAt(1, Location::RequiresFpuRegister());
2360 } else {
2361 locations->SetInAt(1, Location::RequiresRegister());
2362 }
2363}
2364
2365void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002366 const FieldInfo& field_info,
2367 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002368 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2369
2370 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002371 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002372 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002373 Offset offset = field_info.GetFieldOffset();
2374 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002375
Roland Levillain4d027112015-07-01 15:41:14 +01002376 {
2377 // We use a block to end the scratch scope before the write barrier, thus
2378 // freeing the temporary registers so they can be used in `MarkGCCard`.
2379 UseScratchRegisterScope temps(GetVIXLAssembler());
2380
2381 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2382 DCHECK(value.IsW());
2383 Register temp = temps.AcquireW();
2384 __ Mov(temp, value.W());
2385 GetAssembler()->PoisonHeapReference(temp.W());
2386 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002387 }
Roland Levillain4d027112015-07-01 15:41:14 +01002388
2389 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002390 codegen_->StoreRelease(
2391 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002392 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002393 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2394 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002395 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2396 codegen_->MaybeRecordImplicitNullCheck(instruction);
2397 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002398 }
2399
2400 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002401 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002402 }
2403}
2404
Alexandre Rames67555f72014-11-18 10:55:16 +00002405void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002406 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002407
2408 switch (type) {
2409 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002410 case Primitive::kPrimLong: {
2411 Register dst = OutputRegister(instr);
2412 Register lhs = InputRegisterAt(instr, 0);
2413 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002414 if (instr->IsAdd()) {
2415 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002416 } else if (instr->IsAnd()) {
2417 __ And(dst, lhs, rhs);
2418 } else if (instr->IsOr()) {
2419 __ Orr(dst, lhs, rhs);
2420 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002421 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002422 } else if (instr->IsRor()) {
2423 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002424 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002425 __ Ror(dst, lhs, shift);
2426 } else {
2427 // Ensure shift distance is in the same size register as the result. If
2428 // we are rotating a long and the shift comes in a w register originally,
2429 // we don't need to sxtw for use as an x since the shift distances are
2430 // all & reg_bits - 1.
2431 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2432 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002433 } else {
2434 DCHECK(instr->IsXor());
2435 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002436 }
2437 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002438 }
2439 case Primitive::kPrimFloat:
2440 case Primitive::kPrimDouble: {
2441 FPRegister dst = OutputFPRegister(instr);
2442 FPRegister lhs = InputFPRegisterAt(instr, 0);
2443 FPRegister rhs = InputFPRegisterAt(instr, 1);
2444 if (instr->IsAdd()) {
2445 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002446 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002447 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002448 } else {
2449 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002450 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002451 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002452 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002453 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002454 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002455 }
2456}
2457
Serban Constantinescu02164b32014-11-13 14:05:07 +00002458void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2459 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2460
2461 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2462 Primitive::Type type = instr->GetResultType();
2463 switch (type) {
2464 case Primitive::kPrimInt:
2465 case Primitive::kPrimLong: {
2466 locations->SetInAt(0, Location::RequiresRegister());
2467 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002469 break;
2470 }
2471 default:
2472 LOG(FATAL) << "Unexpected shift type " << type;
2473 }
2474}
2475
2476void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2477 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2478
2479 Primitive::Type type = instr->GetType();
2480 switch (type) {
2481 case Primitive::kPrimInt:
2482 case Primitive::kPrimLong: {
2483 Register dst = OutputRegister(instr);
2484 Register lhs = InputRegisterAt(instr, 0);
2485 Operand rhs = InputOperandAt(instr, 1);
2486 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002487 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002488 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002489 if (instr->IsShl()) {
2490 __ Lsl(dst, lhs, shift_value);
2491 } else if (instr->IsShr()) {
2492 __ Asr(dst, lhs, shift_value);
2493 } else {
2494 __ Lsr(dst, lhs, shift_value);
2495 }
2496 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002497 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002498
2499 if (instr->IsShl()) {
2500 __ Lsl(dst, lhs, rhs_reg);
2501 } else if (instr->IsShr()) {
2502 __ Asr(dst, lhs, rhs_reg);
2503 } else {
2504 __ Lsr(dst, lhs, rhs_reg);
2505 }
2506 }
2507 break;
2508 }
2509 default:
2510 LOG(FATAL) << "Unexpected shift operation type " << type;
2511 }
2512}
2513
Alexandre Rames5319def2014-10-23 10:03:10 +01002514void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002515 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002516}
2517
2518void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002519 HandleBinaryOp(instruction);
2520}
2521
2522void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2523 HandleBinaryOp(instruction);
2524}
2525
2526void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2527 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002528}
2529
Artem Serov7fc63502016-02-09 17:15:29 +00002530void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002531 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2532 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2533 locations->SetInAt(0, Location::RequiresRegister());
2534 // There is no immediate variant of negated bitwise instructions in AArch64.
2535 locations->SetInAt(1, Location::RequiresRegister());
2536 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2537}
2538
Artem Serov7fc63502016-02-09 17:15:29 +00002539void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002540 Register dst = OutputRegister(instr);
2541 Register lhs = InputRegisterAt(instr, 0);
2542 Register rhs = InputRegisterAt(instr, 1);
2543
2544 switch (instr->GetOpKind()) {
2545 case HInstruction::kAnd:
2546 __ Bic(dst, lhs, rhs);
2547 break;
2548 case HInstruction::kOr:
2549 __ Orn(dst, lhs, rhs);
2550 break;
2551 case HInstruction::kXor:
2552 __ Eon(dst, lhs, rhs);
2553 break;
2554 default:
2555 LOG(FATAL) << "Unreachable";
2556 }
2557}
2558
Anton Kirilov74234da2017-01-13 14:42:47 +00002559void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2560 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002561 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2562 instruction->GetType() == Primitive::kPrimLong);
2563 LocationSummary* locations =
2564 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2565 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2566 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2567 } else {
2568 locations->SetInAt(0, Location::RequiresRegister());
2569 }
2570 locations->SetInAt(1, Location::RequiresRegister());
2571 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2572}
2573
Anton Kirilov74234da2017-01-13 14:42:47 +00002574void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2575 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002576 Primitive::Type type = instruction->GetType();
2577 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2578 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2579 Register out = OutputRegister(instruction);
2580 Register left;
2581 if (kind != HInstruction::kNeg) {
2582 left = InputRegisterAt(instruction, 0);
2583 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002584 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002585 // shifter operand operation, the IR generating `right_reg` (input to the type
2586 // conversion) can have a different type from the current instruction's type,
2587 // so we manually indicate the type.
2588 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002589 Operand right_operand(0);
2590
Anton Kirilov74234da2017-01-13 14:42:47 +00002591 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2592 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002593 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2594 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002595 right_operand = Operand(right_reg,
2596 helpers::ShiftFromOpKind(op_kind),
2597 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002598 }
2599
2600 // Logical binary operations do not support extension operations in the
2601 // operand. Note that VIXL would still manage if it was passed by generating
2602 // the extension as a separate instruction.
2603 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2604 DCHECK(!right_operand.IsExtendedRegister() ||
2605 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2606 kind != HInstruction::kNeg));
2607 switch (kind) {
2608 case HInstruction::kAdd:
2609 __ Add(out, left, right_operand);
2610 break;
2611 case HInstruction::kAnd:
2612 __ And(out, left, right_operand);
2613 break;
2614 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002615 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002616 __ Neg(out, right_operand);
2617 break;
2618 case HInstruction::kOr:
2619 __ Orr(out, left, right_operand);
2620 break;
2621 case HInstruction::kSub:
2622 __ Sub(out, left, right_operand);
2623 break;
2624 case HInstruction::kXor:
2625 __ Eor(out, left, right_operand);
2626 break;
2627 default:
2628 LOG(FATAL) << "Unexpected operation kind: " << kind;
2629 UNREACHABLE();
2630 }
2631}
2632
Artem Serov328429f2016-07-06 16:23:04 +01002633void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002634 LocationSummary* locations =
2635 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2636 locations->SetInAt(0, Location::RequiresRegister());
2637 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002638 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002639}
2640
Roland Levillain19c54192016-11-04 13:44:09 +00002641void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002642 __ Add(OutputRegister(instruction),
2643 InputRegisterAt(instruction, 0),
2644 Operand(InputOperandAt(instruction, 1)));
2645}
2646
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002647void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002648 LocationSummary* locations =
2649 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002650 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2651 if (instr->GetOpKind() == HInstruction::kSub &&
2652 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002653 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002654 // Don't allocate register for Mneg instruction.
2655 } else {
2656 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2657 Location::RequiresRegister());
2658 }
2659 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2660 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002661 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2662}
2663
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002664void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002665 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002666 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2667 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002668
2669 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2670 // This fixup should be carried out for all multiply-accumulate instructions:
2671 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2672 if (instr->GetType() == Primitive::kPrimLong &&
2673 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2674 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002675 vixl::aarch64::Instruction* prev =
2676 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002677 if (prev->IsLoadOrStore()) {
2678 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002679 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002680 __ nop();
2681 }
2682 }
2683
2684 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002685 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002686 __ Madd(res, mul_left, mul_right, accumulator);
2687 } else {
2688 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002689 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002690 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002691 __ Mneg(res, mul_left, mul_right);
2692 } else {
2693 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2694 __ Msub(res, mul_left, mul_right, accumulator);
2695 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002696 }
2697}
2698
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002699void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002700 bool object_array_get_with_read_barrier =
2701 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002702 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002703 new (GetGraph()->GetArena()) LocationSummary(instruction,
2704 object_array_get_with_read_barrier ?
2705 LocationSummary::kCallOnSlowPath :
2706 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002707 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002708 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002709 // We need a temporary register for the read barrier marking slow
2710 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002711 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2712 !Runtime::Current()->UseJitCompilation() &&
2713 instruction->GetIndex()->IsConstant()) {
2714 // Array loads with constant index are treated as field loads.
2715 // If link-time thunks for the Baker read barrier are enabled, for AOT
2716 // constant index loads we need a temporary only if the offset is too big.
2717 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2718 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
2719 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
2720 if (offset >= kReferenceLoadMinFarOffset) {
2721 locations->AddTemp(FixedTempLocation());
2722 }
2723 } else {
2724 locations->AddTemp(Location::RequiresRegister());
2725 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002726 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002727 locations->SetInAt(0, Location::RequiresRegister());
2728 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002729 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2730 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2731 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002732 // The output overlaps in the case of an object array get with
2733 // read barriers enabled: we do not want the move to overwrite the
2734 // array's location, as we need it to emit the read barrier.
2735 locations->SetOut(
2736 Location::RequiresRegister(),
2737 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002738 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002739}
2740
2741void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002742 Primitive::Type type = instruction->GetType();
2743 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002744 LocationSummary* locations = instruction->GetLocations();
2745 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002746 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002747 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002748 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2749 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002750 MacroAssembler* masm = GetVIXLAssembler();
2751 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002752
Roland Levillain19c54192016-11-04 13:44:09 +00002753 // The read barrier instrumentation of object ArrayGet instructions
2754 // does not support the HIntermediateAddress instruction.
2755 DCHECK(!((type == Primitive::kPrimNot) &&
2756 instruction->GetArray()->IsIntermediateAddress() &&
2757 kEmitCompilerReadBarrier));
2758
Roland Levillain44015862016-01-22 11:47:17 +00002759 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2760 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002761 // Note that a potential implicit null check is handled in the
2762 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002763 if (index.IsConstant()) {
2764 // Array load with a constant index can be treated as a field load.
2765 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2766 Location maybe_temp =
2767 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2768 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2769 out,
2770 obj.W(),
2771 offset,
2772 maybe_temp,
2773 /* needs_null_check */ true,
2774 /* use_load_acquire */ false);
2775 } else {
2776 Register temp = WRegisterFrom(locations->GetTemp(0));
2777 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2778 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
2779 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002780 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002781 // General case.
2782 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002783 Register length;
2784 if (maybe_compressed_char_at) {
2785 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2786 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002787 {
2788 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2789 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2790
2791 if (instruction->GetArray()->IsIntermediateAddress()) {
2792 DCHECK_LT(count_offset, offset);
2793 int64_t adjusted_offset =
2794 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2795 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2796 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2797 } else {
2798 __ Ldr(length, HeapOperand(obj, count_offset));
2799 }
2800 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002801 }
jessicahandojo05765752016-09-09 19:01:32 -07002802 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002803 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002804 if (maybe_compressed_char_at) {
2805 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002806 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2807 "Expecting 0=compressed, 1=uncompressed");
2808 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002809 __ Ldrb(Register(OutputCPURegister(instruction)),
2810 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2811 __ B(&done);
2812 __ Bind(&uncompressed_load);
2813 __ Ldrh(Register(OutputCPURegister(instruction)),
2814 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2815 __ Bind(&done);
2816 } else {
2817 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2818 source = HeapOperand(obj, offset);
2819 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002820 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002821 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002822 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002823 // We do not need to compute the intermediate address from the array: the
2824 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002825 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002826 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002827 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002828 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2829 }
2830 temp = obj;
2831 } else {
2832 __ Add(temp, obj, offset);
2833 }
jessicahandojo05765752016-09-09 19:01:32 -07002834 if (maybe_compressed_char_at) {
2835 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002836 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2837 "Expecting 0=compressed, 1=uncompressed");
2838 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002839 __ Ldrb(Register(OutputCPURegister(instruction)),
2840 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2841 __ B(&done);
2842 __ Bind(&uncompressed_load);
2843 __ Ldrh(Register(OutputCPURegister(instruction)),
2844 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2845 __ Bind(&done);
2846 } else {
2847 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2848 }
Roland Levillain44015862016-01-22 11:47:17 +00002849 }
jessicahandojo05765752016-09-09 19:01:32 -07002850 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002851 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2852 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002853 codegen_->Load(type, OutputCPURegister(instruction), source);
2854 codegen_->MaybeRecordImplicitNullCheck(instruction);
2855 }
Roland Levillain44015862016-01-22 11:47:17 +00002856
2857 if (type == Primitive::kPrimNot) {
2858 static_assert(
2859 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2860 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2861 Location obj_loc = locations->InAt(0);
2862 if (index.IsConstant()) {
2863 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2864 } else {
2865 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2866 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002867 }
Roland Levillain4d027112015-07-01 15:41:14 +01002868 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002869}
2870
Alexandre Rames5319def2014-10-23 10:03:10 +01002871void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2872 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2873 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002874 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002875}
2876
2877void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002878 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002879 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002880 {
2881 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2882 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2883 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2884 codegen_->MaybeRecordImplicitNullCheck(instruction);
2885 }
jessicahandojo05765752016-09-09 19:01:32 -07002886 // Mask out compression flag from String's array length.
2887 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002888 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002889 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002890}
2891
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002892void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002893 Primitive::Type value_type = instruction->GetComponentType();
2894
2895 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002896 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2897 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002898 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002899 LocationSummary::kCallOnSlowPath :
2900 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002901 locations->SetInAt(0, Location::RequiresRegister());
2902 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002903 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2904 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2905 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002906 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002907 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002908 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002909 }
2910}
2911
2912void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2913 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002914 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002915 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002916 bool needs_write_barrier =
2917 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002918
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002919 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002920 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002921 CPURegister source = value;
2922 Location index = locations->InAt(1);
2923 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2924 MemOperand destination = HeapOperand(array);
2925 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002926
2927 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002928 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002929 if (index.IsConstant()) {
2930 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2931 destination = HeapOperand(array, offset);
2932 } else {
2933 UseScratchRegisterScope temps(masm);
2934 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002935 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002936 // We do not need to compute the intermediate address from the array: the
2937 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002938 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002939 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002940 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002941 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2942 }
2943 temp = array;
2944 } else {
2945 __ Add(temp, array, offset);
2946 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002947 destination = HeapOperand(temp,
2948 XRegisterFrom(index),
2949 LSL,
2950 Primitive::ComponentSizeShift(value_type));
2951 }
Artem Serov914d7a82017-02-07 14:33:49 +00002952 {
2953 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2954 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2955 codegen_->Store(value_type, value, destination);
2956 codegen_->MaybeRecordImplicitNullCheck(instruction);
2957 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002958 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002959 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002960 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002961 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002962 {
2963 // We use a block to end the scratch scope before the write barrier, thus
2964 // freeing the temporary registers so they can be used in `MarkGCCard`.
2965 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002966 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002967 if (index.IsConstant()) {
2968 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002969 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002970 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002971 destination = HeapOperand(temp,
2972 XRegisterFrom(index),
2973 LSL,
2974 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002975 }
2976
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002977 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2978 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2979 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2980
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002981 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002982 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2983 codegen_->AddSlowPath(slow_path);
2984 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002985 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002986 __ Cbnz(Register(value), &non_zero);
2987 if (!index.IsConstant()) {
2988 __ Add(temp, array, offset);
2989 }
Artem Serov914d7a82017-02-07 14:33:49 +00002990 {
2991 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
2992 // emitted.
2993 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2994 __ Str(wzr, destination);
2995 codegen_->MaybeRecordImplicitNullCheck(instruction);
2996 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002997 __ B(&done);
2998 __ Bind(&non_zero);
2999 }
3000
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003001 // Note that when Baker read barriers are enabled, the type
3002 // checks are performed without read barriers. This is fine,
3003 // even in the case where a class object is in the from-space
3004 // after the flip, as a comparison involving such a type would
3005 // not produce a false positive; it may of course produce a
3006 // false negative, in which case we would take the ArraySet
3007 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003008
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003009 Register temp2 = temps.AcquireSameSizeAs(array);
3010 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003011 {
3012 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3013 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3014 __ Ldr(temp, HeapOperand(array, class_offset));
3015 codegen_->MaybeRecordImplicitNullCheck(instruction);
3016 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003017 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003018
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003019 // /* HeapReference<Class> */ temp = temp->component_type_
3020 __ Ldr(temp, HeapOperand(temp, component_offset));
3021 // /* HeapReference<Class> */ temp2 = value->klass_
3022 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3023 // If heap poisoning is enabled, no need to unpoison `temp`
3024 // nor `temp2`, as we are comparing two poisoned references.
3025 __ Cmp(temp, temp2);
3026 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003027
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003028 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3029 vixl::aarch64::Label do_put;
3030 __ B(eq, &do_put);
3031 // If heap poisoning is enabled, the `temp` reference has
3032 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003033 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3034
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003035 // /* HeapReference<Class> */ temp = temp->super_class_
3036 __ Ldr(temp, HeapOperand(temp, super_offset));
3037 // If heap poisoning is enabled, no need to unpoison
3038 // `temp`, as we are comparing against null below.
3039 __ Cbnz(temp, slow_path->GetEntryLabel());
3040 __ Bind(&do_put);
3041 } else {
3042 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003043 }
3044 }
3045
3046 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003047 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003048 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003049 __ Mov(temp2, value.W());
3050 GetAssembler()->PoisonHeapReference(temp2);
3051 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003052 }
3053
3054 if (!index.IsConstant()) {
3055 __ Add(temp, array, offset);
3056 }
Artem Serov914d7a82017-02-07 14:33:49 +00003057 {
3058 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3059 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3060 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003061
Artem Serov914d7a82017-02-07 14:33:49 +00003062 if (!may_need_runtime_call_for_type_check) {
3063 codegen_->MaybeRecordImplicitNullCheck(instruction);
3064 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003065 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003066 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003067
3068 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3069
3070 if (done.IsLinked()) {
3071 __ Bind(&done);
3072 }
3073
3074 if (slow_path != nullptr) {
3075 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003076 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003077 }
3078}
3079
Alexandre Rames67555f72014-11-18 10:55:16 +00003080void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003081 RegisterSet caller_saves = RegisterSet::Empty();
3082 InvokeRuntimeCallingConvention calling_convention;
3083 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3084 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3085 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003086 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003087 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003088}
3089
3090void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003091 BoundsCheckSlowPathARM64* slow_path =
3092 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003093 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003094 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3095 __ B(slow_path->GetEntryLabel(), hs);
3096}
3097
Alexandre Rames67555f72014-11-18 10:55:16 +00003098void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3099 LocationSummary* locations =
3100 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3101 locations->SetInAt(0, Location::RequiresRegister());
3102 if (check->HasUses()) {
3103 locations->SetOut(Location::SameAsFirstInput());
3104 }
3105}
3106
3107void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3108 // We assume the class is not null.
3109 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3110 check->GetLoadClass(), check, check->GetDexPc(), true);
3111 codegen_->AddSlowPath(slow_path);
3112 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3113}
3114
Roland Levillain1a653882016-03-18 18:05:57 +00003115static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3116 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3117 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3118}
3119
3120void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3121 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3122 Location rhs_loc = instruction->GetLocations()->InAt(1);
3123 if (rhs_loc.IsConstant()) {
3124 // 0.0 is the only immediate that can be encoded directly in
3125 // an FCMP instruction.
3126 //
3127 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3128 // specify that in a floating-point comparison, positive zero
3129 // and negative zero are considered equal, so we can use the
3130 // literal 0.0 for both cases here.
3131 //
3132 // Note however that some methods (Float.equal, Float.compare,
3133 // Float.compareTo, Double.equal, Double.compare,
3134 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3135 // StrictMath.min) consider 0.0 to be (strictly) greater than
3136 // -0.0. So if we ever translate calls to these methods into a
3137 // HCompare instruction, we must handle the -0.0 case with
3138 // care here.
3139 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3140 __ Fcmp(lhs_reg, 0.0);
3141 } else {
3142 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3143 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003144}
3145
Serban Constantinescu02164b32014-11-13 14:05:07 +00003146void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003147 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003148 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3149 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003150 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003151 case Primitive::kPrimBoolean:
3152 case Primitive::kPrimByte:
3153 case Primitive::kPrimShort:
3154 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003155 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003156 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003157 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003158 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003159 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3160 break;
3161 }
3162 case Primitive::kPrimFloat:
3163 case Primitive::kPrimDouble: {
3164 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003165 locations->SetInAt(1,
3166 IsFloatingPointZeroConstant(compare->InputAt(1))
3167 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3168 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003169 locations->SetOut(Location::RequiresRegister());
3170 break;
3171 }
3172 default:
3173 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3174 }
3175}
3176
3177void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3178 Primitive::Type in_type = compare->InputAt(0)->GetType();
3179
3180 // 0 if: left == right
3181 // 1 if: left > right
3182 // -1 if: left < right
3183 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003184 case Primitive::kPrimBoolean:
3185 case Primitive::kPrimByte:
3186 case Primitive::kPrimShort:
3187 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003188 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003189 case Primitive::kPrimLong: {
3190 Register result = OutputRegister(compare);
3191 Register left = InputRegisterAt(compare, 0);
3192 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003193 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003194 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3195 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003196 break;
3197 }
3198 case Primitive::kPrimFloat:
3199 case Primitive::kPrimDouble: {
3200 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003201 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003202 __ Cset(result, ne);
3203 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003204 break;
3205 }
3206 default:
3207 LOG(FATAL) << "Unimplemented compare type " << in_type;
3208 }
3209}
3210
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003211void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003212 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003213
3214 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3215 locations->SetInAt(0, Location::RequiresFpuRegister());
3216 locations->SetInAt(1,
3217 IsFloatingPointZeroConstant(instruction->InputAt(1))
3218 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3219 : Location::RequiresFpuRegister());
3220 } else {
3221 // Integer cases.
3222 locations->SetInAt(0, Location::RequiresRegister());
3223 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3224 }
3225
David Brazdilb3e773e2016-01-26 11:28:37 +00003226 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003227 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003228 }
3229}
3230
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003231void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003232 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003233 return;
3234 }
3235
3236 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003237 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003238 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003239
Roland Levillain7f63c522015-07-13 15:54:55 +00003240 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003241 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003242 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003243 } else {
3244 // Integer cases.
3245 Register lhs = InputRegisterAt(instruction, 0);
3246 Operand rhs = InputOperandAt(instruction, 1);
3247 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003248 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003249 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003250}
3251
3252#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3253 M(Equal) \
3254 M(NotEqual) \
3255 M(LessThan) \
3256 M(LessThanOrEqual) \
3257 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003258 M(GreaterThanOrEqual) \
3259 M(Below) \
3260 M(BelowOrEqual) \
3261 M(Above) \
3262 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003263#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003264void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3265void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003266FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003267#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003268#undef FOR_EACH_CONDITION_INSTRUCTION
3269
Zheng Xuc6667102015-05-15 16:08:45 +08003270void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3271 DCHECK(instruction->IsDiv() || instruction->IsRem());
3272
3273 LocationSummary* locations = instruction->GetLocations();
3274 Location second = locations->InAt(1);
3275 DCHECK(second.IsConstant());
3276
3277 Register out = OutputRegister(instruction);
3278 Register dividend = InputRegisterAt(instruction, 0);
3279 int64_t imm = Int64FromConstant(second.GetConstant());
3280 DCHECK(imm == 1 || imm == -1);
3281
3282 if (instruction->IsRem()) {
3283 __ Mov(out, 0);
3284 } else {
3285 if (imm == 1) {
3286 __ Mov(out, dividend);
3287 } else {
3288 __ Neg(out, dividend);
3289 }
3290 }
3291}
3292
3293void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3294 DCHECK(instruction->IsDiv() || instruction->IsRem());
3295
3296 LocationSummary* locations = instruction->GetLocations();
3297 Location second = locations->InAt(1);
3298 DCHECK(second.IsConstant());
3299
3300 Register out = OutputRegister(instruction);
3301 Register dividend = InputRegisterAt(instruction, 0);
3302 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003303 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003304 int ctz_imm = CTZ(abs_imm);
3305
3306 UseScratchRegisterScope temps(GetVIXLAssembler());
3307 Register temp = temps.AcquireSameSizeAs(out);
3308
3309 if (instruction->IsDiv()) {
3310 __ Add(temp, dividend, abs_imm - 1);
3311 __ Cmp(dividend, 0);
3312 __ Csel(out, temp, dividend, lt);
3313 if (imm > 0) {
3314 __ Asr(out, out, ctz_imm);
3315 } else {
3316 __ Neg(out, Operand(out, ASR, ctz_imm));
3317 }
3318 } else {
3319 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3320 __ Asr(temp, dividend, bits - 1);
3321 __ Lsr(temp, temp, bits - ctz_imm);
3322 __ Add(out, dividend, temp);
3323 __ And(out, out, abs_imm - 1);
3324 __ Sub(out, out, temp);
3325 }
3326}
3327
3328void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3329 DCHECK(instruction->IsDiv() || instruction->IsRem());
3330
3331 LocationSummary* locations = instruction->GetLocations();
3332 Location second = locations->InAt(1);
3333 DCHECK(second.IsConstant());
3334
3335 Register out = OutputRegister(instruction);
3336 Register dividend = InputRegisterAt(instruction, 0);
3337 int64_t imm = Int64FromConstant(second.GetConstant());
3338
3339 Primitive::Type type = instruction->GetResultType();
3340 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3341
3342 int64_t magic;
3343 int shift;
3344 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3345
3346 UseScratchRegisterScope temps(GetVIXLAssembler());
3347 Register temp = temps.AcquireSameSizeAs(out);
3348
3349 // temp = get_high(dividend * magic)
3350 __ Mov(temp, magic);
3351 if (type == Primitive::kPrimLong) {
3352 __ Smulh(temp, dividend, temp);
3353 } else {
3354 __ Smull(temp.X(), dividend, temp);
3355 __ Lsr(temp.X(), temp.X(), 32);
3356 }
3357
3358 if (imm > 0 && magic < 0) {
3359 __ Add(temp, temp, dividend);
3360 } else if (imm < 0 && magic > 0) {
3361 __ Sub(temp, temp, dividend);
3362 }
3363
3364 if (shift != 0) {
3365 __ Asr(temp, temp, shift);
3366 }
3367
3368 if (instruction->IsDiv()) {
3369 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3370 } else {
3371 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3372 // TODO: Strength reduction for msub.
3373 Register temp_imm = temps.AcquireSameSizeAs(out);
3374 __ Mov(temp_imm, imm);
3375 __ Msub(out, temp, temp_imm, dividend);
3376 }
3377}
3378
3379void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3380 DCHECK(instruction->IsDiv() || instruction->IsRem());
3381 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003382 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003383
3384 LocationSummary* locations = instruction->GetLocations();
3385 Register out = OutputRegister(instruction);
3386 Location second = locations->InAt(1);
3387
3388 if (second.IsConstant()) {
3389 int64_t imm = Int64FromConstant(second.GetConstant());
3390
3391 if (imm == 0) {
3392 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3393 } else if (imm == 1 || imm == -1) {
3394 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003395 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003396 DivRemByPowerOfTwo(instruction);
3397 } else {
3398 DCHECK(imm <= -2 || imm >= 2);
3399 GenerateDivRemWithAnyConstant(instruction);
3400 }
3401 } else {
3402 Register dividend = InputRegisterAt(instruction, 0);
3403 Register divisor = InputRegisterAt(instruction, 1);
3404 if (instruction->IsDiv()) {
3405 __ Sdiv(out, dividend, divisor);
3406 } else {
3407 UseScratchRegisterScope temps(GetVIXLAssembler());
3408 Register temp = temps.AcquireSameSizeAs(out);
3409 __ Sdiv(temp, dividend, divisor);
3410 __ Msub(out, temp, divisor, dividend);
3411 }
3412 }
3413}
3414
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003415void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3416 LocationSummary* locations =
3417 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3418 switch (div->GetResultType()) {
3419 case Primitive::kPrimInt:
3420 case Primitive::kPrimLong:
3421 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003422 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003423 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3424 break;
3425
3426 case Primitive::kPrimFloat:
3427 case Primitive::kPrimDouble:
3428 locations->SetInAt(0, Location::RequiresFpuRegister());
3429 locations->SetInAt(1, Location::RequiresFpuRegister());
3430 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3431 break;
3432
3433 default:
3434 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3435 }
3436}
3437
3438void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3439 Primitive::Type type = div->GetResultType();
3440 switch (type) {
3441 case Primitive::kPrimInt:
3442 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003443 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003444 break;
3445
3446 case Primitive::kPrimFloat:
3447 case Primitive::kPrimDouble:
3448 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3449 break;
3450
3451 default:
3452 LOG(FATAL) << "Unexpected div type " << type;
3453 }
3454}
3455
Alexandre Rames67555f72014-11-18 10:55:16 +00003456void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003457 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003458 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003459}
3460
3461void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3462 SlowPathCodeARM64* slow_path =
3463 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3464 codegen_->AddSlowPath(slow_path);
3465 Location value = instruction->GetLocations()->InAt(0);
3466
Alexandre Rames3e69f162014-12-10 10:36:50 +00003467 Primitive::Type type = instruction->GetType();
3468
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003469 if (!Primitive::IsIntegralType(type)) {
3470 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003471 return;
3472 }
3473
Alexandre Rames67555f72014-11-18 10:55:16 +00003474 if (value.IsConstant()) {
3475 int64_t divisor = Int64ConstantFrom(value);
3476 if (divisor == 0) {
3477 __ B(slow_path->GetEntryLabel());
3478 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003479 // A division by a non-null constant is valid. We don't need to perform
3480 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003481 }
3482 } else {
3483 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3484 }
3485}
3486
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003487void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3488 LocationSummary* locations =
3489 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3490 locations->SetOut(Location::ConstantLocation(constant));
3491}
3492
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003493void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3494 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003495 // Will be generated at use site.
3496}
3497
Alexandre Rames5319def2014-10-23 10:03:10 +01003498void LocationsBuilderARM64::VisitExit(HExit* exit) {
3499 exit->SetLocations(nullptr);
3500}
3501
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003502void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003503}
3504
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003505void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3506 LocationSummary* locations =
3507 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3508 locations->SetOut(Location::ConstantLocation(constant));
3509}
3510
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003511void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003512 // Will be generated at use site.
3513}
3514
David Brazdilfc6a86a2015-06-26 10:33:45 +00003515void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003516 DCHECK(!successor->IsExitBlock());
3517 HBasicBlock* block = got->GetBlock();
3518 HInstruction* previous = got->GetPrevious();
3519 HLoopInformation* info = block->GetLoopInformation();
3520
David Brazdil46e2a392015-03-16 17:31:52 +00003521 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003522 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3523 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3524 return;
3525 }
3526 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3527 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3528 }
3529 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003530 __ B(codegen_->GetLabelOf(successor));
3531 }
3532}
3533
David Brazdilfc6a86a2015-06-26 10:33:45 +00003534void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3535 got->SetLocations(nullptr);
3536}
3537
3538void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3539 HandleGoto(got, got->GetSuccessor());
3540}
3541
3542void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3543 try_boundary->SetLocations(nullptr);
3544}
3545
3546void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3547 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3548 if (!successor->IsExitBlock()) {
3549 HandleGoto(try_boundary, successor);
3550 }
3551}
3552
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003553void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003554 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003555 vixl::aarch64::Label* true_target,
3556 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003557 // FP branching requires both targets to be explicit. If either of the targets
3558 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003559 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003560 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003561
David Brazdil0debae72015-11-12 18:37:00 +00003562 if (true_target == nullptr && false_target == nullptr) {
3563 // Nothing to do. The code always falls through.
3564 return;
3565 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003566 // Constant condition, statically compared against "true" (integer value 1).
3567 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003568 if (true_target != nullptr) {
3569 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003570 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003571 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003572 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003573 if (false_target != nullptr) {
3574 __ B(false_target);
3575 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003576 }
David Brazdil0debae72015-11-12 18:37:00 +00003577 return;
3578 }
3579
3580 // The following code generates these patterns:
3581 // (1) true_target == nullptr && false_target != nullptr
3582 // - opposite condition true => branch to false_target
3583 // (2) true_target != nullptr && false_target == nullptr
3584 // - condition true => branch to true_target
3585 // (3) true_target != nullptr && false_target != nullptr
3586 // - condition true => branch to true_target
3587 // - branch to false_target
3588 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003589 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003590 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003591 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003592 if (true_target == nullptr) {
3593 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3594 } else {
3595 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3596 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003597 } else {
3598 // The condition instruction has not been materialized, use its inputs as
3599 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003600 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003601
David Brazdil0debae72015-11-12 18:37:00 +00003602 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003603 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003604 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003605 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003606 IfCondition opposite_condition = condition->GetOppositeCondition();
3607 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003608 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003609 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003610 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003611 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003612 // Integer cases.
3613 Register lhs = InputRegisterAt(condition, 0);
3614 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003615
3616 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003617 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003618 if (true_target == nullptr) {
3619 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3620 non_fallthrough_target = false_target;
3621 } else {
3622 arm64_cond = ARM64Condition(condition->GetCondition());
3623 non_fallthrough_target = true_target;
3624 }
3625
Aart Bik086d27e2016-01-20 17:02:00 -08003626 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003627 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003628 switch (arm64_cond) {
3629 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003630 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003631 break;
3632 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003633 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003634 break;
3635 case lt:
3636 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003637 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003638 break;
3639 case ge:
3640 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003641 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003642 break;
3643 default:
3644 // Without the `static_cast` the compiler throws an error for
3645 // `-Werror=sign-promo`.
3646 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3647 }
3648 } else {
3649 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003650 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003651 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003652 }
3653 }
David Brazdil0debae72015-11-12 18:37:00 +00003654
3655 // If neither branch falls through (case 3), the conditional branch to `true_target`
3656 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3657 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003658 __ B(false_target);
3659 }
David Brazdil0debae72015-11-12 18:37:00 +00003660
3661 if (fallthrough_target.IsLinked()) {
3662 __ Bind(&fallthrough_target);
3663 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003664}
3665
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003666void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3667 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003668 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003669 locations->SetInAt(0, Location::RequiresRegister());
3670 }
3671}
3672
3673void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003674 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3675 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003676 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3677 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3678 true_target = nullptr;
3679 }
3680 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3681 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3682 false_target = nullptr;
3683 }
David Brazdil0debae72015-11-12 18:37:00 +00003684 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003685}
3686
3687void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3688 LocationSummary* locations = new (GetGraph()->GetArena())
3689 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003690 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003691 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003692 locations->SetInAt(0, Location::RequiresRegister());
3693 }
3694}
3695
3696void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003697 SlowPathCodeARM64* slow_path =
3698 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003699 GenerateTestAndBranch(deoptimize,
3700 /* condition_input_index */ 0,
3701 slow_path->GetEntryLabel(),
3702 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003703}
3704
Mingyao Yang063fc772016-08-02 11:02:54 -07003705void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3706 LocationSummary* locations = new (GetGraph()->GetArena())
3707 LocationSummary(flag, LocationSummary::kNoCall);
3708 locations->SetOut(Location::RequiresRegister());
3709}
3710
3711void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3712 __ Ldr(OutputRegister(flag),
3713 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3714}
3715
David Brazdilc0b601b2016-02-08 14:20:45 +00003716static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3717 return condition->IsCondition() &&
3718 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3719}
3720
Alexandre Rames880f1192016-06-13 16:04:50 +01003721static inline Condition GetConditionForSelect(HCondition* condition) {
3722 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003723 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3724 : ARM64Condition(cond);
3725}
3726
David Brazdil74eb1b22015-12-14 11:44:01 +00003727void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3728 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003729 if (Primitive::IsFloatingPointType(select->GetType())) {
3730 locations->SetInAt(0, Location::RequiresFpuRegister());
3731 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003732 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003733 } else {
3734 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3735 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3736 bool is_true_value_constant = cst_true_value != nullptr;
3737 bool is_false_value_constant = cst_false_value != nullptr;
3738 // Ask VIXL whether we should synthesize constants in registers.
3739 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3740 Operand true_op = is_true_value_constant ?
3741 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3742 Operand false_op = is_false_value_constant ?
3743 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3744 bool true_value_in_register = false;
3745 bool false_value_in_register = false;
3746 MacroAssembler::GetCselSynthesisInformation(
3747 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3748 true_value_in_register |= !is_true_value_constant;
3749 false_value_in_register |= !is_false_value_constant;
3750
3751 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3752 : Location::ConstantLocation(cst_true_value));
3753 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3754 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003755 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003756 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003757
David Brazdil74eb1b22015-12-14 11:44:01 +00003758 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3759 locations->SetInAt(2, Location::RequiresRegister());
3760 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003761}
3762
3763void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003764 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003765 Condition csel_cond;
3766
3767 if (IsBooleanValueOrMaterializedCondition(cond)) {
3768 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003769 // Use the condition flags set by the previous instruction.
3770 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003771 } else {
3772 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003773 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003774 }
3775 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003776 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003777 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003778 } else {
3779 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003780 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003781 }
3782
Alexandre Rames880f1192016-06-13 16:04:50 +01003783 if (Primitive::IsFloatingPointType(select->GetType())) {
3784 __ Fcsel(OutputFPRegister(select),
3785 InputFPRegisterAt(select, 1),
3786 InputFPRegisterAt(select, 0),
3787 csel_cond);
3788 } else {
3789 __ Csel(OutputRegister(select),
3790 InputOperandAt(select, 1),
3791 InputOperandAt(select, 0),
3792 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003793 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003794}
3795
David Srbecky0cf44932015-12-09 14:09:59 +00003796void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3797 new (GetGraph()->GetArena()) LocationSummary(info);
3798}
3799
David Srbeckyd28f4a02016-03-14 17:14:24 +00003800void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3801 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003802}
3803
3804void CodeGeneratorARM64::GenerateNop() {
3805 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003806}
3807
Alexandre Rames5319def2014-10-23 10:03:10 +01003808void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003809 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003810}
3811
3812void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003813 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003814}
3815
3816void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003817 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003818}
3819
3820void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003821 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003822}
3823
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003824// Temp is used for read barrier.
3825static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3826 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003827 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003828 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3829 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3830 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3831 return 1;
3832 }
3833 return 0;
3834}
3835
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003836// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003837// interface pointer, one for loading the current interface.
3838// The other checks have one temp for loading the object's class.
3839static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3840 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3841 return 3;
3842 }
3843 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003844}
3845
Alexandre Rames67555f72014-11-18 10:55:16 +00003846void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003847 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003848 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003849 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003850 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003851 case TypeCheckKind::kExactCheck:
3852 case TypeCheckKind::kAbstractClassCheck:
3853 case TypeCheckKind::kClassHierarchyCheck:
3854 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003855 call_kind =
3856 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003857 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003858 break;
3859 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003860 case TypeCheckKind::kUnresolvedCheck:
3861 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003862 call_kind = LocationSummary::kCallOnSlowPath;
3863 break;
3864 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003865
Alexandre Rames67555f72014-11-18 10:55:16 +00003866 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003867 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003868 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003869 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003870 locations->SetInAt(0, Location::RequiresRegister());
3871 locations->SetInAt(1, Location::RequiresRegister());
3872 // The "out" register is used as a temporary, so it overlaps with the inputs.
3873 // Note that TypeCheckSlowPathARM64 uses this register too.
3874 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003875 // Add temps if necessary for read barriers.
3876 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003877}
3878
3879void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003880 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003881 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003882 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003883 Register obj = InputRegisterAt(instruction, 0);
3884 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003885 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003886 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003887 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3888 DCHECK_LE(num_temps, 1u);
3889 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003890 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3891 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3892 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3893 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003894
Scott Wakeling97c72b72016-06-24 16:19:36 +01003895 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003896 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003897
3898 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003899 // Avoid null check if we know `obj` is not null.
3900 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003901 __ Cbz(obj, &zero);
3902 }
3903
Roland Levillain44015862016-01-22 11:47:17 +00003904 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003905 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003906 // /* HeapReference<Class> */ out = obj->klass_
3907 GenerateReferenceLoadTwoRegisters(instruction,
3908 out_loc,
3909 obj_loc,
3910 class_offset,
3911 maybe_temp_loc,
3912 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003913 __ Cmp(out, cls);
3914 __ Cset(out, eq);
3915 if (zero.IsLinked()) {
3916 __ B(&done);
3917 }
3918 break;
3919 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003920
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003921 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003922 // /* HeapReference<Class> */ out = obj->klass_
3923 GenerateReferenceLoadTwoRegisters(instruction,
3924 out_loc,
3925 obj_loc,
3926 class_offset,
3927 maybe_temp_loc,
3928 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003929 // If the class is abstract, we eagerly fetch the super class of the
3930 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003931 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003932 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003933 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003934 GenerateReferenceLoadOneRegister(instruction,
3935 out_loc,
3936 super_offset,
3937 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003938 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003939 // If `out` is null, we use it for the result, and jump to `done`.
3940 __ Cbz(out, &done);
3941 __ Cmp(out, cls);
3942 __ B(ne, &loop);
3943 __ Mov(out, 1);
3944 if (zero.IsLinked()) {
3945 __ B(&done);
3946 }
3947 break;
3948 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003949
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003950 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003951 // /* HeapReference<Class> */ out = obj->klass_
3952 GenerateReferenceLoadTwoRegisters(instruction,
3953 out_loc,
3954 obj_loc,
3955 class_offset,
3956 maybe_temp_loc,
3957 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003958 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003959 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003960 __ Bind(&loop);
3961 __ Cmp(out, cls);
3962 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003963 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003964 GenerateReferenceLoadOneRegister(instruction,
3965 out_loc,
3966 super_offset,
3967 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003968 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003969 __ Cbnz(out, &loop);
3970 // If `out` is null, we use it for the result, and jump to `done`.
3971 __ B(&done);
3972 __ Bind(&success);
3973 __ Mov(out, 1);
3974 if (zero.IsLinked()) {
3975 __ B(&done);
3976 }
3977 break;
3978 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003979
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003980 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003981 // /* HeapReference<Class> */ out = obj->klass_
3982 GenerateReferenceLoadTwoRegisters(instruction,
3983 out_loc,
3984 obj_loc,
3985 class_offset,
3986 maybe_temp_loc,
3987 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003988 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003989 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003990 __ Cmp(out, cls);
3991 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003992 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003993 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003994 GenerateReferenceLoadOneRegister(instruction,
3995 out_loc,
3996 component_offset,
3997 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003998 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003999 // If `out` is null, we use it for the result, and jump to `done`.
4000 __ Cbz(out, &done);
4001 __ Ldrh(out, HeapOperand(out, primitive_offset));
4002 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4003 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004004 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004005 __ Mov(out, 1);
4006 __ B(&done);
4007 break;
4008 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004009
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004010 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004011 // No read barrier since the slow path will retry upon failure.
4012 // /* HeapReference<Class> */ out = obj->klass_
4013 GenerateReferenceLoadTwoRegisters(instruction,
4014 out_loc,
4015 obj_loc,
4016 class_offset,
4017 maybe_temp_loc,
4018 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004019 __ Cmp(out, cls);
4020 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004021 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4022 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004023 codegen_->AddSlowPath(slow_path);
4024 __ B(ne, slow_path->GetEntryLabel());
4025 __ Mov(out, 1);
4026 if (zero.IsLinked()) {
4027 __ B(&done);
4028 }
4029 break;
4030 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004031
Calin Juravle98893e12015-10-02 21:05:03 +01004032 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004033 case TypeCheckKind::kInterfaceCheck: {
4034 // Note that we indeed only call on slow path, but we always go
4035 // into the slow path for the unresolved and interface check
4036 // cases.
4037 //
4038 // We cannot directly call the InstanceofNonTrivial runtime
4039 // entry point without resorting to a type checking slow path
4040 // here (i.e. by calling InvokeRuntime directly), as it would
4041 // require to assign fixed registers for the inputs of this
4042 // HInstanceOf instruction (following the runtime calling
4043 // convention), which might be cluttered by the potential first
4044 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004045 //
4046 // TODO: Introduce a new runtime entry point taking the object
4047 // to test (instead of its class) as argument, and let it deal
4048 // with the read barrier issues. This will let us refactor this
4049 // case of the `switch` code as it was previously (with a direct
4050 // call to the runtime not using a type checking slow path).
4051 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004052 DCHECK(locations->OnlyCallsOnSlowPath());
4053 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4054 /* is_fatal */ false);
4055 codegen_->AddSlowPath(slow_path);
4056 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004057 if (zero.IsLinked()) {
4058 __ B(&done);
4059 }
4060 break;
4061 }
4062 }
4063
4064 if (zero.IsLinked()) {
4065 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004066 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004067 }
4068
4069 if (done.IsLinked()) {
4070 __ Bind(&done);
4071 }
4072
4073 if (slow_path != nullptr) {
4074 __ Bind(slow_path->GetExitLabel());
4075 }
4076}
4077
4078void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4079 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4080 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4081
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004082 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4083 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004084 case TypeCheckKind::kExactCheck:
4085 case TypeCheckKind::kAbstractClassCheck:
4086 case TypeCheckKind::kClassHierarchyCheck:
4087 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004088 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4089 LocationSummary::kCallOnSlowPath :
4090 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004091 break;
4092 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004093 case TypeCheckKind::kUnresolvedCheck:
4094 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004095 call_kind = LocationSummary::kCallOnSlowPath;
4096 break;
4097 }
4098
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004099 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4100 locations->SetInAt(0, Location::RequiresRegister());
4101 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004102 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4103 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004104}
4105
4106void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004107 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004108 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004109 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004110 Register obj = InputRegisterAt(instruction, 0);
4111 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004112 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4113 DCHECK_GE(num_temps, 1u);
4114 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004115 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004116 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4117 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004118 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004119 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4120 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4121 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4122 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4123 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4124 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4125 const uint32_t object_array_data_offset =
4126 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004127
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004128 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004129 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4130 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4131 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004132 if (!kEmitCompilerReadBarrier) {
4133 is_type_check_slow_path_fatal =
4134 (type_check_kind == TypeCheckKind::kExactCheck ||
4135 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4136 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4137 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4138 !instruction->CanThrowIntoCatchBlock();
4139 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004140 SlowPathCodeARM64* type_check_slow_path =
4141 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4142 is_type_check_slow_path_fatal);
4143 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004144
Scott Wakeling97c72b72016-06-24 16:19:36 +01004145 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004146 // Avoid null check if we know obj is not null.
4147 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004148 __ Cbz(obj, &done);
4149 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004150
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004151 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004152 case TypeCheckKind::kExactCheck:
4153 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004154 // /* HeapReference<Class> */ temp = obj->klass_
4155 GenerateReferenceLoadTwoRegisters(instruction,
4156 temp_loc,
4157 obj_loc,
4158 class_offset,
4159 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004160 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004161
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004162 __ Cmp(temp, cls);
4163 // Jump to slow path for throwing the exception or doing a
4164 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004165 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004166 break;
4167 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004168
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004169 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004170 // /* HeapReference<Class> */ temp = obj->klass_
4171 GenerateReferenceLoadTwoRegisters(instruction,
4172 temp_loc,
4173 obj_loc,
4174 class_offset,
4175 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004176 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004177
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004178 // If the class is abstract, we eagerly fetch the super class of the
4179 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004180 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004181 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004182 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004183 GenerateReferenceLoadOneRegister(instruction,
4184 temp_loc,
4185 super_offset,
4186 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004187 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004188
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004189 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4190 // exception.
4191 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4192 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004193 __ Cmp(temp, cls);
4194 __ B(ne, &loop);
4195 break;
4196 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004197
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004198 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004199 // /* HeapReference<Class> */ temp = obj->klass_
4200 GenerateReferenceLoadTwoRegisters(instruction,
4201 temp_loc,
4202 obj_loc,
4203 class_offset,
4204 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004205 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004206
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004207 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004208 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004209 __ Bind(&loop);
4210 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004211 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004212
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004213 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004214 GenerateReferenceLoadOneRegister(instruction,
4215 temp_loc,
4216 super_offset,
4217 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004218 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004219
4220 // If the class reference currently in `temp` is not null, jump
4221 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004222 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004223 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004224 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004225 break;
4226 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004227
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004228 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004229 // /* HeapReference<Class> */ temp = obj->klass_
4230 GenerateReferenceLoadTwoRegisters(instruction,
4231 temp_loc,
4232 obj_loc,
4233 class_offset,
4234 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004235 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004236
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004237 // Do an exact check.
4238 __ Cmp(temp, cls);
4239 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004240
4241 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004242 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004243 GenerateReferenceLoadOneRegister(instruction,
4244 temp_loc,
4245 component_offset,
4246 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004247 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004248
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004249 // If the component type is null, jump to the slow path to throw the exception.
4250 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4251 // Otherwise, the object is indeed an array. Further check that this component type is not a
4252 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004253 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4254 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004255 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004256 break;
4257 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004258
Calin Juravle98893e12015-10-02 21:05:03 +01004259 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004260 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004261 //
4262 // We cannot directly call the CheckCast runtime entry point
4263 // without resorting to a type checking slow path here (i.e. by
4264 // calling InvokeRuntime directly), as it would require to
4265 // assign fixed registers for the inputs of this HInstanceOf
4266 // instruction (following the runtime calling convention), which
4267 // might be cluttered by the potential first read barrier
4268 // emission at the beginning of this method.
4269 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004270 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004271 case TypeCheckKind::kInterfaceCheck: {
4272 // /* HeapReference<Class> */ temp = obj->klass_
4273 GenerateReferenceLoadTwoRegisters(instruction,
4274 temp_loc,
4275 obj_loc,
4276 class_offset,
4277 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004278 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004279
4280 // /* HeapReference<Class> */ temp = temp->iftable_
4281 GenerateReferenceLoadTwoRegisters(instruction,
4282 temp_loc,
4283 temp_loc,
4284 iftable_offset,
4285 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004286 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004287 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004288 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004289 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004290 vixl::aarch64::Label start_loop;
4291 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004292 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004293 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4294 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004295 // Go to next interface.
4296 __ Add(temp, temp, 2 * kHeapReferenceSize);
4297 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004298 // Compare the classes and continue the loop if they do not match.
4299 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4300 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004301 break;
4302 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004303 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004304 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004305
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004306 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004307}
4308
Alexandre Rames5319def2014-10-23 10:03:10 +01004309void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4310 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4311 locations->SetOut(Location::ConstantLocation(constant));
4312}
4313
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004314void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004315 // Will be generated at use site.
4316}
4317
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004318void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4319 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4320 locations->SetOut(Location::ConstantLocation(constant));
4321}
4322
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004323void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004324 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004325}
4326
Calin Juravle175dc732015-08-25 15:42:32 +01004327void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4328 // The trampoline uses the same calling convention as dex calling conventions,
4329 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4330 // the method_idx.
4331 HandleInvoke(invoke);
4332}
4333
4334void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4335 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4336}
4337
Alexandre Rames5319def2014-10-23 10:03:10 +01004338void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004339 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004340 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004341}
4342
Alexandre Rames67555f72014-11-18 10:55:16 +00004343void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4344 HandleInvoke(invoke);
4345}
4346
4347void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4348 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004349 LocationSummary* locations = invoke->GetLocations();
4350 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004351 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004352 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004353 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004354
4355 // The register ip1 is required to be used for the hidden argument in
4356 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004357 MacroAssembler* masm = GetVIXLAssembler();
4358 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004359 scratch_scope.Exclude(ip1);
4360 __ Mov(ip1, invoke->GetDexMethodIndex());
4361
Artem Serov914d7a82017-02-07 14:33:49 +00004362 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004363 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004364 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004365 {
4366 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4367 // /* HeapReference<Class> */ temp = temp->klass_
4368 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4369 codegen_->MaybeRecordImplicitNullCheck(invoke);
4370 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004371 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004372 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004373 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004374 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004375 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004376 }
Artem Serov914d7a82017-02-07 14:33:49 +00004377
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004378 // Instead of simply (possibly) unpoisoning `temp` here, we should
4379 // emit a read barrier for the previous class reference load.
4380 // However this is not required in practice, as this is an
4381 // intermediate/temporary reference and because the current
4382 // concurrent copying collector keeps the from-space memory
4383 // intact/accessible until the end of the marking phase (the
4384 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004385 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004386 __ Ldr(temp,
4387 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4388 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004389 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004390 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004391 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004392 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004393 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004394
4395 {
4396 // Ensure the pc position is recorded immediately after the `blr` instruction.
4397 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4398
4399 // lr();
4400 __ blr(lr);
4401 DCHECK(!codegen_->IsLeafMethod());
4402 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4403 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004404}
4405
4406void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004407 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004408 if (intrinsic.TryDispatch(invoke)) {
4409 return;
4410 }
4411
Alexandre Rames67555f72014-11-18 10:55:16 +00004412 HandleInvoke(invoke);
4413}
4414
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004415void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004416 // Explicit clinit checks triggered by static invokes must have been pruned by
4417 // art::PrepareForRegisterAllocation.
4418 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004419
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004420 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004421 if (intrinsic.TryDispatch(invoke)) {
4422 return;
4423 }
4424
Alexandre Rames67555f72014-11-18 10:55:16 +00004425 HandleInvoke(invoke);
4426}
4427
Andreas Gampe878d58c2015-01-15 23:24:00 -08004428static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4429 if (invoke->GetLocations()->Intrinsified()) {
4430 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4431 intrinsic.Dispatch(invoke);
4432 return true;
4433 }
4434 return false;
4435}
4436
Vladimir Markodc151b22015-10-15 18:02:30 +01004437HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4438 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004439 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004440 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004441 return desired_dispatch_info;
4442}
4443
TatWai Chongd8c052a2016-11-02 16:12:48 +08004444Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4445 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004446 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004447 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4448 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004449 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4450 uint32_t offset =
4451 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004452 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004453 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004454 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004455 }
Vladimir Marko58155012015-08-19 12:49:41 +00004456 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004457 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004458 break;
4459 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4460 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004461 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004462 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004463 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4464 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004465 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004466 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004467 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004468 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004469 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004470 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004471 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004472 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004473 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004474 }
Vladimir Marko58155012015-08-19 12:49:41 +00004475 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004476 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004477 Register reg = XRegisterFrom(temp);
4478 Register method_reg;
4479 if (current_method.IsRegister()) {
4480 method_reg = XRegisterFrom(current_method);
4481 } else {
4482 DCHECK(invoke->GetLocations()->Intrinsified());
4483 DCHECK(!current_method.IsValid());
4484 method_reg = reg;
4485 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4486 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004487
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004488 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004489 __ Ldr(reg.X(),
4490 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004491 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004492 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004493 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4494 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004495 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4496 break;
4497 }
4498 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004499 return callee_method;
4500}
4501
4502void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4503 // All registers are assumed to be correctly set up.
4504 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004505
4506 switch (invoke->GetCodePtrLocation()) {
4507 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4508 __ Bl(&frame_entry_label_);
4509 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004510 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4511 // LR = callee_method->entry_point_from_quick_compiled_code_;
4512 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004513 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004514 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004515 {
4516 // To ensure that the pc position is recorded immediately after the `blr` instruction
4517 // BLR must be the last instruction emitted in this function.
4518 // Recording the pc will occur right after returning from this function.
4519 ExactAssemblyScope eas(GetVIXLAssembler(),
4520 kInstructionSize,
4521 CodeBufferCheckScope::kExactSize);
4522 // lr()
4523 __ blr(lr);
4524 }
Vladimir Marko58155012015-08-19 12:49:41 +00004525 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004526 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004527
Andreas Gampe878d58c2015-01-15 23:24:00 -08004528 DCHECK(!IsLeafMethod());
4529}
4530
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004531void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004532 // Use the calling convention instead of the location of the receiver, as
4533 // intrinsics may have put the receiver in a different register. In the intrinsics
4534 // slow path, the arguments have been moved to the right place, so here we are
4535 // guaranteed that the receiver is the first register of the calling convention.
4536 InvokeDexCallingConvention calling_convention;
4537 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004538 Register temp = XRegisterFrom(temp_in);
4539 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4540 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4541 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004542 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004543
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004544 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004545
4546 {
4547 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4548 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4549 // /* HeapReference<Class> */ temp = receiver->klass_
4550 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4551 MaybeRecordImplicitNullCheck(invoke);
4552 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004553 // Instead of simply (possibly) unpoisoning `temp` here, we should
4554 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004555 // intermediate/temporary reference and because the current
4556 // concurrent copying collector keeps the from-space memory
4557 // intact/accessible until the end of the marking phase (the
4558 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004559 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4560 // temp = temp->GetMethodAt(method_offset);
4561 __ Ldr(temp, MemOperand(temp, method_offset));
4562 // lr = temp->GetEntryPoint();
4563 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004564 {
4565 // To ensure that the pc position is recorded immediately after the `blr` instruction
4566 // BLR should be the last instruction emitted in this function.
4567 // Recording the pc will occur right after returning from this function.
4568 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4569 // lr();
4570 __ blr(lr);
4571 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004572}
4573
Orion Hodsonac141392017-01-13 11:53:47 +00004574void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4575 HandleInvoke(invoke);
4576}
4577
4578void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4579 codegen_->GenerateInvokePolymorphicCall(invoke);
4580}
4581
Scott Wakeling97c72b72016-06-24 16:19:36 +01004582vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4583 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004584 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004585 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004586 return
4587 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004588}
4589
Scott Wakeling97c72b72016-06-24 16:19:36 +01004590vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4591 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004592 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004593 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004594 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004595}
4596
Vladimir Marko1998cd02017-01-13 13:02:58 +00004597vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4598 const DexFile& dex_file,
4599 dex::TypeIndex type_index,
4600 vixl::aarch64::Label* adrp_label) {
4601 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4602}
4603
Scott Wakeling97c72b72016-06-24 16:19:36 +01004604vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4605 const DexFile& dex_file,
4606 uint32_t element_offset,
4607 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004608 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4609}
4610
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004611vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4612 baker_read_barrier_patches_.emplace_back(custom_data);
4613 return &baker_read_barrier_patches_.back().label;
4614}
4615
Scott Wakeling97c72b72016-06-24 16:19:36 +01004616vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4617 const DexFile& dex_file,
4618 uint32_t offset_or_index,
4619 vixl::aarch64::Label* adrp_label,
4620 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004621 // Add a patch entry and return the label.
4622 patches->emplace_back(dex_file, offset_or_index);
4623 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004624 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004625 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4626 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4627 return label;
4628}
4629
Scott Wakeling97c72b72016-06-24 16:19:36 +01004630vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004631 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004632 return boot_image_string_patches_.GetOrCreate(
4633 StringReference(&dex_file, string_index),
4634 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4635}
4636
Scott Wakeling97c72b72016-06-24 16:19:36 +01004637vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004638 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004639 return boot_image_type_patches_.GetOrCreate(
4640 TypeReference(&dex_file, type_index),
4641 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4642}
4643
Scott Wakeling97c72b72016-06-24 16:19:36 +01004644vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4645 uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00004646 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004647}
4648
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004649vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004650 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4651 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4652 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004653 return jit_string_patches_.GetOrCreate(
4654 StringReference(&dex_file, string_index),
4655 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4656}
4657
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004658vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004659 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4660 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4661 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004662 return jit_class_patches_.GetOrCreate(
4663 TypeReference(&dex_file, type_index),
4664 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4665}
4666
Vladimir Markoaad75c62016-10-03 08:46:48 +00004667void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4668 vixl::aarch64::Register reg) {
4669 DCHECK(reg.IsX());
4670 SingleEmissionCheckScope guard(GetVIXLAssembler());
4671 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004672 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004673}
4674
4675void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4676 vixl::aarch64::Register out,
4677 vixl::aarch64::Register base) {
4678 DCHECK(out.IsX());
4679 DCHECK(base.IsX());
4680 SingleEmissionCheckScope guard(GetVIXLAssembler());
4681 __ Bind(fixup_label);
4682 __ add(out, base, Operand(/* offset placeholder */ 0));
4683}
4684
4685void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4686 vixl::aarch64::Register out,
4687 vixl::aarch64::Register base) {
4688 DCHECK(base.IsX());
4689 SingleEmissionCheckScope guard(GetVIXLAssembler());
4690 __ Bind(fixup_label);
4691 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4692}
4693
4694template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4695inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4696 const ArenaDeque<PcRelativePatchInfo>& infos,
4697 ArenaVector<LinkerPatch>* linker_patches) {
4698 for (const PcRelativePatchInfo& info : infos) {
4699 linker_patches->push_back(Factory(info.label.GetLocation(),
4700 &info.target_dex_file,
4701 info.pc_insn_label->GetLocation(),
4702 info.offset_or_index));
4703 }
4704}
4705
Vladimir Marko58155012015-08-19 12:49:41 +00004706void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4707 DCHECK(linker_patches->empty());
4708 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004709 pc_relative_dex_cache_patches_.size() +
4710 boot_image_string_patches_.size() +
4711 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004712 boot_image_type_patches_.size() +
4713 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004714 type_bss_entry_patches_.size() +
4715 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004716 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004717 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004718 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004719 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004720 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004721 info.offset_or_index));
4722 }
4723 for (const auto& entry : boot_image_string_patches_) {
4724 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004725 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4726 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004727 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004728 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004729 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004730 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004731 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004732 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4733 linker_patches);
4734 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004735 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4736 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004737 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4738 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004739 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004740 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4741 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004742 for (const auto& entry : boot_image_type_patches_) {
4743 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004744 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4745 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004746 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004747 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004748 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004749 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
4750 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
4751 info.custom_data));
4752 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004753 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004754}
4755
Scott Wakeling97c72b72016-06-24 16:19:36 +01004756vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004757 Uint32ToLiteralMap* map) {
4758 return map->GetOrCreate(
4759 value,
4760 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4761}
4762
Scott Wakeling97c72b72016-06-24 16:19:36 +01004763vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004764 return uint64_literals_.GetOrCreate(
4765 value,
4766 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004767}
4768
Scott Wakeling97c72b72016-06-24 16:19:36 +01004769vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004770 MethodReference target_method,
4771 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004772 return map->GetOrCreate(
4773 target_method,
4774 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004775}
4776
Andreas Gampe878d58c2015-01-15 23:24:00 -08004777void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004778 // Explicit clinit checks triggered by static invokes must have been pruned by
4779 // art::PrepareForRegisterAllocation.
4780 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004781
Andreas Gampe878d58c2015-01-15 23:24:00 -08004782 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4783 return;
4784 }
4785
Artem Serov914d7a82017-02-07 14:33:49 +00004786 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4787 // are no pools emitted.
4788 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004789 LocationSummary* locations = invoke->GetLocations();
4790 codegen_->GenerateStaticOrDirectCall(
4791 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004792 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004793}
4794
4795void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004796 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4797 return;
4798 }
4799
Artem Serov914d7a82017-02-07 14:33:49 +00004800 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4801 // are no pools emitted.
4802 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004803 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004804 DCHECK(!codegen_->IsLeafMethod());
4805 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4806}
4807
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004808HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4809 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004810 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004811 case HLoadClass::LoadKind::kInvalid:
4812 LOG(FATAL) << "UNREACHABLE";
4813 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004814 case HLoadClass::LoadKind::kReferrersClass:
4815 break;
4816 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4817 DCHECK(!GetCompilerOptions().GetCompilePic());
4818 break;
4819 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4820 DCHECK(GetCompilerOptions().GetCompilePic());
4821 break;
4822 case HLoadClass::LoadKind::kBootImageAddress:
4823 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004824 case HLoadClass::LoadKind::kBssEntry:
4825 DCHECK(!Runtime::Current()->UseJitCompilation());
4826 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004827 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004828 DCHECK(Runtime::Current()->UseJitCompilation());
4829 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004830 case HLoadClass::LoadKind::kDexCacheViaMethod:
4831 break;
4832 }
4833 return desired_class_load_kind;
4834}
4835
Alexandre Rames67555f72014-11-18 10:55:16 +00004836void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004837 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4838 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004839 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004840 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004841 cls,
4842 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004843 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004844 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004845 return;
4846 }
Vladimir Marko41559982017-01-06 14:04:23 +00004847 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004848
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004849 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4850 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004851 ? LocationSummary::kCallOnSlowPath
4852 : LocationSummary::kNoCall;
4853 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004854 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004855 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004856 }
4857
Vladimir Marko41559982017-01-06 14:04:23 +00004858 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004859 locations->SetInAt(0, Location::RequiresRegister());
4860 }
4861 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004862 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4863 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4864 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004865 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004866 RegisterSet caller_saves = RegisterSet::Empty();
4867 InvokeRuntimeCallingConvention calling_convention;
4868 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4869 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4870 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4871 Primitive::kPrimNot).GetCode());
4872 locations->SetCustomSlowPathCallerSaves(caller_saves);
4873 } else {
4874 // For non-Baker read barrier we have a temp-clobbering call.
4875 }
4876 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004877}
4878
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004879// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4880// move.
4881void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004882 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4883 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4884 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004885 return;
4886 }
Vladimir Marko41559982017-01-06 14:04:23 +00004887 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004888
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004889 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004890 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004891 Register bss_entry_temp;
4892 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004893
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004894 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4895 ? kWithoutReadBarrier
4896 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004897 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004898 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004899 case HLoadClass::LoadKind::kReferrersClass: {
4900 DCHECK(!cls->CanCallRuntime());
4901 DCHECK(!cls->MustGenerateClinitCheck());
4902 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4903 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004904 GenerateGcRootFieldLoad(cls,
4905 out_loc,
4906 current_method,
4907 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004908 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004909 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004910 break;
4911 }
4912 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004913 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004914 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4915 cls->GetTypeIndex()));
4916 break;
4917 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004918 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004919 // Add ADRP with its PC-relative type patch.
4920 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004921 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004922 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004923 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004924 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004925 vixl::aarch64::Label* add_label =
4926 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004927 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004928 break;
4929 }
4930 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004931 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004932 uint32_t address = dchecked_integral_cast<uint32_t>(
4933 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4934 DCHECK_NE(address, 0u);
4935 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004936 break;
4937 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004938 case HLoadClass::LoadKind::kBssEntry: {
4939 // Add ADRP with its PC-relative Class .bss entry patch.
4940 const DexFile& dex_file = cls->GetDexFile();
4941 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004942 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004943 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4944 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004945 // Add LDR with its PC-relative Class patch.
4946 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004947 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004948 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4949 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004950 out_loc,
4951 bss_entry_temp,
4952 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004953 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004954 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004955 generate_null_check = true;
4956 break;
4957 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004958 case HLoadClass::LoadKind::kJitTableAddress: {
4959 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4960 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004961 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004962 GenerateGcRootFieldLoad(cls,
4963 out_loc,
4964 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004965 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004966 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004967 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004968 break;
4969 }
Vladimir Marko41559982017-01-06 14:04:23 +00004970 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004971 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004972 LOG(FATAL) << "UNREACHABLE";
4973 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004974 }
4975
Vladimir Markoea4c1262017-02-06 19:59:33 +00004976 bool do_clinit = cls->MustGenerateClinitCheck();
4977 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004978 DCHECK(cls->CanCallRuntime());
4979 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00004980 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004981 codegen_->AddSlowPath(slow_path);
4982 if (generate_null_check) {
4983 __ Cbz(out, slow_path->GetEntryLabel());
4984 }
4985 if (cls->MustGenerateClinitCheck()) {
4986 GenerateClassInitializationCheck(slow_path, out);
4987 } else {
4988 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004989 }
4990 }
4991}
4992
David Brazdilcb1c0552015-08-04 16:22:25 +01004993static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004994 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004995}
4996
Alexandre Rames67555f72014-11-18 10:55:16 +00004997void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4998 LocationSummary* locations =
4999 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5000 locations->SetOut(Location::RequiresRegister());
5001}
5002
5003void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005004 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5005}
5006
5007void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
5008 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5009}
5010
5011void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5012 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005013}
5014
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005015HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5016 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005017 switch (desired_string_load_kind) {
5018 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5019 DCHECK(!GetCompilerOptions().GetCompilePic());
5020 break;
5021 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5022 DCHECK(GetCompilerOptions().GetCompilePic());
5023 break;
5024 case HLoadString::LoadKind::kBootImageAddress:
5025 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005026 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005027 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005028 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005029 case HLoadString::LoadKind::kJitTableAddress:
5030 DCHECK(Runtime::Current()->UseJitCompilation());
5031 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005032 case HLoadString::LoadKind::kDexCacheViaMethod:
5033 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005034 }
5035 return desired_string_load_kind;
5036}
5037
Alexandre Rames67555f72014-11-18 10:55:16 +00005038void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005039 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005040 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005041 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005042 InvokeRuntimeCallingConvention calling_convention;
5043 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5044 } else {
5045 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005046 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5047 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005048 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005049 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005050 RegisterSet caller_saves = RegisterSet::Empty();
5051 InvokeRuntimeCallingConvention calling_convention;
5052 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5053 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
5054 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
5055 Primitive::kPrimNot).GetCode());
5056 locations->SetCustomSlowPathCallerSaves(caller_saves);
5057 } else {
5058 // For non-Baker read barrier we have a temp-clobbering call.
5059 }
5060 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005061 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005062}
5063
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005064// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5065// move.
5066void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005067 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005068 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005069
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005070 switch (load->GetLoadKind()) {
5071 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005072 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5073 load->GetStringIndex()));
5074 return; // No dex cache slow path.
5075 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005076 // Add ADRP with its PC-relative String patch.
5077 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005078 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005079 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01005080 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005081 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005082 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005083 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005084 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005085 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005086 return; // No dex cache slow path.
5087 }
5088 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005089 uint32_t address = dchecked_integral_cast<uint32_t>(
5090 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5091 DCHECK_NE(address, 0u);
5092 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005093 return; // No dex cache slow path.
5094 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005095 case HLoadString::LoadKind::kBssEntry: {
5096 // Add ADRP with its PC-relative String .bss entry patch.
5097 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005098 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005099 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005100 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005101 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005102 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005103 // Add LDR with its PC-relative String patch.
5104 vixl::aarch64::Label* ldr_label =
5105 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005106 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005107 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005108 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005109 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005110 /* offset placeholder */ 0u,
5111 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005112 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005113 SlowPathCodeARM64* slow_path =
5114 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005115 codegen_->AddSlowPath(slow_path);
5116 __ Cbz(out.X(), slow_path->GetEntryLabel());
5117 __ Bind(slow_path->GetExitLabel());
5118 return;
5119 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005120 case HLoadString::LoadKind::kJitTableAddress: {
5121 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005122 load->GetStringIndex(),
5123 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005124 GenerateGcRootFieldLoad(load,
5125 out_loc,
5126 out.X(),
5127 /* offset */ 0,
5128 /* fixup_label */ nullptr,
5129 kCompilerReadBarrierOption);
5130 return;
5131 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005132 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005133 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005134 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005135
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005136 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005137 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005138 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005139 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005140 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5141 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005142}
5143
Alexandre Rames5319def2014-10-23 10:03:10 +01005144void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5145 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5146 locations->SetOut(Location::ConstantLocation(constant));
5147}
5148
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005149void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005150 // Will be generated at use site.
5151}
5152
Alexandre Rames67555f72014-11-18 10:55:16 +00005153void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5154 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005155 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005156 InvokeRuntimeCallingConvention calling_convention;
5157 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5158}
5159
5160void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005161 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005162 instruction,
5163 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005164 if (instruction->IsEnter()) {
5165 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5166 } else {
5167 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5168 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005169}
5170
Alexandre Rames42d641b2014-10-27 14:00:51 +00005171void LocationsBuilderARM64::VisitMul(HMul* mul) {
5172 LocationSummary* locations =
5173 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5174 switch (mul->GetResultType()) {
5175 case Primitive::kPrimInt:
5176 case Primitive::kPrimLong:
5177 locations->SetInAt(0, Location::RequiresRegister());
5178 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005179 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005180 break;
5181
5182 case Primitive::kPrimFloat:
5183 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005184 locations->SetInAt(0, Location::RequiresFpuRegister());
5185 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005186 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005187 break;
5188
5189 default:
5190 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5191 }
5192}
5193
5194void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5195 switch (mul->GetResultType()) {
5196 case Primitive::kPrimInt:
5197 case Primitive::kPrimLong:
5198 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5199 break;
5200
5201 case Primitive::kPrimFloat:
5202 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005203 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005204 break;
5205
5206 default:
5207 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5208 }
5209}
5210
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005211void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5212 LocationSummary* locations =
5213 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5214 switch (neg->GetResultType()) {
5215 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005216 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005217 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005218 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005219 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005220
5221 case Primitive::kPrimFloat:
5222 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005223 locations->SetInAt(0, Location::RequiresFpuRegister());
5224 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005225 break;
5226
5227 default:
5228 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5229 }
5230}
5231
5232void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5233 switch (neg->GetResultType()) {
5234 case Primitive::kPrimInt:
5235 case Primitive::kPrimLong:
5236 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5237 break;
5238
5239 case Primitive::kPrimFloat:
5240 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005241 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005242 break;
5243
5244 default:
5245 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5246 }
5247}
5248
5249void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5250 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005251 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005252 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005253 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005254 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5255 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005256}
5257
5258void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005259 // Note: if heap poisoning is enabled, the entry point takes cares
5260 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005261 QuickEntrypointEnum entrypoint =
5262 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5263 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005264 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005265}
5266
Alexandre Rames5319def2014-10-23 10:03:10 +01005267void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5268 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005269 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005270 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005271 if (instruction->IsStringAlloc()) {
5272 locations->AddTemp(LocationFrom(kArtMethodRegister));
5273 } else {
5274 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005275 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005276 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5277}
5278
5279void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005280 // Note: if heap poisoning is enabled, the entry point takes cares
5281 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005282 if (instruction->IsStringAlloc()) {
5283 // String is allocated through StringFactory. Call NewEmptyString entry point.
5284 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005285 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005286 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5287 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005288
5289 {
5290 // Ensure the pc position is recorded immediately after the `blr` instruction.
5291 ExactAssemblyScope eas(GetVIXLAssembler(),
5292 kInstructionSize,
5293 CodeBufferCheckScope::kExactSize);
5294 __ blr(lr);
5295 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5296 }
David Brazdil6de19382016-01-08 17:37:10 +00005297 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005298 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005299 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005300 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005301}
5302
5303void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5304 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005305 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005306 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005307}
5308
5309void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005310 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005311 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005312 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005313 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005314 break;
5315
5316 default:
5317 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5318 }
5319}
5320
David Brazdil66d126e2015-04-03 16:02:44 +01005321void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5322 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5323 locations->SetInAt(0, Location::RequiresRegister());
5324 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5325}
5326
5327void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005328 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005329}
5330
Alexandre Rames5319def2014-10-23 10:03:10 +01005331void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005332 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5333 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005334}
5335
Calin Juravle2ae48182016-03-16 14:05:09 +00005336void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5337 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005338 return;
5339 }
Artem Serov914d7a82017-02-07 14:33:49 +00005340 {
5341 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5342 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5343 Location obj = instruction->GetLocations()->InAt(0);
5344 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5345 RecordPcInfo(instruction, instruction->GetDexPc());
5346 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005347}
5348
Calin Juravle2ae48182016-03-16 14:05:09 +00005349void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005350 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005351 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005352
5353 LocationSummary* locations = instruction->GetLocations();
5354 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005355
5356 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005357}
5358
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005359void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005360 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005361}
5362
Alexandre Rames67555f72014-11-18 10:55:16 +00005363void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5364 HandleBinaryOp(instruction);
5365}
5366
5367void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5368 HandleBinaryOp(instruction);
5369}
5370
Alexandre Rames3e69f162014-12-10 10:36:50 +00005371void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5372 LOG(FATAL) << "Unreachable";
5373}
5374
5375void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5376 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5377}
5378
Alexandre Rames5319def2014-10-23 10:03:10 +01005379void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5380 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5381 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5382 if (location.IsStackSlot()) {
5383 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5384 } else if (location.IsDoubleStackSlot()) {
5385 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5386 }
5387 locations->SetOut(location);
5388}
5389
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005390void InstructionCodeGeneratorARM64::VisitParameterValue(
5391 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005392 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005393}
5394
5395void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5396 LocationSummary* locations =
5397 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005398 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005399}
5400
5401void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5402 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5403 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005404}
5405
5406void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5407 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005408 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005409 locations->SetInAt(i, Location::Any());
5410 }
5411 locations->SetOut(Location::Any());
5412}
5413
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005414void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005415 LOG(FATAL) << "Unreachable";
5416}
5417
Serban Constantinescu02164b32014-11-13 14:05:07 +00005418void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005419 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005420 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005421 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5422 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005423 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5424
5425 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005426 case Primitive::kPrimInt:
5427 case Primitive::kPrimLong:
5428 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005429 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005430 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5431 break;
5432
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005433 case Primitive::kPrimFloat:
5434 case Primitive::kPrimDouble: {
5435 InvokeRuntimeCallingConvention calling_convention;
5436 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5437 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5438 locations->SetOut(calling_convention.GetReturnLocation(type));
5439
5440 break;
5441 }
5442
Serban Constantinescu02164b32014-11-13 14:05:07 +00005443 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005444 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005445 }
5446}
5447
5448void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5449 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005450
Serban Constantinescu02164b32014-11-13 14:05:07 +00005451 switch (type) {
5452 case Primitive::kPrimInt:
5453 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005454 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005455 break;
5456 }
5457
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005458 case Primitive::kPrimFloat:
5459 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005460 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5461 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005462 if (type == Primitive::kPrimFloat) {
5463 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5464 } else {
5465 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5466 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005467 break;
5468 }
5469
Serban Constantinescu02164b32014-11-13 14:05:07 +00005470 default:
5471 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005472 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005473 }
5474}
5475
Calin Juravle27df7582015-04-17 19:12:31 +01005476void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5477 memory_barrier->SetLocations(nullptr);
5478}
5479
5480void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005481 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005482}
5483
Alexandre Rames5319def2014-10-23 10:03:10 +01005484void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5485 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5486 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005487 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005488}
5489
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005490void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005491 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005492}
5493
5494void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5495 instruction->SetLocations(nullptr);
5496}
5497
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005498void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005499 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005500}
5501
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005502void LocationsBuilderARM64::VisitRor(HRor* ror) {
5503 HandleBinaryOp(ror);
5504}
5505
5506void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5507 HandleBinaryOp(ror);
5508}
5509
Serban Constantinescu02164b32014-11-13 14:05:07 +00005510void LocationsBuilderARM64::VisitShl(HShl* shl) {
5511 HandleShift(shl);
5512}
5513
5514void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5515 HandleShift(shl);
5516}
5517
5518void LocationsBuilderARM64::VisitShr(HShr* shr) {
5519 HandleShift(shr);
5520}
5521
5522void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5523 HandleShift(shr);
5524}
5525
Alexandre Rames5319def2014-10-23 10:03:10 +01005526void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005527 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005528}
5529
5530void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005531 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005532}
5533
Alexandre Rames67555f72014-11-18 10:55:16 +00005534void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005535 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005536}
5537
5538void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005539 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005540}
5541
5542void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005543 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005544}
5545
Alexandre Rames67555f72014-11-18 10:55:16 +00005546void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005547 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005548}
5549
Calin Juravlee460d1d2015-09-29 04:52:17 +01005550void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5551 HUnresolvedInstanceFieldGet* instruction) {
5552 FieldAccessCallingConventionARM64 calling_convention;
5553 codegen_->CreateUnresolvedFieldLocationSummary(
5554 instruction, instruction->GetFieldType(), calling_convention);
5555}
5556
5557void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5558 HUnresolvedInstanceFieldGet* instruction) {
5559 FieldAccessCallingConventionARM64 calling_convention;
5560 codegen_->GenerateUnresolvedFieldAccess(instruction,
5561 instruction->GetFieldType(),
5562 instruction->GetFieldIndex(),
5563 instruction->GetDexPc(),
5564 calling_convention);
5565}
5566
5567void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5568 HUnresolvedInstanceFieldSet* instruction) {
5569 FieldAccessCallingConventionARM64 calling_convention;
5570 codegen_->CreateUnresolvedFieldLocationSummary(
5571 instruction, instruction->GetFieldType(), calling_convention);
5572}
5573
5574void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5575 HUnresolvedInstanceFieldSet* instruction) {
5576 FieldAccessCallingConventionARM64 calling_convention;
5577 codegen_->GenerateUnresolvedFieldAccess(instruction,
5578 instruction->GetFieldType(),
5579 instruction->GetFieldIndex(),
5580 instruction->GetDexPc(),
5581 calling_convention);
5582}
5583
5584void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5585 HUnresolvedStaticFieldGet* instruction) {
5586 FieldAccessCallingConventionARM64 calling_convention;
5587 codegen_->CreateUnresolvedFieldLocationSummary(
5588 instruction, instruction->GetFieldType(), calling_convention);
5589}
5590
5591void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5592 HUnresolvedStaticFieldGet* instruction) {
5593 FieldAccessCallingConventionARM64 calling_convention;
5594 codegen_->GenerateUnresolvedFieldAccess(instruction,
5595 instruction->GetFieldType(),
5596 instruction->GetFieldIndex(),
5597 instruction->GetDexPc(),
5598 calling_convention);
5599}
5600
5601void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5602 HUnresolvedStaticFieldSet* instruction) {
5603 FieldAccessCallingConventionARM64 calling_convention;
5604 codegen_->CreateUnresolvedFieldLocationSummary(
5605 instruction, instruction->GetFieldType(), calling_convention);
5606}
5607
5608void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5609 HUnresolvedStaticFieldSet* instruction) {
5610 FieldAccessCallingConventionARM64 calling_convention;
5611 codegen_->GenerateUnresolvedFieldAccess(instruction,
5612 instruction->GetFieldType(),
5613 instruction->GetFieldIndex(),
5614 instruction->GetDexPc(),
5615 calling_convention);
5616}
5617
Alexandre Rames5319def2014-10-23 10:03:10 +01005618void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005619 LocationSummary* locations =
5620 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005621 // In suspend check slow path, usually there are no caller-save registers at all.
5622 // If SIMD instructions are present, however, we force spilling all live SIMD
5623 // registers in full width (since the runtime only saves/restores lower part).
5624 locations->SetCustomSlowPathCallerSaves(
5625 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005626}
5627
5628void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005629 HBasicBlock* block = instruction->GetBlock();
5630 if (block->GetLoopInformation() != nullptr) {
5631 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5632 // The back edge will generate the suspend check.
5633 return;
5634 }
5635 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5636 // The goto will generate the suspend check.
5637 return;
5638 }
5639 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005640}
5641
Alexandre Rames67555f72014-11-18 10:55:16 +00005642void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5643 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005644 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005645 InvokeRuntimeCallingConvention calling_convention;
5646 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5647}
5648
5649void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005650 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005651 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005652}
5653
5654void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5655 LocationSummary* locations =
5656 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5657 Primitive::Type input_type = conversion->GetInputType();
5658 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005659 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005660 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5661 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5662 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5663 }
5664
Alexandre Rames542361f2015-01-29 16:57:31 +00005665 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005666 locations->SetInAt(0, Location::RequiresFpuRegister());
5667 } else {
5668 locations->SetInAt(0, Location::RequiresRegister());
5669 }
5670
Alexandre Rames542361f2015-01-29 16:57:31 +00005671 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005672 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5673 } else {
5674 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5675 }
5676}
5677
5678void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5679 Primitive::Type result_type = conversion->GetResultType();
5680 Primitive::Type input_type = conversion->GetInputType();
5681
5682 DCHECK_NE(input_type, result_type);
5683
Alexandre Rames542361f2015-01-29 16:57:31 +00005684 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005685 int result_size = Primitive::ComponentSize(result_type);
5686 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005687 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005688 Register output = OutputRegister(conversion);
5689 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005690 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005691 // 'int' values are used directly as W registers, discarding the top
5692 // bits, so we don't need to sign-extend and can just perform a move.
5693 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5694 // top 32 bits of the target register. We theoretically could leave those
5695 // bits unchanged, but we would have to make sure that no code uses a
5696 // 32bit input value as a 64bit value assuming that the top 32 bits are
5697 // zero.
5698 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005699 } else if (result_type == Primitive::kPrimChar ||
5700 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5701 __ Ubfx(output,
5702 output.IsX() ? source.X() : source.W(),
5703 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005704 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005705 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005706 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005707 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005708 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005709 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005710 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5711 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005712 } else if (Primitive::IsFloatingPointType(result_type) &&
5713 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005714 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5715 } else {
5716 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5717 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005718 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005719}
Alexandre Rames67555f72014-11-18 10:55:16 +00005720
Serban Constantinescu02164b32014-11-13 14:05:07 +00005721void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5722 HandleShift(ushr);
5723}
5724
5725void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5726 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005727}
5728
5729void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5730 HandleBinaryOp(instruction);
5731}
5732
5733void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5734 HandleBinaryOp(instruction);
5735}
5736
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005737void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005738 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005739 LOG(FATAL) << "Unreachable";
5740}
5741
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005742void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005743 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005744 LOG(FATAL) << "Unreachable";
5745}
5746
Mark Mendellfe57faa2015-09-18 09:26:15 -04005747// Simple implementation of packed switch - generate cascaded compare/jumps.
5748void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5749 LocationSummary* locations =
5750 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5751 locations->SetInAt(0, Location::RequiresRegister());
5752}
5753
5754void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5755 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005756 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005757 Register value_reg = InputRegisterAt(switch_instr, 0);
5758 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5759
Zheng Xu3927c8b2015-11-18 17:46:25 +08005760 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005761 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005762 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5763 // make sure we don't emit it if the target may run out of range.
5764 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5765 // ranges and emit the tables only as required.
5766 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005767
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005768 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005769 // Current instruction id is an upper bound of the number of HIRs in the graph.
5770 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5771 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005772 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5773 Register temp = temps.AcquireW();
5774 __ Subs(temp, value_reg, Operand(lower_bound));
5775
Zheng Xu3927c8b2015-11-18 17:46:25 +08005776 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005777 // Jump to successors[0] if value == lower_bound.
5778 __ B(eq, codegen_->GetLabelOf(successors[0]));
5779 int32_t last_index = 0;
5780 for (; num_entries - last_index > 2; last_index += 2) {
5781 __ Subs(temp, temp, Operand(2));
5782 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5783 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5784 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5785 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5786 }
5787 if (num_entries - last_index == 2) {
5788 // The last missing case_value.
5789 __ Cmp(temp, Operand(1));
5790 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005791 }
5792
5793 // And the default for any other value.
5794 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5795 __ B(codegen_->GetLabelOf(default_block));
5796 }
5797 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005798 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005799
5800 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5801
5802 // Below instructions should use at most one blocked register. Since there are two blocked
5803 // registers, we are free to block one.
5804 Register temp_w = temps.AcquireW();
5805 Register index;
5806 // Remove the bias.
5807 if (lower_bound != 0) {
5808 index = temp_w;
5809 __ Sub(index, value_reg, Operand(lower_bound));
5810 } else {
5811 index = value_reg;
5812 }
5813
5814 // Jump to default block if index is out of the range.
5815 __ Cmp(index, Operand(num_entries));
5816 __ B(hs, codegen_->GetLabelOf(default_block));
5817
5818 // In current VIXL implementation, it won't require any blocked registers to encode the
5819 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5820 // register pressure.
5821 Register table_base = temps.AcquireX();
5822 // Load jump offset from the table.
5823 __ Adr(table_base, jump_table->GetTableStartLabel());
5824 Register jump_offset = temp_w;
5825 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5826
5827 // Jump to target block by branching to table_base(pc related) + offset.
5828 Register target_address = table_base;
5829 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5830 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005831 }
5832}
5833
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005834void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5835 HInstruction* instruction,
5836 Location out,
5837 uint32_t offset,
5838 Location maybe_temp,
5839 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005840 Primitive::Type type = Primitive::kPrimNot;
5841 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005842 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005843 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005844 if (kUseBakerReadBarrier) {
5845 // Load with fast path based Baker's read barrier.
5846 // /* HeapReference<Object> */ out = *(out + offset)
5847 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5848 out,
5849 out_reg,
5850 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005851 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005852 /* needs_null_check */ false,
5853 /* use_load_acquire */ false);
5854 } else {
5855 // Load with slow path based read barrier.
5856 // Save the value of `out` into `maybe_temp` before overwriting it
5857 // in the following move operation, as we will need it for the
5858 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005859 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005860 __ Mov(temp_reg, out_reg);
5861 // /* HeapReference<Object> */ out = *(out + offset)
5862 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5863 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5864 }
5865 } else {
5866 // Plain load with no read barrier.
5867 // /* HeapReference<Object> */ out = *(out + offset)
5868 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5869 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5870 }
5871}
5872
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005873void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5874 HInstruction* instruction,
5875 Location out,
5876 Location obj,
5877 uint32_t offset,
5878 Location maybe_temp,
5879 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005880 Primitive::Type type = Primitive::kPrimNot;
5881 Register out_reg = RegisterFrom(out, type);
5882 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005883 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005884 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005885 if (kUseBakerReadBarrier) {
5886 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005887 // /* HeapReference<Object> */ out = *(obj + offset)
5888 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5889 out,
5890 obj_reg,
5891 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005892 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005893 /* needs_null_check */ false,
5894 /* use_load_acquire */ false);
5895 } else {
5896 // Load with slow path based read barrier.
5897 // /* HeapReference<Object> */ out = *(obj + offset)
5898 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5899 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5900 }
5901 } else {
5902 // Plain load with no read barrier.
5903 // /* HeapReference<Object> */ out = *(obj + offset)
5904 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5905 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5906 }
5907}
5908
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005909void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5910 HInstruction* instruction,
5911 Location root,
5912 Register obj,
5913 uint32_t offset,
5914 vixl::aarch64::Label* fixup_label,
5915 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005916 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005917 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005918 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005919 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005920 if (kUseBakerReadBarrier) {
5921 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005922 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005923 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5924 !Runtime::Current()->UseJitCompilation()) {
5925 // Note that we do not actually check the value of `GetIsGcMarking()`
5926 // to decide whether to mark the loaded GC root or not. Instead, we
5927 // load into `temp` the read barrier mark introspection entrypoint.
5928 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5929 // vice versa.
5930 //
5931 // We use link-time generated thunks for the slow path. That thunk
5932 // checks the reference and jumps to the entrypoint if needed.
5933 //
5934 // temp = Thread::Current()->pReadBarrierMarkIntrospection
5935 // lr = &return_address;
5936 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5937 // if (temp != nullptr) {
5938 // goto gc_root_thunk<root_reg>(lr)
5939 // }
5940 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00005941
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005942 UseScratchRegisterScope temps(GetVIXLAssembler());
5943 DCHECK(temps.IsAvailable(ip0));
5944 DCHECK(temps.IsAvailable(ip1));
5945 temps.Exclude(ip0, ip1);
5946 uint32_t custom_data =
5947 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
5948 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00005949
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005950 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
5951 DCHECK_EQ(ip0.GetCode(), 16u);
5952 const int32_t entry_point_offset =
5953 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
5954 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
5955 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5956 vixl::aarch64::Label return_address;
5957 __ adr(lr, &return_address);
5958 if (fixup_label != nullptr) {
5959 __ Bind(fixup_label);
5960 }
5961 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
5962 "GC root LDR must be 2 instruction (8B) before the return address label.");
5963 __ ldr(root_reg, MemOperand(obj.X(), offset));
5964 __ Bind(cbnz_label);
5965 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
5966 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005967 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005968 // Note that we do not actually check the value of
5969 // `GetIsGcMarking()` to decide whether to mark the loaded GC
5970 // root or not. Instead, we load into `temp` the read barrier
5971 // mark entry point corresponding to register `root`. If `temp`
5972 // is null, it means that `GetIsGcMarking()` is false, and vice
5973 // versa.
5974 //
5975 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5976 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5977 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
5978 // // Slow path.
5979 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
5980 // }
Roland Levillain44015862016-01-22 11:47:17 +00005981
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005982 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
5983 Register temp = lr;
5984 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
5985 instruction, root, /* entrypoint */ LocationFrom(temp));
5986 codegen_->AddSlowPath(slow_path);
5987
5988 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5989 const int32_t entry_point_offset =
5990 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5991 // Loading the entrypoint does not require a load acquire since it is only changed when
5992 // threads are suspended or running a checkpoint.
5993 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5994
5995 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5996 if (fixup_label == nullptr) {
5997 __ Ldr(root_reg, MemOperand(obj, offset));
5998 } else {
5999 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
6000 }
6001 static_assert(
6002 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6003 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6004 "have different sizes.");
6005 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6006 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6007 "have different sizes.");
6008
6009 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6010 // checking GetIsGcMarking.
6011 __ Cbnz(temp, slow_path->GetEntryLabel());
6012 __ Bind(slow_path->GetExitLabel());
6013 }
Roland Levillain44015862016-01-22 11:47:17 +00006014 } else {
6015 // GC root loaded through a slow path for read barriers other
6016 // than Baker's.
6017 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006018 if (fixup_label == nullptr) {
6019 __ Add(root_reg.X(), obj.X(), offset);
6020 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006021 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006022 }
Roland Levillain44015862016-01-22 11:47:17 +00006023 // /* mirror::Object* */ root = root->Read()
6024 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6025 }
6026 } else {
6027 // Plain GC root load with no read barrier.
6028 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006029 if (fixup_label == nullptr) {
6030 __ Ldr(root_reg, MemOperand(obj, offset));
6031 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006032 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006033 }
Roland Levillain44015862016-01-22 11:47:17 +00006034 // Note that GC roots are not affected by heap poisoning, thus we
6035 // do not have to unpoison `root_reg` here.
6036 }
6037}
6038
6039void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6040 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006041 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006042 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006043 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006044 bool needs_null_check,
6045 bool use_load_acquire) {
6046 DCHECK(kEmitCompilerReadBarrier);
6047 DCHECK(kUseBakerReadBarrier);
6048
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006049 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6050 !use_load_acquire &&
6051 !Runtime::Current()->UseJitCompilation()) {
6052 // Note that we do not actually check the value of `GetIsGcMarking()`
6053 // to decide whether to mark the loaded GC root or not. Instead, we
6054 // load into `temp` the read barrier mark introspection entrypoint.
6055 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6056 // vice versa.
6057 //
6058 // We use link-time generated thunks for the slow path. That thunk checks
6059 // the holder and jumps to the entrypoint if needed. If the holder is not
6060 // gray, it creates a fake dependency and returns to the LDR instruction.
6061 //
6062 // temp = Thread::Current()->pReadBarrierMarkIntrospection
6063 // lr = &return_address;
6064 // if (temp != nullptr) {
6065 // goto field_thunk<holder_reg, base_reg>(lr)
6066 // }
6067 // not_gray_return_address:
6068 // // Original reference load. If the offset is too large to fit
6069 // // into LDR, we use an adjusted base register here.
6070 // GcRoot<mirror::Object> root = *(obj+offset);
6071 // gray_return_address:
6072
6073 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6074 Register base = obj;
6075 if (offset >= kReferenceLoadMinFarOffset) {
6076 DCHECK(maybe_temp.IsRegister());
6077 base = WRegisterFrom(maybe_temp);
6078 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6079 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6080 offset &= (kReferenceLoadMinFarOffset - 1u);
6081 }
6082 UseScratchRegisterScope temps(GetVIXLAssembler());
6083 DCHECK(temps.IsAvailable(ip0));
6084 DCHECK(temps.IsAvailable(ip1));
6085 temps.Exclude(ip0, ip1);
6086 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6087 base.GetCode(),
6088 obj.GetCode());
6089 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6090
6091 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6092 DCHECK_EQ(ip0.GetCode(), 16u);
6093 const int32_t entry_point_offset =
6094 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6095 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
6096 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6097 vixl::aarch64::Label return_address;
6098 __ adr(lr, &return_address);
6099 __ Bind(cbnz_label);
6100 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6101 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == -4,
6102 "Field LDR must be 1 instruction (4B) before the return address label.");
6103 __ ldr(RegisterFrom(ref, Primitive::kPrimNot), MemOperand(base.X(), offset));
6104 if (needs_null_check) {
6105 MaybeRecordImplicitNullCheck(instruction);
6106 }
6107 __ Bind(&return_address);
6108 return;
6109 }
6110
Roland Levillain44015862016-01-22 11:47:17 +00006111 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006112 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006113 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006114 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006115 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6116 ref,
6117 obj,
6118 offset,
6119 no_index,
6120 no_scale_factor,
6121 temp,
6122 needs_null_check,
6123 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006124}
6125
6126void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6127 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006128 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006129 uint32_t data_offset,
6130 Location index,
6131 Register temp,
6132 bool needs_null_check) {
6133 DCHECK(kEmitCompilerReadBarrier);
6134 DCHECK(kUseBakerReadBarrier);
6135
6136 // Array cells are never volatile variables, therefore array loads
6137 // never use Load-Acquire instructions on ARM64.
6138 const bool use_load_acquire = false;
6139
Roland Levillainbfea3352016-06-23 13:48:47 +01006140 static_assert(
6141 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6142 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00006143 // /* HeapReference<Object> */ ref =
6144 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006145 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
6146 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6147 ref,
6148 obj,
6149 data_offset,
6150 index,
6151 scale_factor,
6152 temp,
6153 needs_null_check,
6154 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006155}
6156
6157void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6158 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006159 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006160 uint32_t offset,
6161 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006162 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006163 Register temp,
6164 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006165 bool use_load_acquire,
6166 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00006167 DCHECK(kEmitCompilerReadBarrier);
6168 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006169 // If we are emitting an array load, we should not be using a
6170 // Load Acquire instruction. In other words:
6171 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6172 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006173
Roland Levillain54f869e2017-03-06 13:54:11 +00006174 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6175 // whether we need to enter the slow path to mark the reference.
6176 // Then, in the slow path, check the gray bit in the lock word of
6177 // the reference's holder (`obj`) to decide whether to mark `ref` or
6178 // not.
Roland Levillain44015862016-01-22 11:47:17 +00006179 //
Roland Levillainba650a42017-03-06 13:52:32 +00006180 // Note that we do not actually check the value of `GetIsGcMarking()`;
6181 // instead, we load into `temp2` the read barrier mark entry point
6182 // corresponding to register `ref`. If `temp2` is null, it means
6183 // that `GetIsGcMarking()` is false, and vice versa.
6184 //
6185 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00006186 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6187 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006188 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6189 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6190 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6191 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6192 // if (is_gray) {
6193 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6194 // }
6195 // } else {
6196 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006197 // }
Roland Levillain44015862016-01-22 11:47:17 +00006198
Roland Levillainba650a42017-03-06 13:52:32 +00006199 // Slow path marking the object `ref` when the GC is marking. The
6200 // entrypoint will already be loaded in `temp2`.
6201 Register temp2 = lr;
6202 Location temp2_loc = LocationFrom(temp2);
6203 SlowPathCodeARM64* slow_path;
6204 if (always_update_field) {
Roland Levillain54f869e2017-03-06 13:54:11 +00006205 // LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
6206 // only supports address of the form `obj + field_offset`, where
6207 // `obj` is a register and `field_offset` is a register. Thus
6208 // `offset` and `scale_factor` above are expected to be null in
6209 // this code path.
Roland Levillainba650a42017-03-06 13:52:32 +00006210 DCHECK_EQ(offset, 0u);
6211 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
Roland Levillain54f869e2017-03-06 13:54:11 +00006212 Location field_offset = index;
6213 slow_path =
6214 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6215 instruction,
6216 ref,
6217 obj,
6218 offset,
6219 /* index */ field_offset,
6220 scale_factor,
6221 needs_null_check,
6222 use_load_acquire,
6223 temp,
6224 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006225 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +00006226 slow_path = new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6227 instruction,
6228 ref,
6229 obj,
6230 offset,
6231 index,
6232 scale_factor,
6233 needs_null_check,
6234 use_load_acquire,
6235 temp,
6236 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006237 }
6238 AddSlowPath(slow_path);
6239
6240 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6241 const int32_t entry_point_offset =
6242 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6243 // Loading the entrypoint does not require a load acquire since it is only changed when
6244 // threads are suspended or running a checkpoint.
6245 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006246 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6247 // checking GetIsGcMarking.
6248 __ Cbnz(temp2, slow_path->GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +00006249 // Fast path: just load the reference.
6250 GenerateRawReferenceLoad(
6251 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006252 __ Bind(slow_path->GetExitLabel());
6253}
6254
6255void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6256 Location ref,
6257 Register obj,
6258 uint32_t offset,
6259 Location index,
6260 size_t scale_factor,
6261 bool needs_null_check,
6262 bool use_load_acquire) {
6263 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006264 Primitive::Type type = Primitive::kPrimNot;
6265 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006266
Roland Levillainba650a42017-03-06 13:52:32 +00006267 // If needed, vixl::EmissionCheckScope guards are used to ensure
6268 // that no pools are emitted between the load (macro) instruction
6269 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006270
Roland Levillain44015862016-01-22 11:47:17 +00006271 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006272 // Load types involving an "index": ArrayGet,
6273 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6274 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006275 if (use_load_acquire) {
6276 // UnsafeGetObjectVolatile intrinsic case.
6277 // Register `index` is not an index in an object array, but an
6278 // offset to an object reference field within object `obj`.
6279 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6280 DCHECK(instruction->GetLocations()->Intrinsified());
6281 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6282 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006283 DCHECK_EQ(offset, 0u);
6284 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006285 DCHECK_EQ(needs_null_check, false);
6286 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006287 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6288 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006289 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006290 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6291 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006292 if (index.IsConstant()) {
6293 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006294 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006295 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006296 if (needs_null_check) {
6297 MaybeRecordImplicitNullCheck(instruction);
6298 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006299 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006300 UseScratchRegisterScope temps(GetVIXLAssembler());
6301 Register temp = temps.AcquireW();
6302 __ Add(temp, obj, offset);
6303 {
6304 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6305 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6306 if (needs_null_check) {
6307 MaybeRecordImplicitNullCheck(instruction);
6308 }
6309 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006310 }
Roland Levillain44015862016-01-22 11:47:17 +00006311 }
Roland Levillain44015862016-01-22 11:47:17 +00006312 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006313 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006314 MemOperand field = HeapOperand(obj, offset);
6315 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006316 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6317 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006318 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006319 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006320 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006321 if (needs_null_check) {
6322 MaybeRecordImplicitNullCheck(instruction);
6323 }
Roland Levillain44015862016-01-22 11:47:17 +00006324 }
6325 }
6326
6327 // Object* ref = ref_addr->AsMirrorPtr()
6328 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006329}
6330
6331void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6332 Location out,
6333 Location ref,
6334 Location obj,
6335 uint32_t offset,
6336 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006337 DCHECK(kEmitCompilerReadBarrier);
6338
Roland Levillain44015862016-01-22 11:47:17 +00006339 // Insert a slow path based read barrier *after* the reference load.
6340 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006341 // If heap poisoning is enabled, the unpoisoning of the loaded
6342 // reference will be carried out by the runtime within the slow
6343 // path.
6344 //
6345 // Note that `ref` currently does not get unpoisoned (when heap
6346 // poisoning is enabled), which is alright as the `ref` argument is
6347 // not used by the artReadBarrierSlow entry point.
6348 //
6349 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6350 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6351 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6352 AddSlowPath(slow_path);
6353
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006354 __ B(slow_path->GetEntryLabel());
6355 __ Bind(slow_path->GetExitLabel());
6356}
6357
Roland Levillain44015862016-01-22 11:47:17 +00006358void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6359 Location out,
6360 Location ref,
6361 Location obj,
6362 uint32_t offset,
6363 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006364 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006365 // Baker's read barriers shall be handled by the fast path
6366 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6367 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006368 // If heap poisoning is enabled, unpoisoning will be taken care of
6369 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006370 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006371 } else if (kPoisonHeapReferences) {
6372 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6373 }
6374}
6375
Roland Levillain44015862016-01-22 11:47:17 +00006376void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6377 Location out,
6378 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006379 DCHECK(kEmitCompilerReadBarrier);
6380
Roland Levillain44015862016-01-22 11:47:17 +00006381 // Insert a slow path based read barrier *after* the GC root load.
6382 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006383 // Note that GC roots are not affected by heap poisoning, so we do
6384 // not need to do anything special for this here.
6385 SlowPathCodeARM64* slow_path =
6386 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6387 AddSlowPath(slow_path);
6388
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006389 __ B(slow_path->GetEntryLabel());
6390 __ Bind(slow_path->GetExitLabel());
6391}
6392
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006393void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6394 LocationSummary* locations =
6395 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6396 locations->SetInAt(0, Location::RequiresRegister());
6397 locations->SetOut(Location::RequiresRegister());
6398}
6399
6400void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6401 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006402 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006403 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006404 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006405 __ Ldr(XRegisterFrom(locations->Out()),
6406 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006407 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006408 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006409 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006410 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6411 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006412 __ Ldr(XRegisterFrom(locations->Out()),
6413 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006414 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006415}
6416
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006417static void PatchJitRootUse(uint8_t* code,
6418 const uint8_t* roots_data,
6419 vixl::aarch64::Literal<uint32_t>* literal,
6420 uint64_t index_in_table) {
6421 uint32_t literal_offset = literal->GetOffset();
6422 uintptr_t address =
6423 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6424 uint8_t* data = code + literal_offset;
6425 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6426}
6427
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006428void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6429 for (const auto& entry : jit_string_patches_) {
6430 const auto& it = jit_string_roots_.find(entry.first);
6431 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006432 PatchJitRootUse(code, roots_data, entry.second, it->second);
6433 }
6434 for (const auto& entry : jit_class_patches_) {
6435 const auto& it = jit_class_roots_.find(entry.first);
6436 DCHECK(it != jit_class_roots_.end());
6437 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006438 }
6439}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006440
Alexandre Rames67555f72014-11-18 10:55:16 +00006441#undef __
6442#undef QUICK_ENTRY_POINT
6443
Alexandre Rames5319def2014-10-23 10:03:10 +01006444} // namespace arm64
6445} // namespace art