blob: cbe52aa4a3e737565b9cbd98cd0ef9c07c7967a0 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Vladimir Markof4f2daa2017-03-20 18:26:59 +000019#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000020#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080022#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010024#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080025#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010026#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080027#include "intrinsics.h"
28#include "intrinsics_arm64.h"
Vladimir Markof4f2daa2017-03-20 18:26:59 +000029#include "linker/arm64/relative_patcher_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010030#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000032#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010033#include "thread.h"
34#include "utils/arm64/assembler_arm64.h"
35#include "utils/assembler.h"
36#include "utils/stack_checks.h"
37
Scott Wakeling97c72b72016-06-24 16:19:36 +010038using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000039using vixl::ExactAssemblyScope;
40using vixl::CodeBufferCheckScope;
41using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010042
43#ifdef __
44#error "ARM64 Codegen VIXL macro-assembler macro already defined."
45#endif
46
Alexandre Rames5319def2014-10-23 10:03:10 +010047namespace art {
48
Roland Levillain22ccc3a2015-11-24 13:10:05 +000049template<class MirrorType>
50class GcRoot;
51
Alexandre Rames5319def2014-10-23 10:03:10 +010052namespace arm64 {
53
Alexandre Ramesbe919d92016-08-23 18:33:36 +010054using helpers::ARM64EncodableConstantOrRegister;
55using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080056using helpers::CPURegisterFrom;
57using helpers::DRegisterFrom;
58using helpers::FPRegisterFrom;
59using helpers::HeapOperand;
60using helpers::HeapOperandFrom;
61using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010062using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080063using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080064using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010065using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080066using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010067using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080068using helpers::LocationFrom;
69using helpers::OperandFromMemOperand;
70using helpers::OutputCPURegister;
71using helpers::OutputFPRegister;
72using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010073using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080074using helpers::RegisterFrom;
75using helpers::StackOperandFrom;
76using helpers::VIXLRegCodeFromART;
77using helpers::WRegisterFrom;
78using helpers::XRegisterFrom;
79
Alexandre Rames5319def2014-10-23 10:03:10 +010080static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000081// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080082// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
83// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000084static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010085
Vladimir Markof4f2daa2017-03-20 18:26:59 +000086// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
87// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
88// For the Baker read barrier implementation using link-generated thunks we need to split
89// the offset explicitly.
90constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
91
92// Flags controlling the use of link-time generated thunks for Baker read barriers.
Vladimir Markod1ef8732017-04-18 13:55:13 +010093constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
Vladimir Marko66d691d2017-04-07 17:53:39 +010094constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
Vladimir Markod1ef8732017-04-18 13:55:13 +010095constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
Vladimir Markof4f2daa2017-03-20 18:26:59 +000096
97// Some instructions have special requirements for a temporary, for example
98// LoadClass/kBssEntry and LoadString/kBssEntry for Baker read barrier require
99// temp that's not an R0 (to avoid an extra move) and Baker read barrier field
100// loads with large offsets need a fixed register to limit the number of link-time
101// thunks we generate. For these and similar cases, we want to reserve a specific
102// register that's neither callee-save nor an argument register. We choose x15.
103inline Location FixedTempLocation() {
104 return Location::RegisterLocation(x15.GetCode());
105}
106
Alexandre Rames5319def2014-10-23 10:03:10 +0100107inline Condition ARM64Condition(IfCondition cond) {
108 switch (cond) {
109 case kCondEQ: return eq;
110 case kCondNE: return ne;
111 case kCondLT: return lt;
112 case kCondLE: return le;
113 case kCondGT: return gt;
114 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700115 case kCondB: return lo;
116 case kCondBE: return ls;
117 case kCondA: return hi;
118 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100119 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000120 LOG(FATAL) << "Unreachable";
121 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100122}
123
Vladimir Markod6e069b2016-01-18 11:11:01 +0000124inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
125 // The ARM64 condition codes can express all the necessary branches, see the
126 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
127 // There is no dex instruction or HIR that would need the missing conditions
128 // "equal or unordered" or "not equal".
129 switch (cond) {
130 case kCondEQ: return eq;
131 case kCondNE: return ne /* unordered */;
132 case kCondLT: return gt_bias ? cc : lt /* unordered */;
133 case kCondLE: return gt_bias ? ls : le /* unordered */;
134 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
135 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
136 default:
137 LOG(FATAL) << "UNREACHABLE";
138 UNREACHABLE();
139 }
140}
141
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000142Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000143 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
144 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
145 // but we use the exact registers for clarity.
146 if (return_type == Primitive::kPrimFloat) {
147 return LocationFrom(s0);
148 } else if (return_type == Primitive::kPrimDouble) {
149 return LocationFrom(d0);
150 } else if (return_type == Primitive::kPrimLong) {
151 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100152 } else if (return_type == Primitive::kPrimVoid) {
153 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000154 } else {
155 return LocationFrom(w0);
156 }
157}
158
Alexandre Rames5319def2014-10-23 10:03:10 +0100159Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000160 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100161}
162
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100163// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
164#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700165#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100166
Zheng Xuda403092015-04-24 17:35:39 +0800167// Calculate memory accessing operand for save/restore live registers.
168static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100169 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800170 int64_t spill_offset,
171 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100172 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
173 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
174 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800175 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100176 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800177 codegen->GetNumberOfFloatingPointRegisters()));
178
Vladimir Marko804b03f2016-09-14 16:26:36 +0100179 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100180 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
181 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800182
183 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
184 UseScratchRegisterScope temps(masm);
185
186 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100187 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
188 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800189 int64_t reg_size = kXRegSizeInBytes;
190 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
191 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100192 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800193 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
194 // If the offset does not fit in the instruction's immediate field, use an alternate register
195 // to compute the base address(float point registers spill base address).
196 Register new_base = temps.AcquireSameSizeAs(base);
197 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
198 base = new_base;
199 spill_offset = -core_spill_size;
200 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
201 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
202 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
203 }
204
205 if (is_save) {
206 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
207 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
208 } else {
209 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
210 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
211 }
212}
213
214void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800215 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100216 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
217 for (uint32_t i : LowToHighBits(core_spills)) {
218 // If the register holds an object, update the stack mask.
219 if (locations->RegisterContainsObject(i)) {
220 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800221 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100222 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
223 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
224 saved_core_stack_offsets_[i] = stack_offset;
225 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800226 }
227
Vladimir Marko804b03f2016-09-14 16:26:36 +0100228 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
229 for (uint32_t i : LowToHighBits(fp_spills)) {
230 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
231 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
232 saved_fpu_stack_offsets_[i] = stack_offset;
233 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800234 }
235
Vladimir Marko804b03f2016-09-14 16:26:36 +0100236 SaveRestoreLiveRegistersHelper(codegen,
237 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800238 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
239}
240
241void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100242 SaveRestoreLiveRegistersHelper(codegen,
243 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800244 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
245}
246
Alexandre Rames5319def2014-10-23 10:03:10 +0100247class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
248 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000249 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100250
Alexandre Rames67555f72014-11-18 10:55:16 +0000251 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100252 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000253 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100254
Alexandre Rames5319def2014-10-23 10:03:10 +0100255 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000256 if (instruction_->CanThrowIntoCatchBlock()) {
257 // Live registers will be restored in the catch block if caught.
258 SaveLiveRegisters(codegen, instruction_->GetLocations());
259 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000260 // We're moving two locations to locations that could overlap, so we need a parallel
261 // move resolver.
262 InvokeRuntimeCallingConvention calling_convention;
263 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100264 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
265 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000266 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
267 ? kQuickThrowStringBounds
268 : kQuickThrowArrayBounds;
269 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100270 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800271 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100272 }
273
Alexandre Rames8158f282015-08-07 10:26:17 +0100274 bool IsFatal() const OVERRIDE { return true; }
275
Alexandre Rames9931f312015-06-19 14:47:01 +0100276 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
277
Alexandre Rames5319def2014-10-23 10:03:10 +0100278 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100279 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
280};
281
Alexandre Rames67555f72014-11-18 10:55:16 +0000282class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
283 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000284 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000285
286 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
287 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
288 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000289 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800290 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000291 }
292
Alexandre Rames8158f282015-08-07 10:26:17 +0100293 bool IsFatal() const OVERRIDE { return true; }
294
Alexandre Rames9931f312015-06-19 14:47:01 +0100295 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
296
Alexandre Rames67555f72014-11-18 10:55:16 +0000297 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
299};
300
301class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
302 public:
303 LoadClassSlowPathARM64(HLoadClass* cls,
304 HInstruction* at,
305 uint32_t dex_pc,
Vladimir Markoea4c1262017-02-06 19:59:33 +0000306 bool do_clinit,
307 vixl::aarch64::Register bss_entry_temp = vixl::aarch64::Register(),
308 vixl::aarch64::Label* bss_entry_adrp_label = nullptr)
309 : SlowPathCodeARM64(at),
310 cls_(cls),
311 dex_pc_(dex_pc),
312 do_clinit_(do_clinit),
313 bss_entry_temp_(bss_entry_temp),
314 bss_entry_adrp_label_(bss_entry_adrp_label) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000315 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
316 }
317
318 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000319 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000320 Location out = locations->Out();
321 constexpr bool call_saves_everything_except_r0_ip0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexandre Rames67555f72014-11-18 10:55:16 +0000322 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
323
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000324 InvokeRuntimeCallingConvention calling_convention;
325 // For HLoadClass/kBssEntry/kSaveEverything, the page address of the entry is in a temp
326 // register, make sure it's not clobbered by the call or by saving/restoring registers.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000327 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
328 bool is_load_class_bss_entry =
329 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000330 if (is_load_class_bss_entry) {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000331 DCHECK(bss_entry_temp_.IsValid());
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000332 DCHECK(!bss_entry_temp_.Is(calling_convention.GetRegisterAt(0)));
333 DCHECK(
334 !UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(bss_entry_temp_));
Vladimir Markoea4c1262017-02-06 19:59:33 +0000335 }
336
Alexandre Rames67555f72014-11-18 10:55:16 +0000337 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000338 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000339
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000340 dex::TypeIndex type_index = cls_->GetTypeIndex();
341 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000342 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
343 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000344 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800345 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100346 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800347 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100348 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800349 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000350
351 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 if (out.IsValid()) {
353 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000354 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000355 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000357 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000358 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000359 if (is_load_class_bss_entry) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000360 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000361 const DexFile& dex_file = cls_->GetDexFile();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000362 if (call_saves_everything_except_r0_ip0) {
363 // The class entry page address was preserved in bss_entry_temp_ thanks to kSaveEverything.
364 } else {
365 // For non-Baker read barrier, we need to re-calculate the address of the class entry page.
366 bss_entry_adrp_label_ = arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
367 arm64_codegen->EmitAdrpPlaceholder(bss_entry_adrp_label_, bss_entry_temp_);
368 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000369 vixl::aarch64::Label* strp_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +0000370 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label_);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000371 {
372 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
373 __ Bind(strp_label);
374 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Markoea4c1262017-02-06 19:59:33 +0000375 MemOperand(bss_entry_temp_, /* offset placeholder */ 0));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000376 }
377 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000378 __ B(GetExitLabel());
379 }
380
Alexandre Rames9931f312015-06-19 14:47:01 +0100381 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
382
Alexandre Rames67555f72014-11-18 10:55:16 +0000383 private:
384 // The class this slow path will load.
385 HLoadClass* const cls_;
386
Alexandre Rames67555f72014-11-18 10:55:16 +0000387 // The dex PC of `at_`.
388 const uint32_t dex_pc_;
389
390 // Whether to initialize the class.
391 const bool do_clinit_;
392
Vladimir Markoea4c1262017-02-06 19:59:33 +0000393 // For HLoadClass/kBssEntry, the temp register and the label of the ADRP where it was loaded.
394 vixl::aarch64::Register bss_entry_temp_;
395 vixl::aarch64::Label* bss_entry_adrp_label_;
396
Alexandre Rames67555f72014-11-18 10:55:16 +0000397 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
398};
399
Vladimir Markoaad75c62016-10-03 08:46:48 +0000400class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
401 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100402 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
403 : SlowPathCodeARM64(instruction),
404 temp_(temp),
405 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000406
407 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
408 LocationSummary* locations = instruction_->GetLocations();
409 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
410 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
411
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000412 InvokeRuntimeCallingConvention calling_convention;
413 // Make sure `temp_` is not clobbered by the call or by saving/restoring registers.
414 DCHECK(temp_.IsValid());
415 DCHECK(!temp_.Is(calling_convention.GetRegisterAt(0)));
416 DCHECK(!UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(temp_));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100417
Vladimir Markoaad75c62016-10-03 08:46:48 +0000418 __ Bind(GetEntryLabel());
419 SaveLiveRegisters(codegen, locations);
420
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000421 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
422 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000423 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
424 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
425 Primitive::Type type = instruction_->GetType();
426 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
427
428 RestoreLiveRegisters(codegen, locations);
429
430 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000431 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100432 if (!kUseReadBarrier || kUseBakerReadBarrier) {
433 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
434 } else {
435 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
436 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
437 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
438 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000439 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100440 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000441 {
442 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
443 __ Bind(strp_label);
444 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100445 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000446 }
447
448 __ B(GetExitLabel());
449 }
450
451 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
452
453 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100454 const Register temp_;
455 vixl::aarch64::Label* adrp_label_;
456
Vladimir Markoaad75c62016-10-03 08:46:48 +0000457 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
458};
459
Alexandre Rames5319def2014-10-23 10:03:10 +0100460class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
461 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000462 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100463
Alexandre Rames67555f72014-11-18 10:55:16 +0000464 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
465 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100466 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000467 if (instruction_->CanThrowIntoCatchBlock()) {
468 // Live registers will be restored in the catch block if caught.
469 SaveLiveRegisters(codegen, instruction_->GetLocations());
470 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000471 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
472 instruction_,
473 instruction_->GetDexPc(),
474 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800475 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100476 }
477
Alexandre Rames8158f282015-08-07 10:26:17 +0100478 bool IsFatal() const OVERRIDE { return true; }
479
Alexandre Rames9931f312015-06-19 14:47:01 +0100480 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
481
Alexandre Rames5319def2014-10-23 10:03:10 +0100482 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100483 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
484};
485
486class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
487 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100488 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000489 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100490
Alexandre Rames67555f72014-11-18 10:55:16 +0000491 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100492 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000493 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100494 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100495 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000496 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800497 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100498 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000499 if (successor_ == nullptr) {
500 __ B(GetReturnLabel());
501 } else {
502 __ B(arm64_codegen->GetLabelOf(successor_));
503 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100504 }
505
Scott Wakeling97c72b72016-06-24 16:19:36 +0100506 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100507 DCHECK(successor_ == nullptr);
508 return &return_label_;
509 }
510
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100511 HBasicBlock* GetSuccessor() const {
512 return successor_;
513 }
514
Alexandre Rames9931f312015-06-19 14:47:01 +0100515 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
516
Alexandre Rames5319def2014-10-23 10:03:10 +0100517 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100518 // If not null, the block to branch to after the suspend check.
519 HBasicBlock* const successor_;
520
521 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100522 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100523
524 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
525};
526
Alexandre Rames67555f72014-11-18 10:55:16 +0000527class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
528 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000529 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000530 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000531
532 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000533 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800534
Alexandre Rames3e69f162014-12-10 10:36:50 +0000535 DCHECK(instruction_->IsCheckCast()
536 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
537 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100538 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000539
Alexandre Rames67555f72014-11-18 10:55:16 +0000540 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000541
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000542 if (!is_fatal_) {
543 SaveLiveRegisters(codegen, locations);
544 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000545
546 // We're moving two locations to locations that could overlap, so we need a parallel
547 // move resolver.
548 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800549 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800550 LocationFrom(calling_convention.GetRegisterAt(0)),
551 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800552 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800553 LocationFrom(calling_convention.GetRegisterAt(1)),
554 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000555 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000556 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800557 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000558 Primitive::Type ret_type = instruction_->GetType();
559 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
560 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
561 } else {
562 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800563 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
564 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000565 }
566
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000567 if (!is_fatal_) {
568 RestoreLiveRegisters(codegen, locations);
569 __ B(GetExitLabel());
570 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000571 }
572
Alexandre Rames9931f312015-06-19 14:47:01 +0100573 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100574 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100575
Alexandre Rames67555f72014-11-18 10:55:16 +0000576 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000577 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000578
Alexandre Rames67555f72014-11-18 10:55:16 +0000579 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
580};
581
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700582class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
583 public:
Aart Bik42249c32016-01-07 15:33:50 -0800584 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000585 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700586
587 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800588 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700589 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100590 LocationSummary* locations = instruction_->GetLocations();
591 SaveLiveRegisters(codegen, locations);
592 InvokeRuntimeCallingConvention calling_convention;
593 __ Mov(calling_convention.GetRegisterAt(0),
594 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000595 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100596 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700597 }
598
Alexandre Rames9931f312015-06-19 14:47:01 +0100599 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
600
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700601 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700602 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
603};
604
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100605class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
606 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000607 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100608
609 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
610 LocationSummary* locations = instruction_->GetLocations();
611 __ Bind(GetEntryLabel());
612 SaveLiveRegisters(codegen, locations);
613
614 InvokeRuntimeCallingConvention calling_convention;
615 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
616 parallel_move.AddMove(
617 locations->InAt(0),
618 LocationFrom(calling_convention.GetRegisterAt(0)),
619 Primitive::kPrimNot,
620 nullptr);
621 parallel_move.AddMove(
622 locations->InAt(1),
623 LocationFrom(calling_convention.GetRegisterAt(1)),
624 Primitive::kPrimInt,
625 nullptr);
626 parallel_move.AddMove(
627 locations->InAt(2),
628 LocationFrom(calling_convention.GetRegisterAt(2)),
629 Primitive::kPrimNot,
630 nullptr);
631 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
632
633 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000634 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100635 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
636 RestoreLiveRegisters(codegen, locations);
637 __ B(GetExitLabel());
638 }
639
640 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
641
642 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100643 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
644};
645
Zheng Xu3927c8b2015-11-18 17:46:25 +0800646void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
647 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000648 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800649
650 // We are about to use the assembler to place literals directly. Make sure we have enough
651 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000652 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
653 num_entries * sizeof(int32_t),
654 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800655
656 __ Bind(&table_start_);
657 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
658 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100659 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800660 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100661 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800662 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
663 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
664 Literal<int32_t> literal(jump_offset);
665 __ place(&literal);
666 }
667}
668
Roland Levillain54f869e2017-03-06 13:54:11 +0000669// Abstract base class for read barrier slow paths marking a reference
670// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000671//
Roland Levillain54f869e2017-03-06 13:54:11 +0000672// Argument `entrypoint` must be a register location holding the read
673// barrier marking runtime entry point to be invoked.
674class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
675 protected:
676 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
677 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000678 DCHECK(kEmitCompilerReadBarrier);
679 }
680
Roland Levillain54f869e2017-03-06 13:54:11 +0000681 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000682
Roland Levillain54f869e2017-03-06 13:54:11 +0000683 // Generate assembly code calling the read barrier marking runtime
684 // entry point (ReadBarrierMarkRegX).
685 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000686 // No need to save live registers; it's taken care of by the
687 // entrypoint. Also, there is no need to update the stack mask,
688 // as this runtime call will not trigger a garbage collection.
689 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
690 DCHECK_NE(ref_.reg(), LR);
691 DCHECK_NE(ref_.reg(), WSP);
692 DCHECK_NE(ref_.reg(), WZR);
693 // IP0 is used internally by the ReadBarrierMarkRegX entry point
694 // as a temporary, it cannot be the entry point's input/output.
695 DCHECK_NE(ref_.reg(), IP0);
696 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
697 // "Compact" slow path, saving two moves.
698 //
699 // Instead of using the standard runtime calling convention (input
700 // and output in W0):
701 //
702 // W0 <- ref
703 // W0 <- ReadBarrierMark(W0)
704 // ref <- W0
705 //
706 // we just use rX (the register containing `ref`) as input and output
707 // of a dedicated entrypoint:
708 //
709 // rX <- ReadBarrierMarkRegX(rX)
710 //
711 if (entrypoint_.IsValid()) {
712 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
713 __ Blr(XRegisterFrom(entrypoint_));
714 } else {
715 // Entrypoint is not already loaded, load from the thread.
716 int32_t entry_point_offset =
717 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
718 // This runtime call does not require a stack map.
719 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
720 }
721 }
722
723 // The location (register) of the marked object reference.
724 const Location ref_;
725
726 // The location of the entrypoint if it is already loaded.
727 const Location entrypoint_;
728
Roland Levillain54f869e2017-03-06 13:54:11 +0000729 private:
730 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
731};
732
Alexandre Rames5319def2014-10-23 10:03:10 +0100733// Slow path marking an object reference `ref` during a read
734// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000735// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100736//
737// This means that after the execution of this slow path, `ref` will
738// always be up-to-date, but `obj.field` may not; i.e., after the
739// flip, `ref` will be a to-space reference, but `obj.field` will
740// probably still be a from-space reference (unless it gets updated by
741// another thread, or if another thread installed another object
742// reference (different from `ref`) in `obj.field`).
743//
744// If `entrypoint` is a valid location it is assumed to already be
745// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000746// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000747class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100748 public:
749 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
750 Location ref,
751 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000752 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100753 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100754 }
755
756 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
757
758 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000759 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100760 DCHECK(locations->CanCall());
761 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000762 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000763 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
764 << "Unexpected instruction in read barrier marking slow path: "
765 << instruction_->DebugName();
766
767 __ Bind(GetEntryLabel());
768 GenerateReadBarrierMarkRuntimeCall(codegen);
769 __ B(GetExitLabel());
770 }
771
772 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000773 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
774};
775
Roland Levillain54f869e2017-03-06 13:54:11 +0000776// Slow path loading `obj`'s lock word, loading a reference from
777// object `*(obj + offset + (index << scale_factor))` into `ref`, and
778// marking `ref` if `obj` is gray according to the lock word (Baker
779// read barrier). The field `obj.field` in the object `obj` holding
780// this reference does not get updated by this slow path after marking
781// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
782// below for that).
783//
784// This means that after the execution of this slow path, `ref` will
785// always be up-to-date, but `obj.field` may not; i.e., after the
786// flip, `ref` will be a to-space reference, but `obj.field` will
787// probably still be a from-space reference (unless it gets updated by
788// another thread, or if another thread installed another object
789// reference (different from `ref`) in `obj.field`).
790//
791// Argument `entrypoint` must be a register location holding the read
792// barrier marking runtime entry point to be invoked.
793class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
794 public:
795 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
796 Location ref,
797 Register obj,
798 uint32_t offset,
799 Location index,
800 size_t scale_factor,
801 bool needs_null_check,
802 bool use_load_acquire,
803 Register temp,
804 Location entrypoint)
805 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
806 obj_(obj),
807 offset_(offset),
808 index_(index),
809 scale_factor_(scale_factor),
810 needs_null_check_(needs_null_check),
811 use_load_acquire_(use_load_acquire),
812 temp_(temp) {
813 DCHECK(kEmitCompilerReadBarrier);
814 DCHECK(kUseBakerReadBarrier);
815 }
816
817 const char* GetDescription() const OVERRIDE {
818 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
819 }
820
821 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
822 LocationSummary* locations = instruction_->GetLocations();
823 DCHECK(locations->CanCall());
824 DCHECK(ref_.IsRegister()) << ref_;
825 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
826 DCHECK(obj_.IsW());
827 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100828 DCHECK(instruction_->IsInstanceFieldGet() ||
829 instruction_->IsStaticFieldGet() ||
830 instruction_->IsArrayGet() ||
831 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100832 instruction_->IsInstanceOf() ||
833 instruction_->IsCheckCast() ||
834 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
835 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
836 << "Unexpected instruction in read barrier marking slow path: "
837 << instruction_->DebugName();
838 // The read barrier instrumentation of object ArrayGet
839 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000840 // instruction.
841 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000842 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
843
Roland Levillain54f869e2017-03-06 13:54:11 +0000844 // Temporary register `temp_`, used to store the lock word, must
845 // not be IP0 nor IP1, as we may use them to emit the reference
846 // load (in the call to GenerateRawReferenceLoad below), and we
847 // need the lock word to still be in `temp_` after the reference
848 // load.
849 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
850 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
851
Alexandre Rames5319def2014-10-23 10:03:10 +0100852 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000853
854 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
855 // inserted after the original load. However, in fast path based
856 // Baker's read barriers, we need to perform the load of
857 // mirror::Object::monitor_ *before* the original reference load.
858 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000859 // The slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100860 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000861 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
862 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
863 // HeapReference<mirror::Object> ref = *src; // Original reference load.
864 // bool is_gray = (rb_state == ReadBarrier::GrayState());
865 // if (is_gray) {
866 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
867 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000868 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000869 // Note: the original implementation in ReadBarrier::Barrier is
870 // slightly more complex as it performs additional checks that we do
871 // not do here for performance reasons.
872
873 // /* int32_t */ monitor = obj->monitor_
874 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
875 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
876 if (needs_null_check_) {
877 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100878 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000879 // /* LockWord */ lock_word = LockWord(monitor)
880 static_assert(sizeof(LockWord) == sizeof(int32_t),
881 "art::LockWord and int32_t have different sizes.");
882
883 // Introduce a dependency on the lock_word including rb_state,
884 // to prevent load-load reordering, and without using
885 // a memory barrier (which would be more expensive).
886 // `obj` is unchanged by this operation, but its value now depends
887 // on `temp`.
888 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
889
890 // The actual reference load.
891 // A possible implicit null check has already been handled above.
892 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
893 arm64_codegen->GenerateRawReferenceLoad(instruction_,
894 ref_,
895 obj_,
896 offset_,
897 index_,
898 scale_factor_,
899 /* needs_null_check */ false,
900 use_load_acquire_);
901
902 // Mark the object `ref` when `obj` is gray.
903 //
904 // if (rb_state == ReadBarrier::GrayState())
905 // ref = ReadBarrier::Mark(ref);
906 //
907 // Given the numeric representation, it's enough to check the low bit of the rb_state.
908 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
909 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
910 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
911 GenerateReadBarrierMarkRuntimeCall(codegen);
912
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000913 __ B(GetExitLabel());
914 }
915
916 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000917 // The register containing the object holding the marked object reference field.
918 Register obj_;
919 // The offset, index and scale factor to access the reference in `obj_`.
920 uint32_t offset_;
921 Location index_;
922 size_t scale_factor_;
923 // Is a null check required?
924 bool needs_null_check_;
925 // Should this reference load use Load-Acquire semantics?
926 bool use_load_acquire_;
927 // A temporary register used to hold the lock word of `obj_`.
928 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000929
Roland Levillain54f869e2017-03-06 13:54:11 +0000930 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000931};
932
Roland Levillain54f869e2017-03-06 13:54:11 +0000933// Slow path loading `obj`'s lock word, loading a reference from
934// object `*(obj + offset + (index << scale_factor))` into `ref`, and
935// marking `ref` if `obj` is gray according to the lock word (Baker
936// read barrier). If needed, this slow path also atomically updates
937// the field `obj.field` in the object `obj` holding this reference
938// after marking (contrary to
939// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
940// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100941//
942// This means that after the execution of this slow path, both `ref`
943// and `obj.field` will be up-to-date; i.e., after the flip, both will
944// hold the same to-space reference (unless another thread installed
945// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000946//
Roland Levillain54f869e2017-03-06 13:54:11 +0000947// Argument `entrypoint` must be a register location holding the read
948// barrier marking runtime entry point to be invoked.
949class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
950 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100951 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000952 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(HInstruction* instruction,
953 Location ref,
954 Register obj,
955 uint32_t offset,
956 Location index,
957 size_t scale_factor,
958 bool needs_null_check,
959 bool use_load_acquire,
960 Register temp,
961 Location entrypoint)
962 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100963 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000964 offset_(offset),
965 index_(index),
966 scale_factor_(scale_factor),
967 needs_null_check_(needs_null_check),
968 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000969 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100970 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000971 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100972 }
973
974 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000975 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100976 }
977
978 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
979 LocationSummary* locations = instruction_->GetLocations();
980 Register ref_reg = WRegisterFrom(ref_);
981 DCHECK(locations->CanCall());
982 DCHECK(ref_.IsRegister()) << ref_;
983 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000984 DCHECK(obj_.IsW());
985 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
986
987 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100988 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
989 << "Unexpected instruction in read barrier marking and field updating slow path: "
990 << instruction_->DebugName();
991 DCHECK(instruction_->GetLocations()->Intrinsified());
992 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000993 DCHECK_EQ(offset_, 0u);
994 DCHECK_EQ(scale_factor_, 0u);
995 DCHECK_EQ(use_load_acquire_, false);
996 // The location of the offset of the marked reference field within `obj_`.
997 Location field_offset = index_;
998 DCHECK(field_offset.IsRegister()) << field_offset;
999
1000 // Temporary register `temp_`, used to store the lock word, must
1001 // not be IP0 nor IP1, as we may use them to emit the reference
1002 // load (in the call to GenerateRawReferenceLoad below), and we
1003 // need the lock word to still be in `temp_` after the reference
1004 // load.
1005 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1006 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001007
1008 __ Bind(GetEntryLabel());
1009
Roland Levillainff487002017-03-07 16:50:01 +00001010 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM64's:
1011 //
1012 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1013 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1014 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1015 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1016 // if (is_gray) {
1017 // old_ref = ref;
1018 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1019 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1020 // }
1021
Roland Levillain54f869e2017-03-06 13:54:11 +00001022 // /* int32_t */ monitor = obj->monitor_
1023 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1024 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
1025 if (needs_null_check_) {
1026 codegen->MaybeRecordImplicitNullCheck(instruction_);
1027 }
1028 // /* LockWord */ lock_word = LockWord(monitor)
1029 static_assert(sizeof(LockWord) == sizeof(int32_t),
1030 "art::LockWord and int32_t have different sizes.");
1031
1032 // Introduce a dependency on the lock_word including rb_state,
1033 // to prevent load-load reordering, and without using
1034 // a memory barrier (which would be more expensive).
1035 // `obj` is unchanged by this operation, but its value now depends
1036 // on `temp`.
1037 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
1038
1039 // The actual reference load.
1040 // A possible implicit null check has already been handled above.
1041 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1042 arm64_codegen->GenerateRawReferenceLoad(instruction_,
1043 ref_,
1044 obj_,
1045 offset_,
1046 index_,
1047 scale_factor_,
1048 /* needs_null_check */ false,
1049 use_load_acquire_);
1050
1051 // Mark the object `ref` when `obj` is gray.
1052 //
1053 // if (rb_state == ReadBarrier::GrayState())
1054 // ref = ReadBarrier::Mark(ref);
1055 //
1056 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1057 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1058 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1059 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1060
1061 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001062 // Note that we cannot use IP to save the old reference, as IP is
1063 // used internally by the ReadBarrierMarkRegX entry point, and we
1064 // need the old reference after the call to that entry point.
1065 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1066 __ Mov(temp_.W(), ref_reg);
1067
Roland Levillain54f869e2017-03-06 13:54:11 +00001068 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001069
1070 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001071 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001072 //
1073 // Note that this field could also hold a different object, if
1074 // another thread had concurrently changed it. In that case, the
1075 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1076 // (CAS) operation below would abort the CAS, leaving the field
1077 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001078 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001079 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001080
1081 // Update the the holder's field atomically. This may fail if
1082 // mutator updates before us, but it's OK. This is achieved
1083 // using a strong compare-and-set (CAS) operation with relaxed
1084 // memory synchronization ordering, where the expected value is
1085 // the old reference and the desired value is the new reference.
1086
1087 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1088 UseScratchRegisterScope temps(masm);
1089
1090 // Convenience aliases.
1091 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001092 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001093 Register expected = temp_.W();
1094 Register value = ref_reg;
1095 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1096 Register tmp_value = temps.AcquireW(); // Value in memory.
1097
1098 __ Add(tmp_ptr, base.X(), Operand(offset));
1099
1100 if (kPoisonHeapReferences) {
1101 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1102 if (value.Is(expected)) {
1103 // Do not poison `value`, as it is the same register as
1104 // `expected`, which has just been poisoned.
1105 } else {
1106 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1107 }
1108 }
1109
1110 // do {
1111 // tmp_value = [tmp_ptr] - expected;
1112 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1113
Roland Levillain24a4d112016-10-26 13:10:46 +01001114 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001115 __ Bind(&loop_head);
1116 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1117 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001118 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001119 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1120 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001121 __ B(&exit_loop);
1122 __ Bind(&comparison_failed);
1123 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001124 __ Bind(&exit_loop);
1125
1126 if (kPoisonHeapReferences) {
1127 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1128 if (value.Is(expected)) {
1129 // Do not unpoison `value`, as it is the same register as
1130 // `expected`, which has just been unpoisoned.
1131 } else {
1132 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1133 }
1134 }
1135
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001136 __ B(GetExitLabel());
1137 }
1138
1139 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001140 // The register containing the object holding the marked object reference field.
1141 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001142 // The offset, index and scale factor to access the reference in `obj_`.
1143 uint32_t offset_;
1144 Location index_;
1145 size_t scale_factor_;
1146 // Is a null check required?
1147 bool needs_null_check_;
1148 // Should this reference load use Load-Acquire semantics?
1149 bool use_load_acquire_;
1150 // A temporary register used to hold the lock word of `obj_`; and
1151 // also to hold the original reference value, when the reference is
1152 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001153 const Register temp_;
1154
Roland Levillain54f869e2017-03-06 13:54:11 +00001155 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001156};
1157
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001158// Slow path generating a read barrier for a heap reference.
1159class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1160 public:
1161 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1162 Location out,
1163 Location ref,
1164 Location obj,
1165 uint32_t offset,
1166 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001167 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001168 out_(out),
1169 ref_(ref),
1170 obj_(obj),
1171 offset_(offset),
1172 index_(index) {
1173 DCHECK(kEmitCompilerReadBarrier);
1174 // If `obj` is equal to `out` or `ref`, it means the initial object
1175 // has been overwritten by (or after) the heap object reference load
1176 // to be instrumented, e.g.:
1177 //
1178 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001179 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001180 //
1181 // In that case, we have lost the information about the original
1182 // object, and the emitted read barrier cannot work properly.
1183 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1184 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1185 }
1186
1187 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1188 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1189 LocationSummary* locations = instruction_->GetLocations();
1190 Primitive::Type type = Primitive::kPrimNot;
1191 DCHECK(locations->CanCall());
1192 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001193 DCHECK(instruction_->IsInstanceFieldGet() ||
1194 instruction_->IsStaticFieldGet() ||
1195 instruction_->IsArrayGet() ||
1196 instruction_->IsInstanceOf() ||
1197 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001198 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001199 << "Unexpected instruction in read barrier for heap reference slow path: "
1200 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001201 // The read barrier instrumentation of object ArrayGet
1202 // instructions does not support the HIntermediateAddress
1203 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001204 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001205 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001206
1207 __ Bind(GetEntryLabel());
1208
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001209 SaveLiveRegisters(codegen, locations);
1210
1211 // We may have to change the index's value, but as `index_` is a
1212 // constant member (like other "inputs" of this slow path),
1213 // introduce a copy of it, `index`.
1214 Location index = index_;
1215 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001216 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001217 if (instruction_->IsArrayGet()) {
1218 // Compute the actual memory offset and store it in `index`.
1219 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
1220 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1221 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1222 // We are about to change the value of `index_reg` (see the
1223 // calls to vixl::MacroAssembler::Lsl and
1224 // vixl::MacroAssembler::Mov below), but it has
1225 // not been saved by the previous call to
1226 // art::SlowPathCode::SaveLiveRegisters, as it is a
1227 // callee-save register --
1228 // art::SlowPathCode::SaveLiveRegisters does not consider
1229 // callee-save registers, as it has been designed with the
1230 // assumption that callee-save registers are supposed to be
1231 // handled by the called function. So, as a callee-save
1232 // register, `index_reg` _would_ eventually be saved onto
1233 // the stack, but it would be too late: we would have
1234 // changed its value earlier. Therefore, we manually save
1235 // it here into another freely available register,
1236 // `free_reg`, chosen of course among the caller-save
1237 // registers (as a callee-save `free_reg` register would
1238 // exhibit the same problem).
1239 //
1240 // Note we could have requested a temporary register from
1241 // the register allocator instead; but we prefer not to, as
1242 // this is a slow path, and we know we can find a
1243 // caller-save register that is available.
1244 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1245 __ Mov(free_reg.W(), index_reg);
1246 index_reg = free_reg;
1247 index = LocationFrom(index_reg);
1248 } else {
1249 // The initial register stored in `index_` has already been
1250 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1251 // (as it is not a callee-save register), so we can freely
1252 // use it.
1253 }
1254 // Shifting the index value contained in `index_reg` by the scale
1255 // factor (2) cannot overflow in practice, as the runtime is
1256 // unable to allocate object arrays with a size larger than
1257 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1258 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1259 static_assert(
1260 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1261 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1262 __ Add(index_reg, index_reg, Operand(offset_));
1263 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001264 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1265 // intrinsics, `index_` is not shifted by a scale factor of 2
1266 // (as in the case of ArrayGet), as it is actually an offset
1267 // to an object field within an object.
1268 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001269 DCHECK(instruction_->GetLocations()->Intrinsified());
1270 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1271 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1272 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001273 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001274 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001275 }
1276 }
1277
1278 // We're moving two or three locations to locations that could
1279 // overlap, so we need a parallel move resolver.
1280 InvokeRuntimeCallingConvention calling_convention;
1281 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1282 parallel_move.AddMove(ref_,
1283 LocationFrom(calling_convention.GetRegisterAt(0)),
1284 type,
1285 nullptr);
1286 parallel_move.AddMove(obj_,
1287 LocationFrom(calling_convention.GetRegisterAt(1)),
1288 type,
1289 nullptr);
1290 if (index.IsValid()) {
1291 parallel_move.AddMove(index,
1292 LocationFrom(calling_convention.GetRegisterAt(2)),
1293 Primitive::kPrimInt,
1294 nullptr);
1295 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1296 } else {
1297 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1298 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1299 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001300 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001301 instruction_,
1302 instruction_->GetDexPc(),
1303 this);
1304 CheckEntrypointTypes<
1305 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1306 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1307
1308 RestoreLiveRegisters(codegen, locations);
1309
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001310 __ B(GetExitLabel());
1311 }
1312
1313 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1314
1315 private:
1316 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001317 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1318 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001319 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1320 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1321 return Register(VIXLRegCodeFromART(i), kXRegSize);
1322 }
1323 }
1324 // We shall never fail to find a free caller-save register, as
1325 // there are more than two core caller-save registers on ARM64
1326 // (meaning it is possible to find one which is different from
1327 // `ref` and `obj`).
1328 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1329 LOG(FATAL) << "Could not find a free register";
1330 UNREACHABLE();
1331 }
1332
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001333 const Location out_;
1334 const Location ref_;
1335 const Location obj_;
1336 const uint32_t offset_;
1337 // An additional location containing an index to an array.
1338 // Only used for HArrayGet and the UnsafeGetObject &
1339 // UnsafeGetObjectVolatile intrinsics.
1340 const Location index_;
1341
1342 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1343};
1344
1345// Slow path generating a read barrier for a GC root.
1346class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1347 public:
1348 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001349 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001350 DCHECK(kEmitCompilerReadBarrier);
1351 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001352
1353 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1354 LocationSummary* locations = instruction_->GetLocations();
1355 Primitive::Type type = Primitive::kPrimNot;
1356 DCHECK(locations->CanCall());
1357 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001358 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1359 << "Unexpected instruction in read barrier for GC root slow path: "
1360 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001361
1362 __ Bind(GetEntryLabel());
1363 SaveLiveRegisters(codegen, locations);
1364
1365 InvokeRuntimeCallingConvention calling_convention;
1366 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1367 // The argument of the ReadBarrierForRootSlow is not a managed
1368 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1369 // thus we need a 64-bit move here, and we cannot use
1370 //
1371 // arm64_codegen->MoveLocation(
1372 // LocationFrom(calling_convention.GetRegisterAt(0)),
1373 // root_,
1374 // type);
1375 //
1376 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1377 // reference type (`Primitive::kPrimNot`).
1378 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001379 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001380 instruction_,
1381 instruction_->GetDexPc(),
1382 this);
1383 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1384 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1385
1386 RestoreLiveRegisters(codegen, locations);
1387 __ B(GetExitLabel());
1388 }
1389
1390 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1391
1392 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001393 const Location out_;
1394 const Location root_;
1395
1396 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1397};
1398
Alexandre Rames5319def2014-10-23 10:03:10 +01001399#undef __
1400
1401Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
1402 Location next_location;
1403 if (type == Primitive::kPrimVoid) {
1404 LOG(FATAL) << "Unreachable type " << type;
1405 }
1406
1407 if (Primitive::IsFloatingPointType(type) &&
1408 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001409 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1410 } else if (!Primitive::IsFloatingPointType(type) &&
1411 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1412 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1413 } else {
1414 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001415 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1416 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001417 }
1418
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001419 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001420 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001421 return next_location;
1422}
1423
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001424Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001425 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001426}
1427
Serban Constantinescu579885a2015-02-22 20:51:33 +00001428CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1429 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001430 const CompilerOptions& compiler_options,
1431 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001432 : CodeGenerator(graph,
1433 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001434 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001435 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001436 callee_saved_core_registers.GetList(),
1437 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001438 compiler_options,
1439 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001440 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001441 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001442 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001443 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001444 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001445 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001446 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001447 uint32_literals_(std::less<uint32_t>(),
1448 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001449 uint64_literals_(std::less<uint64_t>(),
1450 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001451 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1452 boot_image_string_patches_(StringReferenceValueComparator(),
1453 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1454 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001455 boot_image_type_patches_(TypeReferenceValueComparator(),
1456 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1457 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001458 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001459 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001460 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001461 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1462 jit_class_patches_(TypeReferenceValueComparator(),
1463 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001464 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001465 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001466}
Alexandre Rames5319def2014-10-23 10:03:10 +01001467
Alexandre Rames67555f72014-11-18 10:55:16 +00001468#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001469
Zheng Xu3927c8b2015-11-18 17:46:25 +08001470void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001471 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001472 jump_table->EmitTable(this);
1473 }
1474}
1475
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001476void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001477 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001478 // Ensure we emit the literal pool.
1479 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001480
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001481 CodeGenerator::Finalize(allocator);
1482}
1483
Zheng Xuad4450e2015-04-17 18:48:56 +08001484void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1485 // Note: There are 6 kinds of moves:
1486 // 1. constant -> GPR/FPR (non-cycle)
1487 // 2. constant -> stack (non-cycle)
1488 // 3. GPR/FPR -> GPR/FPR
1489 // 4. GPR/FPR -> stack
1490 // 5. stack -> GPR/FPR
1491 // 6. stack -> stack (non-cycle)
1492 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1493 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1494 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1495 // dependency.
1496 vixl_temps_.Open(GetVIXLAssembler());
1497}
1498
1499void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1500 vixl_temps_.Close();
1501}
1502
1503Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001504 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1505 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1506 || kind == Location::kSIMDStackSlot);
1507 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1508 ? Location::kFpuRegister
1509 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001510 Location scratch = GetScratchLocation(kind);
1511 if (!scratch.Equals(Location::NoLocation())) {
1512 return scratch;
1513 }
1514 // Allocate from VIXL temp registers.
1515 if (kind == Location::kRegister) {
1516 scratch = LocationFrom(vixl_temps_.AcquireX());
1517 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001518 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001519 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1520 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1521 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001522 }
1523 AddScratchLocation(scratch);
1524 return scratch;
1525}
1526
1527void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1528 if (loc.IsRegister()) {
1529 vixl_temps_.Release(XRegisterFrom(loc));
1530 } else {
1531 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001532 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001533 }
1534 RemoveScratchLocation(loc);
1535}
1536
Alexandre Rames3e69f162014-12-10 10:36:50 +00001537void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001538 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001539 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001540}
1541
Alexandre Rames5319def2014-10-23 10:03:10 +01001542void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001543 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001544 __ Bind(&frame_entry_label_);
1545
Serban Constantinescu02164b32014-11-13 14:05:07 +00001546 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1547 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001548 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001549 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001550 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001551 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001552 {
1553 // Ensure that between load and RecordPcInfo there are no pools emitted.
1554 ExactAssemblyScope eas(GetVIXLAssembler(),
1555 kInstructionSize,
1556 CodeBufferCheckScope::kExactSize);
1557 __ ldr(wzr, MemOperand(temp, 0));
1558 RecordPcInfo(nullptr, 0);
1559 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001560 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001561
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001562 if (!HasEmptyFrame()) {
1563 int frame_size = GetFrameSize();
1564 // Stack layout:
1565 // sp[frame_size - 8] : lr.
1566 // ... : other preserved core registers.
1567 // ... : other preserved fp registers.
1568 // ... : reserved frame space.
1569 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001570
1571 // Save the current method if we need it. Note that we do not
1572 // do this in HCurrentMethod, as the instruction might have been removed
1573 // in the SSA graph.
1574 if (RequiresCurrentMethod()) {
1575 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001576 } else {
1577 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001578 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001579 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001580 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1581 frame_size - GetCoreSpillSize());
1582 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1583 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001584
1585 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1586 // Initialize should_deoptimize flag to 0.
1587 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1588 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1589 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001590 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001591}
1592
1593void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001594 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001595 if (!HasEmptyFrame()) {
1596 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001597 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1598 frame_size - FrameEntrySpillSize());
1599 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1600 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001601 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001602 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001603 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001604 __ Ret();
1605 GetAssembler()->cfi().RestoreState();
1606 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001607}
1608
Scott Wakeling97c72b72016-06-24 16:19:36 +01001609CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001610 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001611 return CPURegList(CPURegister::kRegister, kXRegSize,
1612 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001613}
1614
Scott Wakeling97c72b72016-06-24 16:19:36 +01001615CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001616 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1617 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001618 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1619 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001620}
1621
Alexandre Rames5319def2014-10-23 10:03:10 +01001622void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1623 __ Bind(GetLabelOf(block));
1624}
1625
Calin Juravle175dc732015-08-25 15:42:32 +01001626void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1627 DCHECK(location.IsRegister());
1628 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1629}
1630
Calin Juravlee460d1d2015-09-29 04:52:17 +01001631void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1632 if (location.IsRegister()) {
1633 locations->AddTemp(location);
1634 } else {
1635 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1636 }
1637}
1638
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001639void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001640 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001641 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001642 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001643 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001644 if (value_can_be_null) {
1645 __ Cbz(value, &done);
1646 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001647 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001648 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001649 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001650 if (value_can_be_null) {
1651 __ Bind(&done);
1652 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001653}
1654
David Brazdil58282f42016-01-14 12:45:10 +00001655void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001656 // Blocked core registers:
1657 // lr : Runtime reserved.
1658 // tr : Runtime reserved.
1659 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1660 // ip1 : VIXL core temp.
1661 // ip0 : VIXL core temp.
1662 //
1663 // Blocked fp registers:
1664 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001665 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1666 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001667 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001668 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001669 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001670
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001671 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001672 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001673 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001674 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001675
David Brazdil58282f42016-01-14 12:45:10 +00001676 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001677 // Stubs do not save callee-save floating point registers. If the graph
1678 // is debuggable, we need to deal with these registers differently. For
1679 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001680 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1681 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001682 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001683 }
1684 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001685}
1686
Alexandre Rames3e69f162014-12-10 10:36:50 +00001687size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1688 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1689 __ Str(reg, MemOperand(sp, stack_index));
1690 return kArm64WordSize;
1691}
1692
1693size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1694 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1695 __ Ldr(reg, MemOperand(sp, stack_index));
1696 return kArm64WordSize;
1697}
1698
1699size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1700 FPRegister reg = FPRegister(reg_id, kDRegSize);
1701 __ Str(reg, MemOperand(sp, stack_index));
1702 return kArm64WordSize;
1703}
1704
1705size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1706 FPRegister reg = FPRegister(reg_id, kDRegSize);
1707 __ Ldr(reg, MemOperand(sp, stack_index));
1708 return kArm64WordSize;
1709}
1710
Alexandre Rames5319def2014-10-23 10:03:10 +01001711void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001712 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001713}
1714
1715void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001716 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001717}
1718
Alexandre Rames67555f72014-11-18 10:55:16 +00001719void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001720 if (constant->IsIntConstant()) {
1721 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1722 } else if (constant->IsLongConstant()) {
1723 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1724 } else if (constant->IsNullConstant()) {
1725 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001726 } else if (constant->IsFloatConstant()) {
1727 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1728 } else {
1729 DCHECK(constant->IsDoubleConstant());
1730 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1731 }
1732}
1733
Alexandre Rames3e69f162014-12-10 10:36:50 +00001734
1735static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1736 DCHECK(constant.IsConstant());
1737 HConstant* cst = constant.GetConstant();
1738 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001739 // Null is mapped to a core W register, which we associate with kPrimInt.
1740 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001741 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1742 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1743 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1744}
1745
Roland Levillain952b2352017-05-03 19:49:14 +01001746// Allocate a scratch register from the VIXL pool, querying first
1747// the floating-point register pool, and then the core register
1748// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001749// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1750// using a different allocation strategy.
1751static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1752 vixl::aarch64::UseScratchRegisterScope* temps,
1753 int size_in_bits) {
1754 return masm->GetScratchFPRegisterList()->IsEmpty()
1755 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1756 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1757}
1758
Calin Juravlee460d1d2015-09-29 04:52:17 +01001759void CodeGeneratorARM64::MoveLocation(Location destination,
1760 Location source,
1761 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001762 if (source.Equals(destination)) {
1763 return;
1764 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001765
1766 // A valid move can always be inferred from the destination and source
1767 // locations. When moving from and to a register, the argument type can be
1768 // used to generate 32bit instead of 64bit moves. In debug mode we also
1769 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001770 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001771
1772 if (destination.IsRegister() || destination.IsFpuRegister()) {
1773 if (unspecified_type) {
1774 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1775 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001776 (src_cst != nullptr && (src_cst->IsIntConstant()
1777 || src_cst->IsFloatConstant()
1778 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001779 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001780 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001781 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001782 // If the source is a double stack slot or a 64bit constant, a 64bit
1783 // type is appropriate. Else the source is a register, and since the
1784 // type has not been specified, we chose a 64bit type to force a 64bit
1785 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001786 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001787 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001788 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001789 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1790 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1791 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001792 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1793 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1794 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001795 } else if (source.IsSIMDStackSlot()) {
1796 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001797 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001798 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001799 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001800 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001801 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001802 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001803 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001804 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001805 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1806 ? Primitive::kPrimLong
1807 : Primitive::kPrimInt;
1808 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1809 }
1810 } else {
1811 DCHECK(source.IsFpuRegister());
1812 if (destination.IsRegister()) {
1813 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1814 ? Primitive::kPrimDouble
1815 : Primitive::kPrimFloat;
1816 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1817 } else {
1818 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001819 if (GetGraph()->HasSIMD()) {
1820 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1821 } else {
1822 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1823 }
1824 }
1825 }
1826 } else if (destination.IsSIMDStackSlot()) {
1827 if (source.IsFpuRegister()) {
1828 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1829 } else {
1830 DCHECK(source.IsSIMDStackSlot());
1831 UseScratchRegisterScope temps(GetVIXLAssembler());
1832 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1833 Register temp = temps.AcquireX();
1834 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1835 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1836 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1837 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1838 } else {
1839 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1840 __ Ldr(temp, StackOperandFrom(source));
1841 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001842 }
1843 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001844 } else { // The destination is not a register. It must be a stack slot.
1845 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1846 if (source.IsRegister() || source.IsFpuRegister()) {
1847 if (unspecified_type) {
1848 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001849 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001850 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001851 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001852 }
1853 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001854 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1855 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1856 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001857 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001858 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1859 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001860 UseScratchRegisterScope temps(GetVIXLAssembler());
1861 HConstant* src_cst = source.GetConstant();
1862 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001863 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001864 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1865 ? Register(xzr)
1866 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001867 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001868 if (src_cst->IsIntConstant()) {
1869 temp = temps.AcquireW();
1870 } else if (src_cst->IsLongConstant()) {
1871 temp = temps.AcquireX();
1872 } else if (src_cst->IsFloatConstant()) {
1873 temp = temps.AcquireS();
1874 } else {
1875 DCHECK(src_cst->IsDoubleConstant());
1876 temp = temps.AcquireD();
1877 }
1878 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001879 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001880 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001881 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001882 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001883 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001884 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001885 // Use any scratch register (a core or a floating-point one)
1886 // from VIXL scratch register pools as a temporary.
1887 //
1888 // We used to only use the FP scratch register pool, but in some
1889 // rare cases the only register from this pool (D31) would
1890 // already be used (e.g. within a ParallelMove instruction, when
1891 // a move is blocked by a another move requiring a scratch FP
1892 // register, which would reserve D31). To prevent this issue, we
1893 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001894 //
1895 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001896 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001897 // use AcquireFPOrCoreCPURegisterOfSize instead of
1898 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1899 // allocates core scratch registers first.
1900 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1901 GetVIXLAssembler(),
1902 &temps,
1903 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001904 __ Ldr(temp, StackOperandFrom(source));
1905 __ Str(temp, StackOperandFrom(destination));
1906 }
1907 }
1908}
1909
1910void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001911 CPURegister dst,
1912 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001913 switch (type) {
1914 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001915 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001916 break;
1917 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001918 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001919 break;
1920 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001921 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001922 break;
1923 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001924 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001925 break;
1926 case Primitive::kPrimInt:
1927 case Primitive::kPrimNot:
1928 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001929 case Primitive::kPrimFloat:
1930 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001931 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001932 __ Ldr(dst, src);
1933 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001934 case Primitive::kPrimVoid:
1935 LOG(FATAL) << "Unreachable type " << type;
1936 }
1937}
1938
Calin Juravle77520bc2015-01-12 18:45:46 +00001939void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001940 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001941 const MemOperand& src,
1942 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001943 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001944 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001945 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001946 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001947
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001948 DCHECK(!src.IsPreIndex());
1949 DCHECK(!src.IsPostIndex());
1950
1951 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001952 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001953 {
1954 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1955 MemOperand base = MemOperand(temp_base);
1956 switch (type) {
1957 case Primitive::kPrimBoolean:
1958 {
1959 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1960 __ ldarb(Register(dst), base);
1961 if (needs_null_check) {
1962 MaybeRecordImplicitNullCheck(instruction);
1963 }
1964 }
1965 break;
1966 case Primitive::kPrimByte:
1967 {
1968 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1969 __ ldarb(Register(dst), base);
1970 if (needs_null_check) {
1971 MaybeRecordImplicitNullCheck(instruction);
1972 }
1973 }
1974 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1975 break;
1976 case Primitive::kPrimChar:
1977 {
1978 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1979 __ ldarh(Register(dst), base);
1980 if (needs_null_check) {
1981 MaybeRecordImplicitNullCheck(instruction);
1982 }
1983 }
1984 break;
1985 case Primitive::kPrimShort:
1986 {
1987 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1988 __ ldarh(Register(dst), base);
1989 if (needs_null_check) {
1990 MaybeRecordImplicitNullCheck(instruction);
1991 }
1992 }
1993 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1994 break;
1995 case Primitive::kPrimInt:
1996 case Primitive::kPrimNot:
1997 case Primitive::kPrimLong:
1998 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
1999 {
2000 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2001 __ ldar(Register(dst), base);
2002 if (needs_null_check) {
2003 MaybeRecordImplicitNullCheck(instruction);
2004 }
2005 }
2006 break;
2007 case Primitive::kPrimFloat:
2008 case Primitive::kPrimDouble: {
2009 DCHECK(dst.IsFPRegister());
2010 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002011
Artem Serov914d7a82017-02-07 14:33:49 +00002012 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2013 {
2014 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2015 __ ldar(temp, base);
2016 if (needs_null_check) {
2017 MaybeRecordImplicitNullCheck(instruction);
2018 }
2019 }
2020 __ Fmov(FPRegister(dst), temp);
2021 break;
Roland Levillain44015862016-01-22 11:47:17 +00002022 }
Artem Serov914d7a82017-02-07 14:33:49 +00002023 case Primitive::kPrimVoid:
2024 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002025 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002026 }
2027}
2028
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002029void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002030 CPURegister src,
2031 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002032 switch (type) {
2033 case Primitive::kPrimBoolean:
2034 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002035 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002036 break;
2037 case Primitive::kPrimChar:
2038 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002039 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002040 break;
2041 case Primitive::kPrimInt:
2042 case Primitive::kPrimNot:
2043 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002044 case Primitive::kPrimFloat:
2045 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00002046 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002047 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002048 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002049 case Primitive::kPrimVoid:
2050 LOG(FATAL) << "Unreachable type " << type;
2051 }
2052}
2053
Artem Serov914d7a82017-02-07 14:33:49 +00002054void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
2055 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002056 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002057 const MemOperand& dst,
2058 bool needs_null_check) {
2059 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002060 UseScratchRegisterScope temps(GetVIXLAssembler());
2061 Register temp_base = temps.AcquireX();
2062
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002063 DCHECK(!dst.IsPreIndex());
2064 DCHECK(!dst.IsPostIndex());
2065
2066 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002067 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002068 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002069 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002070 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002071 switch (type) {
2072 case Primitive::kPrimBoolean:
2073 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00002074 {
2075 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2076 __ stlrb(Register(src), base);
2077 if (needs_null_check) {
2078 MaybeRecordImplicitNullCheck(instruction);
2079 }
2080 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002081 break;
2082 case Primitive::kPrimChar:
2083 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00002084 {
2085 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2086 __ stlrh(Register(src), base);
2087 if (needs_null_check) {
2088 MaybeRecordImplicitNullCheck(instruction);
2089 }
2090 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002091 break;
2092 case Primitive::kPrimInt:
2093 case Primitive::kPrimNot:
2094 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002095 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002096 {
2097 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2098 __ stlr(Register(src), base);
2099 if (needs_null_check) {
2100 MaybeRecordImplicitNullCheck(instruction);
2101 }
2102 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002103 break;
2104 case Primitive::kPrimFloat:
2105 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002106 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002107 Register temp_src;
2108 if (src.IsZero()) {
2109 // The zero register is used to avoid synthesizing zero constants.
2110 temp_src = Register(src);
2111 } else {
2112 DCHECK(src.IsFPRegister());
2113 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2114 __ Fmov(temp_src, FPRegister(src));
2115 }
Artem Serov914d7a82017-02-07 14:33:49 +00002116 {
2117 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2118 __ stlr(temp_src, base);
2119 if (needs_null_check) {
2120 MaybeRecordImplicitNullCheck(instruction);
2121 }
2122 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002123 break;
2124 }
2125 case Primitive::kPrimVoid:
2126 LOG(FATAL) << "Unreachable type " << type;
2127 }
2128}
2129
Calin Juravle175dc732015-08-25 15:42:32 +01002130void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2131 HInstruction* instruction,
2132 uint32_t dex_pc,
2133 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002134 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002135
2136 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2137 {
2138 // Ensure the pc position is recorded immediately after the `blr` instruction.
2139 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2140 __ blr(lr);
2141 if (EntrypointRequiresStackMap(entrypoint)) {
2142 RecordPcInfo(instruction, dex_pc, slow_path);
2143 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002144 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002145}
2146
Roland Levillaindec8f632016-07-22 17:10:06 +01002147void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2148 HInstruction* instruction,
2149 SlowPathCode* slow_path) {
2150 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002151 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2152 __ Blr(lr);
2153}
2154
Alexandre Rames67555f72014-11-18 10:55:16 +00002155void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002156 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002157 UseScratchRegisterScope temps(GetVIXLAssembler());
2158 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002159 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2160
Serban Constantinescu02164b32014-11-13 14:05:07 +00002161 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002162 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2163 __ Add(temp, class_reg, status_offset);
2164 __ Ldar(temp, HeapOperand(temp));
2165 __ Cmp(temp, mirror::Class::kStatusInitialized);
2166 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002167 __ Bind(slow_path->GetExitLabel());
2168}
Alexandre Rames5319def2014-10-23 10:03:10 +01002169
Roland Levillain44015862016-01-22 11:47:17 +00002170void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002171 BarrierType type = BarrierAll;
2172
2173 switch (kind) {
2174 case MemBarrierKind::kAnyAny:
2175 case MemBarrierKind::kAnyStore: {
2176 type = BarrierAll;
2177 break;
2178 }
2179 case MemBarrierKind::kLoadAny: {
2180 type = BarrierReads;
2181 break;
2182 }
2183 case MemBarrierKind::kStoreStore: {
2184 type = BarrierWrites;
2185 break;
2186 }
2187 default:
2188 LOG(FATAL) << "Unexpected memory barrier " << kind;
2189 }
2190 __ Dmb(InnerShareable, type);
2191}
2192
Serban Constantinescu02164b32014-11-13 14:05:07 +00002193void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2194 HBasicBlock* successor) {
2195 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002196 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2197 if (slow_path == nullptr) {
2198 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2199 instruction->SetSlowPath(slow_path);
2200 codegen_->AddSlowPath(slow_path);
2201 if (successor != nullptr) {
2202 DCHECK(successor->IsLoopHeader());
2203 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2204 }
2205 } else {
2206 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2207 }
2208
Serban Constantinescu02164b32014-11-13 14:05:07 +00002209 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2210 Register temp = temps.AcquireW();
2211
Andreas Gampe542451c2016-07-26 09:02:02 -07002212 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002213 if (successor == nullptr) {
2214 __ Cbnz(temp, slow_path->GetEntryLabel());
2215 __ Bind(slow_path->GetReturnLabel());
2216 } else {
2217 __ Cbz(temp, codegen_->GetLabelOf(successor));
2218 __ B(slow_path->GetEntryLabel());
2219 // slow_path will return to GetLabelOf(successor).
2220 }
2221}
2222
Alexandre Rames5319def2014-10-23 10:03:10 +01002223InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2224 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002225 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002226 assembler_(codegen->GetAssembler()),
2227 codegen_(codegen) {}
2228
2229#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002230 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002231
2232#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2233
2234enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002235 // Using a base helps identify when we hit such breakpoints.
2236 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002237#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2238 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2239#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2240};
2241
2242#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002243 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002244 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2245 } \
2246 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2247 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2248 locations->SetOut(Location::Any()); \
2249 }
2250 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2251#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2252
2253#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002254#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002255
Alexandre Rames67555f72014-11-18 10:55:16 +00002256void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002257 DCHECK_EQ(instr->InputCount(), 2U);
2258 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2259 Primitive::Type type = instr->GetResultType();
2260 switch (type) {
2261 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002262 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002263 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002264 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002265 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002266 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002267
2268 case Primitive::kPrimFloat:
2269 case Primitive::kPrimDouble:
2270 locations->SetInAt(0, Location::RequiresFpuRegister());
2271 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002272 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002273 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002274
Alexandre Rames5319def2014-10-23 10:03:10 +01002275 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002276 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002277 }
2278}
2279
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002280void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2281 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002282 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2283
2284 bool object_field_get_with_read_barrier =
2285 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002286 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002287 new (GetGraph()->GetArena()) LocationSummary(instruction,
2288 object_field_get_with_read_barrier ?
2289 LocationSummary::kCallOnSlowPath :
2290 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002291 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002292 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002293 // We need a temporary register for the read barrier marking slow
2294 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002295 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2296 !Runtime::Current()->UseJitCompilation() &&
2297 !field_info.IsVolatile()) {
2298 // If link-time thunks for the Baker read barrier are enabled, for AOT
2299 // non-volatile loads we need a temporary only if the offset is too big.
2300 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2301 locations->AddTemp(FixedTempLocation());
2302 }
2303 } else {
2304 locations->AddTemp(Location::RequiresRegister());
2305 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002306 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002307 locations->SetInAt(0, Location::RequiresRegister());
2308 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2309 locations->SetOut(Location::RequiresFpuRegister());
2310 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002311 // The output overlaps for an object field get when read barriers
2312 // are enabled: we do not want the load to overwrite the object's
2313 // location, as we need it to emit the read barrier.
2314 locations->SetOut(
2315 Location::RequiresRegister(),
2316 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002317 }
2318}
2319
2320void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2321 const FieldInfo& field_info) {
2322 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002323 LocationSummary* locations = instruction->GetLocations();
2324 Location base_loc = locations->InAt(0);
2325 Location out = locations->Out();
2326 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002327 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002328 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002329
Roland Levillain44015862016-01-22 11:47:17 +00002330 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2331 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002332 // /* HeapReference<Object> */ out = *(base + offset)
2333 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002334 Location maybe_temp =
2335 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002336 // Note that potential implicit null checks are handled in this
2337 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2338 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2339 instruction,
2340 out,
2341 base,
2342 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002343 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002344 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002345 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002346 } else {
2347 // General case.
2348 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002349 // Note that a potential implicit null check is handled in this
2350 // CodeGeneratorARM64::LoadAcquire call.
2351 // NB: LoadAcquire will record the pc info if needed.
2352 codegen_->LoadAcquire(
2353 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002354 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002355 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2356 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002357 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002358 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002359 }
Roland Levillain44015862016-01-22 11:47:17 +00002360 if (field_type == Primitive::kPrimNot) {
2361 // If read barriers are enabled, emit read barriers other than
2362 // Baker's using a slow path (and also unpoison the loaded
2363 // reference, if heap poisoning is enabled).
2364 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2365 }
Roland Levillain4d027112015-07-01 15:41:14 +01002366 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002367}
2368
2369void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2370 LocationSummary* locations =
2371 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2372 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002373 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2374 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2375 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002376 locations->SetInAt(1, Location::RequiresFpuRegister());
2377 } else {
2378 locations->SetInAt(1, Location::RequiresRegister());
2379 }
2380}
2381
2382void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002383 const FieldInfo& field_info,
2384 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002385 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2386
2387 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002388 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002389 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002390 Offset offset = field_info.GetFieldOffset();
2391 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002392
Roland Levillain4d027112015-07-01 15:41:14 +01002393 {
2394 // We use a block to end the scratch scope before the write barrier, thus
2395 // freeing the temporary registers so they can be used in `MarkGCCard`.
2396 UseScratchRegisterScope temps(GetVIXLAssembler());
2397
2398 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2399 DCHECK(value.IsW());
2400 Register temp = temps.AcquireW();
2401 __ Mov(temp, value.W());
2402 GetAssembler()->PoisonHeapReference(temp.W());
2403 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002404 }
Roland Levillain4d027112015-07-01 15:41:14 +01002405
2406 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002407 codegen_->StoreRelease(
2408 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002409 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002410 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2411 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002412 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2413 codegen_->MaybeRecordImplicitNullCheck(instruction);
2414 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002415 }
2416
2417 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002418 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002419 }
2420}
2421
Alexandre Rames67555f72014-11-18 10:55:16 +00002422void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002423 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002424
2425 switch (type) {
2426 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002427 case Primitive::kPrimLong: {
2428 Register dst = OutputRegister(instr);
2429 Register lhs = InputRegisterAt(instr, 0);
2430 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002431 if (instr->IsAdd()) {
2432 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002433 } else if (instr->IsAnd()) {
2434 __ And(dst, lhs, rhs);
2435 } else if (instr->IsOr()) {
2436 __ Orr(dst, lhs, rhs);
2437 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002438 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002439 } else if (instr->IsRor()) {
2440 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002441 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002442 __ Ror(dst, lhs, shift);
2443 } else {
2444 // Ensure shift distance is in the same size register as the result. If
2445 // we are rotating a long and the shift comes in a w register originally,
2446 // we don't need to sxtw for use as an x since the shift distances are
2447 // all & reg_bits - 1.
2448 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2449 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002450 } else {
2451 DCHECK(instr->IsXor());
2452 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002453 }
2454 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002455 }
2456 case Primitive::kPrimFloat:
2457 case Primitive::kPrimDouble: {
2458 FPRegister dst = OutputFPRegister(instr);
2459 FPRegister lhs = InputFPRegisterAt(instr, 0);
2460 FPRegister rhs = InputFPRegisterAt(instr, 1);
2461 if (instr->IsAdd()) {
2462 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002463 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002464 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002465 } else {
2466 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002467 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002468 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002469 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002470 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002471 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002472 }
2473}
2474
Serban Constantinescu02164b32014-11-13 14:05:07 +00002475void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2476 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2477
2478 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2479 Primitive::Type type = instr->GetResultType();
2480 switch (type) {
2481 case Primitive::kPrimInt:
2482 case Primitive::kPrimLong: {
2483 locations->SetInAt(0, Location::RequiresRegister());
2484 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002485 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002486 break;
2487 }
2488 default:
2489 LOG(FATAL) << "Unexpected shift type " << type;
2490 }
2491}
2492
2493void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2494 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2495
2496 Primitive::Type type = instr->GetType();
2497 switch (type) {
2498 case Primitive::kPrimInt:
2499 case Primitive::kPrimLong: {
2500 Register dst = OutputRegister(instr);
2501 Register lhs = InputRegisterAt(instr, 0);
2502 Operand rhs = InputOperandAt(instr, 1);
2503 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002504 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002505 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002506 if (instr->IsShl()) {
2507 __ Lsl(dst, lhs, shift_value);
2508 } else if (instr->IsShr()) {
2509 __ Asr(dst, lhs, shift_value);
2510 } else {
2511 __ Lsr(dst, lhs, shift_value);
2512 }
2513 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002514 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002515
2516 if (instr->IsShl()) {
2517 __ Lsl(dst, lhs, rhs_reg);
2518 } else if (instr->IsShr()) {
2519 __ Asr(dst, lhs, rhs_reg);
2520 } else {
2521 __ Lsr(dst, lhs, rhs_reg);
2522 }
2523 }
2524 break;
2525 }
2526 default:
2527 LOG(FATAL) << "Unexpected shift operation type " << type;
2528 }
2529}
2530
Alexandre Rames5319def2014-10-23 10:03:10 +01002531void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002532 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002533}
2534
2535void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002536 HandleBinaryOp(instruction);
2537}
2538
2539void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2540 HandleBinaryOp(instruction);
2541}
2542
2543void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2544 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002545}
2546
Artem Serov7fc63502016-02-09 17:15:29 +00002547void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002548 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2549 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2550 locations->SetInAt(0, Location::RequiresRegister());
2551 // There is no immediate variant of negated bitwise instructions in AArch64.
2552 locations->SetInAt(1, Location::RequiresRegister());
2553 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2554}
2555
Artem Serov7fc63502016-02-09 17:15:29 +00002556void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002557 Register dst = OutputRegister(instr);
2558 Register lhs = InputRegisterAt(instr, 0);
2559 Register rhs = InputRegisterAt(instr, 1);
2560
2561 switch (instr->GetOpKind()) {
2562 case HInstruction::kAnd:
2563 __ Bic(dst, lhs, rhs);
2564 break;
2565 case HInstruction::kOr:
2566 __ Orn(dst, lhs, rhs);
2567 break;
2568 case HInstruction::kXor:
2569 __ Eon(dst, lhs, rhs);
2570 break;
2571 default:
2572 LOG(FATAL) << "Unreachable";
2573 }
2574}
2575
Anton Kirilov74234da2017-01-13 14:42:47 +00002576void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2577 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002578 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2579 instruction->GetType() == Primitive::kPrimLong);
2580 LocationSummary* locations =
2581 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2582 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2583 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2584 } else {
2585 locations->SetInAt(0, Location::RequiresRegister());
2586 }
2587 locations->SetInAt(1, Location::RequiresRegister());
2588 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2589}
2590
Anton Kirilov74234da2017-01-13 14:42:47 +00002591void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2592 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002593 Primitive::Type type = instruction->GetType();
2594 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2595 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2596 Register out = OutputRegister(instruction);
2597 Register left;
2598 if (kind != HInstruction::kNeg) {
2599 left = InputRegisterAt(instruction, 0);
2600 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002601 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002602 // shifter operand operation, the IR generating `right_reg` (input to the type
2603 // conversion) can have a different type from the current instruction's type,
2604 // so we manually indicate the type.
2605 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002606 Operand right_operand(0);
2607
Anton Kirilov74234da2017-01-13 14:42:47 +00002608 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2609 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002610 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2611 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002612 right_operand = Operand(right_reg,
2613 helpers::ShiftFromOpKind(op_kind),
2614 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002615 }
2616
2617 // Logical binary operations do not support extension operations in the
2618 // operand. Note that VIXL would still manage if it was passed by generating
2619 // the extension as a separate instruction.
2620 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2621 DCHECK(!right_operand.IsExtendedRegister() ||
2622 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2623 kind != HInstruction::kNeg));
2624 switch (kind) {
2625 case HInstruction::kAdd:
2626 __ Add(out, left, right_operand);
2627 break;
2628 case HInstruction::kAnd:
2629 __ And(out, left, right_operand);
2630 break;
2631 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002632 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002633 __ Neg(out, right_operand);
2634 break;
2635 case HInstruction::kOr:
2636 __ Orr(out, left, right_operand);
2637 break;
2638 case HInstruction::kSub:
2639 __ Sub(out, left, right_operand);
2640 break;
2641 case HInstruction::kXor:
2642 __ Eor(out, left, right_operand);
2643 break;
2644 default:
2645 LOG(FATAL) << "Unexpected operation kind: " << kind;
2646 UNREACHABLE();
2647 }
2648}
2649
Artem Serov328429f2016-07-06 16:23:04 +01002650void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002651 LocationSummary* locations =
2652 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2653 locations->SetInAt(0, Location::RequiresRegister());
2654 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002655 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002656}
2657
Roland Levillain19c54192016-11-04 13:44:09 +00002658void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002659 __ Add(OutputRegister(instruction),
2660 InputRegisterAt(instruction, 0),
2661 Operand(InputOperandAt(instruction, 1)));
2662}
2663
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002664void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002665 LocationSummary* locations =
2666 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002667 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2668 if (instr->GetOpKind() == HInstruction::kSub &&
2669 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002670 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002671 // Don't allocate register for Mneg instruction.
2672 } else {
2673 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2674 Location::RequiresRegister());
2675 }
2676 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2677 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002678 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2679}
2680
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002681void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002682 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002683 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2684 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002685
2686 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2687 // This fixup should be carried out for all multiply-accumulate instructions:
2688 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2689 if (instr->GetType() == Primitive::kPrimLong &&
2690 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2691 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002692 vixl::aarch64::Instruction* prev =
2693 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002694 if (prev->IsLoadOrStore()) {
2695 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002696 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002697 __ nop();
2698 }
2699 }
2700
2701 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002702 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002703 __ Madd(res, mul_left, mul_right, accumulator);
2704 } else {
2705 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002706 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002707 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002708 __ Mneg(res, mul_left, mul_right);
2709 } else {
2710 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2711 __ Msub(res, mul_left, mul_right, accumulator);
2712 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002713 }
2714}
2715
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002716void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002717 bool object_array_get_with_read_barrier =
2718 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002719 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002720 new (GetGraph()->GetArena()) LocationSummary(instruction,
2721 object_array_get_with_read_barrier ?
2722 LocationSummary::kCallOnSlowPath :
2723 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002724 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002725 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002726 // We need a temporary register for the read barrier marking slow
2727 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002728 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2729 !Runtime::Current()->UseJitCompilation() &&
2730 instruction->GetIndex()->IsConstant()) {
2731 // Array loads with constant index are treated as field loads.
2732 // If link-time thunks for the Baker read barrier are enabled, for AOT
2733 // constant index loads we need a temporary only if the offset is too big.
2734 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2735 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
2736 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
2737 if (offset >= kReferenceLoadMinFarOffset) {
2738 locations->AddTemp(FixedTempLocation());
2739 }
2740 } else {
2741 locations->AddTemp(Location::RequiresRegister());
2742 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002743 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002744 locations->SetInAt(0, Location::RequiresRegister());
2745 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002746 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2747 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2748 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002749 // The output overlaps in the case of an object array get with
2750 // read barriers enabled: we do not want the move to overwrite the
2751 // array's location, as we need it to emit the read barrier.
2752 locations->SetOut(
2753 Location::RequiresRegister(),
2754 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002755 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002756}
2757
2758void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002759 Primitive::Type type = instruction->GetType();
2760 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002761 LocationSummary* locations = instruction->GetLocations();
2762 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002763 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002764 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002765 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2766 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002767 MacroAssembler* masm = GetVIXLAssembler();
2768 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002769
Roland Levillain19c54192016-11-04 13:44:09 +00002770 // The read barrier instrumentation of object ArrayGet instructions
2771 // does not support the HIntermediateAddress instruction.
2772 DCHECK(!((type == Primitive::kPrimNot) &&
2773 instruction->GetArray()->IsIntermediateAddress() &&
2774 kEmitCompilerReadBarrier));
2775
Roland Levillain44015862016-01-22 11:47:17 +00002776 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2777 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002778 // Note that a potential implicit null check is handled in the
2779 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002780 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002781 if (index.IsConstant()) {
2782 // Array load with a constant index can be treated as a field load.
2783 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2784 Location maybe_temp =
2785 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2786 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2787 out,
2788 obj.W(),
2789 offset,
2790 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002791 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002792 /* use_load_acquire */ false);
2793 } else {
2794 Register temp = WRegisterFrom(locations->GetTemp(0));
2795 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002796 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002797 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002798 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002799 // General case.
2800 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002801 Register length;
2802 if (maybe_compressed_char_at) {
2803 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2804 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002805 {
2806 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2807 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2808
2809 if (instruction->GetArray()->IsIntermediateAddress()) {
2810 DCHECK_LT(count_offset, offset);
2811 int64_t adjusted_offset =
2812 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2813 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2814 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2815 } else {
2816 __ Ldr(length, HeapOperand(obj, count_offset));
2817 }
2818 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002819 }
jessicahandojo05765752016-09-09 19:01:32 -07002820 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002821 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002822 if (maybe_compressed_char_at) {
2823 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002824 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2825 "Expecting 0=compressed, 1=uncompressed");
2826 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002827 __ Ldrb(Register(OutputCPURegister(instruction)),
2828 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2829 __ B(&done);
2830 __ Bind(&uncompressed_load);
2831 __ Ldrh(Register(OutputCPURegister(instruction)),
2832 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2833 __ Bind(&done);
2834 } else {
2835 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2836 source = HeapOperand(obj, offset);
2837 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002838 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002839 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002840 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002841 // We do not need to compute the intermediate address from the array: the
2842 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002843 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002844 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002845 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002846 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2847 }
2848 temp = obj;
2849 } else {
2850 __ Add(temp, obj, offset);
2851 }
jessicahandojo05765752016-09-09 19:01:32 -07002852 if (maybe_compressed_char_at) {
2853 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002854 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2855 "Expecting 0=compressed, 1=uncompressed");
2856 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002857 __ Ldrb(Register(OutputCPURegister(instruction)),
2858 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2859 __ B(&done);
2860 __ Bind(&uncompressed_load);
2861 __ Ldrh(Register(OutputCPURegister(instruction)),
2862 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2863 __ Bind(&done);
2864 } else {
2865 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2866 }
Roland Levillain44015862016-01-22 11:47:17 +00002867 }
jessicahandojo05765752016-09-09 19:01:32 -07002868 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002869 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2870 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002871 codegen_->Load(type, OutputCPURegister(instruction), source);
2872 codegen_->MaybeRecordImplicitNullCheck(instruction);
2873 }
Roland Levillain44015862016-01-22 11:47:17 +00002874
2875 if (type == Primitive::kPrimNot) {
2876 static_assert(
2877 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2878 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2879 Location obj_loc = locations->InAt(0);
2880 if (index.IsConstant()) {
2881 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2882 } else {
2883 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2884 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002885 }
Roland Levillain4d027112015-07-01 15:41:14 +01002886 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002887}
2888
Alexandre Rames5319def2014-10-23 10:03:10 +01002889void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2890 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2891 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002892 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002893}
2894
2895void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002896 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002897 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002898 {
2899 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2900 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2901 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2902 codegen_->MaybeRecordImplicitNullCheck(instruction);
2903 }
jessicahandojo05765752016-09-09 19:01:32 -07002904 // Mask out compression flag from String's array length.
2905 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002906 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002907 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002908}
2909
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002910void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002911 Primitive::Type value_type = instruction->GetComponentType();
2912
2913 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002914 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2915 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002916 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002917 LocationSummary::kCallOnSlowPath :
2918 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002919 locations->SetInAt(0, Location::RequiresRegister());
2920 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002921 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2922 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2923 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002924 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002925 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002926 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002927 }
2928}
2929
2930void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2931 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002932 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002933 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002934 bool needs_write_barrier =
2935 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002936
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002937 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002938 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002939 CPURegister source = value;
2940 Location index = locations->InAt(1);
2941 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2942 MemOperand destination = HeapOperand(array);
2943 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002944
2945 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002946 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002947 if (index.IsConstant()) {
2948 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2949 destination = HeapOperand(array, offset);
2950 } else {
2951 UseScratchRegisterScope temps(masm);
2952 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002953 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002954 // We do not need to compute the intermediate address from the array: the
2955 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002956 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002957 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002958 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002959 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2960 }
2961 temp = array;
2962 } else {
2963 __ Add(temp, array, offset);
2964 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002965 destination = HeapOperand(temp,
2966 XRegisterFrom(index),
2967 LSL,
2968 Primitive::ComponentSizeShift(value_type));
2969 }
Artem Serov914d7a82017-02-07 14:33:49 +00002970 {
2971 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2972 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2973 codegen_->Store(value_type, value, destination);
2974 codegen_->MaybeRecordImplicitNullCheck(instruction);
2975 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002976 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002977 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002978 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002979 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002980 {
2981 // We use a block to end the scratch scope before the write barrier, thus
2982 // freeing the temporary registers so they can be used in `MarkGCCard`.
2983 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002984 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002985 if (index.IsConstant()) {
2986 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002987 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002988 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002989 destination = HeapOperand(temp,
2990 XRegisterFrom(index),
2991 LSL,
2992 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002993 }
2994
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002995 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2996 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2997 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2998
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002999 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003000 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
3001 codegen_->AddSlowPath(slow_path);
3002 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003003 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003004 __ Cbnz(Register(value), &non_zero);
3005 if (!index.IsConstant()) {
3006 __ Add(temp, array, offset);
3007 }
Artem Serov914d7a82017-02-07 14:33:49 +00003008 {
3009 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
3010 // emitted.
3011 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3012 __ Str(wzr, destination);
3013 codegen_->MaybeRecordImplicitNullCheck(instruction);
3014 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003015 __ B(&done);
3016 __ Bind(&non_zero);
3017 }
3018
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003019 // Note that when Baker read barriers are enabled, the type
3020 // checks are performed without read barriers. This is fine,
3021 // even in the case where a class object is in the from-space
3022 // after the flip, as a comparison involving such a type would
3023 // not produce a false positive; it may of course produce a
3024 // false negative, in which case we would take the ArraySet
3025 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003026
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003027 Register temp2 = temps.AcquireSameSizeAs(array);
3028 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003029 {
3030 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3031 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3032 __ Ldr(temp, HeapOperand(array, class_offset));
3033 codegen_->MaybeRecordImplicitNullCheck(instruction);
3034 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003035 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003036
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003037 // /* HeapReference<Class> */ temp = temp->component_type_
3038 __ Ldr(temp, HeapOperand(temp, component_offset));
3039 // /* HeapReference<Class> */ temp2 = value->klass_
3040 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3041 // If heap poisoning is enabled, no need to unpoison `temp`
3042 // nor `temp2`, as we are comparing two poisoned references.
3043 __ Cmp(temp, temp2);
3044 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003045
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003046 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3047 vixl::aarch64::Label do_put;
3048 __ B(eq, &do_put);
3049 // If heap poisoning is enabled, the `temp` reference has
3050 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003051 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3052
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003053 // /* HeapReference<Class> */ temp = temp->super_class_
3054 __ Ldr(temp, HeapOperand(temp, super_offset));
3055 // If heap poisoning is enabled, no need to unpoison
3056 // `temp`, as we are comparing against null below.
3057 __ Cbnz(temp, slow_path->GetEntryLabel());
3058 __ Bind(&do_put);
3059 } else {
3060 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003061 }
3062 }
3063
3064 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003065 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003066 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003067 __ Mov(temp2, value.W());
3068 GetAssembler()->PoisonHeapReference(temp2);
3069 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003070 }
3071
3072 if (!index.IsConstant()) {
3073 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003074 } else {
3075 // We no longer need the `temp` here so release it as the store below may
3076 // need a scratch register (if the constant index makes the offset too large)
3077 // and the poisoned `source` could be using the other scratch register.
3078 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003079 }
Artem Serov914d7a82017-02-07 14:33:49 +00003080 {
3081 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3082 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3083 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003084
Artem Serov914d7a82017-02-07 14:33:49 +00003085 if (!may_need_runtime_call_for_type_check) {
3086 codegen_->MaybeRecordImplicitNullCheck(instruction);
3087 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003088 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003089 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003090
3091 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3092
3093 if (done.IsLinked()) {
3094 __ Bind(&done);
3095 }
3096
3097 if (slow_path != nullptr) {
3098 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003099 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003100 }
3101}
3102
Alexandre Rames67555f72014-11-18 10:55:16 +00003103void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003104 RegisterSet caller_saves = RegisterSet::Empty();
3105 InvokeRuntimeCallingConvention calling_convention;
3106 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3107 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3108 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003109 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003110 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003111}
3112
3113void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003114 BoundsCheckSlowPathARM64* slow_path =
3115 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003116 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003117 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3118 __ B(slow_path->GetEntryLabel(), hs);
3119}
3120
Alexandre Rames67555f72014-11-18 10:55:16 +00003121void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3122 LocationSummary* locations =
3123 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3124 locations->SetInAt(0, Location::RequiresRegister());
3125 if (check->HasUses()) {
3126 locations->SetOut(Location::SameAsFirstInput());
3127 }
3128}
3129
3130void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3131 // We assume the class is not null.
3132 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3133 check->GetLoadClass(), check, check->GetDexPc(), true);
3134 codegen_->AddSlowPath(slow_path);
3135 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3136}
3137
Roland Levillain1a653882016-03-18 18:05:57 +00003138static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3139 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3140 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3141}
3142
3143void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3144 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3145 Location rhs_loc = instruction->GetLocations()->InAt(1);
3146 if (rhs_loc.IsConstant()) {
3147 // 0.0 is the only immediate that can be encoded directly in
3148 // an FCMP instruction.
3149 //
3150 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3151 // specify that in a floating-point comparison, positive zero
3152 // and negative zero are considered equal, so we can use the
3153 // literal 0.0 for both cases here.
3154 //
3155 // Note however that some methods (Float.equal, Float.compare,
3156 // Float.compareTo, Double.equal, Double.compare,
3157 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3158 // StrictMath.min) consider 0.0 to be (strictly) greater than
3159 // -0.0. So if we ever translate calls to these methods into a
3160 // HCompare instruction, we must handle the -0.0 case with
3161 // care here.
3162 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3163 __ Fcmp(lhs_reg, 0.0);
3164 } else {
3165 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3166 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003167}
3168
Serban Constantinescu02164b32014-11-13 14:05:07 +00003169void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003170 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003171 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3172 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003173 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003174 case Primitive::kPrimBoolean:
3175 case Primitive::kPrimByte:
3176 case Primitive::kPrimShort:
3177 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003178 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003179 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003180 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003181 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003182 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3183 break;
3184 }
3185 case Primitive::kPrimFloat:
3186 case Primitive::kPrimDouble: {
3187 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003188 locations->SetInAt(1,
3189 IsFloatingPointZeroConstant(compare->InputAt(1))
3190 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3191 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003192 locations->SetOut(Location::RequiresRegister());
3193 break;
3194 }
3195 default:
3196 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3197 }
3198}
3199
3200void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3201 Primitive::Type in_type = compare->InputAt(0)->GetType();
3202
3203 // 0 if: left == right
3204 // 1 if: left > right
3205 // -1 if: left < right
3206 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003207 case Primitive::kPrimBoolean:
3208 case Primitive::kPrimByte:
3209 case Primitive::kPrimShort:
3210 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003211 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003212 case Primitive::kPrimLong: {
3213 Register result = OutputRegister(compare);
3214 Register left = InputRegisterAt(compare, 0);
3215 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003216 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003217 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3218 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003219 break;
3220 }
3221 case Primitive::kPrimFloat:
3222 case Primitive::kPrimDouble: {
3223 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003224 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003225 __ Cset(result, ne);
3226 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003227 break;
3228 }
3229 default:
3230 LOG(FATAL) << "Unimplemented compare type " << in_type;
3231 }
3232}
3233
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003234void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003235 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003236
3237 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3238 locations->SetInAt(0, Location::RequiresFpuRegister());
3239 locations->SetInAt(1,
3240 IsFloatingPointZeroConstant(instruction->InputAt(1))
3241 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3242 : Location::RequiresFpuRegister());
3243 } else {
3244 // Integer cases.
3245 locations->SetInAt(0, Location::RequiresRegister());
3246 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3247 }
3248
David Brazdilb3e773e2016-01-26 11:28:37 +00003249 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003250 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003251 }
3252}
3253
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003254void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003255 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003256 return;
3257 }
3258
3259 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003260 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003261 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003262
Roland Levillain7f63c522015-07-13 15:54:55 +00003263 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003264 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003265 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003266 } else {
3267 // Integer cases.
3268 Register lhs = InputRegisterAt(instruction, 0);
3269 Operand rhs = InputOperandAt(instruction, 1);
3270 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003271 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003272 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003273}
3274
3275#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3276 M(Equal) \
3277 M(NotEqual) \
3278 M(LessThan) \
3279 M(LessThanOrEqual) \
3280 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003281 M(GreaterThanOrEqual) \
3282 M(Below) \
3283 M(BelowOrEqual) \
3284 M(Above) \
3285 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003286#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003287void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3288void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003289FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003290#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003291#undef FOR_EACH_CONDITION_INSTRUCTION
3292
Zheng Xuc6667102015-05-15 16:08:45 +08003293void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(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());
3303 DCHECK(imm == 1 || imm == -1);
3304
3305 if (instruction->IsRem()) {
3306 __ Mov(out, 0);
3307 } else {
3308 if (imm == 1) {
3309 __ Mov(out, dividend);
3310 } else {
3311 __ Neg(out, dividend);
3312 }
3313 }
3314}
3315
3316void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3317 DCHECK(instruction->IsDiv() || instruction->IsRem());
3318
3319 LocationSummary* locations = instruction->GetLocations();
3320 Location second = locations->InAt(1);
3321 DCHECK(second.IsConstant());
3322
3323 Register out = OutputRegister(instruction);
3324 Register dividend = InputRegisterAt(instruction, 0);
3325 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003326 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003327 int ctz_imm = CTZ(abs_imm);
3328
3329 UseScratchRegisterScope temps(GetVIXLAssembler());
3330 Register temp = temps.AcquireSameSizeAs(out);
3331
3332 if (instruction->IsDiv()) {
3333 __ Add(temp, dividend, abs_imm - 1);
3334 __ Cmp(dividend, 0);
3335 __ Csel(out, temp, dividend, lt);
3336 if (imm > 0) {
3337 __ Asr(out, out, ctz_imm);
3338 } else {
3339 __ Neg(out, Operand(out, ASR, ctz_imm));
3340 }
3341 } else {
3342 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3343 __ Asr(temp, dividend, bits - 1);
3344 __ Lsr(temp, temp, bits - ctz_imm);
3345 __ Add(out, dividend, temp);
3346 __ And(out, out, abs_imm - 1);
3347 __ Sub(out, out, temp);
3348 }
3349}
3350
3351void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3352 DCHECK(instruction->IsDiv() || instruction->IsRem());
3353
3354 LocationSummary* locations = instruction->GetLocations();
3355 Location second = locations->InAt(1);
3356 DCHECK(second.IsConstant());
3357
3358 Register out = OutputRegister(instruction);
3359 Register dividend = InputRegisterAt(instruction, 0);
3360 int64_t imm = Int64FromConstant(second.GetConstant());
3361
3362 Primitive::Type type = instruction->GetResultType();
3363 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3364
3365 int64_t magic;
3366 int shift;
3367 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3368
3369 UseScratchRegisterScope temps(GetVIXLAssembler());
3370 Register temp = temps.AcquireSameSizeAs(out);
3371
3372 // temp = get_high(dividend * magic)
3373 __ Mov(temp, magic);
3374 if (type == Primitive::kPrimLong) {
3375 __ Smulh(temp, dividend, temp);
3376 } else {
3377 __ Smull(temp.X(), dividend, temp);
3378 __ Lsr(temp.X(), temp.X(), 32);
3379 }
3380
3381 if (imm > 0 && magic < 0) {
3382 __ Add(temp, temp, dividend);
3383 } else if (imm < 0 && magic > 0) {
3384 __ Sub(temp, temp, dividend);
3385 }
3386
3387 if (shift != 0) {
3388 __ Asr(temp, temp, shift);
3389 }
3390
3391 if (instruction->IsDiv()) {
3392 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3393 } else {
3394 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3395 // TODO: Strength reduction for msub.
3396 Register temp_imm = temps.AcquireSameSizeAs(out);
3397 __ Mov(temp_imm, imm);
3398 __ Msub(out, temp, temp_imm, dividend);
3399 }
3400}
3401
3402void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3403 DCHECK(instruction->IsDiv() || instruction->IsRem());
3404 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003405 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003406
3407 LocationSummary* locations = instruction->GetLocations();
3408 Register out = OutputRegister(instruction);
3409 Location second = locations->InAt(1);
3410
3411 if (second.IsConstant()) {
3412 int64_t imm = Int64FromConstant(second.GetConstant());
3413
3414 if (imm == 0) {
3415 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3416 } else if (imm == 1 || imm == -1) {
3417 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003418 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003419 DivRemByPowerOfTwo(instruction);
3420 } else {
3421 DCHECK(imm <= -2 || imm >= 2);
3422 GenerateDivRemWithAnyConstant(instruction);
3423 }
3424 } else {
3425 Register dividend = InputRegisterAt(instruction, 0);
3426 Register divisor = InputRegisterAt(instruction, 1);
3427 if (instruction->IsDiv()) {
3428 __ Sdiv(out, dividend, divisor);
3429 } else {
3430 UseScratchRegisterScope temps(GetVIXLAssembler());
3431 Register temp = temps.AcquireSameSizeAs(out);
3432 __ Sdiv(temp, dividend, divisor);
3433 __ Msub(out, temp, divisor, dividend);
3434 }
3435 }
3436}
3437
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003438void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3439 LocationSummary* locations =
3440 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3441 switch (div->GetResultType()) {
3442 case Primitive::kPrimInt:
3443 case Primitive::kPrimLong:
3444 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003445 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003446 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3447 break;
3448
3449 case Primitive::kPrimFloat:
3450 case Primitive::kPrimDouble:
3451 locations->SetInAt(0, Location::RequiresFpuRegister());
3452 locations->SetInAt(1, Location::RequiresFpuRegister());
3453 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3454 break;
3455
3456 default:
3457 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3458 }
3459}
3460
3461void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3462 Primitive::Type type = div->GetResultType();
3463 switch (type) {
3464 case Primitive::kPrimInt:
3465 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003466 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003467 break;
3468
3469 case Primitive::kPrimFloat:
3470 case Primitive::kPrimDouble:
3471 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3472 break;
3473
3474 default:
3475 LOG(FATAL) << "Unexpected div type " << type;
3476 }
3477}
3478
Alexandre Rames67555f72014-11-18 10:55:16 +00003479void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003480 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003481 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003482}
3483
3484void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3485 SlowPathCodeARM64* slow_path =
3486 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3487 codegen_->AddSlowPath(slow_path);
3488 Location value = instruction->GetLocations()->InAt(0);
3489
Alexandre Rames3e69f162014-12-10 10:36:50 +00003490 Primitive::Type type = instruction->GetType();
3491
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003492 if (!Primitive::IsIntegralType(type)) {
3493 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003494 return;
3495 }
3496
Alexandre Rames67555f72014-11-18 10:55:16 +00003497 if (value.IsConstant()) {
3498 int64_t divisor = Int64ConstantFrom(value);
3499 if (divisor == 0) {
3500 __ B(slow_path->GetEntryLabel());
3501 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003502 // A division by a non-null constant is valid. We don't need to perform
3503 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003504 }
3505 } else {
3506 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3507 }
3508}
3509
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003510void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3511 LocationSummary* locations =
3512 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3513 locations->SetOut(Location::ConstantLocation(constant));
3514}
3515
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003516void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3517 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003518 // Will be generated at use site.
3519}
3520
Alexandre Rames5319def2014-10-23 10:03:10 +01003521void LocationsBuilderARM64::VisitExit(HExit* exit) {
3522 exit->SetLocations(nullptr);
3523}
3524
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003525void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003526}
3527
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003528void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3529 LocationSummary* locations =
3530 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3531 locations->SetOut(Location::ConstantLocation(constant));
3532}
3533
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003534void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003535 // Will be generated at use site.
3536}
3537
David Brazdilfc6a86a2015-06-26 10:33:45 +00003538void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003539 DCHECK(!successor->IsExitBlock());
3540 HBasicBlock* block = got->GetBlock();
3541 HInstruction* previous = got->GetPrevious();
3542 HLoopInformation* info = block->GetLoopInformation();
3543
David Brazdil46e2a392015-03-16 17:31:52 +00003544 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003545 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3546 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3547 return;
3548 }
3549 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3550 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3551 }
3552 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003553 __ B(codegen_->GetLabelOf(successor));
3554 }
3555}
3556
David Brazdilfc6a86a2015-06-26 10:33:45 +00003557void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3558 got->SetLocations(nullptr);
3559}
3560
3561void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3562 HandleGoto(got, got->GetSuccessor());
3563}
3564
3565void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3566 try_boundary->SetLocations(nullptr);
3567}
3568
3569void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3570 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3571 if (!successor->IsExitBlock()) {
3572 HandleGoto(try_boundary, successor);
3573 }
3574}
3575
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003576void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003577 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003578 vixl::aarch64::Label* true_target,
3579 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003580 // FP branching requires both targets to be explicit. If either of the targets
3581 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003582 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003583 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003584
David Brazdil0debae72015-11-12 18:37:00 +00003585 if (true_target == nullptr && false_target == nullptr) {
3586 // Nothing to do. The code always falls through.
3587 return;
3588 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003589 // Constant condition, statically compared against "true" (integer value 1).
3590 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003591 if (true_target != nullptr) {
3592 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003593 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003594 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003595 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003596 if (false_target != nullptr) {
3597 __ B(false_target);
3598 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003599 }
David Brazdil0debae72015-11-12 18:37:00 +00003600 return;
3601 }
3602
3603 // The following code generates these patterns:
3604 // (1) true_target == nullptr && false_target != nullptr
3605 // - opposite condition true => branch to false_target
3606 // (2) true_target != nullptr && false_target == nullptr
3607 // - condition true => branch to true_target
3608 // (3) true_target != nullptr && false_target != nullptr
3609 // - condition true => branch to true_target
3610 // - branch to false_target
3611 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003612 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003613 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003614 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003615 if (true_target == nullptr) {
3616 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3617 } else {
3618 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3619 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003620 } else {
3621 // The condition instruction has not been materialized, use its inputs as
3622 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003623 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003624
David Brazdil0debae72015-11-12 18:37:00 +00003625 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003626 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003627 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003628 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003629 IfCondition opposite_condition = condition->GetOppositeCondition();
3630 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003631 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003632 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003633 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003634 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003635 // Integer cases.
3636 Register lhs = InputRegisterAt(condition, 0);
3637 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003638
3639 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003640 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003641 if (true_target == nullptr) {
3642 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3643 non_fallthrough_target = false_target;
3644 } else {
3645 arm64_cond = ARM64Condition(condition->GetCondition());
3646 non_fallthrough_target = true_target;
3647 }
3648
Aart Bik086d27e2016-01-20 17:02:00 -08003649 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003650 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003651 switch (arm64_cond) {
3652 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003653 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003654 break;
3655 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003656 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003657 break;
3658 case lt:
3659 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003660 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003661 break;
3662 case ge:
3663 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003664 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003665 break;
3666 default:
3667 // Without the `static_cast` the compiler throws an error for
3668 // `-Werror=sign-promo`.
3669 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3670 }
3671 } else {
3672 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003673 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003674 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003675 }
3676 }
David Brazdil0debae72015-11-12 18:37:00 +00003677
3678 // If neither branch falls through (case 3), the conditional branch to `true_target`
3679 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3680 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003681 __ B(false_target);
3682 }
David Brazdil0debae72015-11-12 18:37:00 +00003683
3684 if (fallthrough_target.IsLinked()) {
3685 __ Bind(&fallthrough_target);
3686 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003687}
3688
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003689void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3690 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003691 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003692 locations->SetInAt(0, Location::RequiresRegister());
3693 }
3694}
3695
3696void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003697 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3698 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003699 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3700 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3701 true_target = nullptr;
3702 }
3703 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3704 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3705 false_target = nullptr;
3706 }
David Brazdil0debae72015-11-12 18:37:00 +00003707 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003708}
3709
3710void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3711 LocationSummary* locations = new (GetGraph()->GetArena())
3712 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003713 InvokeRuntimeCallingConvention calling_convention;
3714 RegisterSet caller_saves = RegisterSet::Empty();
3715 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3716 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003717 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003718 locations->SetInAt(0, Location::RequiresRegister());
3719 }
3720}
3721
3722void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003723 SlowPathCodeARM64* slow_path =
3724 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003725 GenerateTestAndBranch(deoptimize,
3726 /* condition_input_index */ 0,
3727 slow_path->GetEntryLabel(),
3728 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003729}
3730
Mingyao Yang063fc772016-08-02 11:02:54 -07003731void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3732 LocationSummary* locations = new (GetGraph()->GetArena())
3733 LocationSummary(flag, LocationSummary::kNoCall);
3734 locations->SetOut(Location::RequiresRegister());
3735}
3736
3737void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3738 __ Ldr(OutputRegister(flag),
3739 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3740}
3741
David Brazdilc0b601b2016-02-08 14:20:45 +00003742static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3743 return condition->IsCondition() &&
3744 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3745}
3746
Alexandre Rames880f1192016-06-13 16:04:50 +01003747static inline Condition GetConditionForSelect(HCondition* condition) {
3748 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003749 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3750 : ARM64Condition(cond);
3751}
3752
David Brazdil74eb1b22015-12-14 11:44:01 +00003753void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3754 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003755 if (Primitive::IsFloatingPointType(select->GetType())) {
3756 locations->SetInAt(0, Location::RequiresFpuRegister());
3757 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003758 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003759 } else {
3760 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3761 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3762 bool is_true_value_constant = cst_true_value != nullptr;
3763 bool is_false_value_constant = cst_false_value != nullptr;
3764 // Ask VIXL whether we should synthesize constants in registers.
3765 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3766 Operand true_op = is_true_value_constant ?
3767 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3768 Operand false_op = is_false_value_constant ?
3769 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3770 bool true_value_in_register = false;
3771 bool false_value_in_register = false;
3772 MacroAssembler::GetCselSynthesisInformation(
3773 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3774 true_value_in_register |= !is_true_value_constant;
3775 false_value_in_register |= !is_false_value_constant;
3776
3777 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3778 : Location::ConstantLocation(cst_true_value));
3779 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3780 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003781 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003782 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003783
David Brazdil74eb1b22015-12-14 11:44:01 +00003784 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3785 locations->SetInAt(2, Location::RequiresRegister());
3786 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003787}
3788
3789void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003790 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003791 Condition csel_cond;
3792
3793 if (IsBooleanValueOrMaterializedCondition(cond)) {
3794 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003795 // Use the condition flags set by the previous instruction.
3796 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003797 } else {
3798 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003799 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003800 }
3801 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003802 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003803 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003804 } else {
3805 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003806 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003807 }
3808
Alexandre Rames880f1192016-06-13 16:04:50 +01003809 if (Primitive::IsFloatingPointType(select->GetType())) {
3810 __ Fcsel(OutputFPRegister(select),
3811 InputFPRegisterAt(select, 1),
3812 InputFPRegisterAt(select, 0),
3813 csel_cond);
3814 } else {
3815 __ Csel(OutputRegister(select),
3816 InputOperandAt(select, 1),
3817 InputOperandAt(select, 0),
3818 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003819 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003820}
3821
David Srbecky0cf44932015-12-09 14:09:59 +00003822void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3823 new (GetGraph()->GetArena()) LocationSummary(info);
3824}
3825
David Srbeckyd28f4a02016-03-14 17:14:24 +00003826void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3827 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003828}
3829
3830void CodeGeneratorARM64::GenerateNop() {
3831 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003832}
3833
Alexandre Rames5319def2014-10-23 10:03:10 +01003834void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003835 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003836}
3837
3838void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003839 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003840}
3841
3842void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003843 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003844}
3845
3846void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003847 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003848}
3849
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003850// Temp is used for read barrier.
3851static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3852 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003853 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003854 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3855 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3856 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3857 return 1;
3858 }
3859 return 0;
3860}
3861
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003862// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003863// interface pointer, one for loading the current interface.
3864// The other checks have one temp for loading the object's class.
3865static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3866 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3867 return 3;
3868 }
3869 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003870}
3871
Alexandre Rames67555f72014-11-18 10:55:16 +00003872void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003873 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003874 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003875 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003876 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003877 case TypeCheckKind::kExactCheck:
3878 case TypeCheckKind::kAbstractClassCheck:
3879 case TypeCheckKind::kClassHierarchyCheck:
3880 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003881 call_kind =
3882 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003883 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003884 break;
3885 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003886 case TypeCheckKind::kUnresolvedCheck:
3887 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003888 call_kind = LocationSummary::kCallOnSlowPath;
3889 break;
3890 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003891
Alexandre Rames67555f72014-11-18 10:55:16 +00003892 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003893 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003894 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003895 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003896 locations->SetInAt(0, Location::RequiresRegister());
3897 locations->SetInAt(1, Location::RequiresRegister());
3898 // The "out" register is used as a temporary, so it overlaps with the inputs.
3899 // Note that TypeCheckSlowPathARM64 uses this register too.
3900 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003901 // Add temps if necessary for read barriers.
3902 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003903}
3904
3905void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003906 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003907 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003908 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003909 Register obj = InputRegisterAt(instruction, 0);
3910 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003911 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003912 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003913 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3914 DCHECK_LE(num_temps, 1u);
3915 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003916 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3917 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3918 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3919 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003920
Scott Wakeling97c72b72016-06-24 16:19:36 +01003921 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003922 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003923
3924 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003925 // Avoid null check if we know `obj` is not null.
3926 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003927 __ Cbz(obj, &zero);
3928 }
3929
Roland Levillain44015862016-01-22 11:47:17 +00003930 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003931 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003932 // /* HeapReference<Class> */ out = obj->klass_
3933 GenerateReferenceLoadTwoRegisters(instruction,
3934 out_loc,
3935 obj_loc,
3936 class_offset,
3937 maybe_temp_loc,
3938 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003939 __ Cmp(out, cls);
3940 __ Cset(out, eq);
3941 if (zero.IsLinked()) {
3942 __ B(&done);
3943 }
3944 break;
3945 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003946
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003947 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003948 // /* HeapReference<Class> */ out = obj->klass_
3949 GenerateReferenceLoadTwoRegisters(instruction,
3950 out_loc,
3951 obj_loc,
3952 class_offset,
3953 maybe_temp_loc,
3954 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003955 // If the class is abstract, we eagerly fetch the super class of the
3956 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003957 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003958 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003959 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003960 GenerateReferenceLoadOneRegister(instruction,
3961 out_loc,
3962 super_offset,
3963 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003964 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003965 // If `out` is null, we use it for the result, and jump to `done`.
3966 __ Cbz(out, &done);
3967 __ Cmp(out, cls);
3968 __ B(ne, &loop);
3969 __ Mov(out, 1);
3970 if (zero.IsLinked()) {
3971 __ B(&done);
3972 }
3973 break;
3974 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003975
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003976 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003977 // /* HeapReference<Class> */ out = obj->klass_
3978 GenerateReferenceLoadTwoRegisters(instruction,
3979 out_loc,
3980 obj_loc,
3981 class_offset,
3982 maybe_temp_loc,
3983 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003984 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003985 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003986 __ Bind(&loop);
3987 __ Cmp(out, cls);
3988 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003989 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003990 GenerateReferenceLoadOneRegister(instruction,
3991 out_loc,
3992 super_offset,
3993 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003994 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003995 __ Cbnz(out, &loop);
3996 // If `out` is null, we use it for the result, and jump to `done`.
3997 __ B(&done);
3998 __ Bind(&success);
3999 __ Mov(out, 1);
4000 if (zero.IsLinked()) {
4001 __ B(&done);
4002 }
4003 break;
4004 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004005
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004006 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004007 // /* HeapReference<Class> */ out = obj->klass_
4008 GenerateReferenceLoadTwoRegisters(instruction,
4009 out_loc,
4010 obj_loc,
4011 class_offset,
4012 maybe_temp_loc,
4013 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004014 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004015 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004016 __ Cmp(out, cls);
4017 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004018 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004019 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004020 GenerateReferenceLoadOneRegister(instruction,
4021 out_loc,
4022 component_offset,
4023 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004024 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004025 // If `out` is null, we use it for the result, and jump to `done`.
4026 __ Cbz(out, &done);
4027 __ Ldrh(out, HeapOperand(out, primitive_offset));
4028 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4029 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004030 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004031 __ Mov(out, 1);
4032 __ B(&done);
4033 break;
4034 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004035
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004036 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004037 // No read barrier since the slow path will retry upon failure.
4038 // /* HeapReference<Class> */ out = obj->klass_
4039 GenerateReferenceLoadTwoRegisters(instruction,
4040 out_loc,
4041 obj_loc,
4042 class_offset,
4043 maybe_temp_loc,
4044 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004045 __ Cmp(out, cls);
4046 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004047 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4048 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004049 codegen_->AddSlowPath(slow_path);
4050 __ B(ne, slow_path->GetEntryLabel());
4051 __ Mov(out, 1);
4052 if (zero.IsLinked()) {
4053 __ B(&done);
4054 }
4055 break;
4056 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004057
Calin Juravle98893e12015-10-02 21:05:03 +01004058 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004059 case TypeCheckKind::kInterfaceCheck: {
4060 // Note that we indeed only call on slow path, but we always go
4061 // into the slow path for the unresolved and interface check
4062 // cases.
4063 //
4064 // We cannot directly call the InstanceofNonTrivial runtime
4065 // entry point without resorting to a type checking slow path
4066 // here (i.e. by calling InvokeRuntime directly), as it would
4067 // require to assign fixed registers for the inputs of this
4068 // HInstanceOf instruction (following the runtime calling
4069 // convention), which might be cluttered by the potential first
4070 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004071 //
4072 // TODO: Introduce a new runtime entry point taking the object
4073 // to test (instead of its class) as argument, and let it deal
4074 // with the read barrier issues. This will let us refactor this
4075 // case of the `switch` code as it was previously (with a direct
4076 // call to the runtime not using a type checking slow path).
4077 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004078 DCHECK(locations->OnlyCallsOnSlowPath());
4079 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4080 /* is_fatal */ false);
4081 codegen_->AddSlowPath(slow_path);
4082 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004083 if (zero.IsLinked()) {
4084 __ B(&done);
4085 }
4086 break;
4087 }
4088 }
4089
4090 if (zero.IsLinked()) {
4091 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004092 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004093 }
4094
4095 if (done.IsLinked()) {
4096 __ Bind(&done);
4097 }
4098
4099 if (slow_path != nullptr) {
4100 __ Bind(slow_path->GetExitLabel());
4101 }
4102}
4103
4104void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4105 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4106 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4107
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004108 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4109 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004110 case TypeCheckKind::kExactCheck:
4111 case TypeCheckKind::kAbstractClassCheck:
4112 case TypeCheckKind::kClassHierarchyCheck:
4113 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004114 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4115 LocationSummary::kCallOnSlowPath :
4116 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004117 break;
4118 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004119 case TypeCheckKind::kUnresolvedCheck:
4120 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004121 call_kind = LocationSummary::kCallOnSlowPath;
4122 break;
4123 }
4124
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004125 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4126 locations->SetInAt(0, Location::RequiresRegister());
4127 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004128 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4129 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004130}
4131
4132void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004133 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004134 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004135 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004136 Register obj = InputRegisterAt(instruction, 0);
4137 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004138 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4139 DCHECK_GE(num_temps, 1u);
4140 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004141 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004142 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4143 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004144 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004145 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4146 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4147 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4148 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4149 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4150 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4151 const uint32_t object_array_data_offset =
4152 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004153
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004154 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004155 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4156 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4157 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004158 if (!kEmitCompilerReadBarrier) {
4159 is_type_check_slow_path_fatal =
4160 (type_check_kind == TypeCheckKind::kExactCheck ||
4161 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4162 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4163 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4164 !instruction->CanThrowIntoCatchBlock();
4165 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004166 SlowPathCodeARM64* type_check_slow_path =
4167 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4168 is_type_check_slow_path_fatal);
4169 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004170
Scott Wakeling97c72b72016-06-24 16:19:36 +01004171 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004172 // Avoid null check if we know obj is not null.
4173 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004174 __ Cbz(obj, &done);
4175 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004176
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004177 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004178 case TypeCheckKind::kExactCheck:
4179 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004180 // /* HeapReference<Class> */ temp = obj->klass_
4181 GenerateReferenceLoadTwoRegisters(instruction,
4182 temp_loc,
4183 obj_loc,
4184 class_offset,
4185 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004186 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004187
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004188 __ Cmp(temp, cls);
4189 // Jump to slow path for throwing the exception or doing a
4190 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004191 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004192 break;
4193 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004194
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004195 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004196 // /* HeapReference<Class> */ temp = obj->klass_
4197 GenerateReferenceLoadTwoRegisters(instruction,
4198 temp_loc,
4199 obj_loc,
4200 class_offset,
4201 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004202 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004203
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004204 // If the class is abstract, we eagerly fetch the super class of the
4205 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004206 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004207 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004208 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004209 GenerateReferenceLoadOneRegister(instruction,
4210 temp_loc,
4211 super_offset,
4212 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004213 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004214
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004215 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4216 // exception.
4217 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4218 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004219 __ Cmp(temp, cls);
4220 __ B(ne, &loop);
4221 break;
4222 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004223
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004224 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004225 // /* HeapReference<Class> */ temp = obj->klass_
4226 GenerateReferenceLoadTwoRegisters(instruction,
4227 temp_loc,
4228 obj_loc,
4229 class_offset,
4230 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004231 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004232
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004233 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004234 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004235 __ Bind(&loop);
4236 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004237 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004238
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004239 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004240 GenerateReferenceLoadOneRegister(instruction,
4241 temp_loc,
4242 super_offset,
4243 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004244 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004245
4246 // If the class reference currently in `temp` is not null, jump
4247 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004248 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004249 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004250 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004251 break;
4252 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004253
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004254 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004255 // /* HeapReference<Class> */ temp = obj->klass_
4256 GenerateReferenceLoadTwoRegisters(instruction,
4257 temp_loc,
4258 obj_loc,
4259 class_offset,
4260 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004261 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004262
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004263 // Do an exact check.
4264 __ Cmp(temp, cls);
4265 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004266
4267 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004268 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004269 GenerateReferenceLoadOneRegister(instruction,
4270 temp_loc,
4271 component_offset,
4272 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004273 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004274
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004275 // If the component type is null, jump to the slow path to throw the exception.
4276 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4277 // Otherwise, the object is indeed an array. Further check that this component type is not a
4278 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004279 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4280 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004281 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004282 break;
4283 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004284
Calin Juravle98893e12015-10-02 21:05:03 +01004285 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004286 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004287 //
4288 // We cannot directly call the CheckCast runtime entry point
4289 // without resorting to a type checking slow path here (i.e. by
4290 // calling InvokeRuntime directly), as it would require to
4291 // assign fixed registers for the inputs of this HInstanceOf
4292 // instruction (following the runtime calling convention), which
4293 // might be cluttered by the potential first read barrier
4294 // emission at the beginning of this method.
4295 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004296 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004297 case TypeCheckKind::kInterfaceCheck: {
4298 // /* HeapReference<Class> */ temp = obj->klass_
4299 GenerateReferenceLoadTwoRegisters(instruction,
4300 temp_loc,
4301 obj_loc,
4302 class_offset,
4303 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004304 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004305
4306 // /* HeapReference<Class> */ temp = temp->iftable_
4307 GenerateReferenceLoadTwoRegisters(instruction,
4308 temp_loc,
4309 temp_loc,
4310 iftable_offset,
4311 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004312 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004313 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004314 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004315 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004316 vixl::aarch64::Label start_loop;
4317 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004318 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004319 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4320 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004321 // Go to next interface.
4322 __ Add(temp, temp, 2 * kHeapReferenceSize);
4323 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004324 // Compare the classes and continue the loop if they do not match.
4325 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4326 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004327 break;
4328 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004329 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004330 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004331
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004332 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004333}
4334
Alexandre Rames5319def2014-10-23 10:03:10 +01004335void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4336 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4337 locations->SetOut(Location::ConstantLocation(constant));
4338}
4339
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004340void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004341 // Will be generated at use site.
4342}
4343
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004344void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4345 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4346 locations->SetOut(Location::ConstantLocation(constant));
4347}
4348
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004349void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004350 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004351}
4352
Calin Juravle175dc732015-08-25 15:42:32 +01004353void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4354 // The trampoline uses the same calling convention as dex calling conventions,
4355 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4356 // the method_idx.
4357 HandleInvoke(invoke);
4358}
4359
4360void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4361 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4362}
4363
Alexandre Rames5319def2014-10-23 10:03:10 +01004364void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004365 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004366 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004367}
4368
Alexandre Rames67555f72014-11-18 10:55:16 +00004369void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4370 HandleInvoke(invoke);
4371}
4372
4373void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4374 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004375 LocationSummary* locations = invoke->GetLocations();
4376 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004377 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004378 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004379 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004380
4381 // The register ip1 is required to be used for the hidden argument in
4382 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004383 MacroAssembler* masm = GetVIXLAssembler();
4384 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004385 scratch_scope.Exclude(ip1);
4386 __ Mov(ip1, invoke->GetDexMethodIndex());
4387
Artem Serov914d7a82017-02-07 14:33:49 +00004388 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004389 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004390 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004391 {
4392 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4393 // /* HeapReference<Class> */ temp = temp->klass_
4394 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4395 codegen_->MaybeRecordImplicitNullCheck(invoke);
4396 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004397 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004398 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004399 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004400 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004401 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004402 }
Artem Serov914d7a82017-02-07 14:33:49 +00004403
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004404 // Instead of simply (possibly) unpoisoning `temp` here, we should
4405 // emit a read barrier for the previous class reference load.
4406 // However this is not required in practice, as this is an
4407 // intermediate/temporary reference and because the current
4408 // concurrent copying collector keeps the from-space memory
4409 // intact/accessible until the end of the marking phase (the
4410 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004411 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004412 __ Ldr(temp,
4413 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4414 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004415 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004416 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004417 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004418 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004419 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004420
4421 {
4422 // Ensure the pc position is recorded immediately after the `blr` instruction.
4423 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4424
4425 // lr();
4426 __ blr(lr);
4427 DCHECK(!codegen_->IsLeafMethod());
4428 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4429 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004430}
4431
4432void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004433 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004434 if (intrinsic.TryDispatch(invoke)) {
4435 return;
4436 }
4437
Alexandre Rames67555f72014-11-18 10:55:16 +00004438 HandleInvoke(invoke);
4439}
4440
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004441void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004442 // Explicit clinit checks triggered by static invokes must have been pruned by
4443 // art::PrepareForRegisterAllocation.
4444 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004445
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004446 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004447 if (intrinsic.TryDispatch(invoke)) {
4448 return;
4449 }
4450
Alexandre Rames67555f72014-11-18 10:55:16 +00004451 HandleInvoke(invoke);
4452}
4453
Andreas Gampe878d58c2015-01-15 23:24:00 -08004454static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4455 if (invoke->GetLocations()->Intrinsified()) {
4456 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4457 intrinsic.Dispatch(invoke);
4458 return true;
4459 }
4460 return false;
4461}
4462
Vladimir Markodc151b22015-10-15 18:02:30 +01004463HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4464 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004465 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004466 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004467 return desired_dispatch_info;
4468}
4469
TatWai Chongd8c052a2016-11-02 16:12:48 +08004470Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4471 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004472 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004473 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4474 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004475 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4476 uint32_t offset =
4477 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004478 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004479 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004480 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004481 }
Vladimir Marko58155012015-08-19 12:49:41 +00004482 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004483 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004484 break;
4485 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4486 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004487 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004488 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004489 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4490 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004491 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004492 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004493 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004494 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004495 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004496 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004497 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004498 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004499 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004500 }
Vladimir Marko58155012015-08-19 12:49:41 +00004501 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004502 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004503 Register reg = XRegisterFrom(temp);
4504 Register method_reg;
4505 if (current_method.IsRegister()) {
4506 method_reg = XRegisterFrom(current_method);
4507 } else {
4508 DCHECK(invoke->GetLocations()->Intrinsified());
4509 DCHECK(!current_method.IsValid());
4510 method_reg = reg;
4511 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4512 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004513
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004514 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004515 __ Ldr(reg.X(),
4516 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004517 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004518 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004519 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4520 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004521 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4522 break;
4523 }
4524 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004525 return callee_method;
4526}
4527
4528void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4529 // All registers are assumed to be correctly set up.
4530 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004531
4532 switch (invoke->GetCodePtrLocation()) {
4533 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4534 __ Bl(&frame_entry_label_);
4535 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004536 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4537 // LR = callee_method->entry_point_from_quick_compiled_code_;
4538 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004539 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004540 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004541 {
4542 // To ensure that the pc position is recorded immediately after the `blr` instruction
4543 // BLR must be the last instruction emitted in this function.
4544 // Recording the pc will occur right after returning from this function.
4545 ExactAssemblyScope eas(GetVIXLAssembler(),
4546 kInstructionSize,
4547 CodeBufferCheckScope::kExactSize);
4548 // lr()
4549 __ blr(lr);
4550 }
Vladimir Marko58155012015-08-19 12:49:41 +00004551 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004552 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004553
Andreas Gampe878d58c2015-01-15 23:24:00 -08004554 DCHECK(!IsLeafMethod());
4555}
4556
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004557void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004558 // Use the calling convention instead of the location of the receiver, as
4559 // intrinsics may have put the receiver in a different register. In the intrinsics
4560 // slow path, the arguments have been moved to the right place, so here we are
4561 // guaranteed that the receiver is the first register of the calling convention.
4562 InvokeDexCallingConvention calling_convention;
4563 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004564 Register temp = XRegisterFrom(temp_in);
4565 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4566 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4567 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004568 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004569
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004570 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004571
4572 {
4573 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4574 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4575 // /* HeapReference<Class> */ temp = receiver->klass_
4576 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4577 MaybeRecordImplicitNullCheck(invoke);
4578 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004579 // Instead of simply (possibly) unpoisoning `temp` here, we should
4580 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004581 // intermediate/temporary reference and because the current
4582 // concurrent copying collector keeps the from-space memory
4583 // intact/accessible until the end of the marking phase (the
4584 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004585 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4586 // temp = temp->GetMethodAt(method_offset);
4587 __ Ldr(temp, MemOperand(temp, method_offset));
4588 // lr = temp->GetEntryPoint();
4589 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004590 {
4591 // To ensure that the pc position is recorded immediately after the `blr` instruction
4592 // BLR should be the last instruction emitted in this function.
4593 // Recording the pc will occur right after returning from this function.
4594 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4595 // lr();
4596 __ blr(lr);
4597 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004598}
4599
Orion Hodsonac141392017-01-13 11:53:47 +00004600void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4601 HandleInvoke(invoke);
4602}
4603
4604void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4605 codegen_->GenerateInvokePolymorphicCall(invoke);
4606}
4607
Scott Wakeling97c72b72016-06-24 16:19:36 +01004608vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4609 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004610 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004611 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004612 return
4613 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004614}
4615
Scott Wakeling97c72b72016-06-24 16:19:36 +01004616vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4617 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004618 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004619 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004620 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004621}
4622
Vladimir Marko1998cd02017-01-13 13:02:58 +00004623vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4624 const DexFile& dex_file,
4625 dex::TypeIndex type_index,
4626 vixl::aarch64::Label* adrp_label) {
4627 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4628}
4629
Scott Wakeling97c72b72016-06-24 16:19:36 +01004630vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4631 const DexFile& dex_file,
4632 uint32_t element_offset,
4633 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004634 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4635}
4636
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004637vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4638 baker_read_barrier_patches_.emplace_back(custom_data);
4639 return &baker_read_barrier_patches_.back().label;
4640}
4641
Scott Wakeling97c72b72016-06-24 16:19:36 +01004642vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4643 const DexFile& dex_file,
4644 uint32_t offset_or_index,
4645 vixl::aarch64::Label* adrp_label,
4646 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004647 // Add a patch entry and return the label.
4648 patches->emplace_back(dex_file, offset_or_index);
4649 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004650 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004651 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4652 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4653 return label;
4654}
4655
Scott Wakeling97c72b72016-06-24 16:19:36 +01004656vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004657 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004658 return boot_image_string_patches_.GetOrCreate(
4659 StringReference(&dex_file, string_index),
4660 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4661}
4662
Scott Wakeling97c72b72016-06-24 16:19:36 +01004663vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004664 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004665 return boot_image_type_patches_.GetOrCreate(
4666 TypeReference(&dex_file, type_index),
4667 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4668}
4669
Scott Wakeling97c72b72016-06-24 16:19:36 +01004670vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4671 uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00004672 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004673}
4674
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004675vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004676 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4677 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4678 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004679 return jit_string_patches_.GetOrCreate(
4680 StringReference(&dex_file, string_index),
4681 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4682}
4683
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004684vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004685 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4686 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4687 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004688 return jit_class_patches_.GetOrCreate(
4689 TypeReference(&dex_file, type_index),
4690 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4691}
4692
Vladimir Markoaad75c62016-10-03 08:46:48 +00004693void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4694 vixl::aarch64::Register reg) {
4695 DCHECK(reg.IsX());
4696 SingleEmissionCheckScope guard(GetVIXLAssembler());
4697 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004698 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004699}
4700
4701void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4702 vixl::aarch64::Register out,
4703 vixl::aarch64::Register base) {
4704 DCHECK(out.IsX());
4705 DCHECK(base.IsX());
4706 SingleEmissionCheckScope guard(GetVIXLAssembler());
4707 __ Bind(fixup_label);
4708 __ add(out, base, Operand(/* offset placeholder */ 0));
4709}
4710
4711void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4712 vixl::aarch64::Register out,
4713 vixl::aarch64::Register base) {
4714 DCHECK(base.IsX());
4715 SingleEmissionCheckScope guard(GetVIXLAssembler());
4716 __ Bind(fixup_label);
4717 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4718}
4719
4720template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4721inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4722 const ArenaDeque<PcRelativePatchInfo>& infos,
4723 ArenaVector<LinkerPatch>* linker_patches) {
4724 for (const PcRelativePatchInfo& info : infos) {
4725 linker_patches->push_back(Factory(info.label.GetLocation(),
4726 &info.target_dex_file,
4727 info.pc_insn_label->GetLocation(),
4728 info.offset_or_index));
4729 }
4730}
4731
Vladimir Marko58155012015-08-19 12:49:41 +00004732void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4733 DCHECK(linker_patches->empty());
4734 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004735 pc_relative_dex_cache_patches_.size() +
4736 boot_image_string_patches_.size() +
4737 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004738 boot_image_type_patches_.size() +
4739 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004740 type_bss_entry_patches_.size() +
4741 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004742 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004743 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004744 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004745 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004746 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004747 info.offset_or_index));
4748 }
4749 for (const auto& entry : boot_image_string_patches_) {
4750 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004751 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4752 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004753 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004754 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004755 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004756 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004757 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004758 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4759 linker_patches);
4760 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004761 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4762 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004763 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4764 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004765 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004766 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4767 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004768 for (const auto& entry : boot_image_type_patches_) {
4769 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004770 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4771 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004772 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004773 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004774 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004775 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
4776 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
4777 info.custom_data));
4778 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004779 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004780}
4781
Scott Wakeling97c72b72016-06-24 16:19:36 +01004782vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004783 Uint32ToLiteralMap* map) {
4784 return map->GetOrCreate(
4785 value,
4786 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4787}
4788
Scott Wakeling97c72b72016-06-24 16:19:36 +01004789vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004790 return uint64_literals_.GetOrCreate(
4791 value,
4792 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004793}
4794
Scott Wakeling97c72b72016-06-24 16:19:36 +01004795vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004796 MethodReference target_method,
4797 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004798 return map->GetOrCreate(
4799 target_method,
4800 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004801}
4802
Andreas Gampe878d58c2015-01-15 23:24:00 -08004803void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004804 // Explicit clinit checks triggered by static invokes must have been pruned by
4805 // art::PrepareForRegisterAllocation.
4806 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004807
Andreas Gampe878d58c2015-01-15 23:24:00 -08004808 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4809 return;
4810 }
4811
Artem Serov914d7a82017-02-07 14:33:49 +00004812 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4813 // are no pools emitted.
4814 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004815 LocationSummary* locations = invoke->GetLocations();
4816 codegen_->GenerateStaticOrDirectCall(
4817 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004818 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004819}
4820
4821void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004822 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4823 return;
4824 }
4825
Artem Serov914d7a82017-02-07 14:33:49 +00004826 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4827 // are no pools emitted.
4828 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004829 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004830 DCHECK(!codegen_->IsLeafMethod());
4831 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4832}
4833
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004834HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4835 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004836 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004837 case HLoadClass::LoadKind::kInvalid:
4838 LOG(FATAL) << "UNREACHABLE";
4839 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004840 case HLoadClass::LoadKind::kReferrersClass:
4841 break;
4842 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4843 DCHECK(!GetCompilerOptions().GetCompilePic());
4844 break;
4845 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4846 DCHECK(GetCompilerOptions().GetCompilePic());
4847 break;
4848 case HLoadClass::LoadKind::kBootImageAddress:
4849 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004850 case HLoadClass::LoadKind::kBssEntry:
4851 DCHECK(!Runtime::Current()->UseJitCompilation());
4852 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004853 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004854 DCHECK(Runtime::Current()->UseJitCompilation());
4855 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004856 case HLoadClass::LoadKind::kDexCacheViaMethod:
4857 break;
4858 }
4859 return desired_class_load_kind;
4860}
4861
Alexandre Rames67555f72014-11-18 10:55:16 +00004862void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004863 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4864 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004865 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004866 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004867 cls,
4868 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004869 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004870 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004871 return;
4872 }
Vladimir Marko41559982017-01-06 14:04:23 +00004873 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004874
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004875 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4876 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004877 ? LocationSummary::kCallOnSlowPath
4878 : LocationSummary::kNoCall;
4879 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004880 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004881 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004882 }
4883
Vladimir Marko41559982017-01-06 14:04:23 +00004884 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004885 locations->SetInAt(0, Location::RequiresRegister());
4886 }
4887 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004888 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4889 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4890 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004891 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004892 RegisterSet caller_saves = RegisterSet::Empty();
4893 InvokeRuntimeCallingConvention calling_convention;
4894 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4895 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4896 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4897 Primitive::kPrimNot).GetCode());
4898 locations->SetCustomSlowPathCallerSaves(caller_saves);
4899 } else {
4900 // For non-Baker read barrier we have a temp-clobbering call.
4901 }
4902 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004903}
4904
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004905// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4906// move.
4907void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004908 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4909 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4910 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004911 return;
4912 }
Vladimir Marko41559982017-01-06 14:04:23 +00004913 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004914
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004915 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004916 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004917 Register bss_entry_temp;
4918 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004919
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004920 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4921 ? kWithoutReadBarrier
4922 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004923 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004924 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004925 case HLoadClass::LoadKind::kReferrersClass: {
4926 DCHECK(!cls->CanCallRuntime());
4927 DCHECK(!cls->MustGenerateClinitCheck());
4928 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4929 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004930 GenerateGcRootFieldLoad(cls,
4931 out_loc,
4932 current_method,
4933 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004934 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004935 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004936 break;
4937 }
4938 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004939 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004940 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4941 cls->GetTypeIndex()));
4942 break;
4943 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004944 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004945 // Add ADRP with its PC-relative type patch.
4946 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004947 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004948 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004949 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004950 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004951 vixl::aarch64::Label* add_label =
4952 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004953 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004954 break;
4955 }
4956 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004957 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004958 uint32_t address = dchecked_integral_cast<uint32_t>(
4959 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4960 DCHECK_NE(address, 0u);
4961 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004962 break;
4963 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004964 case HLoadClass::LoadKind::kBssEntry: {
4965 // Add ADRP with its PC-relative Class .bss entry patch.
4966 const DexFile& dex_file = cls->GetDexFile();
4967 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004968 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004969 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4970 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004971 // Add LDR with its PC-relative Class patch.
4972 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004973 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004974 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4975 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004976 out_loc,
4977 bss_entry_temp,
4978 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004979 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004980 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004981 generate_null_check = true;
4982 break;
4983 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004984 case HLoadClass::LoadKind::kJitTableAddress: {
4985 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4986 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004987 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004988 GenerateGcRootFieldLoad(cls,
4989 out_loc,
4990 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004991 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004992 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004993 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004994 break;
4995 }
Vladimir Marko41559982017-01-06 14:04:23 +00004996 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004997 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004998 LOG(FATAL) << "UNREACHABLE";
4999 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005000 }
5001
Vladimir Markoea4c1262017-02-06 19:59:33 +00005002 bool do_clinit = cls->MustGenerateClinitCheck();
5003 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005004 DCHECK(cls->CanCallRuntime());
5005 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00005006 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005007 codegen_->AddSlowPath(slow_path);
5008 if (generate_null_check) {
5009 __ Cbz(out, slow_path->GetEntryLabel());
5010 }
5011 if (cls->MustGenerateClinitCheck()) {
5012 GenerateClassInitializationCheck(slow_path, out);
5013 } else {
5014 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005015 }
5016 }
5017}
5018
David Brazdilcb1c0552015-08-04 16:22:25 +01005019static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005020 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005021}
5022
Alexandre Rames67555f72014-11-18 10:55:16 +00005023void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5024 LocationSummary* locations =
5025 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5026 locations->SetOut(Location::RequiresRegister());
5027}
5028
5029void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005030 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5031}
5032
5033void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
5034 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5035}
5036
5037void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5038 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005039}
5040
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005041HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5042 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005043 switch (desired_string_load_kind) {
5044 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5045 DCHECK(!GetCompilerOptions().GetCompilePic());
5046 break;
5047 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5048 DCHECK(GetCompilerOptions().GetCompilePic());
5049 break;
5050 case HLoadString::LoadKind::kBootImageAddress:
5051 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005052 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005053 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005054 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005055 case HLoadString::LoadKind::kJitTableAddress:
5056 DCHECK(Runtime::Current()->UseJitCompilation());
5057 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005058 case HLoadString::LoadKind::kDexCacheViaMethod:
5059 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005060 }
5061 return desired_string_load_kind;
5062}
5063
Alexandre Rames67555f72014-11-18 10:55:16 +00005064void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005065 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005066 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005067 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005068 InvokeRuntimeCallingConvention calling_convention;
5069 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5070 } else {
5071 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005072 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5073 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005074 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005075 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005076 RegisterSet caller_saves = RegisterSet::Empty();
5077 InvokeRuntimeCallingConvention calling_convention;
5078 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5079 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
5080 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
5081 Primitive::kPrimNot).GetCode());
5082 locations->SetCustomSlowPathCallerSaves(caller_saves);
5083 } else {
5084 // For non-Baker read barrier we have a temp-clobbering call.
5085 }
5086 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005087 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005088}
5089
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005090// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5091// move.
5092void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005093 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005094 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005095
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005096 switch (load->GetLoadKind()) {
5097 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005098 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5099 load->GetStringIndex()));
5100 return; // No dex cache slow path.
5101 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005102 // Add ADRP with its PC-relative String patch.
5103 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005104 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005105 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01005106 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005107 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005108 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005109 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005110 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005111 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005112 return; // No dex cache slow path.
5113 }
5114 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005115 uint32_t address = dchecked_integral_cast<uint32_t>(
5116 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5117 DCHECK_NE(address, 0u);
5118 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005119 return; // No dex cache slow path.
5120 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005121 case HLoadString::LoadKind::kBssEntry: {
5122 // Add ADRP with its PC-relative String .bss entry patch.
5123 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005124 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005125 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005126 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005127 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005128 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005129 // Add LDR with its PC-relative String patch.
5130 vixl::aarch64::Label* ldr_label =
5131 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005132 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005133 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005134 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005135 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005136 /* offset placeholder */ 0u,
5137 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005138 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005139 SlowPathCodeARM64* slow_path =
5140 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005141 codegen_->AddSlowPath(slow_path);
5142 __ Cbz(out.X(), slow_path->GetEntryLabel());
5143 __ Bind(slow_path->GetExitLabel());
5144 return;
5145 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005146 case HLoadString::LoadKind::kJitTableAddress: {
5147 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005148 load->GetStringIndex(),
5149 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005150 GenerateGcRootFieldLoad(load,
5151 out_loc,
5152 out.X(),
5153 /* offset */ 0,
5154 /* fixup_label */ nullptr,
5155 kCompilerReadBarrierOption);
5156 return;
5157 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005158 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005159 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005160 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005161
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005162 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005163 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005164 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005165 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005166 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5167 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005168}
5169
Alexandre Rames5319def2014-10-23 10:03:10 +01005170void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5171 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5172 locations->SetOut(Location::ConstantLocation(constant));
5173}
5174
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005175void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005176 // Will be generated at use site.
5177}
5178
Alexandre Rames67555f72014-11-18 10:55:16 +00005179void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5180 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005181 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005182 InvokeRuntimeCallingConvention calling_convention;
5183 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5184}
5185
5186void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005187 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005188 instruction,
5189 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005190 if (instruction->IsEnter()) {
5191 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5192 } else {
5193 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5194 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005195}
5196
Alexandre Rames42d641b2014-10-27 14:00:51 +00005197void LocationsBuilderARM64::VisitMul(HMul* mul) {
5198 LocationSummary* locations =
5199 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5200 switch (mul->GetResultType()) {
5201 case Primitive::kPrimInt:
5202 case Primitive::kPrimLong:
5203 locations->SetInAt(0, Location::RequiresRegister());
5204 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005205 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005206 break;
5207
5208 case Primitive::kPrimFloat:
5209 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005210 locations->SetInAt(0, Location::RequiresFpuRegister());
5211 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005212 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005213 break;
5214
5215 default:
5216 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5217 }
5218}
5219
5220void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5221 switch (mul->GetResultType()) {
5222 case Primitive::kPrimInt:
5223 case Primitive::kPrimLong:
5224 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5225 break;
5226
5227 case Primitive::kPrimFloat:
5228 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005229 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005230 break;
5231
5232 default:
5233 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5234 }
5235}
5236
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005237void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5238 LocationSummary* locations =
5239 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5240 switch (neg->GetResultType()) {
5241 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005242 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005243 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005244 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005245 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005246
5247 case Primitive::kPrimFloat:
5248 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005249 locations->SetInAt(0, Location::RequiresFpuRegister());
5250 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005251 break;
5252
5253 default:
5254 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5255 }
5256}
5257
5258void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5259 switch (neg->GetResultType()) {
5260 case Primitive::kPrimInt:
5261 case Primitive::kPrimLong:
5262 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5263 break;
5264
5265 case Primitive::kPrimFloat:
5266 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005267 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005268 break;
5269
5270 default:
5271 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5272 }
5273}
5274
5275void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5276 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005277 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005278 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005279 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005280 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5281 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005282}
5283
5284void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005285 // Note: if heap poisoning is enabled, the entry point takes cares
5286 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005287 QuickEntrypointEnum entrypoint =
5288 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5289 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005290 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005291}
5292
Alexandre Rames5319def2014-10-23 10:03:10 +01005293void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5294 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005295 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005296 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005297 if (instruction->IsStringAlloc()) {
5298 locations->AddTemp(LocationFrom(kArtMethodRegister));
5299 } else {
5300 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005301 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005302 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5303}
5304
5305void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005306 // Note: if heap poisoning is enabled, the entry point takes cares
5307 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005308 if (instruction->IsStringAlloc()) {
5309 // String is allocated through StringFactory. Call NewEmptyString entry point.
5310 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005311 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005312 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5313 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005314
5315 {
5316 // Ensure the pc position is recorded immediately after the `blr` instruction.
5317 ExactAssemblyScope eas(GetVIXLAssembler(),
5318 kInstructionSize,
5319 CodeBufferCheckScope::kExactSize);
5320 __ blr(lr);
5321 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5322 }
David Brazdil6de19382016-01-08 17:37:10 +00005323 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005324 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005325 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005326 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005327}
5328
5329void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5330 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005331 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005332 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005333}
5334
5335void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005336 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005337 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005338 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005339 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005340 break;
5341
5342 default:
5343 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5344 }
5345}
5346
David Brazdil66d126e2015-04-03 16:02:44 +01005347void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5348 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5349 locations->SetInAt(0, Location::RequiresRegister());
5350 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5351}
5352
5353void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005354 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005355}
5356
Alexandre Rames5319def2014-10-23 10:03:10 +01005357void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005358 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5359 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005360}
5361
Calin Juravle2ae48182016-03-16 14:05:09 +00005362void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5363 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005364 return;
5365 }
Artem Serov914d7a82017-02-07 14:33:49 +00005366 {
5367 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5368 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5369 Location obj = instruction->GetLocations()->InAt(0);
5370 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5371 RecordPcInfo(instruction, instruction->GetDexPc());
5372 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005373}
5374
Calin Juravle2ae48182016-03-16 14:05:09 +00005375void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005376 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005377 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005378
5379 LocationSummary* locations = instruction->GetLocations();
5380 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005381
5382 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005383}
5384
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005385void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005386 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005387}
5388
Alexandre Rames67555f72014-11-18 10:55:16 +00005389void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5390 HandleBinaryOp(instruction);
5391}
5392
5393void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5394 HandleBinaryOp(instruction);
5395}
5396
Alexandre Rames3e69f162014-12-10 10:36:50 +00005397void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5398 LOG(FATAL) << "Unreachable";
5399}
5400
5401void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5402 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5403}
5404
Alexandre Rames5319def2014-10-23 10:03:10 +01005405void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5407 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5408 if (location.IsStackSlot()) {
5409 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5410 } else if (location.IsDoubleStackSlot()) {
5411 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5412 }
5413 locations->SetOut(location);
5414}
5415
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005416void InstructionCodeGeneratorARM64::VisitParameterValue(
5417 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005418 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005419}
5420
5421void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5422 LocationSummary* locations =
5423 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005424 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005425}
5426
5427void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5428 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5429 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005430}
5431
5432void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5433 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005434 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005435 locations->SetInAt(i, Location::Any());
5436 }
5437 locations->SetOut(Location::Any());
5438}
5439
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005440void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005441 LOG(FATAL) << "Unreachable";
5442}
5443
Serban Constantinescu02164b32014-11-13 14:05:07 +00005444void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005445 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005446 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005447 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5448 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005449 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5450
5451 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005452 case Primitive::kPrimInt:
5453 case Primitive::kPrimLong:
5454 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005455 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005456 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5457 break;
5458
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005459 case Primitive::kPrimFloat:
5460 case Primitive::kPrimDouble: {
5461 InvokeRuntimeCallingConvention calling_convention;
5462 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5463 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5464 locations->SetOut(calling_convention.GetReturnLocation(type));
5465
5466 break;
5467 }
5468
Serban Constantinescu02164b32014-11-13 14:05:07 +00005469 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005470 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005471 }
5472}
5473
5474void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5475 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005476
Serban Constantinescu02164b32014-11-13 14:05:07 +00005477 switch (type) {
5478 case Primitive::kPrimInt:
5479 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005480 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005481 break;
5482 }
5483
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005484 case Primitive::kPrimFloat:
5485 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005486 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5487 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005488 if (type == Primitive::kPrimFloat) {
5489 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5490 } else {
5491 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5492 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005493 break;
5494 }
5495
Serban Constantinescu02164b32014-11-13 14:05:07 +00005496 default:
5497 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005498 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005499 }
5500}
5501
Igor Murashkind01745e2017-04-05 16:40:31 -07005502void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5503 constructor_fence->SetLocations(nullptr);
5504}
5505
5506void InstructionCodeGeneratorARM64::VisitConstructorFence(
5507 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5508 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5509}
5510
Calin Juravle27df7582015-04-17 19:12:31 +01005511void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5512 memory_barrier->SetLocations(nullptr);
5513}
5514
5515void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005516 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005517}
5518
Alexandre Rames5319def2014-10-23 10:03:10 +01005519void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5520 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5521 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005522 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005523}
5524
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005525void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005526 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005527}
5528
5529void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5530 instruction->SetLocations(nullptr);
5531}
5532
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005533void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005534 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005535}
5536
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005537void LocationsBuilderARM64::VisitRor(HRor* ror) {
5538 HandleBinaryOp(ror);
5539}
5540
5541void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5542 HandleBinaryOp(ror);
5543}
5544
Serban Constantinescu02164b32014-11-13 14:05:07 +00005545void LocationsBuilderARM64::VisitShl(HShl* shl) {
5546 HandleShift(shl);
5547}
5548
5549void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5550 HandleShift(shl);
5551}
5552
5553void LocationsBuilderARM64::VisitShr(HShr* shr) {
5554 HandleShift(shr);
5555}
5556
5557void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5558 HandleShift(shr);
5559}
5560
Alexandre Rames5319def2014-10-23 10:03:10 +01005561void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005562 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005563}
5564
5565void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005566 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005567}
5568
Alexandre Rames67555f72014-11-18 10:55:16 +00005569void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005570 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005571}
5572
5573void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005574 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005575}
5576
5577void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005578 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005579}
5580
Alexandre Rames67555f72014-11-18 10:55:16 +00005581void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005582 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005583}
5584
Calin Juravlee460d1d2015-09-29 04:52:17 +01005585void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5586 HUnresolvedInstanceFieldGet* instruction) {
5587 FieldAccessCallingConventionARM64 calling_convention;
5588 codegen_->CreateUnresolvedFieldLocationSummary(
5589 instruction, instruction->GetFieldType(), calling_convention);
5590}
5591
5592void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5593 HUnresolvedInstanceFieldGet* instruction) {
5594 FieldAccessCallingConventionARM64 calling_convention;
5595 codegen_->GenerateUnresolvedFieldAccess(instruction,
5596 instruction->GetFieldType(),
5597 instruction->GetFieldIndex(),
5598 instruction->GetDexPc(),
5599 calling_convention);
5600}
5601
5602void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5603 HUnresolvedInstanceFieldSet* instruction) {
5604 FieldAccessCallingConventionARM64 calling_convention;
5605 codegen_->CreateUnresolvedFieldLocationSummary(
5606 instruction, instruction->GetFieldType(), calling_convention);
5607}
5608
5609void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5610 HUnresolvedInstanceFieldSet* instruction) {
5611 FieldAccessCallingConventionARM64 calling_convention;
5612 codegen_->GenerateUnresolvedFieldAccess(instruction,
5613 instruction->GetFieldType(),
5614 instruction->GetFieldIndex(),
5615 instruction->GetDexPc(),
5616 calling_convention);
5617}
5618
5619void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5620 HUnresolvedStaticFieldGet* instruction) {
5621 FieldAccessCallingConventionARM64 calling_convention;
5622 codegen_->CreateUnresolvedFieldLocationSummary(
5623 instruction, instruction->GetFieldType(), calling_convention);
5624}
5625
5626void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5627 HUnresolvedStaticFieldGet* instruction) {
5628 FieldAccessCallingConventionARM64 calling_convention;
5629 codegen_->GenerateUnresolvedFieldAccess(instruction,
5630 instruction->GetFieldType(),
5631 instruction->GetFieldIndex(),
5632 instruction->GetDexPc(),
5633 calling_convention);
5634}
5635
5636void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5637 HUnresolvedStaticFieldSet* instruction) {
5638 FieldAccessCallingConventionARM64 calling_convention;
5639 codegen_->CreateUnresolvedFieldLocationSummary(
5640 instruction, instruction->GetFieldType(), calling_convention);
5641}
5642
5643void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5644 HUnresolvedStaticFieldSet* instruction) {
5645 FieldAccessCallingConventionARM64 calling_convention;
5646 codegen_->GenerateUnresolvedFieldAccess(instruction,
5647 instruction->GetFieldType(),
5648 instruction->GetFieldIndex(),
5649 instruction->GetDexPc(),
5650 calling_convention);
5651}
5652
Alexandre Rames5319def2014-10-23 10:03:10 +01005653void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005654 LocationSummary* locations =
5655 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005656 // In suspend check slow path, usually there are no caller-save registers at all.
5657 // If SIMD instructions are present, however, we force spilling all live SIMD
5658 // registers in full width (since the runtime only saves/restores lower part).
5659 locations->SetCustomSlowPathCallerSaves(
5660 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005661}
5662
5663void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005664 HBasicBlock* block = instruction->GetBlock();
5665 if (block->GetLoopInformation() != nullptr) {
5666 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5667 // The back edge will generate the suspend check.
5668 return;
5669 }
5670 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5671 // The goto will generate the suspend check.
5672 return;
5673 }
5674 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005675}
5676
Alexandre Rames67555f72014-11-18 10:55:16 +00005677void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5678 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005679 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005680 InvokeRuntimeCallingConvention calling_convention;
5681 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5682}
5683
5684void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005685 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005686 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005687}
5688
5689void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5690 LocationSummary* locations =
5691 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5692 Primitive::Type input_type = conversion->GetInputType();
5693 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005694 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005695 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5696 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5697 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5698 }
5699
Alexandre Rames542361f2015-01-29 16:57:31 +00005700 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005701 locations->SetInAt(0, Location::RequiresFpuRegister());
5702 } else {
5703 locations->SetInAt(0, Location::RequiresRegister());
5704 }
5705
Alexandre Rames542361f2015-01-29 16:57:31 +00005706 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005707 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5708 } else {
5709 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5710 }
5711}
5712
5713void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5714 Primitive::Type result_type = conversion->GetResultType();
5715 Primitive::Type input_type = conversion->GetInputType();
5716
5717 DCHECK_NE(input_type, result_type);
5718
Alexandre Rames542361f2015-01-29 16:57:31 +00005719 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005720 int result_size = Primitive::ComponentSize(result_type);
5721 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005722 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005723 Register output = OutputRegister(conversion);
5724 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005725 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005726 // 'int' values are used directly as W registers, discarding the top
5727 // bits, so we don't need to sign-extend and can just perform a move.
5728 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5729 // top 32 bits of the target register. We theoretically could leave those
5730 // bits unchanged, but we would have to make sure that no code uses a
5731 // 32bit input value as a 64bit value assuming that the top 32 bits are
5732 // zero.
5733 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005734 } else if (result_type == Primitive::kPrimChar ||
5735 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5736 __ Ubfx(output,
5737 output.IsX() ? source.X() : source.W(),
5738 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005739 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005740 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005741 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005742 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005743 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005744 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005745 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5746 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005747 } else if (Primitive::IsFloatingPointType(result_type) &&
5748 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005749 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5750 } else {
5751 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5752 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005753 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005754}
Alexandre Rames67555f72014-11-18 10:55:16 +00005755
Serban Constantinescu02164b32014-11-13 14:05:07 +00005756void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5757 HandleShift(ushr);
5758}
5759
5760void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5761 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005762}
5763
5764void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5765 HandleBinaryOp(instruction);
5766}
5767
5768void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5769 HandleBinaryOp(instruction);
5770}
5771
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005772void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005773 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005774 LOG(FATAL) << "Unreachable";
5775}
5776
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005777void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005778 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005779 LOG(FATAL) << "Unreachable";
5780}
5781
Mark Mendellfe57faa2015-09-18 09:26:15 -04005782// Simple implementation of packed switch - generate cascaded compare/jumps.
5783void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5784 LocationSummary* locations =
5785 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5786 locations->SetInAt(0, Location::RequiresRegister());
5787}
5788
5789void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5790 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005791 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005792 Register value_reg = InputRegisterAt(switch_instr, 0);
5793 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5794
Zheng Xu3927c8b2015-11-18 17:46:25 +08005795 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005796 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005797 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5798 // make sure we don't emit it if the target may run out of range.
5799 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5800 // ranges and emit the tables only as required.
5801 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005802
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005803 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005804 // Current instruction id is an upper bound of the number of HIRs in the graph.
5805 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5806 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005807 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5808 Register temp = temps.AcquireW();
5809 __ Subs(temp, value_reg, Operand(lower_bound));
5810
Zheng Xu3927c8b2015-11-18 17:46:25 +08005811 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005812 // Jump to successors[0] if value == lower_bound.
5813 __ B(eq, codegen_->GetLabelOf(successors[0]));
5814 int32_t last_index = 0;
5815 for (; num_entries - last_index > 2; last_index += 2) {
5816 __ Subs(temp, temp, Operand(2));
5817 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5818 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5819 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5820 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5821 }
5822 if (num_entries - last_index == 2) {
5823 // The last missing case_value.
5824 __ Cmp(temp, Operand(1));
5825 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005826 }
5827
5828 // And the default for any other value.
5829 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5830 __ B(codegen_->GetLabelOf(default_block));
5831 }
5832 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005833 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005834
5835 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5836
5837 // Below instructions should use at most one blocked register. Since there are two blocked
5838 // registers, we are free to block one.
5839 Register temp_w = temps.AcquireW();
5840 Register index;
5841 // Remove the bias.
5842 if (lower_bound != 0) {
5843 index = temp_w;
5844 __ Sub(index, value_reg, Operand(lower_bound));
5845 } else {
5846 index = value_reg;
5847 }
5848
5849 // Jump to default block if index is out of the range.
5850 __ Cmp(index, Operand(num_entries));
5851 __ B(hs, codegen_->GetLabelOf(default_block));
5852
5853 // In current VIXL implementation, it won't require any blocked registers to encode the
5854 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5855 // register pressure.
5856 Register table_base = temps.AcquireX();
5857 // Load jump offset from the table.
5858 __ Adr(table_base, jump_table->GetTableStartLabel());
5859 Register jump_offset = temp_w;
5860 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5861
5862 // Jump to target block by branching to table_base(pc related) + offset.
5863 Register target_address = table_base;
5864 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5865 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005866 }
5867}
5868
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005869void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5870 HInstruction* instruction,
5871 Location out,
5872 uint32_t offset,
5873 Location maybe_temp,
5874 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005875 Primitive::Type type = Primitive::kPrimNot;
5876 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005877 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005878 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005879 if (kUseBakerReadBarrier) {
5880 // Load with fast path based Baker's read barrier.
5881 // /* HeapReference<Object> */ out = *(out + offset)
5882 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5883 out,
5884 out_reg,
5885 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005886 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005887 /* needs_null_check */ false,
5888 /* use_load_acquire */ false);
5889 } else {
5890 // Load with slow path based read barrier.
5891 // Save the value of `out` into `maybe_temp` before overwriting it
5892 // in the following move operation, as we will need it for the
5893 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005894 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005895 __ Mov(temp_reg, out_reg);
5896 // /* HeapReference<Object> */ out = *(out + offset)
5897 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5898 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5899 }
5900 } else {
5901 // Plain load with no read barrier.
5902 // /* HeapReference<Object> */ out = *(out + offset)
5903 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5904 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5905 }
5906}
5907
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005908void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5909 HInstruction* instruction,
5910 Location out,
5911 Location obj,
5912 uint32_t offset,
5913 Location maybe_temp,
5914 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005915 Primitive::Type type = Primitive::kPrimNot;
5916 Register out_reg = RegisterFrom(out, type);
5917 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005918 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005919 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005920 if (kUseBakerReadBarrier) {
5921 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005922 // /* HeapReference<Object> */ out = *(obj + offset)
5923 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5924 out,
5925 obj_reg,
5926 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005927 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005928 /* needs_null_check */ false,
5929 /* use_load_acquire */ false);
5930 } else {
5931 // Load with slow path based read barrier.
5932 // /* HeapReference<Object> */ out = *(obj + offset)
5933 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5934 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5935 }
5936 } else {
5937 // Plain load with no read barrier.
5938 // /* HeapReference<Object> */ out = *(obj + offset)
5939 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5940 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5941 }
5942}
5943
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005944void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5945 HInstruction* instruction,
5946 Location root,
5947 Register obj,
5948 uint32_t offset,
5949 vixl::aarch64::Label* fixup_label,
5950 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005951 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005952 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005953 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005954 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005955 if (kUseBakerReadBarrier) {
5956 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005957 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005958 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5959 !Runtime::Current()->UseJitCompilation()) {
5960 // Note that we do not actually check the value of `GetIsGcMarking()`
5961 // to decide whether to mark the loaded GC root or not. Instead, we
Vladimir Marko66d691d2017-04-07 17:53:39 +01005962 // load into `temp` (actually IP1) the read barrier mark introspection
5963 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
5964 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005965 //
5966 // We use link-time generated thunks for the slow path. That thunk
5967 // checks the reference and jumps to the entrypoint if needed.
5968 //
5969 // temp = Thread::Current()->pReadBarrierMarkIntrospection
5970 // lr = &return_address;
5971 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5972 // if (temp != nullptr) {
5973 // goto gc_root_thunk<root_reg>(lr)
5974 // }
5975 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00005976
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005977 UseScratchRegisterScope temps(GetVIXLAssembler());
5978 DCHECK(temps.IsAvailable(ip0));
5979 DCHECK(temps.IsAvailable(ip1));
5980 temps.Exclude(ip0, ip1);
5981 uint32_t custom_data =
5982 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
5983 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00005984
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005985 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
5986 DCHECK_EQ(ip0.GetCode(), 16u);
5987 const int32_t entry_point_offset =
5988 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
5989 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
5990 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5991 vixl::aarch64::Label return_address;
5992 __ adr(lr, &return_address);
5993 if (fixup_label != nullptr) {
5994 __ Bind(fixup_label);
5995 }
5996 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
5997 "GC root LDR must be 2 instruction (8B) before the return address label.");
5998 __ ldr(root_reg, MemOperand(obj.X(), offset));
5999 __ Bind(cbnz_label);
6000 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6001 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006002 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006003 // Note that we do not actually check the value of
6004 // `GetIsGcMarking()` to decide whether to mark the loaded GC
6005 // root or not. Instead, we load into `temp` the read barrier
6006 // mark entry point corresponding to register `root`. If `temp`
6007 // is null, it means that `GetIsGcMarking()` is false, and vice
6008 // versa.
6009 //
6010 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6011 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6012 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6013 // // Slow path.
6014 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
6015 // }
Roland Levillain44015862016-01-22 11:47:17 +00006016
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006017 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
6018 Register temp = lr;
6019 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
6020 instruction, root, /* entrypoint */ LocationFrom(temp));
6021 codegen_->AddSlowPath(slow_path);
6022
6023 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6024 const int32_t entry_point_offset =
6025 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
6026 // Loading the entrypoint does not require a load acquire since it is only changed when
6027 // threads are suspended or running a checkpoint.
6028 __ Ldr(temp, MemOperand(tr, entry_point_offset));
6029
6030 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6031 if (fixup_label == nullptr) {
6032 __ Ldr(root_reg, MemOperand(obj, offset));
6033 } else {
6034 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
6035 }
6036 static_assert(
6037 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6038 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6039 "have different sizes.");
6040 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6041 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6042 "have different sizes.");
6043
6044 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6045 // checking GetIsGcMarking.
6046 __ Cbnz(temp, slow_path->GetEntryLabel());
6047 __ Bind(slow_path->GetExitLabel());
6048 }
Roland Levillain44015862016-01-22 11:47:17 +00006049 } else {
6050 // GC root loaded through a slow path for read barriers other
6051 // than Baker's.
6052 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006053 if (fixup_label == nullptr) {
6054 __ Add(root_reg.X(), obj.X(), offset);
6055 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006056 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006057 }
Roland Levillain44015862016-01-22 11:47:17 +00006058 // /* mirror::Object* */ root = root->Read()
6059 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6060 }
6061 } else {
6062 // Plain GC root load with no read barrier.
6063 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006064 if (fixup_label == nullptr) {
6065 __ Ldr(root_reg, MemOperand(obj, offset));
6066 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006067 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006068 }
Roland Levillain44015862016-01-22 11:47:17 +00006069 // Note that GC roots are not affected by heap poisoning, thus we
6070 // do not have to unpoison `root_reg` here.
6071 }
6072}
6073
6074void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6075 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006076 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006077 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006078 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006079 bool needs_null_check,
6080 bool use_load_acquire) {
6081 DCHECK(kEmitCompilerReadBarrier);
6082 DCHECK(kUseBakerReadBarrier);
6083
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006084 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6085 !use_load_acquire &&
6086 !Runtime::Current()->UseJitCompilation()) {
6087 // Note that we do not actually check the value of `GetIsGcMarking()`
Vladimir Marko66d691d2017-04-07 17:53:39 +01006088 // to decide whether to mark the loaded reference or not. Instead, we
6089 // load into `temp` (actually IP1) the read barrier mark introspection
6090 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6091 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006092 //
6093 // We use link-time generated thunks for the slow path. That thunk checks
6094 // the holder and jumps to the entrypoint if needed. If the holder is not
6095 // gray, it creates a fake dependency and returns to the LDR instruction.
6096 //
6097 // temp = Thread::Current()->pReadBarrierMarkIntrospection
Vladimir Marko66d691d2017-04-07 17:53:39 +01006098 // lr = &gray_return_address;
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006099 // if (temp != nullptr) {
6100 // goto field_thunk<holder_reg, base_reg>(lr)
6101 // }
6102 // not_gray_return_address:
6103 // // Original reference load. If the offset is too large to fit
6104 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006105 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006106 // gray_return_address:
6107
6108 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6109 Register base = obj;
6110 if (offset >= kReferenceLoadMinFarOffset) {
6111 DCHECK(maybe_temp.IsRegister());
6112 base = WRegisterFrom(maybe_temp);
6113 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6114 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6115 offset &= (kReferenceLoadMinFarOffset - 1u);
6116 }
6117 UseScratchRegisterScope temps(GetVIXLAssembler());
6118 DCHECK(temps.IsAvailable(ip0));
6119 DCHECK(temps.IsAvailable(ip1));
6120 temps.Exclude(ip0, ip1);
6121 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6122 base.GetCode(),
6123 obj.GetCode());
6124 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6125
6126 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6127 DCHECK_EQ(ip0.GetCode(), 16u);
6128 const int32_t entry_point_offset =
6129 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6130 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
Vladimir Markod1ef8732017-04-18 13:55:13 +01006131 EmissionCheckScope guard(GetVIXLAssembler(),
6132 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006133 vixl::aarch64::Label return_address;
6134 __ adr(lr, &return_address);
6135 __ Bind(cbnz_label);
6136 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markod1ef8732017-04-18 13:55:13 +01006137 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6138 "Field LDR must be 1 instruction (4B) before the return address label; "
6139 " 2 instructions (8B) for heap poisoning.");
6140 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6141 __ ldr(ref_reg, MemOperand(base.X(), offset));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006142 if (needs_null_check) {
6143 MaybeRecordImplicitNullCheck(instruction);
6144 }
Vladimir Markod1ef8732017-04-18 13:55:13 +01006145 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006146 __ Bind(&return_address);
6147 return;
6148 }
6149
Roland Levillain44015862016-01-22 11:47:17 +00006150 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006151 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006152 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006153 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006154 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6155 ref,
6156 obj,
6157 offset,
6158 no_index,
6159 no_scale_factor,
6160 temp,
6161 needs_null_check,
6162 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006163}
6164
6165void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6166 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006167 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006168 uint32_t data_offset,
6169 Location index,
6170 Register temp,
6171 bool needs_null_check) {
6172 DCHECK(kEmitCompilerReadBarrier);
6173 DCHECK(kUseBakerReadBarrier);
6174
Vladimir Marko66d691d2017-04-07 17:53:39 +01006175 static_assert(
6176 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6177 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6178 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
6179
6180 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6181 !Runtime::Current()->UseJitCompilation()) {
6182 // Note that we do not actually check the value of `GetIsGcMarking()`
6183 // to decide whether to mark the loaded reference or not. Instead, we
6184 // load into `temp` (actually IP1) the read barrier mark introspection
6185 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6186 // false, and vice versa.
6187 //
6188 // We use link-time generated thunks for the slow path. That thunk checks
6189 // the holder and jumps to the entrypoint if needed. If the holder is not
6190 // gray, it creates a fake dependency and returns to the LDR instruction.
6191 //
6192 // temp = Thread::Current()->pReadBarrierMarkIntrospection
6193 // lr = &gray_return_address;
6194 // if (temp != nullptr) {
6195 // goto field_thunk<holder_reg, base_reg>(lr)
6196 // }
6197 // not_gray_return_address:
6198 // // Original reference load. If the offset is too large to fit
6199 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006200 // HeapReference<mirror::Object> reference = data[index];
Vladimir Marko66d691d2017-04-07 17:53:39 +01006201 // gray_return_address:
6202
6203 DCHECK(index.IsValid());
6204 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
6205 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6206
6207 UseScratchRegisterScope temps(GetVIXLAssembler());
6208 DCHECK(temps.IsAvailable(ip0));
6209 DCHECK(temps.IsAvailable(ip1));
6210 temps.Exclude(ip0, ip1);
6211 uint32_t custom_data =
6212 linker::Arm64RelativePatcher::EncodeBakerReadBarrierArrayData(temp.GetCode());
6213 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6214
6215 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6216 DCHECK_EQ(ip0.GetCode(), 16u);
6217 const int32_t entry_point_offset =
6218 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6219 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
6220 __ Add(temp.X(), obj.X(), Operand(data_offset));
6221 EmissionCheckScope guard(GetVIXLAssembler(),
6222 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6223 vixl::aarch64::Label return_address;
6224 __ adr(lr, &return_address);
6225 __ Bind(cbnz_label);
6226 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6227 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6228 "Array LDR must be 1 instruction (4B) before the return address label; "
6229 " 2 instructions (8B) for heap poisoning.");
6230 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6231 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6232 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6233 __ Bind(&return_address);
6234 return;
6235 }
6236
Roland Levillain44015862016-01-22 11:47:17 +00006237 // Array cells are never volatile variables, therefore array loads
6238 // never use Load-Acquire instructions on ARM64.
6239 const bool use_load_acquire = false;
6240
6241 // /* HeapReference<Object> */ ref =
6242 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006243 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6244 ref,
6245 obj,
6246 data_offset,
6247 index,
6248 scale_factor,
6249 temp,
6250 needs_null_check,
6251 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006252}
6253
6254void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6255 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006256 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006257 uint32_t offset,
6258 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006259 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006260 Register temp,
6261 bool needs_null_check,
Roland Levillainff487002017-03-07 16:50:01 +00006262 bool use_load_acquire) {
Roland Levillain44015862016-01-22 11:47:17 +00006263 DCHECK(kEmitCompilerReadBarrier);
6264 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006265 // If we are emitting an array load, we should not be using a
6266 // Load Acquire instruction. In other words:
6267 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6268 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006269
Roland Levillain54f869e2017-03-06 13:54:11 +00006270 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6271 // whether we need to enter the slow path to mark the reference.
6272 // Then, in the slow path, check the gray bit in the lock word of
6273 // the reference's holder (`obj`) to decide whether to mark `ref` or
6274 // not.
Roland Levillain44015862016-01-22 11:47:17 +00006275 //
Roland Levillainba650a42017-03-06 13:52:32 +00006276 // Note that we do not actually check the value of `GetIsGcMarking()`;
6277 // instead, we load into `temp2` the read barrier mark entry point
6278 // corresponding to register `ref`. If `temp2` is null, it means
6279 // that `GetIsGcMarking()` is false, and vice versa.
6280 //
6281 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00006282 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6283 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006284 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6285 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6286 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6287 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6288 // if (is_gray) {
6289 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6290 // }
6291 // } else {
6292 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006293 // }
Roland Levillain44015862016-01-22 11:47:17 +00006294
Roland Levillainba650a42017-03-06 13:52:32 +00006295 // Slow path marking the object `ref` when the GC is marking. The
6296 // entrypoint will already be loaded in `temp2`.
6297 Register temp2 = lr;
6298 Location temp2_loc = LocationFrom(temp2);
Roland Levillainff487002017-03-07 16:50:01 +00006299 SlowPathCodeARM64* slow_path =
6300 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6301 instruction,
6302 ref,
6303 obj,
6304 offset,
6305 index,
6306 scale_factor,
6307 needs_null_check,
6308 use_load_acquire,
6309 temp,
6310 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006311 AddSlowPath(slow_path);
6312
6313 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6314 const int32_t entry_point_offset =
6315 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6316 // Loading the entrypoint does not require a load acquire since it is only changed when
6317 // threads are suspended or running a checkpoint.
6318 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006319 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6320 // checking GetIsGcMarking.
6321 __ Cbnz(temp2, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006322 // Fast path: the GC is not marking: just load the reference.
Roland Levillain54f869e2017-03-06 13:54:11 +00006323 GenerateRawReferenceLoad(
6324 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006325 __ Bind(slow_path->GetExitLabel());
6326}
6327
Roland Levillainff487002017-03-07 16:50:01 +00006328void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6329 Location ref,
6330 Register obj,
6331 Location field_offset,
6332 Register temp,
6333 bool needs_null_check,
6334 bool use_load_acquire) {
6335 DCHECK(kEmitCompilerReadBarrier);
6336 DCHECK(kUseBakerReadBarrier);
6337 // If we are emitting an array load, we should not be using a
6338 // Load Acquire instruction. In other words:
6339 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6340 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6341
6342 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6343 // whether we need to enter the slow path to update the reference
6344 // field within `obj`. Then, in the slow path, check the gray bit
6345 // in the lock word of the reference's holder (`obj`) to decide
6346 // whether to mark `ref` and update the field or not.
6347 //
6348 // Note that we do not actually check the value of `GetIsGcMarking()`;
6349 // instead, we load into `temp2` the read barrier mark entry point
6350 // corresponding to register `ref`. If `temp2` is null, it means
6351 // that `GetIsGcMarking()` is false, and vice versa.
6352 //
6353 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6354 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6355 // // Slow path.
6356 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6357 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6358 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6359 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6360 // if (is_gray) {
6361 // old_ref = ref;
6362 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6363 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6364 // }
6365 // }
6366
6367 // Slow path updating the object reference at address `obj + field_offset`
6368 // when the GC is marking. The entrypoint will already be loaded in `temp2`.
6369 Register temp2 = lr;
6370 Location temp2_loc = LocationFrom(temp2);
6371 SlowPathCodeARM64* slow_path =
6372 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6373 instruction,
6374 ref,
6375 obj,
6376 /* offset */ 0u,
6377 /* index */ field_offset,
6378 /* scale_factor */ 0u /* "times 1" */,
6379 needs_null_check,
6380 use_load_acquire,
6381 temp,
6382 /* entrypoint */ temp2_loc);
6383 AddSlowPath(slow_path);
6384
6385 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6386 const int32_t entry_point_offset =
6387 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6388 // Loading the entrypoint does not require a load acquire since it is only changed when
6389 // threads are suspended or running a checkpoint.
6390 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
6391 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6392 // checking GetIsGcMarking.
6393 __ Cbnz(temp2, slow_path->GetEntryLabel());
6394 // Fast path: the GC is not marking: nothing to do (the field is
6395 // up-to-date, and we don't need to load the reference).
6396 __ Bind(slow_path->GetExitLabel());
6397}
6398
Roland Levillainba650a42017-03-06 13:52:32 +00006399void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6400 Location ref,
6401 Register obj,
6402 uint32_t offset,
6403 Location index,
6404 size_t scale_factor,
6405 bool needs_null_check,
6406 bool use_load_acquire) {
6407 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006408 Primitive::Type type = Primitive::kPrimNot;
6409 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006410
Roland Levillainba650a42017-03-06 13:52:32 +00006411 // If needed, vixl::EmissionCheckScope guards are used to ensure
6412 // that no pools are emitted between the load (macro) instruction
6413 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006414
Roland Levillain44015862016-01-22 11:47:17 +00006415 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006416 // Load types involving an "index": ArrayGet,
6417 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6418 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006419 if (use_load_acquire) {
6420 // UnsafeGetObjectVolatile intrinsic case.
6421 // Register `index` is not an index in an object array, but an
6422 // offset to an object reference field within object `obj`.
6423 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6424 DCHECK(instruction->GetLocations()->Intrinsified());
6425 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6426 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006427 DCHECK_EQ(offset, 0u);
6428 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006429 DCHECK_EQ(needs_null_check, false);
6430 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006431 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6432 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006433 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006434 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6435 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006436 if (index.IsConstant()) {
6437 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006438 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006439 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006440 if (needs_null_check) {
6441 MaybeRecordImplicitNullCheck(instruction);
6442 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006443 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006444 UseScratchRegisterScope temps(GetVIXLAssembler());
6445 Register temp = temps.AcquireW();
6446 __ Add(temp, obj, offset);
6447 {
6448 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6449 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6450 if (needs_null_check) {
6451 MaybeRecordImplicitNullCheck(instruction);
6452 }
6453 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006454 }
Roland Levillain44015862016-01-22 11:47:17 +00006455 }
Roland Levillain44015862016-01-22 11:47:17 +00006456 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006457 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006458 MemOperand field = HeapOperand(obj, offset);
6459 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006460 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6461 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006462 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006463 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006464 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006465 if (needs_null_check) {
6466 MaybeRecordImplicitNullCheck(instruction);
6467 }
Roland Levillain44015862016-01-22 11:47:17 +00006468 }
6469 }
6470
6471 // Object* ref = ref_addr->AsMirrorPtr()
6472 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006473}
6474
6475void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6476 Location out,
6477 Location ref,
6478 Location obj,
6479 uint32_t offset,
6480 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006481 DCHECK(kEmitCompilerReadBarrier);
6482
Roland Levillain44015862016-01-22 11:47:17 +00006483 // Insert a slow path based read barrier *after* the reference load.
6484 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006485 // If heap poisoning is enabled, the unpoisoning of the loaded
6486 // reference will be carried out by the runtime within the slow
6487 // path.
6488 //
6489 // Note that `ref` currently does not get unpoisoned (when heap
6490 // poisoning is enabled), which is alright as the `ref` argument is
6491 // not used by the artReadBarrierSlow entry point.
6492 //
6493 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6494 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6495 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6496 AddSlowPath(slow_path);
6497
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006498 __ B(slow_path->GetEntryLabel());
6499 __ Bind(slow_path->GetExitLabel());
6500}
6501
Roland Levillain44015862016-01-22 11:47:17 +00006502void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6503 Location out,
6504 Location ref,
6505 Location obj,
6506 uint32_t offset,
6507 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006508 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006509 // Baker's read barriers shall be handled by the fast path
6510 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6511 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006512 // If heap poisoning is enabled, unpoisoning will be taken care of
6513 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006514 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006515 } else if (kPoisonHeapReferences) {
6516 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6517 }
6518}
6519
Roland Levillain44015862016-01-22 11:47:17 +00006520void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6521 Location out,
6522 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006523 DCHECK(kEmitCompilerReadBarrier);
6524
Roland Levillain44015862016-01-22 11:47:17 +00006525 // Insert a slow path based read barrier *after* the GC root load.
6526 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006527 // Note that GC roots are not affected by heap poisoning, so we do
6528 // not need to do anything special for this here.
6529 SlowPathCodeARM64* slow_path =
6530 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6531 AddSlowPath(slow_path);
6532
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006533 __ B(slow_path->GetEntryLabel());
6534 __ Bind(slow_path->GetExitLabel());
6535}
6536
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006537void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6538 LocationSummary* locations =
6539 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6540 locations->SetInAt(0, Location::RequiresRegister());
6541 locations->SetOut(Location::RequiresRegister());
6542}
6543
6544void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6545 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006546 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006547 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006548 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006549 __ Ldr(XRegisterFrom(locations->Out()),
6550 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006551 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006552 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006553 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006554 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6555 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006556 __ Ldr(XRegisterFrom(locations->Out()),
6557 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006558 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006559}
6560
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006561static void PatchJitRootUse(uint8_t* code,
6562 const uint8_t* roots_data,
6563 vixl::aarch64::Literal<uint32_t>* literal,
6564 uint64_t index_in_table) {
6565 uint32_t literal_offset = literal->GetOffset();
6566 uintptr_t address =
6567 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6568 uint8_t* data = code + literal_offset;
6569 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6570}
6571
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006572void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6573 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006574 const StringReference& string_reference = entry.first;
6575 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
6576 const auto it = jit_string_roots_.find(string_reference);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006577 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006578 uint64_t index_in_table = it->second;
6579 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006580 }
6581 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006582 const TypeReference& type_reference = entry.first;
6583 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
6584 const auto it = jit_class_roots_.find(type_reference);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006585 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006586 uint64_t index_in_table = it->second;
6587 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006588 }
6589}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006590
Alexandre Rames67555f72014-11-18 10:55:16 +00006591#undef __
6592#undef QUICK_ENTRY_POINT
6593
Alexandre Rames5319def2014-10-23 10:03:10 +01006594} // namespace arm64
6595} // namespace art