blob: 7ff100d870a708fe3d3ec8e3696e7e53759526f7 [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)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001452 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001453 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001454 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001455 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001456 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001457 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1458 jit_class_patches_(TypeReferenceValueComparator(),
1459 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001460 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001461 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001462}
Alexandre Rames5319def2014-10-23 10:03:10 +01001463
Alexandre Rames67555f72014-11-18 10:55:16 +00001464#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001465
Zheng Xu3927c8b2015-11-18 17:46:25 +08001466void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001467 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001468 jump_table->EmitTable(this);
1469 }
1470}
1471
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001472void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001473 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001474 // Ensure we emit the literal pool.
1475 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001476
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001477 CodeGenerator::Finalize(allocator);
1478}
1479
Zheng Xuad4450e2015-04-17 18:48:56 +08001480void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1481 // Note: There are 6 kinds of moves:
1482 // 1. constant -> GPR/FPR (non-cycle)
1483 // 2. constant -> stack (non-cycle)
1484 // 3. GPR/FPR -> GPR/FPR
1485 // 4. GPR/FPR -> stack
1486 // 5. stack -> GPR/FPR
1487 // 6. stack -> stack (non-cycle)
1488 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1489 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1490 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1491 // dependency.
1492 vixl_temps_.Open(GetVIXLAssembler());
1493}
1494
1495void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1496 vixl_temps_.Close();
1497}
1498
1499Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001500 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1501 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1502 || kind == Location::kSIMDStackSlot);
1503 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1504 ? Location::kFpuRegister
1505 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001506 Location scratch = GetScratchLocation(kind);
1507 if (!scratch.Equals(Location::NoLocation())) {
1508 return scratch;
1509 }
1510 // Allocate from VIXL temp registers.
1511 if (kind == Location::kRegister) {
1512 scratch = LocationFrom(vixl_temps_.AcquireX());
1513 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001514 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001515 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1516 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1517 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001518 }
1519 AddScratchLocation(scratch);
1520 return scratch;
1521}
1522
1523void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1524 if (loc.IsRegister()) {
1525 vixl_temps_.Release(XRegisterFrom(loc));
1526 } else {
1527 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001528 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001529 }
1530 RemoveScratchLocation(loc);
1531}
1532
Alexandre Rames3e69f162014-12-10 10:36:50 +00001533void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001534 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001535 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001536}
1537
Alexandre Rames5319def2014-10-23 10:03:10 +01001538void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001539 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001540 __ Bind(&frame_entry_label_);
1541
Serban Constantinescu02164b32014-11-13 14:05:07 +00001542 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1543 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001544 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001545 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001546 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001547 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001548 {
1549 // Ensure that between load and RecordPcInfo there are no pools emitted.
1550 ExactAssemblyScope eas(GetVIXLAssembler(),
1551 kInstructionSize,
1552 CodeBufferCheckScope::kExactSize);
1553 __ ldr(wzr, MemOperand(temp, 0));
1554 RecordPcInfo(nullptr, 0);
1555 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001556 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001557
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001558 if (!HasEmptyFrame()) {
1559 int frame_size = GetFrameSize();
1560 // Stack layout:
1561 // sp[frame_size - 8] : lr.
1562 // ... : other preserved core registers.
1563 // ... : other preserved fp registers.
1564 // ... : reserved frame space.
1565 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001566
1567 // Save the current method if we need it. Note that we do not
1568 // do this in HCurrentMethod, as the instruction might have been removed
1569 // in the SSA graph.
1570 if (RequiresCurrentMethod()) {
1571 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001572 } else {
1573 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001574 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001575 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001576 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1577 frame_size - GetCoreSpillSize());
1578 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1579 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001580
1581 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1582 // Initialize should_deoptimize flag to 0.
1583 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1584 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1585 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001586 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001587}
1588
1589void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001590 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001591 if (!HasEmptyFrame()) {
1592 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001593 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1594 frame_size - FrameEntrySpillSize());
1595 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1596 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001597 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001598 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001599 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001600 __ Ret();
1601 GetAssembler()->cfi().RestoreState();
1602 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001603}
1604
Scott Wakeling97c72b72016-06-24 16:19:36 +01001605CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001606 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001607 return CPURegList(CPURegister::kRegister, kXRegSize,
1608 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001609}
1610
Scott Wakeling97c72b72016-06-24 16:19:36 +01001611CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001612 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1613 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001614 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1615 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001616}
1617
Alexandre Rames5319def2014-10-23 10:03:10 +01001618void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1619 __ Bind(GetLabelOf(block));
1620}
1621
Calin Juravle175dc732015-08-25 15:42:32 +01001622void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1623 DCHECK(location.IsRegister());
1624 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1625}
1626
Calin Juravlee460d1d2015-09-29 04:52:17 +01001627void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1628 if (location.IsRegister()) {
1629 locations->AddTemp(location);
1630 } else {
1631 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1632 }
1633}
1634
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001635void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001636 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001637 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001638 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001639 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001640 if (value_can_be_null) {
1641 __ Cbz(value, &done);
1642 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001643 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001644 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001645 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001646 if (value_can_be_null) {
1647 __ Bind(&done);
1648 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001649}
1650
David Brazdil58282f42016-01-14 12:45:10 +00001651void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001652 // Blocked core registers:
1653 // lr : Runtime reserved.
1654 // tr : Runtime reserved.
1655 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1656 // ip1 : VIXL core temp.
1657 // ip0 : VIXL core temp.
1658 //
1659 // Blocked fp registers:
1660 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001661 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1662 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001663 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001664 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001665 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001666
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001667 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001668 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001669 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001670 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001671
David Brazdil58282f42016-01-14 12:45:10 +00001672 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001673 // Stubs do not save callee-save floating point registers. If the graph
1674 // is debuggable, we need to deal with these registers differently. For
1675 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001676 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1677 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001678 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001679 }
1680 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001681}
1682
Alexandre Rames3e69f162014-12-10 10:36:50 +00001683size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1684 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1685 __ Str(reg, MemOperand(sp, stack_index));
1686 return kArm64WordSize;
1687}
1688
1689size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1690 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1691 __ Ldr(reg, MemOperand(sp, stack_index));
1692 return kArm64WordSize;
1693}
1694
1695size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1696 FPRegister reg = FPRegister(reg_id, kDRegSize);
1697 __ Str(reg, MemOperand(sp, stack_index));
1698 return kArm64WordSize;
1699}
1700
1701size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1702 FPRegister reg = FPRegister(reg_id, kDRegSize);
1703 __ Ldr(reg, MemOperand(sp, stack_index));
1704 return kArm64WordSize;
1705}
1706
Alexandre Rames5319def2014-10-23 10:03:10 +01001707void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001708 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001709}
1710
1711void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001712 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001713}
1714
Alexandre Rames67555f72014-11-18 10:55:16 +00001715void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001716 if (constant->IsIntConstant()) {
1717 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1718 } else if (constant->IsLongConstant()) {
1719 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1720 } else if (constant->IsNullConstant()) {
1721 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001722 } else if (constant->IsFloatConstant()) {
1723 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1724 } else {
1725 DCHECK(constant->IsDoubleConstant());
1726 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1727 }
1728}
1729
Alexandre Rames3e69f162014-12-10 10:36:50 +00001730
1731static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1732 DCHECK(constant.IsConstant());
1733 HConstant* cst = constant.GetConstant();
1734 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001735 // Null is mapped to a core W register, which we associate with kPrimInt.
1736 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001737 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1738 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1739 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1740}
1741
Roland Levillain952b2352017-05-03 19:49:14 +01001742// Allocate a scratch register from the VIXL pool, querying first
1743// the floating-point register pool, and then the core register
1744// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001745// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1746// using a different allocation strategy.
1747static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1748 vixl::aarch64::UseScratchRegisterScope* temps,
1749 int size_in_bits) {
1750 return masm->GetScratchFPRegisterList()->IsEmpty()
1751 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1752 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1753}
1754
Calin Juravlee460d1d2015-09-29 04:52:17 +01001755void CodeGeneratorARM64::MoveLocation(Location destination,
1756 Location source,
1757 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001758 if (source.Equals(destination)) {
1759 return;
1760 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001761
1762 // A valid move can always be inferred from the destination and source
1763 // locations. When moving from and to a register, the argument type can be
1764 // used to generate 32bit instead of 64bit moves. In debug mode we also
1765 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001766 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001767
1768 if (destination.IsRegister() || destination.IsFpuRegister()) {
1769 if (unspecified_type) {
1770 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1771 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001772 (src_cst != nullptr && (src_cst->IsIntConstant()
1773 || src_cst->IsFloatConstant()
1774 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001775 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001776 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001777 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001778 // If the source is a double stack slot or a 64bit constant, a 64bit
1779 // type is appropriate. Else the source is a register, and since the
1780 // type has not been specified, we chose a 64bit type to force a 64bit
1781 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001782 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001783 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001784 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001785 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1786 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1787 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001788 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1789 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1790 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001791 } else if (source.IsSIMDStackSlot()) {
1792 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001793 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001794 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001795 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001796 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001797 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001798 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001799 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001800 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001801 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1802 ? Primitive::kPrimLong
1803 : Primitive::kPrimInt;
1804 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1805 }
1806 } else {
1807 DCHECK(source.IsFpuRegister());
1808 if (destination.IsRegister()) {
1809 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1810 ? Primitive::kPrimDouble
1811 : Primitive::kPrimFloat;
1812 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1813 } else {
1814 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001815 if (GetGraph()->HasSIMD()) {
1816 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1817 } else {
1818 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1819 }
1820 }
1821 }
1822 } else if (destination.IsSIMDStackSlot()) {
1823 if (source.IsFpuRegister()) {
1824 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1825 } else {
1826 DCHECK(source.IsSIMDStackSlot());
1827 UseScratchRegisterScope temps(GetVIXLAssembler());
1828 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1829 Register temp = temps.AcquireX();
1830 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1831 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1832 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1833 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1834 } else {
1835 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1836 __ Ldr(temp, StackOperandFrom(source));
1837 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001838 }
1839 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001840 } else { // The destination is not a register. It must be a stack slot.
1841 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1842 if (source.IsRegister() || source.IsFpuRegister()) {
1843 if (unspecified_type) {
1844 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001845 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001846 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001847 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001848 }
1849 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001850 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1851 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1852 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001853 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001854 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1855 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001856 UseScratchRegisterScope temps(GetVIXLAssembler());
1857 HConstant* src_cst = source.GetConstant();
1858 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001859 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001860 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1861 ? Register(xzr)
1862 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001863 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001864 if (src_cst->IsIntConstant()) {
1865 temp = temps.AcquireW();
1866 } else if (src_cst->IsLongConstant()) {
1867 temp = temps.AcquireX();
1868 } else if (src_cst->IsFloatConstant()) {
1869 temp = temps.AcquireS();
1870 } else {
1871 DCHECK(src_cst->IsDoubleConstant());
1872 temp = temps.AcquireD();
1873 }
1874 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001875 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001876 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001877 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001878 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001879 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001880 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001881 // Use any scratch register (a core or a floating-point one)
1882 // from VIXL scratch register pools as a temporary.
1883 //
1884 // We used to only use the FP scratch register pool, but in some
1885 // rare cases the only register from this pool (D31) would
1886 // already be used (e.g. within a ParallelMove instruction, when
1887 // a move is blocked by a another move requiring a scratch FP
1888 // register, which would reserve D31). To prevent this issue, we
1889 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001890 //
1891 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001892 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001893 // use AcquireFPOrCoreCPURegisterOfSize instead of
1894 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1895 // allocates core scratch registers first.
1896 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1897 GetVIXLAssembler(),
1898 &temps,
1899 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001900 __ Ldr(temp, StackOperandFrom(source));
1901 __ Str(temp, StackOperandFrom(destination));
1902 }
1903 }
1904}
1905
1906void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001907 CPURegister dst,
1908 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001909 switch (type) {
1910 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001911 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001912 break;
1913 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001914 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001915 break;
1916 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001917 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001918 break;
1919 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001920 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001921 break;
1922 case Primitive::kPrimInt:
1923 case Primitive::kPrimNot:
1924 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001925 case Primitive::kPrimFloat:
1926 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001927 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001928 __ Ldr(dst, src);
1929 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001930 case Primitive::kPrimVoid:
1931 LOG(FATAL) << "Unreachable type " << type;
1932 }
1933}
1934
Calin Juravle77520bc2015-01-12 18:45:46 +00001935void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001936 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001937 const MemOperand& src,
1938 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001939 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001940 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001941 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001942 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001943
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001944 DCHECK(!src.IsPreIndex());
1945 DCHECK(!src.IsPostIndex());
1946
1947 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001948 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001949 {
1950 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1951 MemOperand base = MemOperand(temp_base);
1952 switch (type) {
1953 case Primitive::kPrimBoolean:
1954 {
1955 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1956 __ ldarb(Register(dst), base);
1957 if (needs_null_check) {
1958 MaybeRecordImplicitNullCheck(instruction);
1959 }
1960 }
1961 break;
1962 case Primitive::kPrimByte:
1963 {
1964 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1965 __ ldarb(Register(dst), base);
1966 if (needs_null_check) {
1967 MaybeRecordImplicitNullCheck(instruction);
1968 }
1969 }
1970 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1971 break;
1972 case Primitive::kPrimChar:
1973 {
1974 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1975 __ ldarh(Register(dst), base);
1976 if (needs_null_check) {
1977 MaybeRecordImplicitNullCheck(instruction);
1978 }
1979 }
1980 break;
1981 case Primitive::kPrimShort:
1982 {
1983 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1984 __ ldarh(Register(dst), base);
1985 if (needs_null_check) {
1986 MaybeRecordImplicitNullCheck(instruction);
1987 }
1988 }
1989 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1990 break;
1991 case Primitive::kPrimInt:
1992 case Primitive::kPrimNot:
1993 case Primitive::kPrimLong:
1994 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
1995 {
1996 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1997 __ ldar(Register(dst), base);
1998 if (needs_null_check) {
1999 MaybeRecordImplicitNullCheck(instruction);
2000 }
2001 }
2002 break;
2003 case Primitive::kPrimFloat:
2004 case Primitive::kPrimDouble: {
2005 DCHECK(dst.IsFPRegister());
2006 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002007
Artem Serov914d7a82017-02-07 14:33:49 +00002008 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2009 {
2010 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2011 __ ldar(temp, base);
2012 if (needs_null_check) {
2013 MaybeRecordImplicitNullCheck(instruction);
2014 }
2015 }
2016 __ Fmov(FPRegister(dst), temp);
2017 break;
Roland Levillain44015862016-01-22 11:47:17 +00002018 }
Artem Serov914d7a82017-02-07 14:33:49 +00002019 case Primitive::kPrimVoid:
2020 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002021 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002022 }
2023}
2024
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002025void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002026 CPURegister src,
2027 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002028 switch (type) {
2029 case Primitive::kPrimBoolean:
2030 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002031 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002032 break;
2033 case Primitive::kPrimChar:
2034 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002035 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002036 break;
2037 case Primitive::kPrimInt:
2038 case Primitive::kPrimNot:
2039 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002040 case Primitive::kPrimFloat:
2041 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00002042 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002043 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002044 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002045 case Primitive::kPrimVoid:
2046 LOG(FATAL) << "Unreachable type " << type;
2047 }
2048}
2049
Artem Serov914d7a82017-02-07 14:33:49 +00002050void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
2051 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002052 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002053 const MemOperand& dst,
2054 bool needs_null_check) {
2055 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002056 UseScratchRegisterScope temps(GetVIXLAssembler());
2057 Register temp_base = temps.AcquireX();
2058
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002059 DCHECK(!dst.IsPreIndex());
2060 DCHECK(!dst.IsPostIndex());
2061
2062 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002063 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002064 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002065 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002066 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002067 switch (type) {
2068 case Primitive::kPrimBoolean:
2069 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00002070 {
2071 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2072 __ stlrb(Register(src), base);
2073 if (needs_null_check) {
2074 MaybeRecordImplicitNullCheck(instruction);
2075 }
2076 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002077 break;
2078 case Primitive::kPrimChar:
2079 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00002080 {
2081 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2082 __ stlrh(Register(src), base);
2083 if (needs_null_check) {
2084 MaybeRecordImplicitNullCheck(instruction);
2085 }
2086 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002087 break;
2088 case Primitive::kPrimInt:
2089 case Primitive::kPrimNot:
2090 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002091 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002092 {
2093 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2094 __ stlr(Register(src), base);
2095 if (needs_null_check) {
2096 MaybeRecordImplicitNullCheck(instruction);
2097 }
2098 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002099 break;
2100 case Primitive::kPrimFloat:
2101 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002102 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002103 Register temp_src;
2104 if (src.IsZero()) {
2105 // The zero register is used to avoid synthesizing zero constants.
2106 temp_src = Register(src);
2107 } else {
2108 DCHECK(src.IsFPRegister());
2109 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2110 __ Fmov(temp_src, FPRegister(src));
2111 }
Artem Serov914d7a82017-02-07 14:33:49 +00002112 {
2113 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2114 __ stlr(temp_src, base);
2115 if (needs_null_check) {
2116 MaybeRecordImplicitNullCheck(instruction);
2117 }
2118 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002119 break;
2120 }
2121 case Primitive::kPrimVoid:
2122 LOG(FATAL) << "Unreachable type " << type;
2123 }
2124}
2125
Calin Juravle175dc732015-08-25 15:42:32 +01002126void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2127 HInstruction* instruction,
2128 uint32_t dex_pc,
2129 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002130 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002131
2132 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2133 {
2134 // Ensure the pc position is recorded immediately after the `blr` instruction.
2135 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2136 __ blr(lr);
2137 if (EntrypointRequiresStackMap(entrypoint)) {
2138 RecordPcInfo(instruction, dex_pc, slow_path);
2139 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002140 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002141}
2142
Roland Levillaindec8f632016-07-22 17:10:06 +01002143void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2144 HInstruction* instruction,
2145 SlowPathCode* slow_path) {
2146 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002147 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2148 __ Blr(lr);
2149}
2150
Alexandre Rames67555f72014-11-18 10:55:16 +00002151void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002152 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002153 UseScratchRegisterScope temps(GetVIXLAssembler());
2154 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002155 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2156
Serban Constantinescu02164b32014-11-13 14:05:07 +00002157 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002158 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2159 __ Add(temp, class_reg, status_offset);
2160 __ Ldar(temp, HeapOperand(temp));
2161 __ Cmp(temp, mirror::Class::kStatusInitialized);
2162 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002163 __ Bind(slow_path->GetExitLabel());
2164}
Alexandre Rames5319def2014-10-23 10:03:10 +01002165
Roland Levillain44015862016-01-22 11:47:17 +00002166void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002167 BarrierType type = BarrierAll;
2168
2169 switch (kind) {
2170 case MemBarrierKind::kAnyAny:
2171 case MemBarrierKind::kAnyStore: {
2172 type = BarrierAll;
2173 break;
2174 }
2175 case MemBarrierKind::kLoadAny: {
2176 type = BarrierReads;
2177 break;
2178 }
2179 case MemBarrierKind::kStoreStore: {
2180 type = BarrierWrites;
2181 break;
2182 }
2183 default:
2184 LOG(FATAL) << "Unexpected memory barrier " << kind;
2185 }
2186 __ Dmb(InnerShareable, type);
2187}
2188
Serban Constantinescu02164b32014-11-13 14:05:07 +00002189void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2190 HBasicBlock* successor) {
2191 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002192 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2193 if (slow_path == nullptr) {
2194 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2195 instruction->SetSlowPath(slow_path);
2196 codegen_->AddSlowPath(slow_path);
2197 if (successor != nullptr) {
2198 DCHECK(successor->IsLoopHeader());
2199 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2200 }
2201 } else {
2202 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2203 }
2204
Serban Constantinescu02164b32014-11-13 14:05:07 +00002205 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2206 Register temp = temps.AcquireW();
2207
Andreas Gampe542451c2016-07-26 09:02:02 -07002208 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002209 if (successor == nullptr) {
2210 __ Cbnz(temp, slow_path->GetEntryLabel());
2211 __ Bind(slow_path->GetReturnLabel());
2212 } else {
2213 __ Cbz(temp, codegen_->GetLabelOf(successor));
2214 __ B(slow_path->GetEntryLabel());
2215 // slow_path will return to GetLabelOf(successor).
2216 }
2217}
2218
Alexandre Rames5319def2014-10-23 10:03:10 +01002219InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2220 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002221 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002222 assembler_(codegen->GetAssembler()),
2223 codegen_(codegen) {}
2224
2225#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002226 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002227
2228#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2229
2230enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002231 // Using a base helps identify when we hit such breakpoints.
2232 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002233#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2234 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2235#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2236};
2237
2238#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002239 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002240 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2241 } \
2242 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2243 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2244 locations->SetOut(Location::Any()); \
2245 }
2246 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2247#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2248
2249#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002250#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002251
Alexandre Rames67555f72014-11-18 10:55:16 +00002252void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002253 DCHECK_EQ(instr->InputCount(), 2U);
2254 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2255 Primitive::Type type = instr->GetResultType();
2256 switch (type) {
2257 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002258 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002259 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002260 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002261 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002262 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002263
2264 case Primitive::kPrimFloat:
2265 case Primitive::kPrimDouble:
2266 locations->SetInAt(0, Location::RequiresFpuRegister());
2267 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002268 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002269 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002270
Alexandre Rames5319def2014-10-23 10:03:10 +01002271 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002272 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002273 }
2274}
2275
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002276void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2277 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002278 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2279
2280 bool object_field_get_with_read_barrier =
2281 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002282 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002283 new (GetGraph()->GetArena()) LocationSummary(instruction,
2284 object_field_get_with_read_barrier ?
2285 LocationSummary::kCallOnSlowPath :
2286 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002287 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002288 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002289 // We need a temporary register for the read barrier marking slow
2290 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002291 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2292 !Runtime::Current()->UseJitCompilation() &&
2293 !field_info.IsVolatile()) {
2294 // If link-time thunks for the Baker read barrier are enabled, for AOT
2295 // non-volatile loads we need a temporary only if the offset is too big.
2296 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2297 locations->AddTemp(FixedTempLocation());
2298 }
2299 } else {
2300 locations->AddTemp(Location::RequiresRegister());
2301 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002302 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002303 locations->SetInAt(0, Location::RequiresRegister());
2304 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2305 locations->SetOut(Location::RequiresFpuRegister());
2306 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002307 // The output overlaps for an object field get when read barriers
2308 // are enabled: we do not want the load to overwrite the object's
2309 // location, as we need it to emit the read barrier.
2310 locations->SetOut(
2311 Location::RequiresRegister(),
2312 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002313 }
2314}
2315
2316void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2317 const FieldInfo& field_info) {
2318 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002319 LocationSummary* locations = instruction->GetLocations();
2320 Location base_loc = locations->InAt(0);
2321 Location out = locations->Out();
2322 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002323 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002324 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002325
Roland Levillain44015862016-01-22 11:47:17 +00002326 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2327 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002328 // /* HeapReference<Object> */ out = *(base + offset)
2329 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002330 Location maybe_temp =
2331 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002332 // Note that potential implicit null checks are handled in this
2333 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2334 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2335 instruction,
2336 out,
2337 base,
2338 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002339 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002340 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002341 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002342 } else {
2343 // General case.
2344 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002345 // Note that a potential implicit null check is handled in this
2346 // CodeGeneratorARM64::LoadAcquire call.
2347 // NB: LoadAcquire will record the pc info if needed.
2348 codegen_->LoadAcquire(
2349 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002350 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002351 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2352 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002353 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002354 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002355 }
Roland Levillain44015862016-01-22 11:47:17 +00002356 if (field_type == Primitive::kPrimNot) {
2357 // If read barriers are enabled, emit read barriers other than
2358 // Baker's using a slow path (and also unpoison the loaded
2359 // reference, if heap poisoning is enabled).
2360 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2361 }
Roland Levillain4d027112015-07-01 15:41:14 +01002362 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002363}
2364
2365void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2366 LocationSummary* locations =
2367 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2368 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002369 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2370 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2371 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002372 locations->SetInAt(1, Location::RequiresFpuRegister());
2373 } else {
2374 locations->SetInAt(1, Location::RequiresRegister());
2375 }
2376}
2377
2378void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002379 const FieldInfo& field_info,
2380 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002381 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2382
2383 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002384 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002385 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002386 Offset offset = field_info.GetFieldOffset();
2387 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002388
Roland Levillain4d027112015-07-01 15:41:14 +01002389 {
2390 // We use a block to end the scratch scope before the write barrier, thus
2391 // freeing the temporary registers so they can be used in `MarkGCCard`.
2392 UseScratchRegisterScope temps(GetVIXLAssembler());
2393
2394 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2395 DCHECK(value.IsW());
2396 Register temp = temps.AcquireW();
2397 __ Mov(temp, value.W());
2398 GetAssembler()->PoisonHeapReference(temp.W());
2399 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002400 }
Roland Levillain4d027112015-07-01 15:41:14 +01002401
2402 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002403 codegen_->StoreRelease(
2404 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002405 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002406 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2407 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002408 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2409 codegen_->MaybeRecordImplicitNullCheck(instruction);
2410 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002411 }
2412
2413 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002414 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002415 }
2416}
2417
Alexandre Rames67555f72014-11-18 10:55:16 +00002418void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002419 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002420
2421 switch (type) {
2422 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002423 case Primitive::kPrimLong: {
2424 Register dst = OutputRegister(instr);
2425 Register lhs = InputRegisterAt(instr, 0);
2426 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002427 if (instr->IsAdd()) {
2428 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002429 } else if (instr->IsAnd()) {
2430 __ And(dst, lhs, rhs);
2431 } else if (instr->IsOr()) {
2432 __ Orr(dst, lhs, rhs);
2433 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002434 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002435 } else if (instr->IsRor()) {
2436 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002437 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002438 __ Ror(dst, lhs, shift);
2439 } else {
2440 // Ensure shift distance is in the same size register as the result. If
2441 // we are rotating a long and the shift comes in a w register originally,
2442 // we don't need to sxtw for use as an x since the shift distances are
2443 // all & reg_bits - 1.
2444 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2445 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002446 } else {
2447 DCHECK(instr->IsXor());
2448 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002449 }
2450 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002451 }
2452 case Primitive::kPrimFloat:
2453 case Primitive::kPrimDouble: {
2454 FPRegister dst = OutputFPRegister(instr);
2455 FPRegister lhs = InputFPRegisterAt(instr, 0);
2456 FPRegister rhs = InputFPRegisterAt(instr, 1);
2457 if (instr->IsAdd()) {
2458 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002459 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002460 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002461 } else {
2462 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002463 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002464 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002465 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002466 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002467 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002468 }
2469}
2470
Serban Constantinescu02164b32014-11-13 14:05:07 +00002471void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2472 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2473
2474 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2475 Primitive::Type type = instr->GetResultType();
2476 switch (type) {
2477 case Primitive::kPrimInt:
2478 case Primitive::kPrimLong: {
2479 locations->SetInAt(0, Location::RequiresRegister());
2480 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002481 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002482 break;
2483 }
2484 default:
2485 LOG(FATAL) << "Unexpected shift type " << type;
2486 }
2487}
2488
2489void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2490 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2491
2492 Primitive::Type type = instr->GetType();
2493 switch (type) {
2494 case Primitive::kPrimInt:
2495 case Primitive::kPrimLong: {
2496 Register dst = OutputRegister(instr);
2497 Register lhs = InputRegisterAt(instr, 0);
2498 Operand rhs = InputOperandAt(instr, 1);
2499 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002500 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002501 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002502 if (instr->IsShl()) {
2503 __ Lsl(dst, lhs, shift_value);
2504 } else if (instr->IsShr()) {
2505 __ Asr(dst, lhs, shift_value);
2506 } else {
2507 __ Lsr(dst, lhs, shift_value);
2508 }
2509 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002510 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002511
2512 if (instr->IsShl()) {
2513 __ Lsl(dst, lhs, rhs_reg);
2514 } else if (instr->IsShr()) {
2515 __ Asr(dst, lhs, rhs_reg);
2516 } else {
2517 __ Lsr(dst, lhs, rhs_reg);
2518 }
2519 }
2520 break;
2521 }
2522 default:
2523 LOG(FATAL) << "Unexpected shift operation type " << type;
2524 }
2525}
2526
Alexandre Rames5319def2014-10-23 10:03:10 +01002527void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002528 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002529}
2530
2531void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002532 HandleBinaryOp(instruction);
2533}
2534
2535void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2536 HandleBinaryOp(instruction);
2537}
2538
2539void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2540 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002541}
2542
Artem Serov7fc63502016-02-09 17:15:29 +00002543void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002544 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2545 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2546 locations->SetInAt(0, Location::RequiresRegister());
2547 // There is no immediate variant of negated bitwise instructions in AArch64.
2548 locations->SetInAt(1, Location::RequiresRegister());
2549 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2550}
2551
Artem Serov7fc63502016-02-09 17:15:29 +00002552void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002553 Register dst = OutputRegister(instr);
2554 Register lhs = InputRegisterAt(instr, 0);
2555 Register rhs = InputRegisterAt(instr, 1);
2556
2557 switch (instr->GetOpKind()) {
2558 case HInstruction::kAnd:
2559 __ Bic(dst, lhs, rhs);
2560 break;
2561 case HInstruction::kOr:
2562 __ Orn(dst, lhs, rhs);
2563 break;
2564 case HInstruction::kXor:
2565 __ Eon(dst, lhs, rhs);
2566 break;
2567 default:
2568 LOG(FATAL) << "Unreachable";
2569 }
2570}
2571
Anton Kirilov74234da2017-01-13 14:42:47 +00002572void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2573 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002574 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2575 instruction->GetType() == Primitive::kPrimLong);
2576 LocationSummary* locations =
2577 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2578 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2579 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2580 } else {
2581 locations->SetInAt(0, Location::RequiresRegister());
2582 }
2583 locations->SetInAt(1, Location::RequiresRegister());
2584 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2585}
2586
Anton Kirilov74234da2017-01-13 14:42:47 +00002587void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2588 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002589 Primitive::Type type = instruction->GetType();
2590 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2591 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2592 Register out = OutputRegister(instruction);
2593 Register left;
2594 if (kind != HInstruction::kNeg) {
2595 left = InputRegisterAt(instruction, 0);
2596 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002597 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002598 // shifter operand operation, the IR generating `right_reg` (input to the type
2599 // conversion) can have a different type from the current instruction's type,
2600 // so we manually indicate the type.
2601 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002602 Operand right_operand(0);
2603
Anton Kirilov74234da2017-01-13 14:42:47 +00002604 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2605 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002606 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2607 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002608 right_operand = Operand(right_reg,
2609 helpers::ShiftFromOpKind(op_kind),
2610 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002611 }
2612
2613 // Logical binary operations do not support extension operations in the
2614 // operand. Note that VIXL would still manage if it was passed by generating
2615 // the extension as a separate instruction.
2616 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2617 DCHECK(!right_operand.IsExtendedRegister() ||
2618 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2619 kind != HInstruction::kNeg));
2620 switch (kind) {
2621 case HInstruction::kAdd:
2622 __ Add(out, left, right_operand);
2623 break;
2624 case HInstruction::kAnd:
2625 __ And(out, left, right_operand);
2626 break;
2627 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002628 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002629 __ Neg(out, right_operand);
2630 break;
2631 case HInstruction::kOr:
2632 __ Orr(out, left, right_operand);
2633 break;
2634 case HInstruction::kSub:
2635 __ Sub(out, left, right_operand);
2636 break;
2637 case HInstruction::kXor:
2638 __ Eor(out, left, right_operand);
2639 break;
2640 default:
2641 LOG(FATAL) << "Unexpected operation kind: " << kind;
2642 UNREACHABLE();
2643 }
2644}
2645
Artem Serov328429f2016-07-06 16:23:04 +01002646void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002647 LocationSummary* locations =
2648 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2649 locations->SetInAt(0, Location::RequiresRegister());
2650 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002651 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002652}
2653
Roland Levillain19c54192016-11-04 13:44:09 +00002654void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002655 __ Add(OutputRegister(instruction),
2656 InputRegisterAt(instruction, 0),
2657 Operand(InputOperandAt(instruction, 1)));
2658}
2659
Artem Serove1811ed2017-04-27 16:50:47 +01002660void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2661 LocationSummary* locations =
2662 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2663
2664 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2665
2666 locations->SetInAt(0, Location::RequiresRegister());
2667 // For byte case we don't need to shift the index variable so we can encode the data offset into
2668 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2669 // data offset constant generation out of the loop and reduce the critical path length in the
2670 // loop.
2671 locations->SetInAt(1, shift->GetValue() == 0
2672 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2673 : Location::RequiresRegister());
2674 locations->SetInAt(2, Location::ConstantLocation(shift));
2675 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2676}
2677
2678void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2679 HIntermediateAddressIndex* instruction) {
2680 Register index_reg = InputRegisterAt(instruction, 0);
2681 uint32_t shift = Int64ConstantFrom(instruction->GetLocations()->InAt(2));
2682 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2683
2684 if (shift == 0) {
2685 __ Add(OutputRegister(instruction), index_reg, offset);
2686 } else {
2687 Register offset_reg = InputRegisterAt(instruction, 1);
2688 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2689 }
2690}
2691
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002692void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002693 LocationSummary* locations =
2694 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002695 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2696 if (instr->GetOpKind() == HInstruction::kSub &&
2697 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002698 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002699 // Don't allocate register for Mneg instruction.
2700 } else {
2701 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2702 Location::RequiresRegister());
2703 }
2704 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2705 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002706 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2707}
2708
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002709void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002710 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002711 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2712 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002713
2714 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2715 // This fixup should be carried out for all multiply-accumulate instructions:
2716 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2717 if (instr->GetType() == Primitive::kPrimLong &&
2718 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2719 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002720 vixl::aarch64::Instruction* prev =
2721 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002722 if (prev->IsLoadOrStore()) {
2723 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002724 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002725 __ nop();
2726 }
2727 }
2728
2729 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002730 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002731 __ Madd(res, mul_left, mul_right, accumulator);
2732 } else {
2733 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002734 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002735 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002736 __ Mneg(res, mul_left, mul_right);
2737 } else {
2738 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2739 __ Msub(res, mul_left, mul_right, accumulator);
2740 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002741 }
2742}
2743
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002744void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002745 bool object_array_get_with_read_barrier =
2746 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002747 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002748 new (GetGraph()->GetArena()) LocationSummary(instruction,
2749 object_array_get_with_read_barrier ?
2750 LocationSummary::kCallOnSlowPath :
2751 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002752 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002753 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002754 // We need a temporary register for the read barrier marking slow
2755 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002756 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2757 !Runtime::Current()->UseJitCompilation() &&
2758 instruction->GetIndex()->IsConstant()) {
2759 // Array loads with constant index are treated as field loads.
2760 // If link-time thunks for the Baker read barrier are enabled, for AOT
2761 // constant index loads we need a temporary only if the offset is too big.
2762 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2763 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
2764 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
2765 if (offset >= kReferenceLoadMinFarOffset) {
2766 locations->AddTemp(FixedTempLocation());
2767 }
2768 } else {
2769 locations->AddTemp(Location::RequiresRegister());
2770 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002771 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002772 locations->SetInAt(0, Location::RequiresRegister());
2773 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002774 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2775 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2776 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002777 // The output overlaps in the case of an object array get with
2778 // read barriers enabled: we do not want the move to overwrite the
2779 // array's location, as we need it to emit the read barrier.
2780 locations->SetOut(
2781 Location::RequiresRegister(),
2782 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002783 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002784}
2785
2786void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002787 Primitive::Type type = instruction->GetType();
2788 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002789 LocationSummary* locations = instruction->GetLocations();
2790 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002791 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002792 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002793 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2794 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002795 MacroAssembler* masm = GetVIXLAssembler();
2796 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002797
Roland Levillain19c54192016-11-04 13:44:09 +00002798 // The read barrier instrumentation of object ArrayGet instructions
2799 // does not support the HIntermediateAddress instruction.
2800 DCHECK(!((type == Primitive::kPrimNot) &&
2801 instruction->GetArray()->IsIntermediateAddress() &&
2802 kEmitCompilerReadBarrier));
2803
Roland Levillain44015862016-01-22 11:47:17 +00002804 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2805 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002806 // Note that a potential implicit null check is handled in the
2807 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002808 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002809 if (index.IsConstant()) {
2810 // Array load with a constant index can be treated as a field load.
2811 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2812 Location maybe_temp =
2813 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2814 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2815 out,
2816 obj.W(),
2817 offset,
2818 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002819 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002820 /* use_load_acquire */ false);
2821 } else {
2822 Register temp = WRegisterFrom(locations->GetTemp(0));
2823 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002824 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002825 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002826 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002827 // General case.
2828 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002829 Register length;
2830 if (maybe_compressed_char_at) {
2831 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2832 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002833 {
2834 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2835 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2836
2837 if (instruction->GetArray()->IsIntermediateAddress()) {
2838 DCHECK_LT(count_offset, offset);
2839 int64_t adjusted_offset =
2840 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2841 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2842 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2843 } else {
2844 __ Ldr(length, HeapOperand(obj, count_offset));
2845 }
2846 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002847 }
jessicahandojo05765752016-09-09 19:01:32 -07002848 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002849 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002850 if (maybe_compressed_char_at) {
2851 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002852 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2853 "Expecting 0=compressed, 1=uncompressed");
2854 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002855 __ Ldrb(Register(OutputCPURegister(instruction)),
2856 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2857 __ B(&done);
2858 __ Bind(&uncompressed_load);
2859 __ Ldrh(Register(OutputCPURegister(instruction)),
2860 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2861 __ Bind(&done);
2862 } else {
2863 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2864 source = HeapOperand(obj, offset);
2865 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002866 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002867 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002868 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002869 // We do not need to compute the intermediate address from the array: the
2870 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002871 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002872 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002873 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002874 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2875 }
2876 temp = obj;
2877 } else {
2878 __ Add(temp, obj, offset);
2879 }
jessicahandojo05765752016-09-09 19:01:32 -07002880 if (maybe_compressed_char_at) {
2881 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002882 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2883 "Expecting 0=compressed, 1=uncompressed");
2884 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002885 __ Ldrb(Register(OutputCPURegister(instruction)),
2886 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2887 __ B(&done);
2888 __ Bind(&uncompressed_load);
2889 __ Ldrh(Register(OutputCPURegister(instruction)),
2890 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2891 __ Bind(&done);
2892 } else {
2893 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2894 }
Roland Levillain44015862016-01-22 11:47:17 +00002895 }
jessicahandojo05765752016-09-09 19:01:32 -07002896 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002897 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2898 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002899 codegen_->Load(type, OutputCPURegister(instruction), source);
2900 codegen_->MaybeRecordImplicitNullCheck(instruction);
2901 }
Roland Levillain44015862016-01-22 11:47:17 +00002902
2903 if (type == Primitive::kPrimNot) {
2904 static_assert(
2905 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2906 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2907 Location obj_loc = locations->InAt(0);
2908 if (index.IsConstant()) {
2909 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2910 } else {
2911 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2912 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002913 }
Roland Levillain4d027112015-07-01 15:41:14 +01002914 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002915}
2916
Alexandre Rames5319def2014-10-23 10:03:10 +01002917void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2918 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2919 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002920 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002921}
2922
2923void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002924 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002925 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002926 {
2927 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2928 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2929 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2930 codegen_->MaybeRecordImplicitNullCheck(instruction);
2931 }
jessicahandojo05765752016-09-09 19:01:32 -07002932 // Mask out compression flag from String's array length.
2933 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002934 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002935 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002936}
2937
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002938void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002939 Primitive::Type value_type = instruction->GetComponentType();
2940
2941 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002942 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2943 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002944 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002945 LocationSummary::kCallOnSlowPath :
2946 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002947 locations->SetInAt(0, Location::RequiresRegister());
2948 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002949 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2950 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2951 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002952 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002953 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002954 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002955 }
2956}
2957
2958void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2959 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002960 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002961 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002962 bool needs_write_barrier =
2963 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002964
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002965 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002966 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002967 CPURegister source = value;
2968 Location index = locations->InAt(1);
2969 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2970 MemOperand destination = HeapOperand(array);
2971 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002972
2973 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002974 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002975 if (index.IsConstant()) {
2976 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2977 destination = HeapOperand(array, offset);
2978 } else {
2979 UseScratchRegisterScope temps(masm);
2980 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002981 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002982 // We do not need to compute the intermediate address from the array: the
2983 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002984 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002985 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002986 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002987 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2988 }
2989 temp = array;
2990 } else {
2991 __ Add(temp, array, offset);
2992 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002993 destination = HeapOperand(temp,
2994 XRegisterFrom(index),
2995 LSL,
2996 Primitive::ComponentSizeShift(value_type));
2997 }
Artem Serov914d7a82017-02-07 14:33:49 +00002998 {
2999 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3000 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3001 codegen_->Store(value_type, value, destination);
3002 codegen_->MaybeRecordImplicitNullCheck(instruction);
3003 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003004 } else {
Artem Serov328429f2016-07-06 16:23:04 +01003005 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003006 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003007 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01003008 {
3009 // We use a block to end the scratch scope before the write barrier, thus
3010 // freeing the temporary registers so they can be used in `MarkGCCard`.
3011 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003012 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01003013 if (index.IsConstant()) {
3014 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003015 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01003016 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01003017 destination = HeapOperand(temp,
3018 XRegisterFrom(index),
3019 LSL,
3020 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01003021 }
3022
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003023 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3024 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3025 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3026
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003027 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003028 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
3029 codegen_->AddSlowPath(slow_path);
3030 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003031 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003032 __ Cbnz(Register(value), &non_zero);
3033 if (!index.IsConstant()) {
3034 __ Add(temp, array, offset);
3035 }
Artem Serov914d7a82017-02-07 14:33:49 +00003036 {
3037 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
3038 // emitted.
3039 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3040 __ Str(wzr, destination);
3041 codegen_->MaybeRecordImplicitNullCheck(instruction);
3042 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003043 __ B(&done);
3044 __ Bind(&non_zero);
3045 }
3046
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003047 // Note that when Baker read barriers are enabled, the type
3048 // checks are performed without read barriers. This is fine,
3049 // even in the case where a class object is in the from-space
3050 // after the flip, as a comparison involving such a type would
3051 // not produce a false positive; it may of course produce a
3052 // false negative, in which case we would take the ArraySet
3053 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003054
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003055 Register temp2 = temps.AcquireSameSizeAs(array);
3056 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003057 {
3058 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3059 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3060 __ Ldr(temp, HeapOperand(array, class_offset));
3061 codegen_->MaybeRecordImplicitNullCheck(instruction);
3062 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003063 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003064
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003065 // /* HeapReference<Class> */ temp = temp->component_type_
3066 __ Ldr(temp, HeapOperand(temp, component_offset));
3067 // /* HeapReference<Class> */ temp2 = value->klass_
3068 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3069 // If heap poisoning is enabled, no need to unpoison `temp`
3070 // nor `temp2`, as we are comparing two poisoned references.
3071 __ Cmp(temp, temp2);
3072 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003073
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003074 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3075 vixl::aarch64::Label do_put;
3076 __ B(eq, &do_put);
3077 // If heap poisoning is enabled, the `temp` reference has
3078 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003079 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3080
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003081 // /* HeapReference<Class> */ temp = temp->super_class_
3082 __ Ldr(temp, HeapOperand(temp, super_offset));
3083 // If heap poisoning is enabled, no need to unpoison
3084 // `temp`, as we are comparing against null below.
3085 __ Cbnz(temp, slow_path->GetEntryLabel());
3086 __ Bind(&do_put);
3087 } else {
3088 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003089 }
3090 }
3091
3092 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003093 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003094 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003095 __ Mov(temp2, value.W());
3096 GetAssembler()->PoisonHeapReference(temp2);
3097 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003098 }
3099
3100 if (!index.IsConstant()) {
3101 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003102 } else {
3103 // We no longer need the `temp` here so release it as the store below may
3104 // need a scratch register (if the constant index makes the offset too large)
3105 // and the poisoned `source` could be using the other scratch register.
3106 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003107 }
Artem Serov914d7a82017-02-07 14:33:49 +00003108 {
3109 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3110 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3111 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003112
Artem Serov914d7a82017-02-07 14:33:49 +00003113 if (!may_need_runtime_call_for_type_check) {
3114 codegen_->MaybeRecordImplicitNullCheck(instruction);
3115 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003116 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003117 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003118
3119 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3120
3121 if (done.IsLinked()) {
3122 __ Bind(&done);
3123 }
3124
3125 if (slow_path != nullptr) {
3126 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003127 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003128 }
3129}
3130
Alexandre Rames67555f72014-11-18 10:55:16 +00003131void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003132 RegisterSet caller_saves = RegisterSet::Empty();
3133 InvokeRuntimeCallingConvention calling_convention;
3134 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3135 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3136 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003137 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003138 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003139}
3140
3141void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003142 BoundsCheckSlowPathARM64* slow_path =
3143 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003144 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003145 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3146 __ B(slow_path->GetEntryLabel(), hs);
3147}
3148
Alexandre Rames67555f72014-11-18 10:55:16 +00003149void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3150 LocationSummary* locations =
3151 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3152 locations->SetInAt(0, Location::RequiresRegister());
3153 if (check->HasUses()) {
3154 locations->SetOut(Location::SameAsFirstInput());
3155 }
3156}
3157
3158void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3159 // We assume the class is not null.
3160 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3161 check->GetLoadClass(), check, check->GetDexPc(), true);
3162 codegen_->AddSlowPath(slow_path);
3163 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3164}
3165
Roland Levillain1a653882016-03-18 18:05:57 +00003166static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3167 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3168 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3169}
3170
3171void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3172 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3173 Location rhs_loc = instruction->GetLocations()->InAt(1);
3174 if (rhs_loc.IsConstant()) {
3175 // 0.0 is the only immediate that can be encoded directly in
3176 // an FCMP instruction.
3177 //
3178 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3179 // specify that in a floating-point comparison, positive zero
3180 // and negative zero are considered equal, so we can use the
3181 // literal 0.0 for both cases here.
3182 //
3183 // Note however that some methods (Float.equal, Float.compare,
3184 // Float.compareTo, Double.equal, Double.compare,
3185 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3186 // StrictMath.min) consider 0.0 to be (strictly) greater than
3187 // -0.0. So if we ever translate calls to these methods into a
3188 // HCompare instruction, we must handle the -0.0 case with
3189 // care here.
3190 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3191 __ Fcmp(lhs_reg, 0.0);
3192 } else {
3193 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3194 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003195}
3196
Serban Constantinescu02164b32014-11-13 14:05:07 +00003197void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003198 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003199 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3200 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003201 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003202 case Primitive::kPrimBoolean:
3203 case Primitive::kPrimByte:
3204 case Primitive::kPrimShort:
3205 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003206 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003207 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003208 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003209 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003210 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3211 break;
3212 }
3213 case Primitive::kPrimFloat:
3214 case Primitive::kPrimDouble: {
3215 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003216 locations->SetInAt(1,
3217 IsFloatingPointZeroConstant(compare->InputAt(1))
3218 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3219 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003220 locations->SetOut(Location::RequiresRegister());
3221 break;
3222 }
3223 default:
3224 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3225 }
3226}
3227
3228void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3229 Primitive::Type in_type = compare->InputAt(0)->GetType();
3230
3231 // 0 if: left == right
3232 // 1 if: left > right
3233 // -1 if: left < right
3234 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003235 case Primitive::kPrimBoolean:
3236 case Primitive::kPrimByte:
3237 case Primitive::kPrimShort:
3238 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003239 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003240 case Primitive::kPrimLong: {
3241 Register result = OutputRegister(compare);
3242 Register left = InputRegisterAt(compare, 0);
3243 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003244 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003245 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3246 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003247 break;
3248 }
3249 case Primitive::kPrimFloat:
3250 case Primitive::kPrimDouble: {
3251 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003252 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003253 __ Cset(result, ne);
3254 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003255 break;
3256 }
3257 default:
3258 LOG(FATAL) << "Unimplemented compare type " << in_type;
3259 }
3260}
3261
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003262void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003263 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003264
3265 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3266 locations->SetInAt(0, Location::RequiresFpuRegister());
3267 locations->SetInAt(1,
3268 IsFloatingPointZeroConstant(instruction->InputAt(1))
3269 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3270 : Location::RequiresFpuRegister());
3271 } else {
3272 // Integer cases.
3273 locations->SetInAt(0, Location::RequiresRegister());
3274 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3275 }
3276
David Brazdilb3e773e2016-01-26 11:28:37 +00003277 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003278 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003279 }
3280}
3281
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003282void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003283 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003284 return;
3285 }
3286
3287 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003288 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003289 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003290
Roland Levillain7f63c522015-07-13 15:54:55 +00003291 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003292 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003293 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003294 } else {
3295 // Integer cases.
3296 Register lhs = InputRegisterAt(instruction, 0);
3297 Operand rhs = InputOperandAt(instruction, 1);
3298 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003299 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003300 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003301}
3302
3303#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3304 M(Equal) \
3305 M(NotEqual) \
3306 M(LessThan) \
3307 M(LessThanOrEqual) \
3308 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003309 M(GreaterThanOrEqual) \
3310 M(Below) \
3311 M(BelowOrEqual) \
3312 M(Above) \
3313 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003314#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003315void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3316void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003317FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003318#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003319#undef FOR_EACH_CONDITION_INSTRUCTION
3320
Zheng Xuc6667102015-05-15 16:08:45 +08003321void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3322 DCHECK(instruction->IsDiv() || instruction->IsRem());
3323
3324 LocationSummary* locations = instruction->GetLocations();
3325 Location second = locations->InAt(1);
3326 DCHECK(second.IsConstant());
3327
3328 Register out = OutputRegister(instruction);
3329 Register dividend = InputRegisterAt(instruction, 0);
3330 int64_t imm = Int64FromConstant(second.GetConstant());
3331 DCHECK(imm == 1 || imm == -1);
3332
3333 if (instruction->IsRem()) {
3334 __ Mov(out, 0);
3335 } else {
3336 if (imm == 1) {
3337 __ Mov(out, dividend);
3338 } else {
3339 __ Neg(out, dividend);
3340 }
3341 }
3342}
3343
3344void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3345 DCHECK(instruction->IsDiv() || instruction->IsRem());
3346
3347 LocationSummary* locations = instruction->GetLocations();
3348 Location second = locations->InAt(1);
3349 DCHECK(second.IsConstant());
3350
3351 Register out = OutputRegister(instruction);
3352 Register dividend = InputRegisterAt(instruction, 0);
3353 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003354 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003355 int ctz_imm = CTZ(abs_imm);
3356
3357 UseScratchRegisterScope temps(GetVIXLAssembler());
3358 Register temp = temps.AcquireSameSizeAs(out);
3359
3360 if (instruction->IsDiv()) {
3361 __ Add(temp, dividend, abs_imm - 1);
3362 __ Cmp(dividend, 0);
3363 __ Csel(out, temp, dividend, lt);
3364 if (imm > 0) {
3365 __ Asr(out, out, ctz_imm);
3366 } else {
3367 __ Neg(out, Operand(out, ASR, ctz_imm));
3368 }
3369 } else {
3370 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3371 __ Asr(temp, dividend, bits - 1);
3372 __ Lsr(temp, temp, bits - ctz_imm);
3373 __ Add(out, dividend, temp);
3374 __ And(out, out, abs_imm - 1);
3375 __ Sub(out, out, temp);
3376 }
3377}
3378
3379void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3380 DCHECK(instruction->IsDiv() || instruction->IsRem());
3381
3382 LocationSummary* locations = instruction->GetLocations();
3383 Location second = locations->InAt(1);
3384 DCHECK(second.IsConstant());
3385
3386 Register out = OutputRegister(instruction);
3387 Register dividend = InputRegisterAt(instruction, 0);
3388 int64_t imm = Int64FromConstant(second.GetConstant());
3389
3390 Primitive::Type type = instruction->GetResultType();
3391 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3392
3393 int64_t magic;
3394 int shift;
3395 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3396
3397 UseScratchRegisterScope temps(GetVIXLAssembler());
3398 Register temp = temps.AcquireSameSizeAs(out);
3399
3400 // temp = get_high(dividend * magic)
3401 __ Mov(temp, magic);
3402 if (type == Primitive::kPrimLong) {
3403 __ Smulh(temp, dividend, temp);
3404 } else {
3405 __ Smull(temp.X(), dividend, temp);
3406 __ Lsr(temp.X(), temp.X(), 32);
3407 }
3408
3409 if (imm > 0 && magic < 0) {
3410 __ Add(temp, temp, dividend);
3411 } else if (imm < 0 && magic > 0) {
3412 __ Sub(temp, temp, dividend);
3413 }
3414
3415 if (shift != 0) {
3416 __ Asr(temp, temp, shift);
3417 }
3418
3419 if (instruction->IsDiv()) {
3420 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3421 } else {
3422 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3423 // TODO: Strength reduction for msub.
3424 Register temp_imm = temps.AcquireSameSizeAs(out);
3425 __ Mov(temp_imm, imm);
3426 __ Msub(out, temp, temp_imm, dividend);
3427 }
3428}
3429
3430void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3431 DCHECK(instruction->IsDiv() || instruction->IsRem());
3432 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003433 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003434
3435 LocationSummary* locations = instruction->GetLocations();
3436 Register out = OutputRegister(instruction);
3437 Location second = locations->InAt(1);
3438
3439 if (second.IsConstant()) {
3440 int64_t imm = Int64FromConstant(second.GetConstant());
3441
3442 if (imm == 0) {
3443 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3444 } else if (imm == 1 || imm == -1) {
3445 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003446 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003447 DivRemByPowerOfTwo(instruction);
3448 } else {
3449 DCHECK(imm <= -2 || imm >= 2);
3450 GenerateDivRemWithAnyConstant(instruction);
3451 }
3452 } else {
3453 Register dividend = InputRegisterAt(instruction, 0);
3454 Register divisor = InputRegisterAt(instruction, 1);
3455 if (instruction->IsDiv()) {
3456 __ Sdiv(out, dividend, divisor);
3457 } else {
3458 UseScratchRegisterScope temps(GetVIXLAssembler());
3459 Register temp = temps.AcquireSameSizeAs(out);
3460 __ Sdiv(temp, dividend, divisor);
3461 __ Msub(out, temp, divisor, dividend);
3462 }
3463 }
3464}
3465
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003466void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3467 LocationSummary* locations =
3468 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3469 switch (div->GetResultType()) {
3470 case Primitive::kPrimInt:
3471 case Primitive::kPrimLong:
3472 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003473 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003474 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3475 break;
3476
3477 case Primitive::kPrimFloat:
3478 case Primitive::kPrimDouble:
3479 locations->SetInAt(0, Location::RequiresFpuRegister());
3480 locations->SetInAt(1, Location::RequiresFpuRegister());
3481 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3482 break;
3483
3484 default:
3485 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3486 }
3487}
3488
3489void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3490 Primitive::Type type = div->GetResultType();
3491 switch (type) {
3492 case Primitive::kPrimInt:
3493 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003494 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003495 break;
3496
3497 case Primitive::kPrimFloat:
3498 case Primitive::kPrimDouble:
3499 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3500 break;
3501
3502 default:
3503 LOG(FATAL) << "Unexpected div type " << type;
3504 }
3505}
3506
Alexandre Rames67555f72014-11-18 10:55:16 +00003507void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003508 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003509 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003510}
3511
3512void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3513 SlowPathCodeARM64* slow_path =
3514 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3515 codegen_->AddSlowPath(slow_path);
3516 Location value = instruction->GetLocations()->InAt(0);
3517
Alexandre Rames3e69f162014-12-10 10:36:50 +00003518 Primitive::Type type = instruction->GetType();
3519
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003520 if (!Primitive::IsIntegralType(type)) {
3521 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003522 return;
3523 }
3524
Alexandre Rames67555f72014-11-18 10:55:16 +00003525 if (value.IsConstant()) {
3526 int64_t divisor = Int64ConstantFrom(value);
3527 if (divisor == 0) {
3528 __ B(slow_path->GetEntryLabel());
3529 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003530 // A division by a non-null constant is valid. We don't need to perform
3531 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003532 }
3533 } else {
3534 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3535 }
3536}
3537
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003538void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3539 LocationSummary* locations =
3540 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3541 locations->SetOut(Location::ConstantLocation(constant));
3542}
3543
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003544void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3545 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003546 // Will be generated at use site.
3547}
3548
Alexandre Rames5319def2014-10-23 10:03:10 +01003549void LocationsBuilderARM64::VisitExit(HExit* exit) {
3550 exit->SetLocations(nullptr);
3551}
3552
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003553void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003554}
3555
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003556void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3557 LocationSummary* locations =
3558 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3559 locations->SetOut(Location::ConstantLocation(constant));
3560}
3561
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003562void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003563 // Will be generated at use site.
3564}
3565
David Brazdilfc6a86a2015-06-26 10:33:45 +00003566void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003567 DCHECK(!successor->IsExitBlock());
3568 HBasicBlock* block = got->GetBlock();
3569 HInstruction* previous = got->GetPrevious();
3570 HLoopInformation* info = block->GetLoopInformation();
3571
David Brazdil46e2a392015-03-16 17:31:52 +00003572 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003573 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3574 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3575 return;
3576 }
3577 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3578 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3579 }
3580 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003581 __ B(codegen_->GetLabelOf(successor));
3582 }
3583}
3584
David Brazdilfc6a86a2015-06-26 10:33:45 +00003585void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3586 got->SetLocations(nullptr);
3587}
3588
3589void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3590 HandleGoto(got, got->GetSuccessor());
3591}
3592
3593void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3594 try_boundary->SetLocations(nullptr);
3595}
3596
3597void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3598 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3599 if (!successor->IsExitBlock()) {
3600 HandleGoto(try_boundary, successor);
3601 }
3602}
3603
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003604void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003605 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003606 vixl::aarch64::Label* true_target,
3607 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003608 // FP branching requires both targets to be explicit. If either of the targets
3609 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003610 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003611 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003612
David Brazdil0debae72015-11-12 18:37:00 +00003613 if (true_target == nullptr && false_target == nullptr) {
3614 // Nothing to do. The code always falls through.
3615 return;
3616 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003617 // Constant condition, statically compared against "true" (integer value 1).
3618 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003619 if (true_target != nullptr) {
3620 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003621 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003622 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003623 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003624 if (false_target != nullptr) {
3625 __ B(false_target);
3626 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003627 }
David Brazdil0debae72015-11-12 18:37:00 +00003628 return;
3629 }
3630
3631 // The following code generates these patterns:
3632 // (1) true_target == nullptr && false_target != nullptr
3633 // - opposite condition true => branch to false_target
3634 // (2) true_target != nullptr && false_target == nullptr
3635 // - condition true => branch to true_target
3636 // (3) true_target != nullptr && false_target != nullptr
3637 // - condition true => branch to true_target
3638 // - branch to false_target
3639 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003640 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003641 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003642 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003643 if (true_target == nullptr) {
3644 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3645 } else {
3646 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3647 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003648 } else {
3649 // The condition instruction has not been materialized, use its inputs as
3650 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003651 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003652
David Brazdil0debae72015-11-12 18:37:00 +00003653 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003654 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003655 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003656 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003657 IfCondition opposite_condition = condition->GetOppositeCondition();
3658 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003659 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003660 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003661 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003662 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003663 // Integer cases.
3664 Register lhs = InputRegisterAt(condition, 0);
3665 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003666
3667 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003668 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003669 if (true_target == nullptr) {
3670 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3671 non_fallthrough_target = false_target;
3672 } else {
3673 arm64_cond = ARM64Condition(condition->GetCondition());
3674 non_fallthrough_target = true_target;
3675 }
3676
Aart Bik086d27e2016-01-20 17:02:00 -08003677 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003678 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003679 switch (arm64_cond) {
3680 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003681 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003682 break;
3683 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003684 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003685 break;
3686 case lt:
3687 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003688 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003689 break;
3690 case ge:
3691 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003692 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003693 break;
3694 default:
3695 // Without the `static_cast` the compiler throws an error for
3696 // `-Werror=sign-promo`.
3697 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3698 }
3699 } else {
3700 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003701 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003702 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003703 }
3704 }
David Brazdil0debae72015-11-12 18:37:00 +00003705
3706 // If neither branch falls through (case 3), the conditional branch to `true_target`
3707 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3708 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003709 __ B(false_target);
3710 }
David Brazdil0debae72015-11-12 18:37:00 +00003711
3712 if (fallthrough_target.IsLinked()) {
3713 __ Bind(&fallthrough_target);
3714 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003715}
3716
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003717void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3718 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003719 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003720 locations->SetInAt(0, Location::RequiresRegister());
3721 }
3722}
3723
3724void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003725 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3726 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003727 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3728 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3729 true_target = nullptr;
3730 }
3731 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3732 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3733 false_target = nullptr;
3734 }
David Brazdil0debae72015-11-12 18:37:00 +00003735 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003736}
3737
3738void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3739 LocationSummary* locations = new (GetGraph()->GetArena())
3740 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003741 InvokeRuntimeCallingConvention calling_convention;
3742 RegisterSet caller_saves = RegisterSet::Empty();
3743 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3744 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003745 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003746 locations->SetInAt(0, Location::RequiresRegister());
3747 }
3748}
3749
3750void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003751 SlowPathCodeARM64* slow_path =
3752 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003753 GenerateTestAndBranch(deoptimize,
3754 /* condition_input_index */ 0,
3755 slow_path->GetEntryLabel(),
3756 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003757}
3758
Mingyao Yang063fc772016-08-02 11:02:54 -07003759void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3760 LocationSummary* locations = new (GetGraph()->GetArena())
3761 LocationSummary(flag, LocationSummary::kNoCall);
3762 locations->SetOut(Location::RequiresRegister());
3763}
3764
3765void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3766 __ Ldr(OutputRegister(flag),
3767 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3768}
3769
David Brazdilc0b601b2016-02-08 14:20:45 +00003770static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3771 return condition->IsCondition() &&
3772 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3773}
3774
Alexandre Rames880f1192016-06-13 16:04:50 +01003775static inline Condition GetConditionForSelect(HCondition* condition) {
3776 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003777 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3778 : ARM64Condition(cond);
3779}
3780
David Brazdil74eb1b22015-12-14 11:44:01 +00003781void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3782 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003783 if (Primitive::IsFloatingPointType(select->GetType())) {
3784 locations->SetInAt(0, Location::RequiresFpuRegister());
3785 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003786 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003787 } else {
3788 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3789 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3790 bool is_true_value_constant = cst_true_value != nullptr;
3791 bool is_false_value_constant = cst_false_value != nullptr;
3792 // Ask VIXL whether we should synthesize constants in registers.
3793 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3794 Operand true_op = is_true_value_constant ?
3795 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3796 Operand false_op = is_false_value_constant ?
3797 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3798 bool true_value_in_register = false;
3799 bool false_value_in_register = false;
3800 MacroAssembler::GetCselSynthesisInformation(
3801 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3802 true_value_in_register |= !is_true_value_constant;
3803 false_value_in_register |= !is_false_value_constant;
3804
3805 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3806 : Location::ConstantLocation(cst_true_value));
3807 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3808 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003809 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003810 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003811
David Brazdil74eb1b22015-12-14 11:44:01 +00003812 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3813 locations->SetInAt(2, Location::RequiresRegister());
3814 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003815}
3816
3817void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003818 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003819 Condition csel_cond;
3820
3821 if (IsBooleanValueOrMaterializedCondition(cond)) {
3822 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003823 // Use the condition flags set by the previous instruction.
3824 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003825 } else {
3826 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003827 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003828 }
3829 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003830 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003831 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003832 } else {
3833 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003834 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003835 }
3836
Alexandre Rames880f1192016-06-13 16:04:50 +01003837 if (Primitive::IsFloatingPointType(select->GetType())) {
3838 __ Fcsel(OutputFPRegister(select),
3839 InputFPRegisterAt(select, 1),
3840 InputFPRegisterAt(select, 0),
3841 csel_cond);
3842 } else {
3843 __ Csel(OutputRegister(select),
3844 InputOperandAt(select, 1),
3845 InputOperandAt(select, 0),
3846 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003847 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003848}
3849
David Srbecky0cf44932015-12-09 14:09:59 +00003850void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3851 new (GetGraph()->GetArena()) LocationSummary(info);
3852}
3853
David Srbeckyd28f4a02016-03-14 17:14:24 +00003854void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3855 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003856}
3857
3858void CodeGeneratorARM64::GenerateNop() {
3859 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003860}
3861
Alexandre Rames5319def2014-10-23 10:03:10 +01003862void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003863 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003864}
3865
3866void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003867 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003868}
3869
3870void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003871 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003872}
3873
3874void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003875 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003876}
3877
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003878// Temp is used for read barrier.
3879static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3880 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003881 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003882 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3883 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3884 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3885 return 1;
3886 }
3887 return 0;
3888}
3889
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003890// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003891// interface pointer, one for loading the current interface.
3892// The other checks have one temp for loading the object's class.
3893static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3894 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3895 return 3;
3896 }
3897 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003898}
3899
Alexandre Rames67555f72014-11-18 10:55:16 +00003900void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003901 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003902 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003903 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003904 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003905 case TypeCheckKind::kExactCheck:
3906 case TypeCheckKind::kAbstractClassCheck:
3907 case TypeCheckKind::kClassHierarchyCheck:
3908 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003909 call_kind =
3910 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003911 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003912 break;
3913 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003914 case TypeCheckKind::kUnresolvedCheck:
3915 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003916 call_kind = LocationSummary::kCallOnSlowPath;
3917 break;
3918 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003919
Alexandre Rames67555f72014-11-18 10:55:16 +00003920 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003921 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003922 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003923 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003924 locations->SetInAt(0, Location::RequiresRegister());
3925 locations->SetInAt(1, Location::RequiresRegister());
3926 // The "out" register is used as a temporary, so it overlaps with the inputs.
3927 // Note that TypeCheckSlowPathARM64 uses this register too.
3928 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003929 // Add temps if necessary for read barriers.
3930 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003931}
3932
3933void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003934 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003935 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003936 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003937 Register obj = InputRegisterAt(instruction, 0);
3938 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003939 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003940 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003941 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3942 DCHECK_LE(num_temps, 1u);
3943 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003944 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3945 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3946 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3947 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003948
Scott Wakeling97c72b72016-06-24 16:19:36 +01003949 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003950 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003951
3952 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003953 // Avoid null check if we know `obj` is not null.
3954 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003955 __ Cbz(obj, &zero);
3956 }
3957
Roland Levillain44015862016-01-22 11:47:17 +00003958 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003959 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003960 // /* HeapReference<Class> */ out = obj->klass_
3961 GenerateReferenceLoadTwoRegisters(instruction,
3962 out_loc,
3963 obj_loc,
3964 class_offset,
3965 maybe_temp_loc,
3966 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003967 __ Cmp(out, cls);
3968 __ Cset(out, eq);
3969 if (zero.IsLinked()) {
3970 __ B(&done);
3971 }
3972 break;
3973 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003974
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003975 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003976 // /* HeapReference<Class> */ out = obj->klass_
3977 GenerateReferenceLoadTwoRegisters(instruction,
3978 out_loc,
3979 obj_loc,
3980 class_offset,
3981 maybe_temp_loc,
3982 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003983 // If the class is abstract, we eagerly fetch the super class of the
3984 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003985 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003986 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003987 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003988 GenerateReferenceLoadOneRegister(instruction,
3989 out_loc,
3990 super_offset,
3991 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003992 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003993 // If `out` is null, we use it for the result, and jump to `done`.
3994 __ Cbz(out, &done);
3995 __ Cmp(out, cls);
3996 __ B(ne, &loop);
3997 __ Mov(out, 1);
3998 if (zero.IsLinked()) {
3999 __ B(&done);
4000 }
4001 break;
4002 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004003
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004004 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004005 // /* HeapReference<Class> */ out = obj->klass_
4006 GenerateReferenceLoadTwoRegisters(instruction,
4007 out_loc,
4008 obj_loc,
4009 class_offset,
4010 maybe_temp_loc,
4011 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004012 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004013 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004014 __ Bind(&loop);
4015 __ Cmp(out, cls);
4016 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004017 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004018 GenerateReferenceLoadOneRegister(instruction,
4019 out_loc,
4020 super_offset,
4021 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004022 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004023 __ Cbnz(out, &loop);
4024 // If `out` is null, we use it for the result, and jump to `done`.
4025 __ B(&done);
4026 __ Bind(&success);
4027 __ Mov(out, 1);
4028 if (zero.IsLinked()) {
4029 __ B(&done);
4030 }
4031 break;
4032 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004033
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004034 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004035 // /* HeapReference<Class> */ out = obj->klass_
4036 GenerateReferenceLoadTwoRegisters(instruction,
4037 out_loc,
4038 obj_loc,
4039 class_offset,
4040 maybe_temp_loc,
4041 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004042 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004043 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004044 __ Cmp(out, cls);
4045 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004046 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004047 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004048 GenerateReferenceLoadOneRegister(instruction,
4049 out_loc,
4050 component_offset,
4051 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004052 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004053 // If `out` is null, we use it for the result, and jump to `done`.
4054 __ Cbz(out, &done);
4055 __ Ldrh(out, HeapOperand(out, primitive_offset));
4056 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4057 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004058 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004059 __ Mov(out, 1);
4060 __ B(&done);
4061 break;
4062 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004063
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004064 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004065 // No read barrier since the slow path will retry upon failure.
4066 // /* HeapReference<Class> */ out = obj->klass_
4067 GenerateReferenceLoadTwoRegisters(instruction,
4068 out_loc,
4069 obj_loc,
4070 class_offset,
4071 maybe_temp_loc,
4072 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004073 __ Cmp(out, cls);
4074 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004075 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4076 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004077 codegen_->AddSlowPath(slow_path);
4078 __ B(ne, slow_path->GetEntryLabel());
4079 __ Mov(out, 1);
4080 if (zero.IsLinked()) {
4081 __ B(&done);
4082 }
4083 break;
4084 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004085
Calin Juravle98893e12015-10-02 21:05:03 +01004086 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004087 case TypeCheckKind::kInterfaceCheck: {
4088 // Note that we indeed only call on slow path, but we always go
4089 // into the slow path for the unresolved and interface check
4090 // cases.
4091 //
4092 // We cannot directly call the InstanceofNonTrivial runtime
4093 // entry point without resorting to a type checking slow path
4094 // here (i.e. by calling InvokeRuntime directly), as it would
4095 // require to assign fixed registers for the inputs of this
4096 // HInstanceOf instruction (following the runtime calling
4097 // convention), which might be cluttered by the potential first
4098 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004099 //
4100 // TODO: Introduce a new runtime entry point taking the object
4101 // to test (instead of its class) as argument, and let it deal
4102 // with the read barrier issues. This will let us refactor this
4103 // case of the `switch` code as it was previously (with a direct
4104 // call to the runtime not using a type checking slow path).
4105 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004106 DCHECK(locations->OnlyCallsOnSlowPath());
4107 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4108 /* is_fatal */ false);
4109 codegen_->AddSlowPath(slow_path);
4110 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004111 if (zero.IsLinked()) {
4112 __ B(&done);
4113 }
4114 break;
4115 }
4116 }
4117
4118 if (zero.IsLinked()) {
4119 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004120 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004121 }
4122
4123 if (done.IsLinked()) {
4124 __ Bind(&done);
4125 }
4126
4127 if (slow_path != nullptr) {
4128 __ Bind(slow_path->GetExitLabel());
4129 }
4130}
4131
4132void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4133 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4134 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4135
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004136 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4137 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004138 case TypeCheckKind::kExactCheck:
4139 case TypeCheckKind::kAbstractClassCheck:
4140 case TypeCheckKind::kClassHierarchyCheck:
4141 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004142 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4143 LocationSummary::kCallOnSlowPath :
4144 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004145 break;
4146 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004147 case TypeCheckKind::kUnresolvedCheck:
4148 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004149 call_kind = LocationSummary::kCallOnSlowPath;
4150 break;
4151 }
4152
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004153 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4154 locations->SetInAt(0, Location::RequiresRegister());
4155 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004156 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4157 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004158}
4159
4160void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004161 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004162 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004163 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004164 Register obj = InputRegisterAt(instruction, 0);
4165 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004166 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4167 DCHECK_GE(num_temps, 1u);
4168 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004169 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004170 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4171 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004172 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004173 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4174 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4175 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4176 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4177 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4178 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4179 const uint32_t object_array_data_offset =
4180 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004181
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004182 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004183 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4184 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4185 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004186 if (!kEmitCompilerReadBarrier) {
4187 is_type_check_slow_path_fatal =
4188 (type_check_kind == TypeCheckKind::kExactCheck ||
4189 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4190 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4191 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4192 !instruction->CanThrowIntoCatchBlock();
4193 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004194 SlowPathCodeARM64* type_check_slow_path =
4195 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4196 is_type_check_slow_path_fatal);
4197 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004198
Scott Wakeling97c72b72016-06-24 16:19:36 +01004199 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004200 // Avoid null check if we know obj is not null.
4201 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004202 __ Cbz(obj, &done);
4203 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004204
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004205 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004206 case TypeCheckKind::kExactCheck:
4207 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004208 // /* HeapReference<Class> */ temp = obj->klass_
4209 GenerateReferenceLoadTwoRegisters(instruction,
4210 temp_loc,
4211 obj_loc,
4212 class_offset,
4213 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004214 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004215
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004216 __ Cmp(temp, cls);
4217 // Jump to slow path for throwing the exception or doing a
4218 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004219 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004220 break;
4221 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004222
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004223 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004224 // /* HeapReference<Class> */ temp = obj->klass_
4225 GenerateReferenceLoadTwoRegisters(instruction,
4226 temp_loc,
4227 obj_loc,
4228 class_offset,
4229 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004230 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004231
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004232 // If the class is abstract, we eagerly fetch the super class of the
4233 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004234 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004235 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004236 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004237 GenerateReferenceLoadOneRegister(instruction,
4238 temp_loc,
4239 super_offset,
4240 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004241 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004242
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004243 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4244 // exception.
4245 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4246 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004247 __ Cmp(temp, cls);
4248 __ B(ne, &loop);
4249 break;
4250 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004251
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004252 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004253 // /* HeapReference<Class> */ temp = obj->klass_
4254 GenerateReferenceLoadTwoRegisters(instruction,
4255 temp_loc,
4256 obj_loc,
4257 class_offset,
4258 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004259 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004260
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004261 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004262 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004263 __ Bind(&loop);
4264 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004265 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004266
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004267 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004268 GenerateReferenceLoadOneRegister(instruction,
4269 temp_loc,
4270 super_offset,
4271 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004272 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004273
4274 // If the class reference currently in `temp` is not null, jump
4275 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004276 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004277 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004278 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004279 break;
4280 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004281
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004282 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004283 // /* HeapReference<Class> */ temp = obj->klass_
4284 GenerateReferenceLoadTwoRegisters(instruction,
4285 temp_loc,
4286 obj_loc,
4287 class_offset,
4288 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004289 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004290
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004291 // Do an exact check.
4292 __ Cmp(temp, cls);
4293 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004294
4295 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004296 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004297 GenerateReferenceLoadOneRegister(instruction,
4298 temp_loc,
4299 component_offset,
4300 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004301 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004302
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004303 // If the component type is null, jump to the slow path to throw the exception.
4304 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4305 // Otherwise, the object is indeed an array. Further check that this component type is not a
4306 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004307 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4308 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004309 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004310 break;
4311 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004312
Calin Juravle98893e12015-10-02 21:05:03 +01004313 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004314 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004315 //
4316 // We cannot directly call the CheckCast runtime entry point
4317 // without resorting to a type checking slow path here (i.e. by
4318 // calling InvokeRuntime directly), as it would require to
4319 // assign fixed registers for the inputs of this HInstanceOf
4320 // instruction (following the runtime calling convention), which
4321 // might be cluttered by the potential first read barrier
4322 // emission at the beginning of this method.
4323 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004324 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004325 case TypeCheckKind::kInterfaceCheck: {
4326 // /* HeapReference<Class> */ temp = obj->klass_
4327 GenerateReferenceLoadTwoRegisters(instruction,
4328 temp_loc,
4329 obj_loc,
4330 class_offset,
4331 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004332 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004333
4334 // /* HeapReference<Class> */ temp = temp->iftable_
4335 GenerateReferenceLoadTwoRegisters(instruction,
4336 temp_loc,
4337 temp_loc,
4338 iftable_offset,
4339 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004340 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004341 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004342 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004343 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004344 vixl::aarch64::Label start_loop;
4345 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004346 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004347 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4348 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004349 // Go to next interface.
4350 __ Add(temp, temp, 2 * kHeapReferenceSize);
4351 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004352 // Compare the classes and continue the loop if they do not match.
4353 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4354 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004355 break;
4356 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004357 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004358 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004359
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004360 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004361}
4362
Alexandre Rames5319def2014-10-23 10:03:10 +01004363void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4364 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4365 locations->SetOut(Location::ConstantLocation(constant));
4366}
4367
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004368void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004369 // Will be generated at use site.
4370}
4371
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004372void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4373 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4374 locations->SetOut(Location::ConstantLocation(constant));
4375}
4376
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004377void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004378 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004379}
4380
Calin Juravle175dc732015-08-25 15:42:32 +01004381void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4382 // The trampoline uses the same calling convention as dex calling conventions,
4383 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4384 // the method_idx.
4385 HandleInvoke(invoke);
4386}
4387
4388void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4389 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4390}
4391
Alexandre Rames5319def2014-10-23 10:03:10 +01004392void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004393 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004394 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004395}
4396
Alexandre Rames67555f72014-11-18 10:55:16 +00004397void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4398 HandleInvoke(invoke);
4399}
4400
4401void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4402 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004403 LocationSummary* locations = invoke->GetLocations();
4404 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004405 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004406 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004407 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004408
4409 // The register ip1 is required to be used for the hidden argument in
4410 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004411 MacroAssembler* masm = GetVIXLAssembler();
4412 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004413 scratch_scope.Exclude(ip1);
4414 __ Mov(ip1, invoke->GetDexMethodIndex());
4415
Artem Serov914d7a82017-02-07 14:33:49 +00004416 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004417 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004418 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004419 {
4420 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4421 // /* HeapReference<Class> */ temp = temp->klass_
4422 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4423 codegen_->MaybeRecordImplicitNullCheck(invoke);
4424 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004425 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004426 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004427 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004428 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004429 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004430 }
Artem Serov914d7a82017-02-07 14:33:49 +00004431
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004432 // Instead of simply (possibly) unpoisoning `temp` here, we should
4433 // emit a read barrier for the previous class reference load.
4434 // However this is not required in practice, as this is an
4435 // intermediate/temporary reference and because the current
4436 // concurrent copying collector keeps the from-space memory
4437 // intact/accessible until the end of the marking phase (the
4438 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004439 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004440 __ Ldr(temp,
4441 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4442 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004443 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004444 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004445 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004446 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004447 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004448
4449 {
4450 // Ensure the pc position is recorded immediately after the `blr` instruction.
4451 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4452
4453 // lr();
4454 __ blr(lr);
4455 DCHECK(!codegen_->IsLeafMethod());
4456 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4457 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004458}
4459
4460void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004461 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004462 if (intrinsic.TryDispatch(invoke)) {
4463 return;
4464 }
4465
Alexandre Rames67555f72014-11-18 10:55:16 +00004466 HandleInvoke(invoke);
4467}
4468
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004469void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004470 // Explicit clinit checks triggered by static invokes must have been pruned by
4471 // art::PrepareForRegisterAllocation.
4472 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004473
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004474 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004475 if (intrinsic.TryDispatch(invoke)) {
4476 return;
4477 }
4478
Alexandre Rames67555f72014-11-18 10:55:16 +00004479 HandleInvoke(invoke);
4480}
4481
Andreas Gampe878d58c2015-01-15 23:24:00 -08004482static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4483 if (invoke->GetLocations()->Intrinsified()) {
4484 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4485 intrinsic.Dispatch(invoke);
4486 return true;
4487 }
4488 return false;
4489}
4490
Vladimir Markodc151b22015-10-15 18:02:30 +01004491HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4492 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004493 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004494 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004495 return desired_dispatch_info;
4496}
4497
TatWai Chongd8c052a2016-11-02 16:12:48 +08004498Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4499 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004500 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004501 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4502 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004503 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4504 uint32_t offset =
4505 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004506 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004507 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004508 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004509 }
Vladimir Marko58155012015-08-19 12:49:41 +00004510 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004511 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004512 break;
4513 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4514 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004515 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004516 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004517 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4518 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004519 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004520 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004521 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004522 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004523 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004524 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004525 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004526 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004527 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004528 }
Vladimir Marko58155012015-08-19 12:49:41 +00004529 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004530 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004531 Register reg = XRegisterFrom(temp);
4532 Register method_reg;
4533 if (current_method.IsRegister()) {
4534 method_reg = XRegisterFrom(current_method);
4535 } else {
4536 DCHECK(invoke->GetLocations()->Intrinsified());
4537 DCHECK(!current_method.IsValid());
4538 method_reg = reg;
4539 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4540 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004541
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004542 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004543 __ Ldr(reg.X(),
4544 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004545 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004546 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004547 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4548 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004549 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4550 break;
4551 }
4552 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004553 return callee_method;
4554}
4555
4556void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4557 // All registers are assumed to be correctly set up.
4558 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004559
4560 switch (invoke->GetCodePtrLocation()) {
4561 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4562 __ Bl(&frame_entry_label_);
4563 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004564 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4565 // LR = callee_method->entry_point_from_quick_compiled_code_;
4566 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004567 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004568 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004569 {
4570 // To ensure that the pc position is recorded immediately after the `blr` instruction
4571 // BLR must be the last instruction emitted in this function.
4572 // Recording the pc will occur right after returning from this function.
4573 ExactAssemblyScope eas(GetVIXLAssembler(),
4574 kInstructionSize,
4575 CodeBufferCheckScope::kExactSize);
4576 // lr()
4577 __ blr(lr);
4578 }
Vladimir Marko58155012015-08-19 12:49:41 +00004579 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004580 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004581
Andreas Gampe878d58c2015-01-15 23:24:00 -08004582 DCHECK(!IsLeafMethod());
4583}
4584
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004585void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004586 // Use the calling convention instead of the location of the receiver, as
4587 // intrinsics may have put the receiver in a different register. In the intrinsics
4588 // slow path, the arguments have been moved to the right place, so here we are
4589 // guaranteed that the receiver is the first register of the calling convention.
4590 InvokeDexCallingConvention calling_convention;
4591 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004592 Register temp = XRegisterFrom(temp_in);
4593 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4594 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4595 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004596 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004597
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004598 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004599
4600 {
4601 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4602 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4603 // /* HeapReference<Class> */ temp = receiver->klass_
4604 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4605 MaybeRecordImplicitNullCheck(invoke);
4606 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004607 // Instead of simply (possibly) unpoisoning `temp` here, we should
4608 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004609 // intermediate/temporary reference and because the current
4610 // concurrent copying collector keeps the from-space memory
4611 // intact/accessible until the end of the marking phase (the
4612 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004613 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4614 // temp = temp->GetMethodAt(method_offset);
4615 __ Ldr(temp, MemOperand(temp, method_offset));
4616 // lr = temp->GetEntryPoint();
4617 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004618 {
4619 // To ensure that the pc position is recorded immediately after the `blr` instruction
4620 // BLR should be the last instruction emitted in this function.
4621 // Recording the pc will occur right after returning from this function.
4622 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4623 // lr();
4624 __ blr(lr);
4625 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004626}
4627
Orion Hodsonac141392017-01-13 11:53:47 +00004628void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4629 HandleInvoke(invoke);
4630}
4631
4632void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4633 codegen_->GenerateInvokePolymorphicCall(invoke);
4634}
4635
Scott Wakeling97c72b72016-06-24 16:19:36 +01004636vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4637 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004638 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004639 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004640 return
4641 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004642}
4643
Scott Wakeling97c72b72016-06-24 16:19:36 +01004644vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4645 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004646 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004647 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004648 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004649}
4650
Vladimir Marko1998cd02017-01-13 13:02:58 +00004651vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4652 const DexFile& dex_file,
4653 dex::TypeIndex type_index,
4654 vixl::aarch64::Label* adrp_label) {
4655 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4656}
4657
Scott Wakeling97c72b72016-06-24 16:19:36 +01004658vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4659 const DexFile& dex_file,
4660 uint32_t element_offset,
4661 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004662 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4663}
4664
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004665vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4666 baker_read_barrier_patches_.emplace_back(custom_data);
4667 return &baker_read_barrier_patches_.back().label;
4668}
4669
Scott Wakeling97c72b72016-06-24 16:19:36 +01004670vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4671 const DexFile& dex_file,
4672 uint32_t offset_or_index,
4673 vixl::aarch64::Label* adrp_label,
4674 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004675 // Add a patch entry and return the label.
4676 patches->emplace_back(dex_file, offset_or_index);
4677 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004678 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004679 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4680 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4681 return label;
4682}
4683
Scott Wakeling97c72b72016-06-24 16:19:36 +01004684vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4685 uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00004686 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004687}
4688
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004689vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004690 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4691 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4692 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004693 return jit_string_patches_.GetOrCreate(
4694 StringReference(&dex_file, string_index),
4695 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4696}
4697
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004698vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004699 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4700 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4701 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004702 return jit_class_patches_.GetOrCreate(
4703 TypeReference(&dex_file, type_index),
4704 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4705}
4706
Vladimir Markoaad75c62016-10-03 08:46:48 +00004707void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4708 vixl::aarch64::Register reg) {
4709 DCHECK(reg.IsX());
4710 SingleEmissionCheckScope guard(GetVIXLAssembler());
4711 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004712 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004713}
4714
4715void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4716 vixl::aarch64::Register out,
4717 vixl::aarch64::Register base) {
4718 DCHECK(out.IsX());
4719 DCHECK(base.IsX());
4720 SingleEmissionCheckScope guard(GetVIXLAssembler());
4721 __ Bind(fixup_label);
4722 __ add(out, base, Operand(/* offset placeholder */ 0));
4723}
4724
4725void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4726 vixl::aarch64::Register out,
4727 vixl::aarch64::Register base) {
4728 DCHECK(base.IsX());
4729 SingleEmissionCheckScope guard(GetVIXLAssembler());
4730 __ Bind(fixup_label);
4731 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4732}
4733
4734template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4735inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4736 const ArenaDeque<PcRelativePatchInfo>& infos,
4737 ArenaVector<LinkerPatch>* linker_patches) {
4738 for (const PcRelativePatchInfo& info : infos) {
4739 linker_patches->push_back(Factory(info.label.GetLocation(),
4740 &info.target_dex_file,
4741 info.pc_insn_label->GetLocation(),
4742 info.offset_or_index));
4743 }
4744}
4745
Vladimir Marko58155012015-08-19 12:49:41 +00004746void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4747 DCHECK(linker_patches->empty());
4748 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004749 pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004750 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004751 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004752 type_bss_entry_patches_.size() +
4753 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004754 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004755 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004756 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004757 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004758 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004759 info.offset_or_index));
4760 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004761 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004762 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004763 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4764 linker_patches);
4765 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004766 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4767 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004768 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4769 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004770 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004771 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4772 linker_patches);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004773 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
4774 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
4775 info.custom_data));
4776 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004777 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004778}
4779
Scott Wakeling97c72b72016-06-24 16:19:36 +01004780vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004781 Uint32ToLiteralMap* map) {
4782 return map->GetOrCreate(
4783 value,
4784 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4785}
4786
Scott Wakeling97c72b72016-06-24 16:19:36 +01004787vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004788 return uint64_literals_.GetOrCreate(
4789 value,
4790 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004791}
4792
Andreas Gampe878d58c2015-01-15 23:24:00 -08004793void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004794 // Explicit clinit checks triggered by static invokes must have been pruned by
4795 // art::PrepareForRegisterAllocation.
4796 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004797
Andreas Gampe878d58c2015-01-15 23:24:00 -08004798 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4799 return;
4800 }
4801
Artem Serov914d7a82017-02-07 14:33:49 +00004802 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4803 // are no pools emitted.
4804 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004805 LocationSummary* locations = invoke->GetLocations();
4806 codegen_->GenerateStaticOrDirectCall(
4807 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004808 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004809}
4810
4811void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004812 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4813 return;
4814 }
4815
Artem Serov914d7a82017-02-07 14:33:49 +00004816 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4817 // are no pools emitted.
4818 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004819 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004820 DCHECK(!codegen_->IsLeafMethod());
4821 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4822}
4823
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004824HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4825 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004826 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004827 case HLoadClass::LoadKind::kInvalid:
4828 LOG(FATAL) << "UNREACHABLE";
4829 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004830 case HLoadClass::LoadKind::kReferrersClass:
4831 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004832 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004833 case HLoadClass::LoadKind::kBssEntry:
4834 DCHECK(!Runtime::Current()->UseJitCompilation());
4835 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004836 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004837 DCHECK(Runtime::Current()->UseJitCompilation());
4838 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004839 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004840 case HLoadClass::LoadKind::kDexCacheViaMethod:
4841 break;
4842 }
4843 return desired_class_load_kind;
4844}
4845
Alexandre Rames67555f72014-11-18 10:55:16 +00004846void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004847 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4848 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004849 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004850 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004851 cls,
4852 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004853 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004854 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004855 return;
4856 }
Vladimir Marko41559982017-01-06 14:04:23 +00004857 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004858
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004859 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4860 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004861 ? LocationSummary::kCallOnSlowPath
4862 : LocationSummary::kNoCall;
4863 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004864 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004865 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004866 }
4867
Vladimir Marko41559982017-01-06 14:04:23 +00004868 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004869 locations->SetInAt(0, Location::RequiresRegister());
4870 }
4871 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004872 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4873 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4874 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004875 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004876 RegisterSet caller_saves = RegisterSet::Empty();
4877 InvokeRuntimeCallingConvention calling_convention;
4878 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4879 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4880 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4881 Primitive::kPrimNot).GetCode());
4882 locations->SetCustomSlowPathCallerSaves(caller_saves);
4883 } else {
4884 // For non-Baker read barrier we have a temp-clobbering call.
4885 }
4886 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004887}
4888
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004889// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4890// move.
4891void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004892 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4893 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4894 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004895 return;
4896 }
Vladimir Marko41559982017-01-06 14:04:23 +00004897 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004898
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004899 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004900 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004901 Register bss_entry_temp;
4902 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004903
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004904 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4905 ? kWithoutReadBarrier
4906 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004907 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004908 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004909 case HLoadClass::LoadKind::kReferrersClass: {
4910 DCHECK(!cls->CanCallRuntime());
4911 DCHECK(!cls->MustGenerateClinitCheck());
4912 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4913 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004914 GenerateGcRootFieldLoad(cls,
4915 out_loc,
4916 current_method,
4917 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004918 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004919 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004920 break;
4921 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004922 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004923 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004924 // Add ADRP with its PC-relative type patch.
4925 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004926 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004927 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004928 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004929 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004930 vixl::aarch64::Label* add_label =
4931 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004932 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004933 break;
4934 }
4935 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004936 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004937 uint32_t address = dchecked_integral_cast<uint32_t>(
4938 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4939 DCHECK_NE(address, 0u);
4940 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004941 break;
4942 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004943 case HLoadClass::LoadKind::kBssEntry: {
4944 // Add ADRP with its PC-relative Class .bss entry patch.
4945 const DexFile& dex_file = cls->GetDexFile();
4946 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004947 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004948 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4949 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004950 // Add LDR with its PC-relative Class patch.
4951 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004952 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004953 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4954 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004955 out_loc,
4956 bss_entry_temp,
4957 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004958 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004959 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004960 generate_null_check = true;
4961 break;
4962 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004963 case HLoadClass::LoadKind::kJitTableAddress: {
4964 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4965 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004966 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004967 GenerateGcRootFieldLoad(cls,
4968 out_loc,
4969 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004970 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004971 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004972 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004973 break;
4974 }
Vladimir Marko41559982017-01-06 14:04:23 +00004975 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004976 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004977 LOG(FATAL) << "UNREACHABLE";
4978 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004979 }
4980
Vladimir Markoea4c1262017-02-06 19:59:33 +00004981 bool do_clinit = cls->MustGenerateClinitCheck();
4982 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004983 DCHECK(cls->CanCallRuntime());
4984 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00004985 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004986 codegen_->AddSlowPath(slow_path);
4987 if (generate_null_check) {
4988 __ Cbz(out, slow_path->GetEntryLabel());
4989 }
4990 if (cls->MustGenerateClinitCheck()) {
4991 GenerateClassInitializationCheck(slow_path, out);
4992 } else {
4993 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004994 }
4995 }
4996}
4997
David Brazdilcb1c0552015-08-04 16:22:25 +01004998static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004999 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005000}
5001
Alexandre Rames67555f72014-11-18 10:55:16 +00005002void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5003 LocationSummary* locations =
5004 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5005 locations->SetOut(Location::RequiresRegister());
5006}
5007
5008void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005009 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5010}
5011
5012void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
5013 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5014}
5015
5016void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5017 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005018}
5019
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005020HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5021 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005022 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005023 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005024 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005025 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005026 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005027 case HLoadString::LoadKind::kJitTableAddress:
5028 DCHECK(Runtime::Current()->UseJitCompilation());
5029 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005030 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005031 case HLoadString::LoadKind::kDexCacheViaMethod:
5032 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005033 }
5034 return desired_string_load_kind;
5035}
5036
Alexandre Rames67555f72014-11-18 10:55:16 +00005037void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005038 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005039 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005040 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005041 InvokeRuntimeCallingConvention calling_convention;
5042 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5043 } else {
5044 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005045 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5046 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005047 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005048 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005049 RegisterSet caller_saves = RegisterSet::Empty();
5050 InvokeRuntimeCallingConvention calling_convention;
5051 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5052 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
5053 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
5054 Primitive::kPrimNot).GetCode());
5055 locations->SetCustomSlowPathCallerSaves(caller_saves);
5056 } else {
5057 // For non-Baker read barrier we have a temp-clobbering call.
5058 }
5059 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005060 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005061}
5062
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005063// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5064// move.
5065void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005066 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005067 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005068
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005069 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005070 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005071 // Add ADRP with its PC-relative String patch.
5072 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005073 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005074 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01005075 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005076 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005077 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005078 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005079 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005080 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005081 return; // No dex cache slow path.
5082 }
5083 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005084 uint32_t address = dchecked_integral_cast<uint32_t>(
5085 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5086 DCHECK_NE(address, 0u);
5087 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005088 return; // No dex cache slow path.
5089 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005090 case HLoadString::LoadKind::kBssEntry: {
5091 // Add ADRP with its PC-relative String .bss entry patch.
5092 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005093 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005094 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005095 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005096 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005097 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005098 // Add LDR with its PC-relative String patch.
5099 vixl::aarch64::Label* ldr_label =
5100 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005101 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005102 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005103 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005104 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005105 /* offset placeholder */ 0u,
5106 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005107 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005108 SlowPathCodeARM64* slow_path =
5109 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005110 codegen_->AddSlowPath(slow_path);
5111 __ Cbz(out.X(), slow_path->GetEntryLabel());
5112 __ Bind(slow_path->GetExitLabel());
5113 return;
5114 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005115 case HLoadString::LoadKind::kJitTableAddress: {
5116 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005117 load->GetStringIndex(),
5118 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005119 GenerateGcRootFieldLoad(load,
5120 out_loc,
5121 out.X(),
5122 /* offset */ 0,
5123 /* fixup_label */ nullptr,
5124 kCompilerReadBarrierOption);
5125 return;
5126 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005127 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005128 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005129 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005130
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005131 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005132 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005133 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005134 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005135 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5136 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005137}
5138
Alexandre Rames5319def2014-10-23 10:03:10 +01005139void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5140 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5141 locations->SetOut(Location::ConstantLocation(constant));
5142}
5143
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005144void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005145 // Will be generated at use site.
5146}
5147
Alexandre Rames67555f72014-11-18 10:55:16 +00005148void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5149 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005150 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005151 InvokeRuntimeCallingConvention calling_convention;
5152 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5153}
5154
5155void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005156 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005157 instruction,
5158 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005159 if (instruction->IsEnter()) {
5160 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5161 } else {
5162 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5163 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005164}
5165
Alexandre Rames42d641b2014-10-27 14:00:51 +00005166void LocationsBuilderARM64::VisitMul(HMul* mul) {
5167 LocationSummary* locations =
5168 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5169 switch (mul->GetResultType()) {
5170 case Primitive::kPrimInt:
5171 case Primitive::kPrimLong:
5172 locations->SetInAt(0, Location::RequiresRegister());
5173 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005174 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005175 break;
5176
5177 case Primitive::kPrimFloat:
5178 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005179 locations->SetInAt(0, Location::RequiresFpuRegister());
5180 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005181 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005182 break;
5183
5184 default:
5185 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5186 }
5187}
5188
5189void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5190 switch (mul->GetResultType()) {
5191 case Primitive::kPrimInt:
5192 case Primitive::kPrimLong:
5193 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5194 break;
5195
5196 case Primitive::kPrimFloat:
5197 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005198 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005199 break;
5200
5201 default:
5202 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5203 }
5204}
5205
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005206void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5207 LocationSummary* locations =
5208 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5209 switch (neg->GetResultType()) {
5210 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005211 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005212 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005213 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005214 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005215
5216 case Primitive::kPrimFloat:
5217 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005218 locations->SetInAt(0, Location::RequiresFpuRegister());
5219 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005220 break;
5221
5222 default:
5223 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5224 }
5225}
5226
5227void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5228 switch (neg->GetResultType()) {
5229 case Primitive::kPrimInt:
5230 case Primitive::kPrimLong:
5231 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5232 break;
5233
5234 case Primitive::kPrimFloat:
5235 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005236 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005237 break;
5238
5239 default:
5240 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5241 }
5242}
5243
5244void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5245 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005246 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005247 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005248 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005249 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5250 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005251}
5252
5253void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005254 // Note: if heap poisoning is enabled, the entry point takes cares
5255 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005256 QuickEntrypointEnum entrypoint =
5257 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5258 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005259 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005260}
5261
Alexandre Rames5319def2014-10-23 10:03:10 +01005262void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5263 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005264 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005265 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005266 if (instruction->IsStringAlloc()) {
5267 locations->AddTemp(LocationFrom(kArtMethodRegister));
5268 } else {
5269 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005270 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005271 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5272}
5273
5274void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005275 // Note: if heap poisoning is enabled, the entry point takes cares
5276 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005277 if (instruction->IsStringAlloc()) {
5278 // String is allocated through StringFactory. Call NewEmptyString entry point.
5279 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005280 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005281 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5282 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005283
5284 {
5285 // Ensure the pc position is recorded immediately after the `blr` instruction.
5286 ExactAssemblyScope eas(GetVIXLAssembler(),
5287 kInstructionSize,
5288 CodeBufferCheckScope::kExactSize);
5289 __ blr(lr);
5290 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5291 }
David Brazdil6de19382016-01-08 17:37:10 +00005292 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005293 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005294 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005295 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005296}
5297
5298void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5299 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005300 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005301 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005302}
5303
5304void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005305 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005306 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005307 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005308 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005309 break;
5310
5311 default:
5312 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5313 }
5314}
5315
David Brazdil66d126e2015-04-03 16:02:44 +01005316void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5317 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5318 locations->SetInAt(0, Location::RequiresRegister());
5319 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5320}
5321
5322void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005323 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005324}
5325
Alexandre Rames5319def2014-10-23 10:03:10 +01005326void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005327 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5328 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005329}
5330
Calin Juravle2ae48182016-03-16 14:05:09 +00005331void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5332 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005333 return;
5334 }
Artem Serov914d7a82017-02-07 14:33:49 +00005335 {
5336 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5337 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5338 Location obj = instruction->GetLocations()->InAt(0);
5339 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5340 RecordPcInfo(instruction, instruction->GetDexPc());
5341 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005342}
5343
Calin Juravle2ae48182016-03-16 14:05:09 +00005344void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005345 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005346 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005347
5348 LocationSummary* locations = instruction->GetLocations();
5349 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005350
5351 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005352}
5353
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005354void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005355 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005356}
5357
Alexandre Rames67555f72014-11-18 10:55:16 +00005358void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5359 HandleBinaryOp(instruction);
5360}
5361
5362void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5363 HandleBinaryOp(instruction);
5364}
5365
Alexandre Rames3e69f162014-12-10 10:36:50 +00005366void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5367 LOG(FATAL) << "Unreachable";
5368}
5369
5370void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5371 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5372}
5373
Alexandre Rames5319def2014-10-23 10:03:10 +01005374void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5375 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5376 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5377 if (location.IsStackSlot()) {
5378 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5379 } else if (location.IsDoubleStackSlot()) {
5380 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5381 }
5382 locations->SetOut(location);
5383}
5384
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005385void InstructionCodeGeneratorARM64::VisitParameterValue(
5386 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005387 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005388}
5389
5390void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5391 LocationSummary* locations =
5392 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005393 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005394}
5395
5396void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5397 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5398 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005399}
5400
5401void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5402 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005403 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005404 locations->SetInAt(i, Location::Any());
5405 }
5406 locations->SetOut(Location::Any());
5407}
5408
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005409void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005410 LOG(FATAL) << "Unreachable";
5411}
5412
Serban Constantinescu02164b32014-11-13 14:05:07 +00005413void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005414 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005415 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005416 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5417 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005418 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5419
5420 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005421 case Primitive::kPrimInt:
5422 case Primitive::kPrimLong:
5423 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005424 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005425 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5426 break;
5427
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005428 case Primitive::kPrimFloat:
5429 case Primitive::kPrimDouble: {
5430 InvokeRuntimeCallingConvention calling_convention;
5431 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5432 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5433 locations->SetOut(calling_convention.GetReturnLocation(type));
5434
5435 break;
5436 }
5437
Serban Constantinescu02164b32014-11-13 14:05:07 +00005438 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005439 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005440 }
5441}
5442
5443void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5444 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005445
Serban Constantinescu02164b32014-11-13 14:05:07 +00005446 switch (type) {
5447 case Primitive::kPrimInt:
5448 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005449 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005450 break;
5451 }
5452
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005453 case Primitive::kPrimFloat:
5454 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005455 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5456 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005457 if (type == Primitive::kPrimFloat) {
5458 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5459 } else {
5460 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5461 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005462 break;
5463 }
5464
Serban Constantinescu02164b32014-11-13 14:05:07 +00005465 default:
5466 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005467 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005468 }
5469}
5470
Igor Murashkind01745e2017-04-05 16:40:31 -07005471void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5472 constructor_fence->SetLocations(nullptr);
5473}
5474
5475void InstructionCodeGeneratorARM64::VisitConstructorFence(
5476 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5477 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5478}
5479
Calin Juravle27df7582015-04-17 19:12:31 +01005480void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5481 memory_barrier->SetLocations(nullptr);
5482}
5483
5484void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005485 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005486}
5487
Alexandre Rames5319def2014-10-23 10:03:10 +01005488void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5489 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5490 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005491 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005492}
5493
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005494void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005495 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005496}
5497
5498void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5499 instruction->SetLocations(nullptr);
5500}
5501
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005502void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005503 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005504}
5505
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005506void LocationsBuilderARM64::VisitRor(HRor* ror) {
5507 HandleBinaryOp(ror);
5508}
5509
5510void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5511 HandleBinaryOp(ror);
5512}
5513
Serban Constantinescu02164b32014-11-13 14:05:07 +00005514void LocationsBuilderARM64::VisitShl(HShl* shl) {
5515 HandleShift(shl);
5516}
5517
5518void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5519 HandleShift(shl);
5520}
5521
5522void LocationsBuilderARM64::VisitShr(HShr* shr) {
5523 HandleShift(shr);
5524}
5525
5526void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5527 HandleShift(shr);
5528}
5529
Alexandre Rames5319def2014-10-23 10:03:10 +01005530void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005531 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005532}
5533
5534void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005535 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005536}
5537
Alexandre Rames67555f72014-11-18 10:55:16 +00005538void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005539 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005540}
5541
5542void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005543 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005544}
5545
5546void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005547 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005548}
5549
Alexandre Rames67555f72014-11-18 10:55:16 +00005550void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005551 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005552}
5553
Calin Juravlee460d1d2015-09-29 04:52:17 +01005554void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5555 HUnresolvedInstanceFieldGet* instruction) {
5556 FieldAccessCallingConventionARM64 calling_convention;
5557 codegen_->CreateUnresolvedFieldLocationSummary(
5558 instruction, instruction->GetFieldType(), calling_convention);
5559}
5560
5561void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5562 HUnresolvedInstanceFieldGet* instruction) {
5563 FieldAccessCallingConventionARM64 calling_convention;
5564 codegen_->GenerateUnresolvedFieldAccess(instruction,
5565 instruction->GetFieldType(),
5566 instruction->GetFieldIndex(),
5567 instruction->GetDexPc(),
5568 calling_convention);
5569}
5570
5571void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5572 HUnresolvedInstanceFieldSet* instruction) {
5573 FieldAccessCallingConventionARM64 calling_convention;
5574 codegen_->CreateUnresolvedFieldLocationSummary(
5575 instruction, instruction->GetFieldType(), calling_convention);
5576}
5577
5578void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5579 HUnresolvedInstanceFieldSet* instruction) {
5580 FieldAccessCallingConventionARM64 calling_convention;
5581 codegen_->GenerateUnresolvedFieldAccess(instruction,
5582 instruction->GetFieldType(),
5583 instruction->GetFieldIndex(),
5584 instruction->GetDexPc(),
5585 calling_convention);
5586}
5587
5588void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5589 HUnresolvedStaticFieldGet* instruction) {
5590 FieldAccessCallingConventionARM64 calling_convention;
5591 codegen_->CreateUnresolvedFieldLocationSummary(
5592 instruction, instruction->GetFieldType(), calling_convention);
5593}
5594
5595void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5596 HUnresolvedStaticFieldGet* instruction) {
5597 FieldAccessCallingConventionARM64 calling_convention;
5598 codegen_->GenerateUnresolvedFieldAccess(instruction,
5599 instruction->GetFieldType(),
5600 instruction->GetFieldIndex(),
5601 instruction->GetDexPc(),
5602 calling_convention);
5603}
5604
5605void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5606 HUnresolvedStaticFieldSet* instruction) {
5607 FieldAccessCallingConventionARM64 calling_convention;
5608 codegen_->CreateUnresolvedFieldLocationSummary(
5609 instruction, instruction->GetFieldType(), calling_convention);
5610}
5611
5612void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5613 HUnresolvedStaticFieldSet* instruction) {
5614 FieldAccessCallingConventionARM64 calling_convention;
5615 codegen_->GenerateUnresolvedFieldAccess(instruction,
5616 instruction->GetFieldType(),
5617 instruction->GetFieldIndex(),
5618 instruction->GetDexPc(),
5619 calling_convention);
5620}
5621
Alexandre Rames5319def2014-10-23 10:03:10 +01005622void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005623 LocationSummary* locations =
5624 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005625 // In suspend check slow path, usually there are no caller-save registers at all.
5626 // If SIMD instructions are present, however, we force spilling all live SIMD
5627 // registers in full width (since the runtime only saves/restores lower part).
5628 locations->SetCustomSlowPathCallerSaves(
5629 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005630}
5631
5632void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005633 HBasicBlock* block = instruction->GetBlock();
5634 if (block->GetLoopInformation() != nullptr) {
5635 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5636 // The back edge will generate the suspend check.
5637 return;
5638 }
5639 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5640 // The goto will generate the suspend check.
5641 return;
5642 }
5643 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005644}
5645
Alexandre Rames67555f72014-11-18 10:55:16 +00005646void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5647 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005648 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005649 InvokeRuntimeCallingConvention calling_convention;
5650 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5651}
5652
5653void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005654 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005655 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005656}
5657
5658void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5659 LocationSummary* locations =
5660 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5661 Primitive::Type input_type = conversion->GetInputType();
5662 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005663 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005664 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5665 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5666 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5667 }
5668
Alexandre Rames542361f2015-01-29 16:57:31 +00005669 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005670 locations->SetInAt(0, Location::RequiresFpuRegister());
5671 } else {
5672 locations->SetInAt(0, Location::RequiresRegister());
5673 }
5674
Alexandre Rames542361f2015-01-29 16:57:31 +00005675 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005676 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5677 } else {
5678 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5679 }
5680}
5681
5682void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5683 Primitive::Type result_type = conversion->GetResultType();
5684 Primitive::Type input_type = conversion->GetInputType();
5685
5686 DCHECK_NE(input_type, result_type);
5687
Alexandre Rames542361f2015-01-29 16:57:31 +00005688 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005689 int result_size = Primitive::ComponentSize(result_type);
5690 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005691 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005692 Register output = OutputRegister(conversion);
5693 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005694 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005695 // 'int' values are used directly as W registers, discarding the top
5696 // bits, so we don't need to sign-extend and can just perform a move.
5697 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5698 // top 32 bits of the target register. We theoretically could leave those
5699 // bits unchanged, but we would have to make sure that no code uses a
5700 // 32bit input value as a 64bit value assuming that the top 32 bits are
5701 // zero.
5702 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005703 } else if (result_type == Primitive::kPrimChar ||
5704 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5705 __ Ubfx(output,
5706 output.IsX() ? source.X() : source.W(),
5707 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005708 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005709 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005710 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005711 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005712 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005713 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005714 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5715 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005716 } else if (Primitive::IsFloatingPointType(result_type) &&
5717 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005718 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5719 } else {
5720 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5721 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005722 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005723}
Alexandre Rames67555f72014-11-18 10:55:16 +00005724
Serban Constantinescu02164b32014-11-13 14:05:07 +00005725void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5726 HandleShift(ushr);
5727}
5728
5729void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5730 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005731}
5732
5733void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5734 HandleBinaryOp(instruction);
5735}
5736
5737void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5738 HandleBinaryOp(instruction);
5739}
5740
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005741void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005742 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005743 LOG(FATAL) << "Unreachable";
5744}
5745
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005746void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005747 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005748 LOG(FATAL) << "Unreachable";
5749}
5750
Mark Mendellfe57faa2015-09-18 09:26:15 -04005751// Simple implementation of packed switch - generate cascaded compare/jumps.
5752void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5753 LocationSummary* locations =
5754 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5755 locations->SetInAt(0, Location::RequiresRegister());
5756}
5757
5758void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5759 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005760 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005761 Register value_reg = InputRegisterAt(switch_instr, 0);
5762 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5763
Zheng Xu3927c8b2015-11-18 17:46:25 +08005764 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005765 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005766 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5767 // make sure we don't emit it if the target may run out of range.
5768 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5769 // ranges and emit the tables only as required.
5770 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005771
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005772 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005773 // Current instruction id is an upper bound of the number of HIRs in the graph.
5774 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5775 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005776 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5777 Register temp = temps.AcquireW();
5778 __ Subs(temp, value_reg, Operand(lower_bound));
5779
Zheng Xu3927c8b2015-11-18 17:46:25 +08005780 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005781 // Jump to successors[0] if value == lower_bound.
5782 __ B(eq, codegen_->GetLabelOf(successors[0]));
5783 int32_t last_index = 0;
5784 for (; num_entries - last_index > 2; last_index += 2) {
5785 __ Subs(temp, temp, Operand(2));
5786 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5787 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5788 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5789 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5790 }
5791 if (num_entries - last_index == 2) {
5792 // The last missing case_value.
5793 __ Cmp(temp, Operand(1));
5794 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005795 }
5796
5797 // And the default for any other value.
5798 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5799 __ B(codegen_->GetLabelOf(default_block));
5800 }
5801 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005802 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005803
5804 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5805
5806 // Below instructions should use at most one blocked register. Since there are two blocked
5807 // registers, we are free to block one.
5808 Register temp_w = temps.AcquireW();
5809 Register index;
5810 // Remove the bias.
5811 if (lower_bound != 0) {
5812 index = temp_w;
5813 __ Sub(index, value_reg, Operand(lower_bound));
5814 } else {
5815 index = value_reg;
5816 }
5817
5818 // Jump to default block if index is out of the range.
5819 __ Cmp(index, Operand(num_entries));
5820 __ B(hs, codegen_->GetLabelOf(default_block));
5821
5822 // In current VIXL implementation, it won't require any blocked registers to encode the
5823 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5824 // register pressure.
5825 Register table_base = temps.AcquireX();
5826 // Load jump offset from the table.
5827 __ Adr(table_base, jump_table->GetTableStartLabel());
5828 Register jump_offset = temp_w;
5829 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5830
5831 // Jump to target block by branching to table_base(pc related) + offset.
5832 Register target_address = table_base;
5833 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5834 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005835 }
5836}
5837
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005838void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5839 HInstruction* instruction,
5840 Location out,
5841 uint32_t offset,
5842 Location maybe_temp,
5843 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005844 Primitive::Type type = Primitive::kPrimNot;
5845 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005846 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005847 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005848 if (kUseBakerReadBarrier) {
5849 // Load with fast path based Baker's read barrier.
5850 // /* HeapReference<Object> */ out = *(out + offset)
5851 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5852 out,
5853 out_reg,
5854 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005855 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005856 /* needs_null_check */ false,
5857 /* use_load_acquire */ false);
5858 } else {
5859 // Load with slow path based read barrier.
5860 // Save the value of `out` into `maybe_temp` before overwriting it
5861 // in the following move operation, as we will need it for the
5862 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005863 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005864 __ Mov(temp_reg, out_reg);
5865 // /* HeapReference<Object> */ out = *(out + offset)
5866 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5867 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5868 }
5869 } else {
5870 // Plain load with no read barrier.
5871 // /* HeapReference<Object> */ out = *(out + offset)
5872 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5873 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5874 }
5875}
5876
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005877void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5878 HInstruction* instruction,
5879 Location out,
5880 Location obj,
5881 uint32_t offset,
5882 Location maybe_temp,
5883 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005884 Primitive::Type type = Primitive::kPrimNot;
5885 Register out_reg = RegisterFrom(out, type);
5886 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005887 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005888 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005889 if (kUseBakerReadBarrier) {
5890 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005891 // /* HeapReference<Object> */ out = *(obj + offset)
5892 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5893 out,
5894 obj_reg,
5895 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005896 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005897 /* needs_null_check */ false,
5898 /* use_load_acquire */ false);
5899 } else {
5900 // Load with slow path based read barrier.
5901 // /* HeapReference<Object> */ out = *(obj + offset)
5902 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5903 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5904 }
5905 } else {
5906 // Plain load with no read barrier.
5907 // /* HeapReference<Object> */ out = *(obj + offset)
5908 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5909 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5910 }
5911}
5912
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005913void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5914 HInstruction* instruction,
5915 Location root,
5916 Register obj,
5917 uint32_t offset,
5918 vixl::aarch64::Label* fixup_label,
5919 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005920 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005921 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005922 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005923 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005924 if (kUseBakerReadBarrier) {
5925 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005926 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005927 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5928 !Runtime::Current()->UseJitCompilation()) {
5929 // Note that we do not actually check the value of `GetIsGcMarking()`
5930 // to decide whether to mark the loaded GC root or not. Instead, we
Vladimir Marko66d691d2017-04-07 17:53:39 +01005931 // load into `temp` (actually IP1) the read barrier mark introspection
5932 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
5933 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005934 //
5935 // We use link-time generated thunks for the slow path. That thunk
5936 // checks the reference and jumps to the entrypoint if needed.
5937 //
5938 // temp = Thread::Current()->pReadBarrierMarkIntrospection
5939 // lr = &return_address;
5940 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5941 // if (temp != nullptr) {
5942 // goto gc_root_thunk<root_reg>(lr)
5943 // }
5944 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00005945
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005946 UseScratchRegisterScope temps(GetVIXLAssembler());
5947 DCHECK(temps.IsAvailable(ip0));
5948 DCHECK(temps.IsAvailable(ip1));
5949 temps.Exclude(ip0, ip1);
5950 uint32_t custom_data =
5951 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
5952 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00005953
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005954 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
5955 DCHECK_EQ(ip0.GetCode(), 16u);
5956 const int32_t entry_point_offset =
5957 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
5958 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
5959 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5960 vixl::aarch64::Label return_address;
5961 __ adr(lr, &return_address);
5962 if (fixup_label != nullptr) {
5963 __ Bind(fixup_label);
5964 }
5965 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
5966 "GC root LDR must be 2 instruction (8B) before the return address label.");
5967 __ ldr(root_reg, MemOperand(obj.X(), offset));
5968 __ Bind(cbnz_label);
5969 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
5970 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005971 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005972 // Note that we do not actually check the value of
5973 // `GetIsGcMarking()` to decide whether to mark the loaded GC
5974 // root or not. Instead, we load into `temp` the read barrier
5975 // mark entry point corresponding to register `root`. If `temp`
5976 // is null, it means that `GetIsGcMarking()` is false, and vice
5977 // versa.
5978 //
5979 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5980 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5981 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
5982 // // Slow path.
5983 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
5984 // }
Roland Levillain44015862016-01-22 11:47:17 +00005985
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005986 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
5987 Register temp = lr;
5988 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
5989 instruction, root, /* entrypoint */ LocationFrom(temp));
5990 codegen_->AddSlowPath(slow_path);
5991
5992 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5993 const int32_t entry_point_offset =
5994 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5995 // Loading the entrypoint does not require a load acquire since it is only changed when
5996 // threads are suspended or running a checkpoint.
5997 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5998
5999 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6000 if (fixup_label == nullptr) {
6001 __ Ldr(root_reg, MemOperand(obj, offset));
6002 } else {
6003 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
6004 }
6005 static_assert(
6006 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6007 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6008 "have different sizes.");
6009 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6010 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6011 "have different sizes.");
6012
6013 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6014 // checking GetIsGcMarking.
6015 __ Cbnz(temp, slow_path->GetEntryLabel());
6016 __ Bind(slow_path->GetExitLabel());
6017 }
Roland Levillain44015862016-01-22 11:47:17 +00006018 } else {
6019 // GC root loaded through a slow path for read barriers other
6020 // than Baker's.
6021 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006022 if (fixup_label == nullptr) {
6023 __ Add(root_reg.X(), obj.X(), offset);
6024 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006025 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006026 }
Roland Levillain44015862016-01-22 11:47:17 +00006027 // /* mirror::Object* */ root = root->Read()
6028 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6029 }
6030 } else {
6031 // Plain GC root load with no read barrier.
6032 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006033 if (fixup_label == nullptr) {
6034 __ Ldr(root_reg, MemOperand(obj, offset));
6035 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006036 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006037 }
Roland Levillain44015862016-01-22 11:47:17 +00006038 // Note that GC roots are not affected by heap poisoning, thus we
6039 // do not have to unpoison `root_reg` here.
6040 }
6041}
6042
6043void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6044 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006045 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006046 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006047 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006048 bool needs_null_check,
6049 bool use_load_acquire) {
6050 DCHECK(kEmitCompilerReadBarrier);
6051 DCHECK(kUseBakerReadBarrier);
6052
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006053 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6054 !use_load_acquire &&
6055 !Runtime::Current()->UseJitCompilation()) {
6056 // Note that we do not actually check the value of `GetIsGcMarking()`
Vladimir Marko66d691d2017-04-07 17:53:39 +01006057 // to decide whether to mark the loaded reference or not. Instead, we
6058 // load into `temp` (actually IP1) the read barrier mark introspection
6059 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6060 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006061 //
6062 // We use link-time generated thunks for the slow path. That thunk checks
6063 // the holder and jumps to the entrypoint if needed. If the holder is not
6064 // gray, it creates a fake dependency and returns to the LDR instruction.
6065 //
6066 // temp = Thread::Current()->pReadBarrierMarkIntrospection
Vladimir Marko66d691d2017-04-07 17:53:39 +01006067 // lr = &gray_return_address;
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006068 // if (temp != nullptr) {
6069 // goto field_thunk<holder_reg, base_reg>(lr)
6070 // }
6071 // not_gray_return_address:
6072 // // Original reference load. If the offset is too large to fit
6073 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006074 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006075 // gray_return_address:
6076
6077 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6078 Register base = obj;
6079 if (offset >= kReferenceLoadMinFarOffset) {
6080 DCHECK(maybe_temp.IsRegister());
6081 base = WRegisterFrom(maybe_temp);
6082 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6083 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6084 offset &= (kReferenceLoadMinFarOffset - 1u);
6085 }
6086 UseScratchRegisterScope temps(GetVIXLAssembler());
6087 DCHECK(temps.IsAvailable(ip0));
6088 DCHECK(temps.IsAvailable(ip1));
6089 temps.Exclude(ip0, ip1);
6090 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6091 base.GetCode(),
6092 obj.GetCode());
6093 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6094
6095 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6096 DCHECK_EQ(ip0.GetCode(), 16u);
6097 const int32_t entry_point_offset =
6098 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6099 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
Vladimir Markod1ef8732017-04-18 13:55:13 +01006100 EmissionCheckScope guard(GetVIXLAssembler(),
6101 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006102 vixl::aarch64::Label return_address;
6103 __ adr(lr, &return_address);
6104 __ Bind(cbnz_label);
6105 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markod1ef8732017-04-18 13:55:13 +01006106 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6107 "Field LDR must be 1 instruction (4B) before the return address label; "
6108 " 2 instructions (8B) for heap poisoning.");
6109 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6110 __ ldr(ref_reg, MemOperand(base.X(), offset));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006111 if (needs_null_check) {
6112 MaybeRecordImplicitNullCheck(instruction);
6113 }
Vladimir Markod1ef8732017-04-18 13:55:13 +01006114 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006115 __ Bind(&return_address);
6116 return;
6117 }
6118
Roland Levillain44015862016-01-22 11:47:17 +00006119 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006120 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006121 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006122 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006123 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6124 ref,
6125 obj,
6126 offset,
6127 no_index,
6128 no_scale_factor,
6129 temp,
6130 needs_null_check,
6131 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006132}
6133
6134void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6135 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006136 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006137 uint32_t data_offset,
6138 Location index,
6139 Register temp,
6140 bool needs_null_check) {
6141 DCHECK(kEmitCompilerReadBarrier);
6142 DCHECK(kUseBakerReadBarrier);
6143
Vladimir Marko66d691d2017-04-07 17:53:39 +01006144 static_assert(
6145 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6146 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6147 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
6148
6149 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6150 !Runtime::Current()->UseJitCompilation()) {
6151 // Note that we do not actually check the value of `GetIsGcMarking()`
6152 // to decide whether to mark the loaded reference or not. Instead, we
6153 // load into `temp` (actually IP1) the read barrier mark introspection
6154 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6155 // false, and vice versa.
6156 //
6157 // We use link-time generated thunks for the slow path. That thunk checks
6158 // the holder and jumps to the entrypoint if needed. If the holder is not
6159 // gray, it creates a fake dependency and returns to the LDR instruction.
6160 //
6161 // temp = Thread::Current()->pReadBarrierMarkIntrospection
6162 // lr = &gray_return_address;
6163 // if (temp != nullptr) {
6164 // goto field_thunk<holder_reg, base_reg>(lr)
6165 // }
6166 // not_gray_return_address:
6167 // // Original reference load. If the offset is too large to fit
6168 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006169 // HeapReference<mirror::Object> reference = data[index];
Vladimir Marko66d691d2017-04-07 17:53:39 +01006170 // gray_return_address:
6171
6172 DCHECK(index.IsValid());
6173 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
6174 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6175
6176 UseScratchRegisterScope temps(GetVIXLAssembler());
6177 DCHECK(temps.IsAvailable(ip0));
6178 DCHECK(temps.IsAvailable(ip1));
6179 temps.Exclude(ip0, ip1);
6180 uint32_t custom_data =
6181 linker::Arm64RelativePatcher::EncodeBakerReadBarrierArrayData(temp.GetCode());
6182 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6183
6184 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6185 DCHECK_EQ(ip0.GetCode(), 16u);
6186 const int32_t entry_point_offset =
6187 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6188 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
6189 __ Add(temp.X(), obj.X(), Operand(data_offset));
6190 EmissionCheckScope guard(GetVIXLAssembler(),
6191 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6192 vixl::aarch64::Label return_address;
6193 __ adr(lr, &return_address);
6194 __ Bind(cbnz_label);
6195 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6196 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6197 "Array LDR must be 1 instruction (4B) before the return address label; "
6198 " 2 instructions (8B) for heap poisoning.");
6199 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6200 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6201 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6202 __ Bind(&return_address);
6203 return;
6204 }
6205
Roland Levillain44015862016-01-22 11:47:17 +00006206 // Array cells are never volatile variables, therefore array loads
6207 // never use Load-Acquire instructions on ARM64.
6208 const bool use_load_acquire = false;
6209
6210 // /* HeapReference<Object> */ ref =
6211 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006212 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6213 ref,
6214 obj,
6215 data_offset,
6216 index,
6217 scale_factor,
6218 temp,
6219 needs_null_check,
6220 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006221}
6222
6223void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6224 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006225 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006226 uint32_t offset,
6227 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006228 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006229 Register temp,
6230 bool needs_null_check,
Roland Levillainff487002017-03-07 16:50:01 +00006231 bool use_load_acquire) {
Roland Levillain44015862016-01-22 11:47:17 +00006232 DCHECK(kEmitCompilerReadBarrier);
6233 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006234 // If we are emitting an array load, we should not be using a
6235 // Load Acquire instruction. In other words:
6236 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6237 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006238
Roland Levillain54f869e2017-03-06 13:54:11 +00006239 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6240 // whether we need to enter the slow path to mark the reference.
6241 // Then, in the slow path, check the gray bit in the lock word of
6242 // the reference's holder (`obj`) to decide whether to mark `ref` or
6243 // not.
Roland Levillain44015862016-01-22 11:47:17 +00006244 //
Roland Levillainba650a42017-03-06 13:52:32 +00006245 // Note that we do not actually check the value of `GetIsGcMarking()`;
6246 // instead, we load into `temp2` the read barrier mark entry point
6247 // corresponding to register `ref`. If `temp2` is null, it means
6248 // that `GetIsGcMarking()` is false, and vice versa.
6249 //
6250 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00006251 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6252 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006253 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6254 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6255 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6256 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6257 // if (is_gray) {
6258 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6259 // }
6260 // } else {
6261 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006262 // }
Roland Levillain44015862016-01-22 11:47:17 +00006263
Roland Levillainba650a42017-03-06 13:52:32 +00006264 // Slow path marking the object `ref` when the GC is marking. The
6265 // entrypoint will already be loaded in `temp2`.
6266 Register temp2 = lr;
6267 Location temp2_loc = LocationFrom(temp2);
Roland Levillainff487002017-03-07 16:50:01 +00006268 SlowPathCodeARM64* slow_path =
6269 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6270 instruction,
6271 ref,
6272 obj,
6273 offset,
6274 index,
6275 scale_factor,
6276 needs_null_check,
6277 use_load_acquire,
6278 temp,
6279 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006280 AddSlowPath(slow_path);
6281
6282 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6283 const int32_t entry_point_offset =
6284 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6285 // Loading the entrypoint does not require a load acquire since it is only changed when
6286 // threads are suspended or running a checkpoint.
6287 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006288 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6289 // checking GetIsGcMarking.
6290 __ Cbnz(temp2, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006291 // Fast path: the GC is not marking: just load the reference.
Roland Levillain54f869e2017-03-06 13:54:11 +00006292 GenerateRawReferenceLoad(
6293 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006294 __ Bind(slow_path->GetExitLabel());
6295}
6296
Roland Levillainff487002017-03-07 16:50:01 +00006297void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6298 Location ref,
6299 Register obj,
6300 Location field_offset,
6301 Register temp,
6302 bool needs_null_check,
6303 bool use_load_acquire) {
6304 DCHECK(kEmitCompilerReadBarrier);
6305 DCHECK(kUseBakerReadBarrier);
6306 // If we are emitting an array load, we should not be using a
6307 // Load Acquire instruction. In other words:
6308 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6309 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6310
6311 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6312 // whether we need to enter the slow path to update the reference
6313 // field within `obj`. Then, in the slow path, check the gray bit
6314 // in the lock word of the reference's holder (`obj`) to decide
6315 // whether to mark `ref` and update the field or not.
6316 //
6317 // Note that we do not actually check the value of `GetIsGcMarking()`;
6318 // instead, we load into `temp2` the read barrier mark entry point
6319 // corresponding to register `ref`. If `temp2` is null, it means
6320 // that `GetIsGcMarking()` is false, and vice versa.
6321 //
6322 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6323 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6324 // // Slow path.
6325 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6326 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6327 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6328 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6329 // if (is_gray) {
6330 // old_ref = ref;
6331 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6332 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6333 // }
6334 // }
6335
6336 // Slow path updating the object reference at address `obj + field_offset`
6337 // when the GC is marking. The entrypoint will already be loaded in `temp2`.
6338 Register temp2 = lr;
6339 Location temp2_loc = LocationFrom(temp2);
6340 SlowPathCodeARM64* slow_path =
6341 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6342 instruction,
6343 ref,
6344 obj,
6345 /* offset */ 0u,
6346 /* index */ field_offset,
6347 /* scale_factor */ 0u /* "times 1" */,
6348 needs_null_check,
6349 use_load_acquire,
6350 temp,
6351 /* entrypoint */ temp2_loc);
6352 AddSlowPath(slow_path);
6353
6354 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6355 const int32_t entry_point_offset =
6356 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6357 // Loading the entrypoint does not require a load acquire since it is only changed when
6358 // threads are suspended or running a checkpoint.
6359 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
6360 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6361 // checking GetIsGcMarking.
6362 __ Cbnz(temp2, slow_path->GetEntryLabel());
6363 // Fast path: the GC is not marking: nothing to do (the field is
6364 // up-to-date, and we don't need to load the reference).
6365 __ Bind(slow_path->GetExitLabel());
6366}
6367
Roland Levillainba650a42017-03-06 13:52:32 +00006368void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6369 Location ref,
6370 Register obj,
6371 uint32_t offset,
6372 Location index,
6373 size_t scale_factor,
6374 bool needs_null_check,
6375 bool use_load_acquire) {
6376 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006377 Primitive::Type type = Primitive::kPrimNot;
6378 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006379
Roland Levillainba650a42017-03-06 13:52:32 +00006380 // If needed, vixl::EmissionCheckScope guards are used to ensure
6381 // that no pools are emitted between the load (macro) instruction
6382 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006383
Roland Levillain44015862016-01-22 11:47:17 +00006384 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006385 // Load types involving an "index": ArrayGet,
6386 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6387 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006388 if (use_load_acquire) {
6389 // UnsafeGetObjectVolatile intrinsic case.
6390 // Register `index` is not an index in an object array, but an
6391 // offset to an object reference field within object `obj`.
6392 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6393 DCHECK(instruction->GetLocations()->Intrinsified());
6394 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6395 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006396 DCHECK_EQ(offset, 0u);
6397 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006398 DCHECK_EQ(needs_null_check, false);
6399 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006400 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6401 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006402 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006403 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6404 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006405 if (index.IsConstant()) {
6406 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006407 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006408 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006409 if (needs_null_check) {
6410 MaybeRecordImplicitNullCheck(instruction);
6411 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006412 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006413 UseScratchRegisterScope temps(GetVIXLAssembler());
6414 Register temp = temps.AcquireW();
6415 __ Add(temp, obj, offset);
6416 {
6417 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6418 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6419 if (needs_null_check) {
6420 MaybeRecordImplicitNullCheck(instruction);
6421 }
6422 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006423 }
Roland Levillain44015862016-01-22 11:47:17 +00006424 }
Roland Levillain44015862016-01-22 11:47:17 +00006425 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006426 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006427 MemOperand field = HeapOperand(obj, offset);
6428 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006429 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6430 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006431 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006432 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006433 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006434 if (needs_null_check) {
6435 MaybeRecordImplicitNullCheck(instruction);
6436 }
Roland Levillain44015862016-01-22 11:47:17 +00006437 }
6438 }
6439
6440 // Object* ref = ref_addr->AsMirrorPtr()
6441 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006442}
6443
6444void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6445 Location out,
6446 Location ref,
6447 Location obj,
6448 uint32_t offset,
6449 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006450 DCHECK(kEmitCompilerReadBarrier);
6451
Roland Levillain44015862016-01-22 11:47:17 +00006452 // Insert a slow path based read barrier *after* the reference load.
6453 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006454 // If heap poisoning is enabled, the unpoisoning of the loaded
6455 // reference will be carried out by the runtime within the slow
6456 // path.
6457 //
6458 // Note that `ref` currently does not get unpoisoned (when heap
6459 // poisoning is enabled), which is alright as the `ref` argument is
6460 // not used by the artReadBarrierSlow entry point.
6461 //
6462 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6463 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6464 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6465 AddSlowPath(slow_path);
6466
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006467 __ B(slow_path->GetEntryLabel());
6468 __ Bind(slow_path->GetExitLabel());
6469}
6470
Roland Levillain44015862016-01-22 11:47:17 +00006471void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6472 Location out,
6473 Location ref,
6474 Location obj,
6475 uint32_t offset,
6476 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006477 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006478 // Baker's read barriers shall be handled by the fast path
6479 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6480 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006481 // If heap poisoning is enabled, unpoisoning will be taken care of
6482 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006483 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006484 } else if (kPoisonHeapReferences) {
6485 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6486 }
6487}
6488
Roland Levillain44015862016-01-22 11:47:17 +00006489void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6490 Location out,
6491 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006492 DCHECK(kEmitCompilerReadBarrier);
6493
Roland Levillain44015862016-01-22 11:47:17 +00006494 // Insert a slow path based read barrier *after* the GC root load.
6495 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006496 // Note that GC roots are not affected by heap poisoning, so we do
6497 // not need to do anything special for this here.
6498 SlowPathCodeARM64* slow_path =
6499 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6500 AddSlowPath(slow_path);
6501
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006502 __ B(slow_path->GetEntryLabel());
6503 __ Bind(slow_path->GetExitLabel());
6504}
6505
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006506void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6507 LocationSummary* locations =
6508 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6509 locations->SetInAt(0, Location::RequiresRegister());
6510 locations->SetOut(Location::RequiresRegister());
6511}
6512
6513void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6514 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006515 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006516 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006517 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006518 __ Ldr(XRegisterFrom(locations->Out()),
6519 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006520 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006521 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006522 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006523 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6524 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006525 __ Ldr(XRegisterFrom(locations->Out()),
6526 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006527 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006528}
6529
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006530static void PatchJitRootUse(uint8_t* code,
6531 const uint8_t* roots_data,
6532 vixl::aarch64::Literal<uint32_t>* literal,
6533 uint64_t index_in_table) {
6534 uint32_t literal_offset = literal->GetOffset();
6535 uintptr_t address =
6536 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6537 uint8_t* data = code + literal_offset;
6538 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6539}
6540
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006541void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6542 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006543 const StringReference& string_reference = entry.first;
6544 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
6545 const auto it = jit_string_roots_.find(string_reference);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006546 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006547 uint64_t index_in_table = it->second;
6548 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006549 }
6550 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006551 const TypeReference& type_reference = entry.first;
6552 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
6553 const auto it = jit_class_roots_.find(type_reference);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006554 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006555 uint64_t index_in_table = it->second;
6556 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006557 }
6558}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006559
Alexandre Rames67555f72014-11-18 10:55:16 +00006560#undef __
6561#undef QUICK_ENTRY_POINT
6562
Alexandre Rames5319def2014-10-23 10:03:10 +01006563} // namespace arm64
6564} // namespace art