blob: 3be774a4216fdcb0f02e5e2af03e92766b052f6f [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"
Andreas Gampe5678db52017-06-08 14:11:18 -070022#include "base/bit_utils.h"
23#include "base/bit_utils_iterator.h"
Zheng Xuc6667102015-05-15 16:08:45 +080024#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000025#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010026#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080027#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080029#include "intrinsics.h"
30#include "intrinsics_arm64.h"
Vladimir Markof4f2daa2017-03-20 18:26:59 +000031#include "linker/arm64/relative_patcher_arm64.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070032#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010033#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070034#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000035#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010036#include "thread.h"
37#include "utils/arm64/assembler_arm64.h"
38#include "utils/assembler.h"
39#include "utils/stack_checks.h"
40
Scott Wakeling97c72b72016-06-24 16:19:36 +010041using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000042using vixl::ExactAssemblyScope;
43using vixl::CodeBufferCheckScope;
44using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010045
46#ifdef __
47#error "ARM64 Codegen VIXL macro-assembler macro already defined."
48#endif
49
Alexandre Rames5319def2014-10-23 10:03:10 +010050namespace art {
51
Roland Levillain22ccc3a2015-11-24 13:10:05 +000052template<class MirrorType>
53class GcRoot;
54
Alexandre Rames5319def2014-10-23 10:03:10 +010055namespace arm64 {
56
Alexandre Ramesbe919d92016-08-23 18:33:36 +010057using helpers::ARM64EncodableConstantOrRegister;
58using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080059using helpers::CPURegisterFrom;
60using helpers::DRegisterFrom;
61using helpers::FPRegisterFrom;
62using helpers::HeapOperand;
63using helpers::HeapOperandFrom;
64using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010065using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080066using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080067using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010068using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010070using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080071using helpers::LocationFrom;
72using helpers::OperandFromMemOperand;
73using helpers::OutputCPURegister;
74using helpers::OutputFPRegister;
75using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010076using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080077using helpers::RegisterFrom;
78using helpers::StackOperandFrom;
79using helpers::VIXLRegCodeFromART;
80using helpers::WRegisterFrom;
81using helpers::XRegisterFrom;
82
Vladimir Markof3e0ee22015-12-17 15:23:13 +000083// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080084// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
85// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000086static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010087
Vladimir Markof4f2daa2017-03-20 18:26:59 +000088// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
89// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
90// For the Baker read barrier implementation using link-generated thunks we need to split
91// the offset explicitly.
92constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
93
94// Flags controlling the use of link-time generated thunks for Baker read barriers.
Vladimir Markod1ef8732017-04-18 13:55:13 +010095constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
Vladimir Marko66d691d2017-04-07 17:53:39 +010096constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
Vladimir Markod1ef8732017-04-18 13:55:13 +010097constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
Vladimir Markof4f2daa2017-03-20 18:26:59 +000098
99// Some instructions have special requirements for a temporary, for example
100// LoadClass/kBssEntry and LoadString/kBssEntry for Baker read barrier require
101// temp that's not an R0 (to avoid an extra move) and Baker read barrier field
102// loads with large offsets need a fixed register to limit the number of link-time
103// thunks we generate. For these and similar cases, we want to reserve a specific
104// register that's neither callee-save nor an argument register. We choose x15.
105inline Location FixedTempLocation() {
106 return Location::RegisterLocation(x15.GetCode());
107}
108
Alexandre Rames5319def2014-10-23 10:03:10 +0100109inline Condition ARM64Condition(IfCondition cond) {
110 switch (cond) {
111 case kCondEQ: return eq;
112 case kCondNE: return ne;
113 case kCondLT: return lt;
114 case kCondLE: return le;
115 case kCondGT: return gt;
116 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700117 case kCondB: return lo;
118 case kCondBE: return ls;
119 case kCondA: return hi;
120 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100121 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000122 LOG(FATAL) << "Unreachable";
123 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100124}
125
Vladimir Markod6e069b2016-01-18 11:11:01 +0000126inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
127 // The ARM64 condition codes can express all the necessary branches, see the
128 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
129 // There is no dex instruction or HIR that would need the missing conditions
130 // "equal or unordered" or "not equal".
131 switch (cond) {
132 case kCondEQ: return eq;
133 case kCondNE: return ne /* unordered */;
134 case kCondLT: return gt_bias ? cc : lt /* unordered */;
135 case kCondLE: return gt_bias ? ls : le /* unordered */;
136 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
137 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
138 default:
139 LOG(FATAL) << "UNREACHABLE";
140 UNREACHABLE();
141 }
142}
143
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000144Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000145 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
146 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
147 // but we use the exact registers for clarity.
148 if (return_type == Primitive::kPrimFloat) {
149 return LocationFrom(s0);
150 } else if (return_type == Primitive::kPrimDouble) {
151 return LocationFrom(d0);
152 } else if (return_type == Primitive::kPrimLong) {
153 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100154 } else if (return_type == Primitive::kPrimVoid) {
155 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000156 } else {
157 return LocationFrom(w0);
158 }
159}
160
Alexandre Rames5319def2014-10-23 10:03:10 +0100161Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000162 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100163}
164
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100165// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
166#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700167#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100168
Zheng Xuda403092015-04-24 17:35:39 +0800169// Calculate memory accessing operand for save/restore live registers.
170static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100171 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800172 int64_t spill_offset,
173 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100174 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
175 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
176 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800177 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100178 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800179 codegen->GetNumberOfFloatingPointRegisters()));
180
Vladimir Marko804b03f2016-09-14 16:26:36 +0100181 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100182 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
183 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800184
185 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
186 UseScratchRegisterScope temps(masm);
187
188 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100189 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
190 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800191 int64_t reg_size = kXRegSizeInBytes;
192 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
193 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100194 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800195 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
196 // If the offset does not fit in the instruction's immediate field, use an alternate register
197 // to compute the base address(float point registers spill base address).
198 Register new_base = temps.AcquireSameSizeAs(base);
199 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
200 base = new_base;
201 spill_offset = -core_spill_size;
202 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
203 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
204 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
205 }
206
207 if (is_save) {
208 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
209 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
210 } else {
211 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
212 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
213 }
214}
215
216void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800217 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100218 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
219 for (uint32_t i : LowToHighBits(core_spills)) {
220 // If the register holds an object, update the stack mask.
221 if (locations->RegisterContainsObject(i)) {
222 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800223 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100224 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
225 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
226 saved_core_stack_offsets_[i] = stack_offset;
227 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800228 }
229
Vladimir Marko804b03f2016-09-14 16:26:36 +0100230 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
231 for (uint32_t i : LowToHighBits(fp_spills)) {
232 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
233 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
234 saved_fpu_stack_offsets_[i] = stack_offset;
235 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800236 }
237
Vladimir Marko804b03f2016-09-14 16:26:36 +0100238 SaveRestoreLiveRegistersHelper(codegen,
239 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800240 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
241}
242
243void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100244 SaveRestoreLiveRegistersHelper(codegen,
245 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800246 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
247}
248
Alexandre Rames5319def2014-10-23 10:03:10 +0100249class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
250 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000251 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100252
Alexandre Rames67555f72014-11-18 10:55:16 +0000253 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100254 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000255 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100256
Alexandre Rames5319def2014-10-23 10:03:10 +0100257 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000258 if (instruction_->CanThrowIntoCatchBlock()) {
259 // Live registers will be restored in the catch block if caught.
260 SaveLiveRegisters(codegen, instruction_->GetLocations());
261 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000262 // We're moving two locations to locations that could overlap, so we need a parallel
263 // move resolver.
264 InvokeRuntimeCallingConvention calling_convention;
265 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100266 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
267 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000268 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
269 ? kQuickThrowStringBounds
270 : kQuickThrowArrayBounds;
271 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100272 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800273 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100274 }
275
Alexandre Rames8158f282015-08-07 10:26:17 +0100276 bool IsFatal() const OVERRIDE { return true; }
277
Alexandre Rames9931f312015-06-19 14:47:01 +0100278 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
279
Alexandre Rames5319def2014-10-23 10:03:10 +0100280 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100281 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
282};
283
Alexandre Rames67555f72014-11-18 10:55:16 +0000284class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
285 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000286 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000287
288 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
289 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
290 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000291 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800292 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000293 }
294
Alexandre Rames8158f282015-08-07 10:26:17 +0100295 bool IsFatal() const OVERRIDE { return true; }
296
Alexandre Rames9931f312015-06-19 14:47:01 +0100297 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
298
Alexandre Rames67555f72014-11-18 10:55:16 +0000299 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000300 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
301};
302
303class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
304 public:
305 LoadClassSlowPathARM64(HLoadClass* cls,
306 HInstruction* at,
307 uint32_t dex_pc,
Vladimir Markoea4c1262017-02-06 19:59:33 +0000308 bool do_clinit,
309 vixl::aarch64::Register bss_entry_temp = vixl::aarch64::Register(),
310 vixl::aarch64::Label* bss_entry_adrp_label = nullptr)
311 : SlowPathCodeARM64(at),
312 cls_(cls),
313 dex_pc_(dex_pc),
314 do_clinit_(do_clinit),
315 bss_entry_temp_(bss_entry_temp),
316 bss_entry_adrp_label_(bss_entry_adrp_label) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000317 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
318 }
319
320 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000321 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000322 Location out = locations->Out();
323 constexpr bool call_saves_everything_except_r0_ip0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexandre Rames67555f72014-11-18 10:55:16 +0000324 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
325
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000326 InvokeRuntimeCallingConvention calling_convention;
327 // For HLoadClass/kBssEntry/kSaveEverything, the page address of the entry is in a temp
328 // register, make sure it's not clobbered by the call or by saving/restoring registers.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000329 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
330 bool is_load_class_bss_entry =
331 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000332 if (is_load_class_bss_entry) {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000333 DCHECK(bss_entry_temp_.IsValid());
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000334 DCHECK(!bss_entry_temp_.Is(calling_convention.GetRegisterAt(0)));
335 DCHECK(
336 !UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(bss_entry_temp_));
Vladimir Markoea4c1262017-02-06 19:59:33 +0000337 }
338
Alexandre Rames67555f72014-11-18 10:55:16 +0000339 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000340 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000341
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000342 dex::TypeIndex type_index = cls_->GetTypeIndex();
343 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000344 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
345 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000346 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800347 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100348 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800349 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100350 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800351 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000352
353 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000354 if (out.IsValid()) {
355 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000356 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000357 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000358 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000359 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000360 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000361 if (is_load_class_bss_entry) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000362 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000363 const DexFile& dex_file = cls_->GetDexFile();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000364 if (call_saves_everything_except_r0_ip0) {
365 // The class entry page address was preserved in bss_entry_temp_ thanks to kSaveEverything.
366 } else {
367 // For non-Baker read barrier, we need to re-calculate the address of the class entry page.
368 bss_entry_adrp_label_ = arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
369 arm64_codegen->EmitAdrpPlaceholder(bss_entry_adrp_label_, bss_entry_temp_);
370 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000371 vixl::aarch64::Label* strp_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +0000372 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label_);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000373 {
374 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
375 __ Bind(strp_label);
376 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Markoea4c1262017-02-06 19:59:33 +0000377 MemOperand(bss_entry_temp_, /* offset placeholder */ 0));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000378 }
379 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000380 __ B(GetExitLabel());
381 }
382
Alexandre Rames9931f312015-06-19 14:47:01 +0100383 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
384
Alexandre Rames67555f72014-11-18 10:55:16 +0000385 private:
386 // The class this slow path will load.
387 HLoadClass* const cls_;
388
Alexandre Rames67555f72014-11-18 10:55:16 +0000389 // The dex PC of `at_`.
390 const uint32_t dex_pc_;
391
392 // Whether to initialize the class.
393 const bool do_clinit_;
394
Vladimir Markoea4c1262017-02-06 19:59:33 +0000395 // For HLoadClass/kBssEntry, the temp register and the label of the ADRP where it was loaded.
396 vixl::aarch64::Register bss_entry_temp_;
397 vixl::aarch64::Label* bss_entry_adrp_label_;
398
Alexandre Rames67555f72014-11-18 10:55:16 +0000399 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
400};
401
Vladimir Markoaad75c62016-10-03 08:46:48 +0000402class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
403 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100404 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
405 : SlowPathCodeARM64(instruction),
406 temp_(temp),
407 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000408
409 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
410 LocationSummary* locations = instruction_->GetLocations();
411 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
412 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
413
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000414 InvokeRuntimeCallingConvention calling_convention;
415 // Make sure `temp_` is not clobbered by the call or by saving/restoring registers.
416 DCHECK(temp_.IsValid());
417 DCHECK(!temp_.Is(calling_convention.GetRegisterAt(0)));
418 DCHECK(!UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(temp_));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100419
Vladimir Markoaad75c62016-10-03 08:46:48 +0000420 __ Bind(GetEntryLabel());
421 SaveLiveRegisters(codegen, locations);
422
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000423 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
424 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000425 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
426 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
427 Primitive::Type type = instruction_->GetType();
428 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
429
430 RestoreLiveRegisters(codegen, locations);
431
432 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000433 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100434 if (!kUseReadBarrier || kUseBakerReadBarrier) {
435 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
436 } else {
437 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
438 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
439 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
440 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000441 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100442 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000443 {
444 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
445 __ Bind(strp_label);
446 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100447 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000448 }
449
450 __ B(GetExitLabel());
451 }
452
453 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
454
455 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100456 const Register temp_;
457 vixl::aarch64::Label* adrp_label_;
458
Vladimir Markoaad75c62016-10-03 08:46:48 +0000459 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
460};
461
Alexandre Rames5319def2014-10-23 10:03:10 +0100462class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
463 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000464 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100465
Alexandre Rames67555f72014-11-18 10:55:16 +0000466 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
467 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100468 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000469 if (instruction_->CanThrowIntoCatchBlock()) {
470 // Live registers will be restored in the catch block if caught.
471 SaveLiveRegisters(codegen, instruction_->GetLocations());
472 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000473 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
474 instruction_,
475 instruction_->GetDexPc(),
476 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800477 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100478 }
479
Alexandre Rames8158f282015-08-07 10:26:17 +0100480 bool IsFatal() const OVERRIDE { return true; }
481
Alexandre Rames9931f312015-06-19 14:47:01 +0100482 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
483
Alexandre Rames5319def2014-10-23 10:03:10 +0100484 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100485 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
486};
487
488class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
489 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100490 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000491 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100492
Alexandre Rames67555f72014-11-18 10:55:16 +0000493 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100494 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000495 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100496 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100497 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000498 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800499 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100500 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000501 if (successor_ == nullptr) {
502 __ B(GetReturnLabel());
503 } else {
504 __ B(arm64_codegen->GetLabelOf(successor_));
505 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100506 }
507
Scott Wakeling97c72b72016-06-24 16:19:36 +0100508 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100509 DCHECK(successor_ == nullptr);
510 return &return_label_;
511 }
512
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100513 HBasicBlock* GetSuccessor() const {
514 return successor_;
515 }
516
Alexandre Rames9931f312015-06-19 14:47:01 +0100517 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
518
Alexandre Rames5319def2014-10-23 10:03:10 +0100519 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100520 // If not null, the block to branch to after the suspend check.
521 HBasicBlock* const successor_;
522
523 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100524 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100525
526 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
527};
528
Alexandre Rames67555f72014-11-18 10:55:16 +0000529class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
530 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000531 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000532 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000533
534 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000535 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800536
Alexandre Rames3e69f162014-12-10 10:36:50 +0000537 DCHECK(instruction_->IsCheckCast()
538 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
539 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100540 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000541
Alexandre Rames67555f72014-11-18 10:55:16 +0000542 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000543
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000544 if (!is_fatal_) {
545 SaveLiveRegisters(codegen, locations);
546 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000547
548 // We're moving two locations to locations that could overlap, so we need a parallel
549 // move resolver.
550 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800551 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800552 LocationFrom(calling_convention.GetRegisterAt(0)),
553 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800554 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800555 LocationFrom(calling_convention.GetRegisterAt(1)),
556 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000557 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000558 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800559 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000560 Primitive::Type ret_type = instruction_->GetType();
561 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
562 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
563 } else {
564 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800565 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
566 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000567 }
568
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000569 if (!is_fatal_) {
570 RestoreLiveRegisters(codegen, locations);
571 __ B(GetExitLabel());
572 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000573 }
574
Alexandre Rames9931f312015-06-19 14:47:01 +0100575 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100576 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100577
Alexandre Rames67555f72014-11-18 10:55:16 +0000578 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000579 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000580
Alexandre Rames67555f72014-11-18 10:55:16 +0000581 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
582};
583
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700584class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
585 public:
Aart Bik42249c32016-01-07 15:33:50 -0800586 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000587 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700588
589 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800590 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700591 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100592 LocationSummary* locations = instruction_->GetLocations();
593 SaveLiveRegisters(codegen, locations);
594 InvokeRuntimeCallingConvention calling_convention;
595 __ Mov(calling_convention.GetRegisterAt(0),
596 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000597 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100598 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700599 }
600
Alexandre Rames9931f312015-06-19 14:47:01 +0100601 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
602
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700603 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700604 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
605};
606
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100607class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
608 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000609 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100610
611 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
612 LocationSummary* locations = instruction_->GetLocations();
613 __ Bind(GetEntryLabel());
614 SaveLiveRegisters(codegen, locations);
615
616 InvokeRuntimeCallingConvention calling_convention;
617 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
618 parallel_move.AddMove(
619 locations->InAt(0),
620 LocationFrom(calling_convention.GetRegisterAt(0)),
621 Primitive::kPrimNot,
622 nullptr);
623 parallel_move.AddMove(
624 locations->InAt(1),
625 LocationFrom(calling_convention.GetRegisterAt(1)),
626 Primitive::kPrimInt,
627 nullptr);
628 parallel_move.AddMove(
629 locations->InAt(2),
630 LocationFrom(calling_convention.GetRegisterAt(2)),
631 Primitive::kPrimNot,
632 nullptr);
633 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
634
635 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000636 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100637 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
638 RestoreLiveRegisters(codegen, locations);
639 __ B(GetExitLabel());
640 }
641
642 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
643
644 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100645 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
646};
647
Zheng Xu3927c8b2015-11-18 17:46:25 +0800648void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
649 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000650 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800651
652 // We are about to use the assembler to place literals directly. Make sure we have enough
653 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000654 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
655 num_entries * sizeof(int32_t),
656 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800657
658 __ Bind(&table_start_);
659 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
660 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100661 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800662 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100663 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800664 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
665 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
666 Literal<int32_t> literal(jump_offset);
667 __ place(&literal);
668 }
669}
670
Roland Levillain54f869e2017-03-06 13:54:11 +0000671// Abstract base class for read barrier slow paths marking a reference
672// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000673//
Roland Levillain54f869e2017-03-06 13:54:11 +0000674// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100675// barrier marking runtime entry point to be invoked or an empty
676// location; in the latter case, the read barrier marking runtime
677// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000678class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
679 protected:
680 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
681 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000682 DCHECK(kEmitCompilerReadBarrier);
683 }
684
Roland Levillain54f869e2017-03-06 13:54:11 +0000685 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000686
Roland Levillain54f869e2017-03-06 13:54:11 +0000687 // Generate assembly code calling the read barrier marking runtime
688 // entry point (ReadBarrierMarkRegX).
689 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000690 // No need to save live registers; it's taken care of by the
691 // entrypoint. Also, there is no need to update the stack mask,
692 // as this runtime call will not trigger a garbage collection.
693 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
694 DCHECK_NE(ref_.reg(), LR);
695 DCHECK_NE(ref_.reg(), WSP);
696 DCHECK_NE(ref_.reg(), WZR);
697 // IP0 is used internally by the ReadBarrierMarkRegX entry point
698 // as a temporary, it cannot be the entry point's input/output.
699 DCHECK_NE(ref_.reg(), IP0);
700 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
701 // "Compact" slow path, saving two moves.
702 //
703 // Instead of using the standard runtime calling convention (input
704 // and output in W0):
705 //
706 // W0 <- ref
707 // W0 <- ReadBarrierMark(W0)
708 // ref <- W0
709 //
710 // we just use rX (the register containing `ref`) as input and output
711 // of a dedicated entrypoint:
712 //
713 // rX <- ReadBarrierMarkRegX(rX)
714 //
715 if (entrypoint_.IsValid()) {
716 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
717 __ Blr(XRegisterFrom(entrypoint_));
718 } else {
719 // Entrypoint is not already loaded, load from the thread.
720 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100721 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000722 // This runtime call does not require a stack map.
723 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
724 }
725 }
726
727 // The location (register) of the marked object reference.
728 const Location ref_;
729
730 // The location of the entrypoint if it is already loaded.
731 const Location entrypoint_;
732
Roland Levillain54f869e2017-03-06 13:54:11 +0000733 private:
734 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
735};
736
Alexandre Rames5319def2014-10-23 10:03:10 +0100737// Slow path marking an object reference `ref` during a read
738// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000739// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100740//
741// This means that after the execution of this slow path, `ref` will
742// always be up-to-date, but `obj.field` may not; i.e., after the
743// flip, `ref` will be a to-space reference, but `obj.field` will
744// probably still be a from-space reference (unless it gets updated by
745// another thread, or if another thread installed another object
746// reference (different from `ref`) in `obj.field`).
747//
Roland Levillain97c46462017-05-11 14:04:03 +0100748// Argument `entrypoint` must be a register location holding the read
749// barrier marking runtime entry point to be invoked or an empty
750// location; in the latter case, the read barrier marking runtime
751// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000752class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100753 public:
754 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
755 Location ref,
756 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000757 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100758 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100759 }
760
761 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
762
763 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000764 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100765 DCHECK(locations->CanCall());
766 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000767 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000768 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
769 << "Unexpected instruction in read barrier marking slow path: "
770 << instruction_->DebugName();
771
772 __ Bind(GetEntryLabel());
773 GenerateReadBarrierMarkRuntimeCall(codegen);
774 __ B(GetExitLabel());
775 }
776
777 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000778 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
779};
780
Roland Levillain54f869e2017-03-06 13:54:11 +0000781// Slow path loading `obj`'s lock word, loading a reference from
782// object `*(obj + offset + (index << scale_factor))` into `ref`, and
783// marking `ref` if `obj` is gray according to the lock word (Baker
784// read barrier). The field `obj.field` in the object `obj` holding
785// this reference does not get updated by this slow path after marking
786// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
787// below for that).
788//
789// This means that after the execution of this slow path, `ref` will
790// always be up-to-date, but `obj.field` may not; i.e., after the
791// flip, `ref` will be a to-space reference, but `obj.field` will
792// probably still be a from-space reference (unless it gets updated by
793// another thread, or if another thread installed another object
794// reference (different from `ref`) in `obj.field`).
795//
796// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100797// barrier marking runtime entry point to be invoked or an empty
798// location; in the latter case, the read barrier marking runtime
799// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000800class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
801 public:
802 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
803 Location ref,
804 Register obj,
805 uint32_t offset,
806 Location index,
807 size_t scale_factor,
808 bool needs_null_check,
809 bool use_load_acquire,
810 Register temp,
Roland Levillain97c46462017-05-11 14:04:03 +0100811 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000812 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
813 obj_(obj),
814 offset_(offset),
815 index_(index),
816 scale_factor_(scale_factor),
817 needs_null_check_(needs_null_check),
818 use_load_acquire_(use_load_acquire),
819 temp_(temp) {
820 DCHECK(kEmitCompilerReadBarrier);
821 DCHECK(kUseBakerReadBarrier);
822 }
823
824 const char* GetDescription() const OVERRIDE {
825 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
826 }
827
828 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
829 LocationSummary* locations = instruction_->GetLocations();
830 DCHECK(locations->CanCall());
831 DCHECK(ref_.IsRegister()) << ref_;
832 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
833 DCHECK(obj_.IsW());
834 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100835 DCHECK(instruction_->IsInstanceFieldGet() ||
836 instruction_->IsStaticFieldGet() ||
837 instruction_->IsArrayGet() ||
838 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100839 instruction_->IsInstanceOf() ||
840 instruction_->IsCheckCast() ||
841 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
842 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
843 << "Unexpected instruction in read barrier marking slow path: "
844 << instruction_->DebugName();
845 // The read barrier instrumentation of object ArrayGet
846 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000847 // instruction.
848 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000849 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
850
Roland Levillain54f869e2017-03-06 13:54:11 +0000851 // Temporary register `temp_`, used to store the lock word, must
852 // not be IP0 nor IP1, as we may use them to emit the reference
853 // load (in the call to GenerateRawReferenceLoad below), and we
854 // need the lock word to still be in `temp_` after the reference
855 // load.
856 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
857 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
858
Alexandre Rames5319def2014-10-23 10:03:10 +0100859 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000860
861 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
862 // inserted after the original load. However, in fast path based
863 // Baker's read barriers, we need to perform the load of
864 // mirror::Object::monitor_ *before* the original reference load.
865 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000866 // The slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100867 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000868 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
869 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
870 // HeapReference<mirror::Object> ref = *src; // Original reference load.
871 // bool is_gray = (rb_state == ReadBarrier::GrayState());
872 // if (is_gray) {
873 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
874 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000875 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000876 // Note: the original implementation in ReadBarrier::Barrier is
877 // slightly more complex as it performs additional checks that we do
878 // not do here for performance reasons.
879
880 // /* int32_t */ monitor = obj->monitor_
881 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
882 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
883 if (needs_null_check_) {
884 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100885 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000886 // /* LockWord */ lock_word = LockWord(monitor)
887 static_assert(sizeof(LockWord) == sizeof(int32_t),
888 "art::LockWord and int32_t have different sizes.");
889
890 // Introduce a dependency on the lock_word including rb_state,
891 // to prevent load-load reordering, and without using
892 // a memory barrier (which would be more expensive).
893 // `obj` is unchanged by this operation, but its value now depends
894 // on `temp`.
895 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
896
897 // The actual reference load.
898 // A possible implicit null check has already been handled above.
899 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
900 arm64_codegen->GenerateRawReferenceLoad(instruction_,
901 ref_,
902 obj_,
903 offset_,
904 index_,
905 scale_factor_,
906 /* needs_null_check */ false,
907 use_load_acquire_);
908
909 // Mark the object `ref` when `obj` is gray.
910 //
911 // if (rb_state == ReadBarrier::GrayState())
912 // ref = ReadBarrier::Mark(ref);
913 //
914 // Given the numeric representation, it's enough to check the low bit of the rb_state.
915 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
916 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
917 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
918 GenerateReadBarrierMarkRuntimeCall(codegen);
919
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000920 __ B(GetExitLabel());
921 }
922
923 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000924 // The register containing the object holding the marked object reference field.
925 Register obj_;
926 // The offset, index and scale factor to access the reference in `obj_`.
927 uint32_t offset_;
928 Location index_;
929 size_t scale_factor_;
930 // Is a null check required?
931 bool needs_null_check_;
932 // Should this reference load use Load-Acquire semantics?
933 bool use_load_acquire_;
934 // A temporary register used to hold the lock word of `obj_`.
935 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000936
Roland Levillain54f869e2017-03-06 13:54:11 +0000937 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000938};
939
Roland Levillain54f869e2017-03-06 13:54:11 +0000940// Slow path loading `obj`'s lock word, loading a reference from
941// object `*(obj + offset + (index << scale_factor))` into `ref`, and
942// marking `ref` if `obj` is gray according to the lock word (Baker
943// read barrier). If needed, this slow path also atomically updates
944// the field `obj.field` in the object `obj` holding this reference
945// after marking (contrary to
946// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
947// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100948//
949// This means that after the execution of this slow path, both `ref`
950// and `obj.field` will be up-to-date; i.e., after the flip, both will
951// hold the same to-space reference (unless another thread installed
952// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000953//
Roland Levillain54f869e2017-03-06 13:54:11 +0000954// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100955// barrier marking runtime entry point to be invoked or an empty
956// location; in the latter case, the read barrier marking runtime
957// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000958class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
959 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100960 public:
Roland Levillain97c46462017-05-11 14:04:03 +0100961 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
962 HInstruction* instruction,
963 Location ref,
964 Register obj,
965 uint32_t offset,
966 Location index,
967 size_t scale_factor,
968 bool needs_null_check,
969 bool use_load_acquire,
970 Register temp,
971 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000972 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100973 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000974 offset_(offset),
975 index_(index),
976 scale_factor_(scale_factor),
977 needs_null_check_(needs_null_check),
978 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000979 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100980 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000981 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100982 }
983
984 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000985 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100986 }
987
988 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
989 LocationSummary* locations = instruction_->GetLocations();
990 Register ref_reg = WRegisterFrom(ref_);
991 DCHECK(locations->CanCall());
992 DCHECK(ref_.IsRegister()) << ref_;
993 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000994 DCHECK(obj_.IsW());
995 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
996
997 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100998 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
999 << "Unexpected instruction in read barrier marking and field updating slow path: "
1000 << instruction_->DebugName();
1001 DCHECK(instruction_->GetLocations()->Intrinsified());
1002 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +00001003 DCHECK_EQ(offset_, 0u);
1004 DCHECK_EQ(scale_factor_, 0u);
1005 DCHECK_EQ(use_load_acquire_, false);
1006 // The location of the offset of the marked reference field within `obj_`.
1007 Location field_offset = index_;
1008 DCHECK(field_offset.IsRegister()) << field_offset;
1009
1010 // Temporary register `temp_`, used to store the lock word, must
1011 // not be IP0 nor IP1, as we may use them to emit the reference
1012 // load (in the call to GenerateRawReferenceLoad below), and we
1013 // need the lock word to still be in `temp_` after the reference
1014 // load.
1015 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1016 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001017
1018 __ Bind(GetEntryLabel());
1019
Roland Levillainff487002017-03-07 16:50:01 +00001020 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM64's:
1021 //
1022 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1023 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1024 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1025 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1026 // if (is_gray) {
1027 // old_ref = ref;
1028 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1029 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1030 // }
1031
Roland Levillain54f869e2017-03-06 13:54:11 +00001032 // /* int32_t */ monitor = obj->monitor_
1033 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1034 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
1035 if (needs_null_check_) {
1036 codegen->MaybeRecordImplicitNullCheck(instruction_);
1037 }
1038 // /* LockWord */ lock_word = LockWord(monitor)
1039 static_assert(sizeof(LockWord) == sizeof(int32_t),
1040 "art::LockWord and int32_t have different sizes.");
1041
1042 // Introduce a dependency on the lock_word including rb_state,
1043 // to prevent load-load reordering, and without using
1044 // a memory barrier (which would be more expensive).
1045 // `obj` is unchanged by this operation, but its value now depends
1046 // on `temp`.
1047 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
1048
1049 // The actual reference load.
1050 // A possible implicit null check has already been handled above.
1051 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1052 arm64_codegen->GenerateRawReferenceLoad(instruction_,
1053 ref_,
1054 obj_,
1055 offset_,
1056 index_,
1057 scale_factor_,
1058 /* needs_null_check */ false,
1059 use_load_acquire_);
1060
1061 // Mark the object `ref` when `obj` is gray.
1062 //
1063 // if (rb_state == ReadBarrier::GrayState())
1064 // ref = ReadBarrier::Mark(ref);
1065 //
1066 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1067 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1068 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1069 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1070
1071 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001072 // Note that we cannot use IP to save the old reference, as IP is
1073 // used internally by the ReadBarrierMarkRegX entry point, and we
1074 // need the old reference after the call to that entry point.
1075 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1076 __ Mov(temp_.W(), ref_reg);
1077
Roland Levillain54f869e2017-03-06 13:54:11 +00001078 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001079
1080 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001081 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001082 //
1083 // Note that this field could also hold a different object, if
1084 // another thread had concurrently changed it. In that case, the
1085 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1086 // (CAS) operation below would abort the CAS, leaving the field
1087 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001088 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001089 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001090
1091 // Update the the holder's field atomically. This may fail if
1092 // mutator updates before us, but it's OK. This is achieved
1093 // using a strong compare-and-set (CAS) operation with relaxed
1094 // memory synchronization ordering, where the expected value is
1095 // the old reference and the desired value is the new reference.
1096
1097 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1098 UseScratchRegisterScope temps(masm);
1099
1100 // Convenience aliases.
1101 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001102 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001103 Register expected = temp_.W();
1104 Register value = ref_reg;
1105 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1106 Register tmp_value = temps.AcquireW(); // Value in memory.
1107
1108 __ Add(tmp_ptr, base.X(), Operand(offset));
1109
1110 if (kPoisonHeapReferences) {
1111 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1112 if (value.Is(expected)) {
1113 // Do not poison `value`, as it is the same register as
1114 // `expected`, which has just been poisoned.
1115 } else {
1116 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1117 }
1118 }
1119
1120 // do {
1121 // tmp_value = [tmp_ptr] - expected;
1122 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1123
Roland Levillain24a4d112016-10-26 13:10:46 +01001124 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001125 __ Bind(&loop_head);
1126 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1127 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001128 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001129 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1130 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001131 __ B(&exit_loop);
1132 __ Bind(&comparison_failed);
1133 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001134 __ Bind(&exit_loop);
1135
1136 if (kPoisonHeapReferences) {
1137 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1138 if (value.Is(expected)) {
1139 // Do not unpoison `value`, as it is the same register as
1140 // `expected`, which has just been unpoisoned.
1141 } else {
1142 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1143 }
1144 }
1145
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001146 __ B(GetExitLabel());
1147 }
1148
1149 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001150 // The register containing the object holding the marked object reference field.
1151 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001152 // The offset, index and scale factor to access the reference in `obj_`.
1153 uint32_t offset_;
1154 Location index_;
1155 size_t scale_factor_;
1156 // Is a null check required?
1157 bool needs_null_check_;
1158 // Should this reference load use Load-Acquire semantics?
1159 bool use_load_acquire_;
1160 // A temporary register used to hold the lock word of `obj_`; and
1161 // also to hold the original reference value, when the reference is
1162 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001163 const Register temp_;
1164
Roland Levillain54f869e2017-03-06 13:54:11 +00001165 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001166};
1167
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001168// Slow path generating a read barrier for a heap reference.
1169class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1170 public:
1171 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1172 Location out,
1173 Location ref,
1174 Location obj,
1175 uint32_t offset,
1176 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001177 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001178 out_(out),
1179 ref_(ref),
1180 obj_(obj),
1181 offset_(offset),
1182 index_(index) {
1183 DCHECK(kEmitCompilerReadBarrier);
1184 // If `obj` is equal to `out` or `ref`, it means the initial object
1185 // has been overwritten by (or after) the heap object reference load
1186 // to be instrumented, e.g.:
1187 //
1188 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001189 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001190 //
1191 // In that case, we have lost the information about the original
1192 // object, and the emitted read barrier cannot work properly.
1193 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1194 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1195 }
1196
1197 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1198 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1199 LocationSummary* locations = instruction_->GetLocations();
1200 Primitive::Type type = Primitive::kPrimNot;
1201 DCHECK(locations->CanCall());
1202 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001203 DCHECK(instruction_->IsInstanceFieldGet() ||
1204 instruction_->IsStaticFieldGet() ||
1205 instruction_->IsArrayGet() ||
1206 instruction_->IsInstanceOf() ||
1207 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001208 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001209 << "Unexpected instruction in read barrier for heap reference slow path: "
1210 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001211 // The read barrier instrumentation of object ArrayGet
1212 // instructions does not support the HIntermediateAddress
1213 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001214 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001215 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001216
1217 __ Bind(GetEntryLabel());
1218
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001219 SaveLiveRegisters(codegen, locations);
1220
1221 // We may have to change the index's value, but as `index_` is a
1222 // constant member (like other "inputs" of this slow path),
1223 // introduce a copy of it, `index`.
1224 Location index = index_;
1225 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001226 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001227 if (instruction_->IsArrayGet()) {
1228 // Compute the actual memory offset and store it in `index`.
1229 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
1230 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1231 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1232 // We are about to change the value of `index_reg` (see the
1233 // calls to vixl::MacroAssembler::Lsl and
1234 // vixl::MacroAssembler::Mov below), but it has
1235 // not been saved by the previous call to
1236 // art::SlowPathCode::SaveLiveRegisters, as it is a
1237 // callee-save register --
1238 // art::SlowPathCode::SaveLiveRegisters does not consider
1239 // callee-save registers, as it has been designed with the
1240 // assumption that callee-save registers are supposed to be
1241 // handled by the called function. So, as a callee-save
1242 // register, `index_reg` _would_ eventually be saved onto
1243 // the stack, but it would be too late: we would have
1244 // changed its value earlier. Therefore, we manually save
1245 // it here into another freely available register,
1246 // `free_reg`, chosen of course among the caller-save
1247 // registers (as a callee-save `free_reg` register would
1248 // exhibit the same problem).
1249 //
1250 // Note we could have requested a temporary register from
1251 // the register allocator instead; but we prefer not to, as
1252 // this is a slow path, and we know we can find a
1253 // caller-save register that is available.
1254 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1255 __ Mov(free_reg.W(), index_reg);
1256 index_reg = free_reg;
1257 index = LocationFrom(index_reg);
1258 } else {
1259 // The initial register stored in `index_` has already been
1260 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1261 // (as it is not a callee-save register), so we can freely
1262 // use it.
1263 }
1264 // Shifting the index value contained in `index_reg` by the scale
1265 // factor (2) cannot overflow in practice, as the runtime is
1266 // unable to allocate object arrays with a size larger than
1267 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1268 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1269 static_assert(
1270 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1271 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1272 __ Add(index_reg, index_reg, Operand(offset_));
1273 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001274 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1275 // intrinsics, `index_` is not shifted by a scale factor of 2
1276 // (as in the case of ArrayGet), as it is actually an offset
1277 // to an object field within an object.
1278 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001279 DCHECK(instruction_->GetLocations()->Intrinsified());
1280 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1281 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1282 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001283 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001284 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001285 }
1286 }
1287
1288 // We're moving two or three locations to locations that could
1289 // overlap, so we need a parallel move resolver.
1290 InvokeRuntimeCallingConvention calling_convention;
1291 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1292 parallel_move.AddMove(ref_,
1293 LocationFrom(calling_convention.GetRegisterAt(0)),
1294 type,
1295 nullptr);
1296 parallel_move.AddMove(obj_,
1297 LocationFrom(calling_convention.GetRegisterAt(1)),
1298 type,
1299 nullptr);
1300 if (index.IsValid()) {
1301 parallel_move.AddMove(index,
1302 LocationFrom(calling_convention.GetRegisterAt(2)),
1303 Primitive::kPrimInt,
1304 nullptr);
1305 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1306 } else {
1307 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1308 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1309 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001310 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001311 instruction_,
1312 instruction_->GetDexPc(),
1313 this);
1314 CheckEntrypointTypes<
1315 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1316 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1317
1318 RestoreLiveRegisters(codegen, locations);
1319
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001320 __ B(GetExitLabel());
1321 }
1322
1323 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1324
1325 private:
1326 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001327 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1328 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001329 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1330 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1331 return Register(VIXLRegCodeFromART(i), kXRegSize);
1332 }
1333 }
1334 // We shall never fail to find a free caller-save register, as
1335 // there are more than two core caller-save registers on ARM64
1336 // (meaning it is possible to find one which is different from
1337 // `ref` and `obj`).
1338 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1339 LOG(FATAL) << "Could not find a free register";
1340 UNREACHABLE();
1341 }
1342
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001343 const Location out_;
1344 const Location ref_;
1345 const Location obj_;
1346 const uint32_t offset_;
1347 // An additional location containing an index to an array.
1348 // Only used for HArrayGet and the UnsafeGetObject &
1349 // UnsafeGetObjectVolatile intrinsics.
1350 const Location index_;
1351
1352 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1353};
1354
1355// Slow path generating a read barrier for a GC root.
1356class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1357 public:
1358 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001359 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001360 DCHECK(kEmitCompilerReadBarrier);
1361 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001362
1363 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1364 LocationSummary* locations = instruction_->GetLocations();
1365 Primitive::Type type = Primitive::kPrimNot;
1366 DCHECK(locations->CanCall());
1367 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001368 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1369 << "Unexpected instruction in read barrier for GC root slow path: "
1370 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001371
1372 __ Bind(GetEntryLabel());
1373 SaveLiveRegisters(codegen, locations);
1374
1375 InvokeRuntimeCallingConvention calling_convention;
1376 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1377 // The argument of the ReadBarrierForRootSlow is not a managed
1378 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1379 // thus we need a 64-bit move here, and we cannot use
1380 //
1381 // arm64_codegen->MoveLocation(
1382 // LocationFrom(calling_convention.GetRegisterAt(0)),
1383 // root_,
1384 // type);
1385 //
1386 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1387 // reference type (`Primitive::kPrimNot`).
1388 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001389 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001390 instruction_,
1391 instruction_->GetDexPc(),
1392 this);
1393 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1394 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1395
1396 RestoreLiveRegisters(codegen, locations);
1397 __ B(GetExitLabel());
1398 }
1399
1400 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1401
1402 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001403 const Location out_;
1404 const Location root_;
1405
1406 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1407};
1408
Alexandre Rames5319def2014-10-23 10:03:10 +01001409#undef __
1410
1411Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
1412 Location next_location;
1413 if (type == Primitive::kPrimVoid) {
1414 LOG(FATAL) << "Unreachable type " << type;
1415 }
1416
1417 if (Primitive::IsFloatingPointType(type) &&
1418 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001419 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1420 } else if (!Primitive::IsFloatingPointType(type) &&
1421 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1422 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1423 } else {
1424 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001425 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1426 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001427 }
1428
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001429 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001430 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001431 return next_location;
1432}
1433
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001434Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001435 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001436}
1437
Serban Constantinescu579885a2015-02-22 20:51:33 +00001438CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1439 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001440 const CompilerOptions& compiler_options,
1441 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001442 : CodeGenerator(graph,
1443 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001444 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001445 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001446 callee_saved_core_registers.GetList(),
1447 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001448 compiler_options,
1449 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001450 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001451 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001452 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001453 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001454 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001455 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001456 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001457 uint32_literals_(std::less<uint32_t>(),
1458 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001459 uint64_literals_(std::less<uint64_t>(),
1460 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001461 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001462 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001463 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001464 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001465 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001466 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001467 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001468 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1469 jit_class_patches_(TypeReferenceValueComparator(),
1470 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001471 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001472 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001473}
Alexandre Rames5319def2014-10-23 10:03:10 +01001474
Alexandre Rames67555f72014-11-18 10:55:16 +00001475#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001476
Zheng Xu3927c8b2015-11-18 17:46:25 +08001477void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001478 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001479 jump_table->EmitTable(this);
1480 }
1481}
1482
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001483void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001484 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001485 // Ensure we emit the literal pool.
1486 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001487
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001488 CodeGenerator::Finalize(allocator);
1489}
1490
Zheng Xuad4450e2015-04-17 18:48:56 +08001491void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1492 // Note: There are 6 kinds of moves:
1493 // 1. constant -> GPR/FPR (non-cycle)
1494 // 2. constant -> stack (non-cycle)
1495 // 3. GPR/FPR -> GPR/FPR
1496 // 4. GPR/FPR -> stack
1497 // 5. stack -> GPR/FPR
1498 // 6. stack -> stack (non-cycle)
1499 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1500 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1501 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1502 // dependency.
1503 vixl_temps_.Open(GetVIXLAssembler());
1504}
1505
1506void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1507 vixl_temps_.Close();
1508}
1509
1510Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001511 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1512 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1513 || kind == Location::kSIMDStackSlot);
1514 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1515 ? Location::kFpuRegister
1516 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001517 Location scratch = GetScratchLocation(kind);
1518 if (!scratch.Equals(Location::NoLocation())) {
1519 return scratch;
1520 }
1521 // Allocate from VIXL temp registers.
1522 if (kind == Location::kRegister) {
1523 scratch = LocationFrom(vixl_temps_.AcquireX());
1524 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001525 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001526 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1527 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1528 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001529 }
1530 AddScratchLocation(scratch);
1531 return scratch;
1532}
1533
1534void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1535 if (loc.IsRegister()) {
1536 vixl_temps_.Release(XRegisterFrom(loc));
1537 } else {
1538 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001539 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001540 }
1541 RemoveScratchLocation(loc);
1542}
1543
Alexandre Rames3e69f162014-12-10 10:36:50 +00001544void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001545 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001546 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001547}
1548
Alexandre Rames5319def2014-10-23 10:03:10 +01001549void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001550 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001551 __ Bind(&frame_entry_label_);
1552
Serban Constantinescu02164b32014-11-13 14:05:07 +00001553 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1554 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001555 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001556 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001557 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001558 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001559 {
1560 // Ensure that between load and RecordPcInfo there are no pools emitted.
1561 ExactAssemblyScope eas(GetVIXLAssembler(),
1562 kInstructionSize,
1563 CodeBufferCheckScope::kExactSize);
1564 __ ldr(wzr, MemOperand(temp, 0));
1565 RecordPcInfo(nullptr, 0);
1566 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001567 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001568
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001569 if (!HasEmptyFrame()) {
1570 int frame_size = GetFrameSize();
1571 // Stack layout:
1572 // sp[frame_size - 8] : lr.
1573 // ... : other preserved core registers.
1574 // ... : other preserved fp registers.
1575 // ... : reserved frame space.
1576 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001577
1578 // Save the current method if we need it. Note that we do not
1579 // do this in HCurrentMethod, as the instruction might have been removed
1580 // in the SSA graph.
1581 if (RequiresCurrentMethod()) {
1582 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001583 } else {
1584 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001585 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001586 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001587 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1588 frame_size - GetCoreSpillSize());
1589 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1590 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001591
1592 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1593 // Initialize should_deoptimize flag to 0.
1594 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1595 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1596 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001597 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01001598
1599 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001600}
1601
1602void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001603 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001604 if (!HasEmptyFrame()) {
1605 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001606 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1607 frame_size - FrameEntrySpillSize());
1608 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1609 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001610 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001611 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001612 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001613 __ Ret();
1614 GetAssembler()->cfi().RestoreState();
1615 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001616}
1617
Scott Wakeling97c72b72016-06-24 16:19:36 +01001618CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001619 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001620 return CPURegList(CPURegister::kRegister, kXRegSize,
1621 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001622}
1623
Scott Wakeling97c72b72016-06-24 16:19:36 +01001624CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001625 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1626 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001627 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1628 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001629}
1630
Alexandre Rames5319def2014-10-23 10:03:10 +01001631void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1632 __ Bind(GetLabelOf(block));
1633}
1634
Calin Juravle175dc732015-08-25 15:42:32 +01001635void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1636 DCHECK(location.IsRegister());
1637 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1638}
1639
Calin Juravlee460d1d2015-09-29 04:52:17 +01001640void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1641 if (location.IsRegister()) {
1642 locations->AddTemp(location);
1643 } else {
1644 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1645 }
1646}
1647
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001648void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001649 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001650 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001651 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001652 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001653 if (value_can_be_null) {
1654 __ Cbz(value, &done);
1655 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001656 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001657 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001658 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001659 if (value_can_be_null) {
1660 __ Bind(&done);
1661 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001662}
1663
David Brazdil58282f42016-01-14 12:45:10 +00001664void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001665 // Blocked core registers:
1666 // lr : Runtime reserved.
1667 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001668 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001669 // ip1 : VIXL core temp.
1670 // ip0 : VIXL core temp.
1671 //
1672 // Blocked fp registers:
1673 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001674 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1675 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001676 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001677 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001678 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001679
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001680 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001681 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001682 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001683 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001684
David Brazdil58282f42016-01-14 12:45:10 +00001685 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001686 // Stubs do not save callee-save floating point registers. If the graph
1687 // is debuggable, we need to deal with these registers differently. For
1688 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001689 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1690 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001691 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001692 }
1693 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001694}
1695
Alexandre Rames3e69f162014-12-10 10:36:50 +00001696size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1697 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1698 __ Str(reg, MemOperand(sp, stack_index));
1699 return kArm64WordSize;
1700}
1701
1702size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1703 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1704 __ Ldr(reg, MemOperand(sp, stack_index));
1705 return kArm64WordSize;
1706}
1707
1708size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1709 FPRegister reg = FPRegister(reg_id, kDRegSize);
1710 __ Str(reg, MemOperand(sp, stack_index));
1711 return kArm64WordSize;
1712}
1713
1714size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1715 FPRegister reg = FPRegister(reg_id, kDRegSize);
1716 __ Ldr(reg, MemOperand(sp, stack_index));
1717 return kArm64WordSize;
1718}
1719
Alexandre Rames5319def2014-10-23 10:03:10 +01001720void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001721 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001722}
1723
1724void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001725 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001726}
1727
Alexandre Rames67555f72014-11-18 10:55:16 +00001728void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001729 if (constant->IsIntConstant()) {
1730 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1731 } else if (constant->IsLongConstant()) {
1732 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1733 } else if (constant->IsNullConstant()) {
1734 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001735 } else if (constant->IsFloatConstant()) {
1736 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1737 } else {
1738 DCHECK(constant->IsDoubleConstant());
1739 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1740 }
1741}
1742
Alexandre Rames3e69f162014-12-10 10:36:50 +00001743
1744static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1745 DCHECK(constant.IsConstant());
1746 HConstant* cst = constant.GetConstant();
1747 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001748 // Null is mapped to a core W register, which we associate with kPrimInt.
1749 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001750 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1751 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1752 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1753}
1754
Roland Levillain952b2352017-05-03 19:49:14 +01001755// Allocate a scratch register from the VIXL pool, querying first
1756// the floating-point register pool, and then the core register
1757// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001758// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1759// using a different allocation strategy.
1760static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1761 vixl::aarch64::UseScratchRegisterScope* temps,
1762 int size_in_bits) {
1763 return masm->GetScratchFPRegisterList()->IsEmpty()
1764 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1765 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1766}
1767
Calin Juravlee460d1d2015-09-29 04:52:17 +01001768void CodeGeneratorARM64::MoveLocation(Location destination,
1769 Location source,
1770 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001771 if (source.Equals(destination)) {
1772 return;
1773 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001774
1775 // A valid move can always be inferred from the destination and source
1776 // locations. When moving from and to a register, the argument type can be
1777 // used to generate 32bit instead of 64bit moves. In debug mode we also
1778 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001779 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001780
1781 if (destination.IsRegister() || destination.IsFpuRegister()) {
1782 if (unspecified_type) {
1783 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1784 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001785 (src_cst != nullptr && (src_cst->IsIntConstant()
1786 || src_cst->IsFloatConstant()
1787 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001788 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001789 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001790 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001791 // If the source is a double stack slot or a 64bit constant, a 64bit
1792 // type is appropriate. Else the source is a register, and since the
1793 // type has not been specified, we chose a 64bit type to force a 64bit
1794 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001795 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001796 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001797 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001798 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1799 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1800 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001801 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1802 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1803 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001804 } else if (source.IsSIMDStackSlot()) {
1805 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001806 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001807 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001808 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001809 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001810 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001811 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001812 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001813 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001814 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1815 ? Primitive::kPrimLong
1816 : Primitive::kPrimInt;
1817 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1818 }
1819 } else {
1820 DCHECK(source.IsFpuRegister());
1821 if (destination.IsRegister()) {
1822 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1823 ? Primitive::kPrimDouble
1824 : Primitive::kPrimFloat;
1825 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1826 } else {
1827 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001828 if (GetGraph()->HasSIMD()) {
1829 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1830 } else {
1831 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1832 }
1833 }
1834 }
1835 } else if (destination.IsSIMDStackSlot()) {
1836 if (source.IsFpuRegister()) {
1837 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1838 } else {
1839 DCHECK(source.IsSIMDStackSlot());
1840 UseScratchRegisterScope temps(GetVIXLAssembler());
1841 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1842 Register temp = temps.AcquireX();
1843 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1844 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1845 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1846 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1847 } else {
1848 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1849 __ Ldr(temp, StackOperandFrom(source));
1850 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001851 }
1852 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001853 } else { // The destination is not a register. It must be a stack slot.
1854 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1855 if (source.IsRegister() || source.IsFpuRegister()) {
1856 if (unspecified_type) {
1857 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001858 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001859 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001860 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001861 }
1862 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001863 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1864 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1865 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001866 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001867 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1868 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001869 UseScratchRegisterScope temps(GetVIXLAssembler());
1870 HConstant* src_cst = source.GetConstant();
1871 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001872 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001873 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1874 ? Register(xzr)
1875 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001876 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001877 if (src_cst->IsIntConstant()) {
1878 temp = temps.AcquireW();
1879 } else if (src_cst->IsLongConstant()) {
1880 temp = temps.AcquireX();
1881 } else if (src_cst->IsFloatConstant()) {
1882 temp = temps.AcquireS();
1883 } else {
1884 DCHECK(src_cst->IsDoubleConstant());
1885 temp = temps.AcquireD();
1886 }
1887 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001888 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001889 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001890 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001891 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001892 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001893 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001894 // Use any scratch register (a core or a floating-point one)
1895 // from VIXL scratch register pools as a temporary.
1896 //
1897 // We used to only use the FP scratch register pool, but in some
1898 // rare cases the only register from this pool (D31) would
1899 // already be used (e.g. within a ParallelMove instruction, when
1900 // a move is blocked by a another move requiring a scratch FP
1901 // register, which would reserve D31). To prevent this issue, we
1902 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001903 //
1904 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001905 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001906 // use AcquireFPOrCoreCPURegisterOfSize instead of
1907 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1908 // allocates core scratch registers first.
1909 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1910 GetVIXLAssembler(),
1911 &temps,
1912 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001913 __ Ldr(temp, StackOperandFrom(source));
1914 __ Str(temp, StackOperandFrom(destination));
1915 }
1916 }
1917}
1918
1919void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001920 CPURegister dst,
1921 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001922 switch (type) {
1923 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001924 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001925 break;
1926 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001927 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001928 break;
1929 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001930 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001931 break;
1932 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001933 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001934 break;
1935 case Primitive::kPrimInt:
1936 case Primitive::kPrimNot:
1937 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001938 case Primitive::kPrimFloat:
1939 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001940 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001941 __ Ldr(dst, src);
1942 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001943 case Primitive::kPrimVoid:
1944 LOG(FATAL) << "Unreachable type " << type;
1945 }
1946}
1947
Calin Juravle77520bc2015-01-12 18:45:46 +00001948void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001949 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001950 const MemOperand& src,
1951 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001952 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001953 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001954 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001955 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001956
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001957 DCHECK(!src.IsPreIndex());
1958 DCHECK(!src.IsPostIndex());
1959
1960 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001961 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001962 {
1963 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1964 MemOperand base = MemOperand(temp_base);
1965 switch (type) {
1966 case Primitive::kPrimBoolean:
1967 {
1968 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1969 __ ldarb(Register(dst), base);
1970 if (needs_null_check) {
1971 MaybeRecordImplicitNullCheck(instruction);
1972 }
1973 }
1974 break;
1975 case Primitive::kPrimByte:
1976 {
1977 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1978 __ ldarb(Register(dst), base);
1979 if (needs_null_check) {
1980 MaybeRecordImplicitNullCheck(instruction);
1981 }
1982 }
1983 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1984 break;
1985 case Primitive::kPrimChar:
1986 {
1987 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1988 __ ldarh(Register(dst), base);
1989 if (needs_null_check) {
1990 MaybeRecordImplicitNullCheck(instruction);
1991 }
1992 }
1993 break;
1994 case Primitive::kPrimShort:
1995 {
1996 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1997 __ ldarh(Register(dst), base);
1998 if (needs_null_check) {
1999 MaybeRecordImplicitNullCheck(instruction);
2000 }
2001 }
2002 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
2003 break;
2004 case Primitive::kPrimInt:
2005 case Primitive::kPrimNot:
2006 case Primitive::kPrimLong:
2007 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
2008 {
2009 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2010 __ ldar(Register(dst), base);
2011 if (needs_null_check) {
2012 MaybeRecordImplicitNullCheck(instruction);
2013 }
2014 }
2015 break;
2016 case Primitive::kPrimFloat:
2017 case Primitive::kPrimDouble: {
2018 DCHECK(dst.IsFPRegister());
2019 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002020
Artem Serov914d7a82017-02-07 14:33:49 +00002021 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2022 {
2023 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2024 __ ldar(temp, base);
2025 if (needs_null_check) {
2026 MaybeRecordImplicitNullCheck(instruction);
2027 }
2028 }
2029 __ Fmov(FPRegister(dst), temp);
2030 break;
Roland Levillain44015862016-01-22 11:47:17 +00002031 }
Artem Serov914d7a82017-02-07 14:33:49 +00002032 case Primitive::kPrimVoid:
2033 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002034 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002035 }
2036}
2037
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002038void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002039 CPURegister src,
2040 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002041 switch (type) {
2042 case Primitive::kPrimBoolean:
2043 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002044 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002045 break;
2046 case Primitive::kPrimChar:
2047 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002048 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002049 break;
2050 case Primitive::kPrimInt:
2051 case Primitive::kPrimNot:
2052 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002053 case Primitive::kPrimFloat:
2054 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00002055 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002056 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002057 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002058 case Primitive::kPrimVoid:
2059 LOG(FATAL) << "Unreachable type " << type;
2060 }
2061}
2062
Artem Serov914d7a82017-02-07 14:33:49 +00002063void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
2064 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002065 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002066 const MemOperand& dst,
2067 bool needs_null_check) {
2068 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002069 UseScratchRegisterScope temps(GetVIXLAssembler());
2070 Register temp_base = temps.AcquireX();
2071
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002072 DCHECK(!dst.IsPreIndex());
2073 DCHECK(!dst.IsPostIndex());
2074
2075 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002076 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002077 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002078 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002079 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002080 switch (type) {
2081 case Primitive::kPrimBoolean:
2082 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00002083 {
2084 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2085 __ stlrb(Register(src), base);
2086 if (needs_null_check) {
2087 MaybeRecordImplicitNullCheck(instruction);
2088 }
2089 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002090 break;
2091 case Primitive::kPrimChar:
2092 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00002093 {
2094 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2095 __ stlrh(Register(src), base);
2096 if (needs_null_check) {
2097 MaybeRecordImplicitNullCheck(instruction);
2098 }
2099 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002100 break;
2101 case Primitive::kPrimInt:
2102 case Primitive::kPrimNot:
2103 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002104 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002105 {
2106 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2107 __ stlr(Register(src), base);
2108 if (needs_null_check) {
2109 MaybeRecordImplicitNullCheck(instruction);
2110 }
2111 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002112 break;
2113 case Primitive::kPrimFloat:
2114 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002115 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002116 Register temp_src;
2117 if (src.IsZero()) {
2118 // The zero register is used to avoid synthesizing zero constants.
2119 temp_src = Register(src);
2120 } else {
2121 DCHECK(src.IsFPRegister());
2122 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2123 __ Fmov(temp_src, FPRegister(src));
2124 }
Artem Serov914d7a82017-02-07 14:33:49 +00002125 {
2126 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2127 __ stlr(temp_src, base);
2128 if (needs_null_check) {
2129 MaybeRecordImplicitNullCheck(instruction);
2130 }
2131 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002132 break;
2133 }
2134 case Primitive::kPrimVoid:
2135 LOG(FATAL) << "Unreachable type " << type;
2136 }
2137}
2138
Calin Juravle175dc732015-08-25 15:42:32 +01002139void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2140 HInstruction* instruction,
2141 uint32_t dex_pc,
2142 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002143 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002144
2145 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2146 {
2147 // Ensure the pc position is recorded immediately after the `blr` instruction.
2148 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2149 __ blr(lr);
2150 if (EntrypointRequiresStackMap(entrypoint)) {
2151 RecordPcInfo(instruction, dex_pc, slow_path);
2152 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002153 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002154}
2155
Roland Levillaindec8f632016-07-22 17:10:06 +01002156void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2157 HInstruction* instruction,
2158 SlowPathCode* slow_path) {
2159 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002160 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2161 __ Blr(lr);
2162}
2163
Alexandre Rames67555f72014-11-18 10:55:16 +00002164void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002165 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002166 UseScratchRegisterScope temps(GetVIXLAssembler());
2167 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002168 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2169
Serban Constantinescu02164b32014-11-13 14:05:07 +00002170 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002171 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2172 __ Add(temp, class_reg, status_offset);
2173 __ Ldar(temp, HeapOperand(temp));
2174 __ Cmp(temp, mirror::Class::kStatusInitialized);
2175 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002176 __ Bind(slow_path->GetExitLabel());
2177}
Alexandre Rames5319def2014-10-23 10:03:10 +01002178
Roland Levillain44015862016-01-22 11:47:17 +00002179void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002180 BarrierType type = BarrierAll;
2181
2182 switch (kind) {
2183 case MemBarrierKind::kAnyAny:
2184 case MemBarrierKind::kAnyStore: {
2185 type = BarrierAll;
2186 break;
2187 }
2188 case MemBarrierKind::kLoadAny: {
2189 type = BarrierReads;
2190 break;
2191 }
2192 case MemBarrierKind::kStoreStore: {
2193 type = BarrierWrites;
2194 break;
2195 }
2196 default:
2197 LOG(FATAL) << "Unexpected memory barrier " << kind;
2198 }
2199 __ Dmb(InnerShareable, type);
2200}
2201
Serban Constantinescu02164b32014-11-13 14:05:07 +00002202void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2203 HBasicBlock* successor) {
2204 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002205 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2206 if (slow_path == nullptr) {
2207 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2208 instruction->SetSlowPath(slow_path);
2209 codegen_->AddSlowPath(slow_path);
2210 if (successor != nullptr) {
2211 DCHECK(successor->IsLoopHeader());
2212 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2213 }
2214 } else {
2215 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2216 }
2217
Serban Constantinescu02164b32014-11-13 14:05:07 +00002218 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2219 Register temp = temps.AcquireW();
2220
Andreas Gampe542451c2016-07-26 09:02:02 -07002221 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002222 if (successor == nullptr) {
2223 __ Cbnz(temp, slow_path->GetEntryLabel());
2224 __ Bind(slow_path->GetReturnLabel());
2225 } else {
2226 __ Cbz(temp, codegen_->GetLabelOf(successor));
2227 __ B(slow_path->GetEntryLabel());
2228 // slow_path will return to GetLabelOf(successor).
2229 }
2230}
2231
Alexandre Rames5319def2014-10-23 10:03:10 +01002232InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2233 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002234 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002235 assembler_(codegen->GetAssembler()),
2236 codegen_(codegen) {}
2237
2238#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002239 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002240
2241#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2242
2243enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002244 // Using a base helps identify when we hit such breakpoints.
2245 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002246#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2247 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2248#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2249};
2250
2251#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002252 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002253 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2254 } \
2255 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2256 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2257 locations->SetOut(Location::Any()); \
2258 }
2259 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2260#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2261
2262#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002263#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002264
Alexandre Rames67555f72014-11-18 10:55:16 +00002265void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002266 DCHECK_EQ(instr->InputCount(), 2U);
2267 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2268 Primitive::Type type = instr->GetResultType();
2269 switch (type) {
2270 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002271 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002272 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002273 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002274 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002275 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002276
2277 case Primitive::kPrimFloat:
2278 case Primitive::kPrimDouble:
2279 locations->SetInAt(0, Location::RequiresFpuRegister());
2280 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002281 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002282 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002283
Alexandre Rames5319def2014-10-23 10:03:10 +01002284 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002285 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002286 }
2287}
2288
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002289void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2290 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002291 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2292
2293 bool object_field_get_with_read_barrier =
2294 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002295 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002296 new (GetGraph()->GetArena()) LocationSummary(instruction,
2297 object_field_get_with_read_barrier ?
2298 LocationSummary::kCallOnSlowPath :
2299 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002300 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002301 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002302 // We need a temporary register for the read barrier marking slow
2303 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002304 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2305 !Runtime::Current()->UseJitCompilation() &&
2306 !field_info.IsVolatile()) {
2307 // If link-time thunks for the Baker read barrier are enabled, for AOT
2308 // non-volatile loads we need a temporary only if the offset is too big.
2309 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2310 locations->AddTemp(FixedTempLocation());
2311 }
2312 } else {
2313 locations->AddTemp(Location::RequiresRegister());
2314 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002315 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002316 locations->SetInAt(0, Location::RequiresRegister());
2317 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2318 locations->SetOut(Location::RequiresFpuRegister());
2319 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002320 // The output overlaps for an object field get when read barriers
2321 // are enabled: we do not want the load to overwrite the object's
2322 // location, as we need it to emit the read barrier.
2323 locations->SetOut(
2324 Location::RequiresRegister(),
2325 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002326 }
2327}
2328
2329void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2330 const FieldInfo& field_info) {
2331 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002332 LocationSummary* locations = instruction->GetLocations();
2333 Location base_loc = locations->InAt(0);
2334 Location out = locations->Out();
2335 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002336 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002337 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002338
Roland Levillain44015862016-01-22 11:47:17 +00002339 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2340 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002341 // /* HeapReference<Object> */ out = *(base + offset)
2342 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002343 Location maybe_temp =
2344 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002345 // Note that potential implicit null checks are handled in this
2346 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2347 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2348 instruction,
2349 out,
2350 base,
2351 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002352 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002353 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002354 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002355 } else {
2356 // General case.
2357 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002358 // Note that a potential implicit null check is handled in this
2359 // CodeGeneratorARM64::LoadAcquire call.
2360 // NB: LoadAcquire will record the pc info if needed.
2361 codegen_->LoadAcquire(
2362 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002363 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002364 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2365 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002366 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002367 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002368 }
Roland Levillain44015862016-01-22 11:47:17 +00002369 if (field_type == Primitive::kPrimNot) {
2370 // If read barriers are enabled, emit read barriers other than
2371 // Baker's using a slow path (and also unpoison the loaded
2372 // reference, if heap poisoning is enabled).
2373 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2374 }
Roland Levillain4d027112015-07-01 15:41:14 +01002375 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002376}
2377
2378void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2379 LocationSummary* locations =
2380 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2381 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002382 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2383 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2384 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002385 locations->SetInAt(1, Location::RequiresFpuRegister());
2386 } else {
2387 locations->SetInAt(1, Location::RequiresRegister());
2388 }
2389}
2390
2391void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002392 const FieldInfo& field_info,
2393 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002394 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2395
2396 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002397 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002398 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002399 Offset offset = field_info.GetFieldOffset();
2400 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002401
Roland Levillain4d027112015-07-01 15:41:14 +01002402 {
2403 // We use a block to end the scratch scope before the write barrier, thus
2404 // freeing the temporary registers so they can be used in `MarkGCCard`.
2405 UseScratchRegisterScope temps(GetVIXLAssembler());
2406
2407 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2408 DCHECK(value.IsW());
2409 Register temp = temps.AcquireW();
2410 __ Mov(temp, value.W());
2411 GetAssembler()->PoisonHeapReference(temp.W());
2412 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002413 }
Roland Levillain4d027112015-07-01 15:41:14 +01002414
2415 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002416 codegen_->StoreRelease(
2417 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002418 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002419 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2420 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002421 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2422 codegen_->MaybeRecordImplicitNullCheck(instruction);
2423 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002424 }
2425
2426 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002427 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002428 }
2429}
2430
Alexandre Rames67555f72014-11-18 10:55:16 +00002431void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002432 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002433
2434 switch (type) {
2435 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002436 case Primitive::kPrimLong: {
2437 Register dst = OutputRegister(instr);
2438 Register lhs = InputRegisterAt(instr, 0);
2439 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002440 if (instr->IsAdd()) {
2441 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002442 } else if (instr->IsAnd()) {
2443 __ And(dst, lhs, rhs);
2444 } else if (instr->IsOr()) {
2445 __ Orr(dst, lhs, rhs);
2446 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002447 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002448 } else if (instr->IsRor()) {
2449 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002450 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002451 __ Ror(dst, lhs, shift);
2452 } else {
2453 // Ensure shift distance is in the same size register as the result. If
2454 // we are rotating a long and the shift comes in a w register originally,
2455 // we don't need to sxtw for use as an x since the shift distances are
2456 // all & reg_bits - 1.
2457 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2458 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002459 } else {
2460 DCHECK(instr->IsXor());
2461 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002462 }
2463 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002464 }
2465 case Primitive::kPrimFloat:
2466 case Primitive::kPrimDouble: {
2467 FPRegister dst = OutputFPRegister(instr);
2468 FPRegister lhs = InputFPRegisterAt(instr, 0);
2469 FPRegister rhs = InputFPRegisterAt(instr, 1);
2470 if (instr->IsAdd()) {
2471 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002472 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002473 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002474 } else {
2475 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002476 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002477 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002478 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002479 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002480 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002481 }
2482}
2483
Serban Constantinescu02164b32014-11-13 14:05:07 +00002484void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2485 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2486
2487 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2488 Primitive::Type type = instr->GetResultType();
2489 switch (type) {
2490 case Primitive::kPrimInt:
2491 case Primitive::kPrimLong: {
2492 locations->SetInAt(0, Location::RequiresRegister());
2493 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002494 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002495 break;
2496 }
2497 default:
2498 LOG(FATAL) << "Unexpected shift type " << type;
2499 }
2500}
2501
2502void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2503 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2504
2505 Primitive::Type type = instr->GetType();
2506 switch (type) {
2507 case Primitive::kPrimInt:
2508 case Primitive::kPrimLong: {
2509 Register dst = OutputRegister(instr);
2510 Register lhs = InputRegisterAt(instr, 0);
2511 Operand rhs = InputOperandAt(instr, 1);
2512 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002513 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002514 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002515 if (instr->IsShl()) {
2516 __ Lsl(dst, lhs, shift_value);
2517 } else if (instr->IsShr()) {
2518 __ Asr(dst, lhs, shift_value);
2519 } else {
2520 __ Lsr(dst, lhs, shift_value);
2521 }
2522 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002523 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002524
2525 if (instr->IsShl()) {
2526 __ Lsl(dst, lhs, rhs_reg);
2527 } else if (instr->IsShr()) {
2528 __ Asr(dst, lhs, rhs_reg);
2529 } else {
2530 __ Lsr(dst, lhs, rhs_reg);
2531 }
2532 }
2533 break;
2534 }
2535 default:
2536 LOG(FATAL) << "Unexpected shift operation type " << type;
2537 }
2538}
2539
Alexandre Rames5319def2014-10-23 10:03:10 +01002540void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002541 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002542}
2543
2544void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002545 HandleBinaryOp(instruction);
2546}
2547
2548void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2549 HandleBinaryOp(instruction);
2550}
2551
2552void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2553 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002554}
2555
Artem Serov7fc63502016-02-09 17:15:29 +00002556void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002557 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2558 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2559 locations->SetInAt(0, Location::RequiresRegister());
2560 // There is no immediate variant of negated bitwise instructions in AArch64.
2561 locations->SetInAt(1, Location::RequiresRegister());
2562 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2563}
2564
Artem Serov7fc63502016-02-09 17:15:29 +00002565void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002566 Register dst = OutputRegister(instr);
2567 Register lhs = InputRegisterAt(instr, 0);
2568 Register rhs = InputRegisterAt(instr, 1);
2569
2570 switch (instr->GetOpKind()) {
2571 case HInstruction::kAnd:
2572 __ Bic(dst, lhs, rhs);
2573 break;
2574 case HInstruction::kOr:
2575 __ Orn(dst, lhs, rhs);
2576 break;
2577 case HInstruction::kXor:
2578 __ Eon(dst, lhs, rhs);
2579 break;
2580 default:
2581 LOG(FATAL) << "Unreachable";
2582 }
2583}
2584
Anton Kirilov74234da2017-01-13 14:42:47 +00002585void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2586 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002587 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2588 instruction->GetType() == Primitive::kPrimLong);
2589 LocationSummary* locations =
2590 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2591 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2592 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2593 } else {
2594 locations->SetInAt(0, Location::RequiresRegister());
2595 }
2596 locations->SetInAt(1, Location::RequiresRegister());
2597 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2598}
2599
Anton Kirilov74234da2017-01-13 14:42:47 +00002600void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2601 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002602 Primitive::Type type = instruction->GetType();
2603 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2604 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2605 Register out = OutputRegister(instruction);
2606 Register left;
2607 if (kind != HInstruction::kNeg) {
2608 left = InputRegisterAt(instruction, 0);
2609 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002610 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002611 // shifter operand operation, the IR generating `right_reg` (input to the type
2612 // conversion) can have a different type from the current instruction's type,
2613 // so we manually indicate the type.
2614 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002615 Operand right_operand(0);
2616
Anton Kirilov74234da2017-01-13 14:42:47 +00002617 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2618 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002619 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2620 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002621 right_operand = Operand(right_reg,
2622 helpers::ShiftFromOpKind(op_kind),
2623 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002624 }
2625
2626 // Logical binary operations do not support extension operations in the
2627 // operand. Note that VIXL would still manage if it was passed by generating
2628 // the extension as a separate instruction.
2629 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2630 DCHECK(!right_operand.IsExtendedRegister() ||
2631 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2632 kind != HInstruction::kNeg));
2633 switch (kind) {
2634 case HInstruction::kAdd:
2635 __ Add(out, left, right_operand);
2636 break;
2637 case HInstruction::kAnd:
2638 __ And(out, left, right_operand);
2639 break;
2640 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002641 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002642 __ Neg(out, right_operand);
2643 break;
2644 case HInstruction::kOr:
2645 __ Orr(out, left, right_operand);
2646 break;
2647 case HInstruction::kSub:
2648 __ Sub(out, left, right_operand);
2649 break;
2650 case HInstruction::kXor:
2651 __ Eor(out, left, right_operand);
2652 break;
2653 default:
2654 LOG(FATAL) << "Unexpected operation kind: " << kind;
2655 UNREACHABLE();
2656 }
2657}
2658
Artem Serov328429f2016-07-06 16:23:04 +01002659void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002660 LocationSummary* locations =
2661 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2662 locations->SetInAt(0, Location::RequiresRegister());
2663 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002664 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002665}
2666
Roland Levillain19c54192016-11-04 13:44:09 +00002667void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002668 __ Add(OutputRegister(instruction),
2669 InputRegisterAt(instruction, 0),
2670 Operand(InputOperandAt(instruction, 1)));
2671}
2672
Artem Serove1811ed2017-04-27 16:50:47 +01002673void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2674 LocationSummary* locations =
2675 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2676
2677 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2678
2679 locations->SetInAt(0, Location::RequiresRegister());
2680 // For byte case we don't need to shift the index variable so we can encode the data offset into
2681 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2682 // data offset constant generation out of the loop and reduce the critical path length in the
2683 // loop.
2684 locations->SetInAt(1, shift->GetValue() == 0
2685 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2686 : Location::RequiresRegister());
2687 locations->SetInAt(2, Location::ConstantLocation(shift));
2688 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2689}
2690
2691void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2692 HIntermediateAddressIndex* instruction) {
2693 Register index_reg = InputRegisterAt(instruction, 0);
2694 uint32_t shift = Int64ConstantFrom(instruction->GetLocations()->InAt(2));
2695 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2696
2697 if (shift == 0) {
2698 __ Add(OutputRegister(instruction), index_reg, offset);
2699 } else {
2700 Register offset_reg = InputRegisterAt(instruction, 1);
2701 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2702 }
2703}
2704
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002705void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002706 LocationSummary* locations =
2707 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002708 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2709 if (instr->GetOpKind() == HInstruction::kSub &&
2710 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002711 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002712 // Don't allocate register for Mneg instruction.
2713 } else {
2714 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2715 Location::RequiresRegister());
2716 }
2717 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2718 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002719 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2720}
2721
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002722void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002723 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002724 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2725 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002726
2727 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2728 // This fixup should be carried out for all multiply-accumulate instructions:
2729 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2730 if (instr->GetType() == Primitive::kPrimLong &&
2731 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2732 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002733 vixl::aarch64::Instruction* prev =
2734 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002735 if (prev->IsLoadOrStore()) {
2736 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002737 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002738 __ nop();
2739 }
2740 }
2741
2742 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002743 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002744 __ Madd(res, mul_left, mul_right, accumulator);
2745 } else {
2746 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002747 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002748 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002749 __ Mneg(res, mul_left, mul_right);
2750 } else {
2751 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2752 __ Msub(res, mul_left, mul_right, accumulator);
2753 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002754 }
2755}
2756
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002757void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002758 bool object_array_get_with_read_barrier =
2759 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002760 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002761 new (GetGraph()->GetArena()) LocationSummary(instruction,
2762 object_array_get_with_read_barrier ?
2763 LocationSummary::kCallOnSlowPath :
2764 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002765 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002766 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002767 // We need a temporary register for the read barrier marking slow
2768 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002769 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2770 !Runtime::Current()->UseJitCompilation() &&
2771 instruction->GetIndex()->IsConstant()) {
2772 // Array loads with constant index are treated as field loads.
2773 // If link-time thunks for the Baker read barrier are enabled, for AOT
2774 // constant index loads we need a temporary only if the offset is too big.
2775 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2776 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
2777 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
2778 if (offset >= kReferenceLoadMinFarOffset) {
2779 locations->AddTemp(FixedTempLocation());
2780 }
2781 } else {
2782 locations->AddTemp(Location::RequiresRegister());
2783 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002784 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002785 locations->SetInAt(0, Location::RequiresRegister());
2786 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002787 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2788 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2789 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002790 // The output overlaps in the case of an object array get with
2791 // read barriers enabled: we do not want the move to overwrite the
2792 // array's location, as we need it to emit the read barrier.
2793 locations->SetOut(
2794 Location::RequiresRegister(),
2795 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002796 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002797}
2798
2799void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002800 Primitive::Type type = instruction->GetType();
2801 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002802 LocationSummary* locations = instruction->GetLocations();
2803 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002804 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002805 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002806 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2807 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002808 MacroAssembler* masm = GetVIXLAssembler();
2809 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002810
Roland Levillain19c54192016-11-04 13:44:09 +00002811 // The read barrier instrumentation of object ArrayGet instructions
2812 // does not support the HIntermediateAddress instruction.
2813 DCHECK(!((type == Primitive::kPrimNot) &&
2814 instruction->GetArray()->IsIntermediateAddress() &&
2815 kEmitCompilerReadBarrier));
2816
Roland Levillain44015862016-01-22 11:47:17 +00002817 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2818 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002819 // Note that a potential implicit null check is handled in the
2820 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002821 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002822 if (index.IsConstant()) {
2823 // Array load with a constant index can be treated as a field load.
2824 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2825 Location maybe_temp =
2826 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2827 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2828 out,
2829 obj.W(),
2830 offset,
2831 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002832 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002833 /* use_load_acquire */ false);
2834 } else {
2835 Register temp = WRegisterFrom(locations->GetTemp(0));
2836 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002837 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002838 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002839 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002840 // General case.
2841 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002842 Register length;
2843 if (maybe_compressed_char_at) {
2844 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2845 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002846 {
2847 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2848 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2849
2850 if (instruction->GetArray()->IsIntermediateAddress()) {
2851 DCHECK_LT(count_offset, offset);
2852 int64_t adjusted_offset =
2853 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2854 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2855 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2856 } else {
2857 __ Ldr(length, HeapOperand(obj, count_offset));
2858 }
2859 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002860 }
jessicahandojo05765752016-09-09 19:01:32 -07002861 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002862 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002863 if (maybe_compressed_char_at) {
2864 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002865 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2866 "Expecting 0=compressed, 1=uncompressed");
2867 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002868 __ Ldrb(Register(OutputCPURegister(instruction)),
2869 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2870 __ B(&done);
2871 __ Bind(&uncompressed_load);
2872 __ Ldrh(Register(OutputCPURegister(instruction)),
2873 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2874 __ Bind(&done);
2875 } else {
2876 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2877 source = HeapOperand(obj, offset);
2878 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002879 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002880 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002881 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002882 // We do not need to compute the intermediate address from the array: the
2883 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002884 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002885 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002886 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002887 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2888 }
2889 temp = obj;
2890 } else {
2891 __ Add(temp, obj, offset);
2892 }
jessicahandojo05765752016-09-09 19:01:32 -07002893 if (maybe_compressed_char_at) {
2894 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002895 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2896 "Expecting 0=compressed, 1=uncompressed");
2897 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002898 __ Ldrb(Register(OutputCPURegister(instruction)),
2899 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2900 __ B(&done);
2901 __ Bind(&uncompressed_load);
2902 __ Ldrh(Register(OutputCPURegister(instruction)),
2903 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2904 __ Bind(&done);
2905 } else {
2906 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2907 }
Roland Levillain44015862016-01-22 11:47:17 +00002908 }
jessicahandojo05765752016-09-09 19:01:32 -07002909 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002910 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2911 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002912 codegen_->Load(type, OutputCPURegister(instruction), source);
2913 codegen_->MaybeRecordImplicitNullCheck(instruction);
2914 }
Roland Levillain44015862016-01-22 11:47:17 +00002915
2916 if (type == Primitive::kPrimNot) {
2917 static_assert(
2918 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2919 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2920 Location obj_loc = locations->InAt(0);
2921 if (index.IsConstant()) {
2922 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2923 } else {
2924 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2925 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002926 }
Roland Levillain4d027112015-07-01 15:41:14 +01002927 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002928}
2929
Alexandre Rames5319def2014-10-23 10:03:10 +01002930void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2931 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2932 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002933 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002934}
2935
2936void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002937 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002938 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002939 {
2940 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2941 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2942 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2943 codegen_->MaybeRecordImplicitNullCheck(instruction);
2944 }
jessicahandojo05765752016-09-09 19:01:32 -07002945 // Mask out compression flag from String's array length.
2946 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002947 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002948 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002949}
2950
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002951void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002952 Primitive::Type value_type = instruction->GetComponentType();
2953
2954 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002955 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2956 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002957 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002958 LocationSummary::kCallOnSlowPath :
2959 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002960 locations->SetInAt(0, Location::RequiresRegister());
2961 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002962 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2963 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2964 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002965 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002966 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002967 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002968 }
2969}
2970
2971void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2972 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002973 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002974 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002975 bool needs_write_barrier =
2976 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002977
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002978 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002979 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002980 CPURegister source = value;
2981 Location index = locations->InAt(1);
2982 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2983 MemOperand destination = HeapOperand(array);
2984 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002985
2986 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002987 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002988 if (index.IsConstant()) {
2989 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2990 destination = HeapOperand(array, offset);
2991 } else {
2992 UseScratchRegisterScope temps(masm);
2993 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002994 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002995 // We do not need to compute the intermediate address from the array: the
2996 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002997 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002998 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002999 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01003000 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
3001 }
3002 temp = array;
3003 } else {
3004 __ Add(temp, array, offset);
3005 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003006 destination = HeapOperand(temp,
3007 XRegisterFrom(index),
3008 LSL,
3009 Primitive::ComponentSizeShift(value_type));
3010 }
Artem Serov914d7a82017-02-07 14:33:49 +00003011 {
3012 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3013 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3014 codegen_->Store(value_type, value, destination);
3015 codegen_->MaybeRecordImplicitNullCheck(instruction);
3016 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003017 } else {
Artem Serov328429f2016-07-06 16:23:04 +01003018 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003019 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003020 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01003021 {
3022 // We use a block to end the scratch scope before the write barrier, thus
3023 // freeing the temporary registers so they can be used in `MarkGCCard`.
3024 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003025 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01003026 if (index.IsConstant()) {
3027 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003028 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01003029 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01003030 destination = HeapOperand(temp,
3031 XRegisterFrom(index),
3032 LSL,
3033 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01003034 }
3035
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003036 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3037 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3038 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3039
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003040 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003041 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
3042 codegen_->AddSlowPath(slow_path);
3043 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003044 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003045 __ Cbnz(Register(value), &non_zero);
3046 if (!index.IsConstant()) {
3047 __ Add(temp, array, offset);
3048 }
Artem Serov914d7a82017-02-07 14:33:49 +00003049 {
3050 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
3051 // emitted.
3052 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3053 __ Str(wzr, destination);
3054 codegen_->MaybeRecordImplicitNullCheck(instruction);
3055 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003056 __ B(&done);
3057 __ Bind(&non_zero);
3058 }
3059
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003060 // Note that when Baker read barriers are enabled, the type
3061 // checks are performed without read barriers. This is fine,
3062 // even in the case where a class object is in the from-space
3063 // after the flip, as a comparison involving such a type would
3064 // not produce a false positive; it may of course produce a
3065 // false negative, in which case we would take the ArraySet
3066 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003067
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003068 Register temp2 = temps.AcquireSameSizeAs(array);
3069 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003070 {
3071 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3072 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3073 __ Ldr(temp, HeapOperand(array, class_offset));
3074 codegen_->MaybeRecordImplicitNullCheck(instruction);
3075 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003076 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003077
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003078 // /* HeapReference<Class> */ temp = temp->component_type_
3079 __ Ldr(temp, HeapOperand(temp, component_offset));
3080 // /* HeapReference<Class> */ temp2 = value->klass_
3081 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3082 // If heap poisoning is enabled, no need to unpoison `temp`
3083 // nor `temp2`, as we are comparing two poisoned references.
3084 __ Cmp(temp, temp2);
3085 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003086
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003087 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3088 vixl::aarch64::Label do_put;
3089 __ B(eq, &do_put);
3090 // If heap poisoning is enabled, the `temp` reference has
3091 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003092 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3093
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003094 // /* HeapReference<Class> */ temp = temp->super_class_
3095 __ Ldr(temp, HeapOperand(temp, super_offset));
3096 // If heap poisoning is enabled, no need to unpoison
3097 // `temp`, as we are comparing against null below.
3098 __ Cbnz(temp, slow_path->GetEntryLabel());
3099 __ Bind(&do_put);
3100 } else {
3101 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003102 }
3103 }
3104
3105 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003106 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003107 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003108 __ Mov(temp2, value.W());
3109 GetAssembler()->PoisonHeapReference(temp2);
3110 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003111 }
3112
3113 if (!index.IsConstant()) {
3114 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003115 } else {
3116 // We no longer need the `temp` here so release it as the store below may
3117 // need a scratch register (if the constant index makes the offset too large)
3118 // and the poisoned `source` could be using the other scratch register.
3119 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003120 }
Artem Serov914d7a82017-02-07 14:33:49 +00003121 {
3122 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3123 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3124 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003125
Artem Serov914d7a82017-02-07 14:33:49 +00003126 if (!may_need_runtime_call_for_type_check) {
3127 codegen_->MaybeRecordImplicitNullCheck(instruction);
3128 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003129 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003130 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003131
3132 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3133
3134 if (done.IsLinked()) {
3135 __ Bind(&done);
3136 }
3137
3138 if (slow_path != nullptr) {
3139 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003140 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003141 }
3142}
3143
Alexandre Rames67555f72014-11-18 10:55:16 +00003144void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003145 RegisterSet caller_saves = RegisterSet::Empty();
3146 InvokeRuntimeCallingConvention calling_convention;
3147 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3148 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3149 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003150 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003151 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003152}
3153
3154void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003155 BoundsCheckSlowPathARM64* slow_path =
3156 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003157 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003158 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3159 __ B(slow_path->GetEntryLabel(), hs);
3160}
3161
Alexandre Rames67555f72014-11-18 10:55:16 +00003162void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3163 LocationSummary* locations =
3164 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3165 locations->SetInAt(0, Location::RequiresRegister());
3166 if (check->HasUses()) {
3167 locations->SetOut(Location::SameAsFirstInput());
3168 }
3169}
3170
3171void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3172 // We assume the class is not null.
3173 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3174 check->GetLoadClass(), check, check->GetDexPc(), true);
3175 codegen_->AddSlowPath(slow_path);
3176 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3177}
3178
Roland Levillain1a653882016-03-18 18:05:57 +00003179static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3180 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3181 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3182}
3183
3184void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3185 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3186 Location rhs_loc = instruction->GetLocations()->InAt(1);
3187 if (rhs_loc.IsConstant()) {
3188 // 0.0 is the only immediate that can be encoded directly in
3189 // an FCMP instruction.
3190 //
3191 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3192 // specify that in a floating-point comparison, positive zero
3193 // and negative zero are considered equal, so we can use the
3194 // literal 0.0 for both cases here.
3195 //
3196 // Note however that some methods (Float.equal, Float.compare,
3197 // Float.compareTo, Double.equal, Double.compare,
3198 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3199 // StrictMath.min) consider 0.0 to be (strictly) greater than
3200 // -0.0. So if we ever translate calls to these methods into a
3201 // HCompare instruction, we must handle the -0.0 case with
3202 // care here.
3203 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3204 __ Fcmp(lhs_reg, 0.0);
3205 } else {
3206 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3207 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003208}
3209
Serban Constantinescu02164b32014-11-13 14:05:07 +00003210void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003211 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003212 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3213 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003214 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003215 case Primitive::kPrimBoolean:
3216 case Primitive::kPrimByte:
3217 case Primitive::kPrimShort:
3218 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003219 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003220 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003221 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003222 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003223 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3224 break;
3225 }
3226 case Primitive::kPrimFloat:
3227 case Primitive::kPrimDouble: {
3228 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003229 locations->SetInAt(1,
3230 IsFloatingPointZeroConstant(compare->InputAt(1))
3231 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3232 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003233 locations->SetOut(Location::RequiresRegister());
3234 break;
3235 }
3236 default:
3237 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3238 }
3239}
3240
3241void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3242 Primitive::Type in_type = compare->InputAt(0)->GetType();
3243
3244 // 0 if: left == right
3245 // 1 if: left > right
3246 // -1 if: left < right
3247 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003248 case Primitive::kPrimBoolean:
3249 case Primitive::kPrimByte:
3250 case Primitive::kPrimShort:
3251 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003252 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003253 case Primitive::kPrimLong: {
3254 Register result = OutputRegister(compare);
3255 Register left = InputRegisterAt(compare, 0);
3256 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003257 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003258 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3259 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003260 break;
3261 }
3262 case Primitive::kPrimFloat:
3263 case Primitive::kPrimDouble: {
3264 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003265 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003266 __ Cset(result, ne);
3267 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003268 break;
3269 }
3270 default:
3271 LOG(FATAL) << "Unimplemented compare type " << in_type;
3272 }
3273}
3274
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003275void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003276 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003277
3278 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3279 locations->SetInAt(0, Location::RequiresFpuRegister());
3280 locations->SetInAt(1,
3281 IsFloatingPointZeroConstant(instruction->InputAt(1))
3282 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3283 : Location::RequiresFpuRegister());
3284 } else {
3285 // Integer cases.
3286 locations->SetInAt(0, Location::RequiresRegister());
3287 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3288 }
3289
David Brazdilb3e773e2016-01-26 11:28:37 +00003290 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003291 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003292 }
3293}
3294
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003295void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003296 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003297 return;
3298 }
3299
3300 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003301 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003302 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003303
Roland Levillain7f63c522015-07-13 15:54:55 +00003304 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003305 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003306 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003307 } else {
3308 // Integer cases.
3309 Register lhs = InputRegisterAt(instruction, 0);
3310 Operand rhs = InputOperandAt(instruction, 1);
3311 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003312 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003313 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003314}
3315
3316#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3317 M(Equal) \
3318 M(NotEqual) \
3319 M(LessThan) \
3320 M(LessThanOrEqual) \
3321 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003322 M(GreaterThanOrEqual) \
3323 M(Below) \
3324 M(BelowOrEqual) \
3325 M(Above) \
3326 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003327#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003328void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3329void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003330FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003331#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003332#undef FOR_EACH_CONDITION_INSTRUCTION
3333
Zheng Xuc6667102015-05-15 16:08:45 +08003334void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3335 DCHECK(instruction->IsDiv() || instruction->IsRem());
3336
3337 LocationSummary* locations = instruction->GetLocations();
3338 Location second = locations->InAt(1);
3339 DCHECK(second.IsConstant());
3340
3341 Register out = OutputRegister(instruction);
3342 Register dividend = InputRegisterAt(instruction, 0);
3343 int64_t imm = Int64FromConstant(second.GetConstant());
3344 DCHECK(imm == 1 || imm == -1);
3345
3346 if (instruction->IsRem()) {
3347 __ Mov(out, 0);
3348 } else {
3349 if (imm == 1) {
3350 __ Mov(out, dividend);
3351 } else {
3352 __ Neg(out, dividend);
3353 }
3354 }
3355}
3356
3357void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3358 DCHECK(instruction->IsDiv() || instruction->IsRem());
3359
3360 LocationSummary* locations = instruction->GetLocations();
3361 Location second = locations->InAt(1);
3362 DCHECK(second.IsConstant());
3363
3364 Register out = OutputRegister(instruction);
3365 Register dividend = InputRegisterAt(instruction, 0);
3366 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003367 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003368 int ctz_imm = CTZ(abs_imm);
3369
3370 UseScratchRegisterScope temps(GetVIXLAssembler());
3371 Register temp = temps.AcquireSameSizeAs(out);
3372
3373 if (instruction->IsDiv()) {
3374 __ Add(temp, dividend, abs_imm - 1);
3375 __ Cmp(dividend, 0);
3376 __ Csel(out, temp, dividend, lt);
3377 if (imm > 0) {
3378 __ Asr(out, out, ctz_imm);
3379 } else {
3380 __ Neg(out, Operand(out, ASR, ctz_imm));
3381 }
3382 } else {
3383 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3384 __ Asr(temp, dividend, bits - 1);
3385 __ Lsr(temp, temp, bits - ctz_imm);
3386 __ Add(out, dividend, temp);
3387 __ And(out, out, abs_imm - 1);
3388 __ Sub(out, out, temp);
3389 }
3390}
3391
3392void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3393 DCHECK(instruction->IsDiv() || instruction->IsRem());
3394
3395 LocationSummary* locations = instruction->GetLocations();
3396 Location second = locations->InAt(1);
3397 DCHECK(second.IsConstant());
3398
3399 Register out = OutputRegister(instruction);
3400 Register dividend = InputRegisterAt(instruction, 0);
3401 int64_t imm = Int64FromConstant(second.GetConstant());
3402
3403 Primitive::Type type = instruction->GetResultType();
3404 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3405
3406 int64_t magic;
3407 int shift;
3408 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3409
3410 UseScratchRegisterScope temps(GetVIXLAssembler());
3411 Register temp = temps.AcquireSameSizeAs(out);
3412
3413 // temp = get_high(dividend * magic)
3414 __ Mov(temp, magic);
3415 if (type == Primitive::kPrimLong) {
3416 __ Smulh(temp, dividend, temp);
3417 } else {
3418 __ Smull(temp.X(), dividend, temp);
3419 __ Lsr(temp.X(), temp.X(), 32);
3420 }
3421
3422 if (imm > 0 && magic < 0) {
3423 __ Add(temp, temp, dividend);
3424 } else if (imm < 0 && magic > 0) {
3425 __ Sub(temp, temp, dividend);
3426 }
3427
3428 if (shift != 0) {
3429 __ Asr(temp, temp, shift);
3430 }
3431
3432 if (instruction->IsDiv()) {
3433 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3434 } else {
3435 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3436 // TODO: Strength reduction for msub.
3437 Register temp_imm = temps.AcquireSameSizeAs(out);
3438 __ Mov(temp_imm, imm);
3439 __ Msub(out, temp, temp_imm, dividend);
3440 }
3441}
3442
3443void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3444 DCHECK(instruction->IsDiv() || instruction->IsRem());
3445 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003446 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003447
3448 LocationSummary* locations = instruction->GetLocations();
3449 Register out = OutputRegister(instruction);
3450 Location second = locations->InAt(1);
3451
3452 if (second.IsConstant()) {
3453 int64_t imm = Int64FromConstant(second.GetConstant());
3454
3455 if (imm == 0) {
3456 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3457 } else if (imm == 1 || imm == -1) {
3458 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003459 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003460 DivRemByPowerOfTwo(instruction);
3461 } else {
3462 DCHECK(imm <= -2 || imm >= 2);
3463 GenerateDivRemWithAnyConstant(instruction);
3464 }
3465 } else {
3466 Register dividend = InputRegisterAt(instruction, 0);
3467 Register divisor = InputRegisterAt(instruction, 1);
3468 if (instruction->IsDiv()) {
3469 __ Sdiv(out, dividend, divisor);
3470 } else {
3471 UseScratchRegisterScope temps(GetVIXLAssembler());
3472 Register temp = temps.AcquireSameSizeAs(out);
3473 __ Sdiv(temp, dividend, divisor);
3474 __ Msub(out, temp, divisor, dividend);
3475 }
3476 }
3477}
3478
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003479void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3480 LocationSummary* locations =
3481 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3482 switch (div->GetResultType()) {
3483 case Primitive::kPrimInt:
3484 case Primitive::kPrimLong:
3485 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003486 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003487 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3488 break;
3489
3490 case Primitive::kPrimFloat:
3491 case Primitive::kPrimDouble:
3492 locations->SetInAt(0, Location::RequiresFpuRegister());
3493 locations->SetInAt(1, Location::RequiresFpuRegister());
3494 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3495 break;
3496
3497 default:
3498 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3499 }
3500}
3501
3502void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3503 Primitive::Type type = div->GetResultType();
3504 switch (type) {
3505 case Primitive::kPrimInt:
3506 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003507 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003508 break;
3509
3510 case Primitive::kPrimFloat:
3511 case Primitive::kPrimDouble:
3512 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3513 break;
3514
3515 default:
3516 LOG(FATAL) << "Unexpected div type " << type;
3517 }
3518}
3519
Alexandre Rames67555f72014-11-18 10:55:16 +00003520void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003521 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003522 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003523}
3524
3525void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3526 SlowPathCodeARM64* slow_path =
3527 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3528 codegen_->AddSlowPath(slow_path);
3529 Location value = instruction->GetLocations()->InAt(0);
3530
Alexandre Rames3e69f162014-12-10 10:36:50 +00003531 Primitive::Type type = instruction->GetType();
3532
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003533 if (!Primitive::IsIntegralType(type)) {
3534 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003535 return;
3536 }
3537
Alexandre Rames67555f72014-11-18 10:55:16 +00003538 if (value.IsConstant()) {
3539 int64_t divisor = Int64ConstantFrom(value);
3540 if (divisor == 0) {
3541 __ B(slow_path->GetEntryLabel());
3542 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003543 // A division by a non-null constant is valid. We don't need to perform
3544 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003545 }
3546 } else {
3547 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3548 }
3549}
3550
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003551void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3552 LocationSummary* locations =
3553 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3554 locations->SetOut(Location::ConstantLocation(constant));
3555}
3556
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003557void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3558 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003559 // Will be generated at use site.
3560}
3561
Alexandre Rames5319def2014-10-23 10:03:10 +01003562void LocationsBuilderARM64::VisitExit(HExit* exit) {
3563 exit->SetLocations(nullptr);
3564}
3565
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003566void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003567}
3568
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003569void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3570 LocationSummary* locations =
3571 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3572 locations->SetOut(Location::ConstantLocation(constant));
3573}
3574
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003575void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003576 // Will be generated at use site.
3577}
3578
David Brazdilfc6a86a2015-06-26 10:33:45 +00003579void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003580 DCHECK(!successor->IsExitBlock());
3581 HBasicBlock* block = got->GetBlock();
3582 HInstruction* previous = got->GetPrevious();
3583 HLoopInformation* info = block->GetLoopInformation();
3584
David Brazdil46e2a392015-03-16 17:31:52 +00003585 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003586 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3587 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3588 return;
3589 }
3590 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3591 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01003592 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003593 }
3594 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003595 __ B(codegen_->GetLabelOf(successor));
3596 }
3597}
3598
David Brazdilfc6a86a2015-06-26 10:33:45 +00003599void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3600 got->SetLocations(nullptr);
3601}
3602
3603void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3604 HandleGoto(got, got->GetSuccessor());
3605}
3606
3607void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3608 try_boundary->SetLocations(nullptr);
3609}
3610
3611void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3612 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3613 if (!successor->IsExitBlock()) {
3614 HandleGoto(try_boundary, successor);
3615 }
3616}
3617
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003618void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003619 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003620 vixl::aarch64::Label* true_target,
3621 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003622 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003623
David Brazdil0debae72015-11-12 18:37:00 +00003624 if (true_target == nullptr && false_target == nullptr) {
3625 // Nothing to do. The code always falls through.
3626 return;
3627 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003628 // Constant condition, statically compared against "true" (integer value 1).
3629 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003630 if (true_target != nullptr) {
3631 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003632 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003633 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003634 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003635 if (false_target != nullptr) {
3636 __ B(false_target);
3637 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003638 }
David Brazdil0debae72015-11-12 18:37:00 +00003639 return;
3640 }
3641
3642 // The following code generates these patterns:
3643 // (1) true_target == nullptr && false_target != nullptr
3644 // - opposite condition true => branch to false_target
3645 // (2) true_target != nullptr && false_target == nullptr
3646 // - condition true => branch to true_target
3647 // (3) true_target != nullptr && false_target != nullptr
3648 // - condition true => branch to true_target
3649 // - branch to false_target
3650 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003651 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003652 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003653 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003654 if (true_target == nullptr) {
3655 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3656 } else {
3657 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3658 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003659 } else {
3660 // The condition instruction has not been materialized, use its inputs as
3661 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003662 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003663
David Brazdil0debae72015-11-12 18:37:00 +00003664 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003665 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003666 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003667 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003668 IfCondition opposite_condition = condition->GetOppositeCondition();
3669 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003670 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003671 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003672 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003673 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003674 // Integer cases.
3675 Register lhs = InputRegisterAt(condition, 0);
3676 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003677
3678 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003679 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003680 if (true_target == nullptr) {
3681 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3682 non_fallthrough_target = false_target;
3683 } else {
3684 arm64_cond = ARM64Condition(condition->GetCondition());
3685 non_fallthrough_target = true_target;
3686 }
3687
Aart Bik086d27e2016-01-20 17:02:00 -08003688 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003689 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003690 switch (arm64_cond) {
3691 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003692 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003693 break;
3694 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003695 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003696 break;
3697 case lt:
3698 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003699 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003700 break;
3701 case ge:
3702 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003703 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003704 break;
3705 default:
3706 // Without the `static_cast` the compiler throws an error for
3707 // `-Werror=sign-promo`.
3708 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3709 }
3710 } else {
3711 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003712 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003713 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003714 }
3715 }
David Brazdil0debae72015-11-12 18:37:00 +00003716
3717 // If neither branch falls through (case 3), the conditional branch to `true_target`
3718 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3719 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003720 __ B(false_target);
3721 }
3722}
3723
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003724void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3725 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003726 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003727 locations->SetInAt(0, Location::RequiresRegister());
3728 }
3729}
3730
3731void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003732 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3733 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003734 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3735 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3736 true_target = nullptr;
3737 }
3738 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3739 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3740 false_target = nullptr;
3741 }
David Brazdil0debae72015-11-12 18:37:00 +00003742 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003743}
3744
3745void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3746 LocationSummary* locations = new (GetGraph()->GetArena())
3747 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003748 InvokeRuntimeCallingConvention calling_convention;
3749 RegisterSet caller_saves = RegisterSet::Empty();
3750 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3751 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003752 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003753 locations->SetInAt(0, Location::RequiresRegister());
3754 }
3755}
3756
3757void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003758 SlowPathCodeARM64* slow_path =
3759 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003760 GenerateTestAndBranch(deoptimize,
3761 /* condition_input_index */ 0,
3762 slow_path->GetEntryLabel(),
3763 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003764}
3765
Mingyao Yang063fc772016-08-02 11:02:54 -07003766void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3767 LocationSummary* locations = new (GetGraph()->GetArena())
3768 LocationSummary(flag, LocationSummary::kNoCall);
3769 locations->SetOut(Location::RequiresRegister());
3770}
3771
3772void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3773 __ Ldr(OutputRegister(flag),
3774 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3775}
3776
David Brazdilc0b601b2016-02-08 14:20:45 +00003777static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3778 return condition->IsCondition() &&
3779 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3780}
3781
Alexandre Rames880f1192016-06-13 16:04:50 +01003782static inline Condition GetConditionForSelect(HCondition* condition) {
3783 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003784 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3785 : ARM64Condition(cond);
3786}
3787
David Brazdil74eb1b22015-12-14 11:44:01 +00003788void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3789 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003790 if (Primitive::IsFloatingPointType(select->GetType())) {
3791 locations->SetInAt(0, Location::RequiresFpuRegister());
3792 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003793 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003794 } else {
3795 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3796 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3797 bool is_true_value_constant = cst_true_value != nullptr;
3798 bool is_false_value_constant = cst_false_value != nullptr;
3799 // Ask VIXL whether we should synthesize constants in registers.
3800 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3801 Operand true_op = is_true_value_constant ?
3802 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3803 Operand false_op = is_false_value_constant ?
3804 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3805 bool true_value_in_register = false;
3806 bool false_value_in_register = false;
3807 MacroAssembler::GetCselSynthesisInformation(
3808 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3809 true_value_in_register |= !is_true_value_constant;
3810 false_value_in_register |= !is_false_value_constant;
3811
3812 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3813 : Location::ConstantLocation(cst_true_value));
3814 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3815 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003816 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003817 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003818
David Brazdil74eb1b22015-12-14 11:44:01 +00003819 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3820 locations->SetInAt(2, Location::RequiresRegister());
3821 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003822}
3823
3824void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003825 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003826 Condition csel_cond;
3827
3828 if (IsBooleanValueOrMaterializedCondition(cond)) {
3829 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003830 // Use the condition flags set by the previous instruction.
3831 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003832 } else {
3833 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003834 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003835 }
3836 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003837 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003838 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003839 } else {
3840 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003841 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003842 }
3843
Alexandre Rames880f1192016-06-13 16:04:50 +01003844 if (Primitive::IsFloatingPointType(select->GetType())) {
3845 __ Fcsel(OutputFPRegister(select),
3846 InputFPRegisterAt(select, 1),
3847 InputFPRegisterAt(select, 0),
3848 csel_cond);
3849 } else {
3850 __ Csel(OutputRegister(select),
3851 InputOperandAt(select, 1),
3852 InputOperandAt(select, 0),
3853 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003854 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003855}
3856
David Srbecky0cf44932015-12-09 14:09:59 +00003857void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3858 new (GetGraph()->GetArena()) LocationSummary(info);
3859}
3860
David Srbeckyd28f4a02016-03-14 17:14:24 +00003861void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3862 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003863}
3864
3865void CodeGeneratorARM64::GenerateNop() {
3866 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003867}
3868
Alexandre Rames5319def2014-10-23 10:03:10 +01003869void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003870 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003871}
3872
3873void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003874 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003875}
3876
3877void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003878 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003879}
3880
3881void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003882 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003883}
3884
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003885// Temp is used for read barrier.
3886static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3887 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003888 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003889 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3890 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3891 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3892 return 1;
3893 }
3894 return 0;
3895}
3896
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003897// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003898// interface pointer, one for loading the current interface.
3899// The other checks have one temp for loading the object's class.
3900static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3901 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3902 return 3;
3903 }
3904 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003905}
3906
Alexandre Rames67555f72014-11-18 10:55:16 +00003907void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003908 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003909 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003910 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003911 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003912 case TypeCheckKind::kExactCheck:
3913 case TypeCheckKind::kAbstractClassCheck:
3914 case TypeCheckKind::kClassHierarchyCheck:
3915 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003916 call_kind =
3917 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003918 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003919 break;
3920 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003921 case TypeCheckKind::kUnresolvedCheck:
3922 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003923 call_kind = LocationSummary::kCallOnSlowPath;
3924 break;
3925 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003926
Alexandre Rames67555f72014-11-18 10:55:16 +00003927 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003928 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003929 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003930 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003931 locations->SetInAt(0, Location::RequiresRegister());
3932 locations->SetInAt(1, Location::RequiresRegister());
3933 // The "out" register is used as a temporary, so it overlaps with the inputs.
3934 // Note that TypeCheckSlowPathARM64 uses this register too.
3935 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003936 // Add temps if necessary for read barriers.
3937 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003938}
3939
3940void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003941 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003942 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003943 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003944 Register obj = InputRegisterAt(instruction, 0);
3945 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003946 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003947 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003948 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3949 DCHECK_LE(num_temps, 1u);
3950 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003951 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3952 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3953 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3954 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003955
Scott Wakeling97c72b72016-06-24 16:19:36 +01003956 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003957 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003958
3959 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003960 // Avoid null check if we know `obj` is not null.
3961 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003962 __ Cbz(obj, &zero);
3963 }
3964
Roland Levillain44015862016-01-22 11:47:17 +00003965 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003966 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003967 // /* HeapReference<Class> */ out = obj->klass_
3968 GenerateReferenceLoadTwoRegisters(instruction,
3969 out_loc,
3970 obj_loc,
3971 class_offset,
3972 maybe_temp_loc,
3973 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003974 __ Cmp(out, cls);
3975 __ Cset(out, eq);
3976 if (zero.IsLinked()) {
3977 __ B(&done);
3978 }
3979 break;
3980 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003981
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003982 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003983 // /* HeapReference<Class> */ out = obj->klass_
3984 GenerateReferenceLoadTwoRegisters(instruction,
3985 out_loc,
3986 obj_loc,
3987 class_offset,
3988 maybe_temp_loc,
3989 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003990 // If the class is abstract, we eagerly fetch the super class of the
3991 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003992 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003993 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003994 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003995 GenerateReferenceLoadOneRegister(instruction,
3996 out_loc,
3997 super_offset,
3998 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003999 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004000 // If `out` is null, we use it for the result, and jump to `done`.
4001 __ Cbz(out, &done);
4002 __ Cmp(out, cls);
4003 __ B(ne, &loop);
4004 __ Mov(out, 1);
4005 if (zero.IsLinked()) {
4006 __ B(&done);
4007 }
4008 break;
4009 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004010
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004011 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004012 // /* HeapReference<Class> */ out = obj->klass_
4013 GenerateReferenceLoadTwoRegisters(instruction,
4014 out_loc,
4015 obj_loc,
4016 class_offset,
4017 maybe_temp_loc,
4018 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004019 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004020 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004021 __ Bind(&loop);
4022 __ Cmp(out, cls);
4023 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004024 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004025 GenerateReferenceLoadOneRegister(instruction,
4026 out_loc,
4027 super_offset,
4028 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004029 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004030 __ Cbnz(out, &loop);
4031 // If `out` is null, we use it for the result, and jump to `done`.
4032 __ B(&done);
4033 __ Bind(&success);
4034 __ Mov(out, 1);
4035 if (zero.IsLinked()) {
4036 __ B(&done);
4037 }
4038 break;
4039 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004040
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004041 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004042 // /* HeapReference<Class> */ out = obj->klass_
4043 GenerateReferenceLoadTwoRegisters(instruction,
4044 out_loc,
4045 obj_loc,
4046 class_offset,
4047 maybe_temp_loc,
4048 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004049 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004050 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004051 __ Cmp(out, cls);
4052 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004053 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004054 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004055 GenerateReferenceLoadOneRegister(instruction,
4056 out_loc,
4057 component_offset,
4058 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004059 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004060 // If `out` is null, we use it for the result, and jump to `done`.
4061 __ Cbz(out, &done);
4062 __ Ldrh(out, HeapOperand(out, primitive_offset));
4063 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4064 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004065 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004066 __ Mov(out, 1);
4067 __ B(&done);
4068 break;
4069 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004070
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004071 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004072 // No read barrier since the slow path will retry upon failure.
4073 // /* HeapReference<Class> */ out = obj->klass_
4074 GenerateReferenceLoadTwoRegisters(instruction,
4075 out_loc,
4076 obj_loc,
4077 class_offset,
4078 maybe_temp_loc,
4079 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004080 __ Cmp(out, cls);
4081 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004082 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4083 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004084 codegen_->AddSlowPath(slow_path);
4085 __ B(ne, slow_path->GetEntryLabel());
4086 __ Mov(out, 1);
4087 if (zero.IsLinked()) {
4088 __ B(&done);
4089 }
4090 break;
4091 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004092
Calin Juravle98893e12015-10-02 21:05:03 +01004093 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004094 case TypeCheckKind::kInterfaceCheck: {
4095 // Note that we indeed only call on slow path, but we always go
4096 // into the slow path for the unresolved and interface check
4097 // cases.
4098 //
4099 // We cannot directly call the InstanceofNonTrivial runtime
4100 // entry point without resorting to a type checking slow path
4101 // here (i.e. by calling InvokeRuntime directly), as it would
4102 // require to assign fixed registers for the inputs of this
4103 // HInstanceOf instruction (following the runtime calling
4104 // convention), which might be cluttered by the potential first
4105 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004106 //
4107 // TODO: Introduce a new runtime entry point taking the object
4108 // to test (instead of its class) as argument, and let it deal
4109 // with the read barrier issues. This will let us refactor this
4110 // case of the `switch` code as it was previously (with a direct
4111 // call to the runtime not using a type checking slow path).
4112 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004113 DCHECK(locations->OnlyCallsOnSlowPath());
4114 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4115 /* is_fatal */ false);
4116 codegen_->AddSlowPath(slow_path);
4117 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004118 if (zero.IsLinked()) {
4119 __ B(&done);
4120 }
4121 break;
4122 }
4123 }
4124
4125 if (zero.IsLinked()) {
4126 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004127 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004128 }
4129
4130 if (done.IsLinked()) {
4131 __ Bind(&done);
4132 }
4133
4134 if (slow_path != nullptr) {
4135 __ Bind(slow_path->GetExitLabel());
4136 }
4137}
4138
4139void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4140 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4141 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4142
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004143 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4144 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004145 case TypeCheckKind::kExactCheck:
4146 case TypeCheckKind::kAbstractClassCheck:
4147 case TypeCheckKind::kClassHierarchyCheck:
4148 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004149 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4150 LocationSummary::kCallOnSlowPath :
4151 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004152 break;
4153 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004154 case TypeCheckKind::kUnresolvedCheck:
4155 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004156 call_kind = LocationSummary::kCallOnSlowPath;
4157 break;
4158 }
4159
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004160 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4161 locations->SetInAt(0, Location::RequiresRegister());
4162 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004163 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4164 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004165}
4166
4167void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004168 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004169 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004170 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004171 Register obj = InputRegisterAt(instruction, 0);
4172 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004173 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4174 DCHECK_GE(num_temps, 1u);
4175 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004176 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004177 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4178 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004179 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004180 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4181 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4182 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4183 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4184 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4185 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4186 const uint32_t object_array_data_offset =
4187 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004188
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004189 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004190 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4191 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4192 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004193 if (!kEmitCompilerReadBarrier) {
4194 is_type_check_slow_path_fatal =
4195 (type_check_kind == TypeCheckKind::kExactCheck ||
4196 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4197 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4198 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4199 !instruction->CanThrowIntoCatchBlock();
4200 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004201 SlowPathCodeARM64* type_check_slow_path =
4202 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4203 is_type_check_slow_path_fatal);
4204 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004205
Scott Wakeling97c72b72016-06-24 16:19:36 +01004206 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004207 // Avoid null check if we know obj is not null.
4208 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004209 __ Cbz(obj, &done);
4210 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004211
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004212 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004213 case TypeCheckKind::kExactCheck:
4214 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004215 // /* HeapReference<Class> */ temp = obj->klass_
4216 GenerateReferenceLoadTwoRegisters(instruction,
4217 temp_loc,
4218 obj_loc,
4219 class_offset,
4220 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004221 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004222
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004223 __ Cmp(temp, cls);
4224 // Jump to slow path for throwing the exception or doing a
4225 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004226 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004227 break;
4228 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004229
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004230 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004231 // /* HeapReference<Class> */ temp = obj->klass_
4232 GenerateReferenceLoadTwoRegisters(instruction,
4233 temp_loc,
4234 obj_loc,
4235 class_offset,
4236 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004237 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004238
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004239 // If the class is abstract, we eagerly fetch the super class of the
4240 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004241 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004242 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004243 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004244 GenerateReferenceLoadOneRegister(instruction,
4245 temp_loc,
4246 super_offset,
4247 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004248 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004249
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004250 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4251 // exception.
4252 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4253 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004254 __ Cmp(temp, cls);
4255 __ B(ne, &loop);
4256 break;
4257 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004258
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004259 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004260 // /* HeapReference<Class> */ temp = obj->klass_
4261 GenerateReferenceLoadTwoRegisters(instruction,
4262 temp_loc,
4263 obj_loc,
4264 class_offset,
4265 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004266 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004267
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004268 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004269 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004270 __ Bind(&loop);
4271 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004272 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004273
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004274 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004275 GenerateReferenceLoadOneRegister(instruction,
4276 temp_loc,
4277 super_offset,
4278 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004279 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004280
4281 // If the class reference currently in `temp` is not null, jump
4282 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004283 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004284 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004285 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004286 break;
4287 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004288
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004289 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004290 // /* HeapReference<Class> */ temp = obj->klass_
4291 GenerateReferenceLoadTwoRegisters(instruction,
4292 temp_loc,
4293 obj_loc,
4294 class_offset,
4295 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004296 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004297
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004298 // Do an exact check.
4299 __ Cmp(temp, cls);
4300 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004301
4302 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004303 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004304 GenerateReferenceLoadOneRegister(instruction,
4305 temp_loc,
4306 component_offset,
4307 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004308 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004309
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004310 // If the component type is null, jump to the slow path to throw the exception.
4311 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4312 // Otherwise, the object is indeed an array. Further check that this component type is not a
4313 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004314 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4315 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004316 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004317 break;
4318 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004319
Calin Juravle98893e12015-10-02 21:05:03 +01004320 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004321 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004322 //
4323 // We cannot directly call the CheckCast runtime entry point
4324 // without resorting to a type checking slow path here (i.e. by
4325 // calling InvokeRuntime directly), as it would require to
4326 // assign fixed registers for the inputs of this HInstanceOf
4327 // instruction (following the runtime calling convention), which
4328 // might be cluttered by the potential first read barrier
4329 // emission at the beginning of this method.
4330 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004331 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004332 case TypeCheckKind::kInterfaceCheck: {
4333 // /* HeapReference<Class> */ temp = obj->klass_
4334 GenerateReferenceLoadTwoRegisters(instruction,
4335 temp_loc,
4336 obj_loc,
4337 class_offset,
4338 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004339 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004340
4341 // /* HeapReference<Class> */ temp = temp->iftable_
4342 GenerateReferenceLoadTwoRegisters(instruction,
4343 temp_loc,
4344 temp_loc,
4345 iftable_offset,
4346 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004347 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004348 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004349 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004350 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004351 vixl::aarch64::Label start_loop;
4352 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004353 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004354 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4355 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004356 // Go to next interface.
4357 __ Add(temp, temp, 2 * kHeapReferenceSize);
4358 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004359 // Compare the classes and continue the loop if they do not match.
4360 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4361 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004362 break;
4363 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004364 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004365 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004366
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004367 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004368}
4369
Alexandre Rames5319def2014-10-23 10:03:10 +01004370void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4371 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4372 locations->SetOut(Location::ConstantLocation(constant));
4373}
4374
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004375void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004376 // Will be generated at use site.
4377}
4378
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004379void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4380 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4381 locations->SetOut(Location::ConstantLocation(constant));
4382}
4383
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004384void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004385 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004386}
4387
Calin Juravle175dc732015-08-25 15:42:32 +01004388void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4389 // The trampoline uses the same calling convention as dex calling conventions,
4390 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4391 // the method_idx.
4392 HandleInvoke(invoke);
4393}
4394
4395void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4396 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004397 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004398}
4399
Alexandre Rames5319def2014-10-23 10:03:10 +01004400void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004401 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004402 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004403}
4404
Alexandre Rames67555f72014-11-18 10:55:16 +00004405void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4406 HandleInvoke(invoke);
4407}
4408
4409void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4410 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004411 LocationSummary* locations = invoke->GetLocations();
4412 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004413 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004414 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004415 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004416
4417 // The register ip1 is required to be used for the hidden argument in
4418 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004419 MacroAssembler* masm = GetVIXLAssembler();
4420 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004421 scratch_scope.Exclude(ip1);
4422 __ Mov(ip1, invoke->GetDexMethodIndex());
4423
Artem Serov914d7a82017-02-07 14:33:49 +00004424 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004425 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004426 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004427 {
4428 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4429 // /* HeapReference<Class> */ temp = temp->klass_
4430 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4431 codegen_->MaybeRecordImplicitNullCheck(invoke);
4432 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004433 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004434 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004435 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004436 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004437 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004438 }
Artem Serov914d7a82017-02-07 14:33:49 +00004439
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004440 // Instead of simply (possibly) unpoisoning `temp` here, we should
4441 // emit a read barrier for the previous class reference load.
4442 // However this is not required in practice, as this is an
4443 // intermediate/temporary reference and because the current
4444 // concurrent copying collector keeps the from-space memory
4445 // intact/accessible until the end of the marking phase (the
4446 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004447 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004448 __ Ldr(temp,
4449 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4450 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004451 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004452 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004453 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004454 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004455 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004456
4457 {
4458 // Ensure the pc position is recorded immediately after the `blr` instruction.
4459 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4460
4461 // lr();
4462 __ blr(lr);
4463 DCHECK(!codegen_->IsLeafMethod());
4464 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4465 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004466
4467 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004468}
4469
4470void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004471 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004472 if (intrinsic.TryDispatch(invoke)) {
4473 return;
4474 }
4475
Alexandre Rames67555f72014-11-18 10:55:16 +00004476 HandleInvoke(invoke);
4477}
4478
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004479void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004480 // Explicit clinit checks triggered by static invokes must have been pruned by
4481 // art::PrepareForRegisterAllocation.
4482 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004483
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004484 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004485 if (intrinsic.TryDispatch(invoke)) {
4486 return;
4487 }
4488
Alexandre Rames67555f72014-11-18 10:55:16 +00004489 HandleInvoke(invoke);
4490}
4491
Andreas Gampe878d58c2015-01-15 23:24:00 -08004492static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4493 if (invoke->GetLocations()->Intrinsified()) {
4494 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4495 intrinsic.Dispatch(invoke);
4496 return true;
4497 }
4498 return false;
4499}
4500
Vladimir Markodc151b22015-10-15 18:02:30 +01004501HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4502 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004503 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004504 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004505 return desired_dispatch_info;
4506}
4507
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004508void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4509 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004510 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004511 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4512 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004513 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4514 uint32_t offset =
4515 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004516 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004517 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004518 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004519 }
Vladimir Marko58155012015-08-19 12:49:41 +00004520 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004521 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004522 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004523 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4524 DCHECK(GetCompilerOptions().IsBootImage());
4525 // Add ADRP with its PC-relative method patch.
4526 vixl::aarch64::Label* adrp_label = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
4527 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4528 // Add ADD with its PC-relative method patch.
4529 vixl::aarch64::Label* add_label =
4530 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), adrp_label);
4531 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4532 break;
4533 }
Vladimir Marko58155012015-08-19 12:49:41 +00004534 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4535 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004536 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004537 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004538 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko58155012015-08-19 12:49:41 +00004539 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004540 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4541 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004542 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004543 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004544 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004545 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004546 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004547 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004548 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004549 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4550 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4551 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004552 }
4553 }
4554
4555 switch (invoke->GetCodePtrLocation()) {
4556 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004557 {
4558 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4559 ExactAssemblyScope eas(GetVIXLAssembler(),
4560 kInstructionSize,
4561 CodeBufferCheckScope::kExactSize);
4562 __ bl(&frame_entry_label_);
4563 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4564 }
Vladimir Marko58155012015-08-19 12:49:41 +00004565 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004566 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4567 // LR = callee_method->entry_point_from_quick_compiled_code_;
4568 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004569 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004570 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004571 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004572 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004573 ExactAssemblyScope eas(GetVIXLAssembler(),
4574 kInstructionSize,
4575 CodeBufferCheckScope::kExactSize);
4576 // lr()
4577 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004578 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004579 }
Vladimir Marko58155012015-08-19 12:49:41 +00004580 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004581 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004582
Andreas Gampe878d58c2015-01-15 23:24:00 -08004583 DCHECK(!IsLeafMethod());
4584}
4585
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004586void CodeGeneratorARM64::GenerateVirtualCall(
4587 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004588 // Use the calling convention instead of the location of the receiver, as
4589 // intrinsics may have put the receiver in a different register. In the intrinsics
4590 // slow path, the arguments have been moved to the right place, so here we are
4591 // guaranteed that the receiver is the first register of the calling convention.
4592 InvokeDexCallingConvention calling_convention;
4593 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004594 Register temp = XRegisterFrom(temp_in);
4595 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4596 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4597 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004598 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004599
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004600 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004601
4602 {
4603 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4604 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4605 // /* HeapReference<Class> */ temp = receiver->klass_
4606 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4607 MaybeRecordImplicitNullCheck(invoke);
4608 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004609 // Instead of simply (possibly) unpoisoning `temp` here, we should
4610 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004611 // intermediate/temporary reference and because the current
4612 // concurrent copying collector keeps the from-space memory
4613 // intact/accessible until the end of the marking phase (the
4614 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004615 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4616 // temp = temp->GetMethodAt(method_offset);
4617 __ Ldr(temp, MemOperand(temp, method_offset));
4618 // lr = temp->GetEntryPoint();
4619 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004620 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004621 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004622 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4623 // lr();
4624 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004625 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004626 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004627}
4628
Orion Hodsonac141392017-01-13 11:53:47 +00004629void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4630 HandleInvoke(invoke);
4631}
4632
4633void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4634 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004635 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004636}
4637
Vladimir Marko65979462017-05-19 17:25:12 +01004638vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeMethodPatch(
4639 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004640 vixl::aarch64::Label* adrp_label) {
Vladimir Marko65979462017-05-19 17:25:12 +01004641 return NewPcRelativePatch(*target_method.dex_file,
4642 target_method.dex_method_index,
4643 adrp_label,
4644 &pc_relative_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004645}
4646
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004647vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4648 MethodReference target_method,
4649 vixl::aarch64::Label* adrp_label) {
4650 return NewPcRelativePatch(*target_method.dex_file,
4651 target_method.dex_method_index,
4652 adrp_label,
4653 &method_bss_entry_patches_);
4654}
4655
Scott Wakeling97c72b72016-06-24 16:19:36 +01004656vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4657 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004658 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004659 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004660 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004661}
4662
Vladimir Marko1998cd02017-01-13 13:02:58 +00004663vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4664 const DexFile& dex_file,
4665 dex::TypeIndex type_index,
4666 vixl::aarch64::Label* adrp_label) {
4667 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4668}
4669
Vladimir Marko65979462017-05-19 17:25:12 +01004670vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4671 const DexFile& dex_file,
4672 dex::StringIndex string_index,
4673 vixl::aarch64::Label* adrp_label) {
4674 return
4675 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
4676}
4677
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004678vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4679 baker_read_barrier_patches_.emplace_back(custom_data);
4680 return &baker_read_barrier_patches_.back().label;
4681}
4682
Scott Wakeling97c72b72016-06-24 16:19:36 +01004683vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4684 const DexFile& dex_file,
4685 uint32_t offset_or_index,
4686 vixl::aarch64::Label* adrp_label,
4687 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004688 // Add a patch entry and return the label.
4689 patches->emplace_back(dex_file, offset_or_index);
4690 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004691 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004692 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4693 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4694 return label;
4695}
4696
Scott Wakeling97c72b72016-06-24 16:19:36 +01004697vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4698 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004699 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004700}
4701
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004702vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004703 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4704 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4705 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004706 return jit_string_patches_.GetOrCreate(
4707 StringReference(&dex_file, string_index),
4708 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4709}
4710
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004711vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004712 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4713 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4714 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004715 return jit_class_patches_.GetOrCreate(
4716 TypeReference(&dex_file, type_index),
4717 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4718}
4719
Vladimir Markoaad75c62016-10-03 08:46:48 +00004720void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4721 vixl::aarch64::Register reg) {
4722 DCHECK(reg.IsX());
4723 SingleEmissionCheckScope guard(GetVIXLAssembler());
4724 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004725 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004726}
4727
4728void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4729 vixl::aarch64::Register out,
4730 vixl::aarch64::Register base) {
4731 DCHECK(out.IsX());
4732 DCHECK(base.IsX());
4733 SingleEmissionCheckScope guard(GetVIXLAssembler());
4734 __ Bind(fixup_label);
4735 __ add(out, base, Operand(/* offset placeholder */ 0));
4736}
4737
4738void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4739 vixl::aarch64::Register out,
4740 vixl::aarch64::Register base) {
4741 DCHECK(base.IsX());
4742 SingleEmissionCheckScope guard(GetVIXLAssembler());
4743 __ Bind(fixup_label);
4744 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4745}
4746
4747template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4748inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4749 const ArenaDeque<PcRelativePatchInfo>& infos,
4750 ArenaVector<LinkerPatch>* linker_patches) {
4751 for (const PcRelativePatchInfo& info : infos) {
4752 linker_patches->push_back(Factory(info.label.GetLocation(),
4753 &info.target_dex_file,
4754 info.pc_insn_label->GetLocation(),
4755 info.offset_or_index));
4756 }
4757}
4758
Vladimir Marko58155012015-08-19 12:49:41 +00004759void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4760 DCHECK(linker_patches->empty());
4761 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004762 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004763 method_bss_entry_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004764 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004765 type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004766 pc_relative_string_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004767 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004768 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01004769 if (GetCompilerOptions().IsBootImage()) {
4770 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004771 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004772 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4773 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004774 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4775 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004776 } else {
4777 DCHECK(pc_relative_method_patches_.empty());
4778 DCHECK(pc_relative_type_patches_.empty());
4779 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4780 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004781 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004782 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
4783 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004784 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4785 linker_patches);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004786 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
4787 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
4788 info.custom_data));
4789 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004790 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004791}
4792
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004793vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4794 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004795 value,
4796 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4797}
4798
Scott Wakeling97c72b72016-06-24 16:19:36 +01004799vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004800 return uint64_literals_.GetOrCreate(
4801 value,
4802 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004803}
4804
Andreas Gampe878d58c2015-01-15 23:24:00 -08004805void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004806 // Explicit clinit checks triggered by static invokes must have been pruned by
4807 // art::PrepareForRegisterAllocation.
4808 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004809
Andreas Gampe878d58c2015-01-15 23:24:00 -08004810 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004811 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004812 return;
4813 }
4814
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004815 {
4816 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4817 // are no pools emitted.
4818 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4819 LocationSummary* locations = invoke->GetLocations();
4820 codegen_->GenerateStaticOrDirectCall(
4821 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
4822 }
4823
4824 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004825}
4826
4827void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004828 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004829 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004830 return;
4831 }
4832
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004833 {
4834 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4835 // are no pools emitted.
4836 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4837 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
4838 DCHECK(!codegen_->IsLeafMethod());
4839 }
4840
4841 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004842}
4843
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004844HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4845 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004846 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004847 case HLoadClass::LoadKind::kInvalid:
4848 LOG(FATAL) << "UNREACHABLE";
4849 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004850 case HLoadClass::LoadKind::kReferrersClass:
4851 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004852 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004853 case HLoadClass::LoadKind::kBssEntry:
4854 DCHECK(!Runtime::Current()->UseJitCompilation());
4855 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004856 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004857 DCHECK(Runtime::Current()->UseJitCompilation());
4858 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004859 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004860 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004861 break;
4862 }
4863 return desired_class_load_kind;
4864}
4865
Alexandre Rames67555f72014-11-18 10:55:16 +00004866void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004867 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004868 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004869 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004870 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004871 cls,
4872 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004873 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004874 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004875 return;
4876 }
Vladimir Marko41559982017-01-06 14:04:23 +00004877 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004878
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004879 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4880 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004881 ? LocationSummary::kCallOnSlowPath
4882 : LocationSummary::kNoCall;
4883 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004884 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004885 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004886 }
4887
Vladimir Marko41559982017-01-06 14:04:23 +00004888 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004889 locations->SetInAt(0, Location::RequiresRegister());
4890 }
4891 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004892 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4893 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4894 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004895 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004896 RegisterSet caller_saves = RegisterSet::Empty();
4897 InvokeRuntimeCallingConvention calling_convention;
4898 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4899 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4900 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4901 Primitive::kPrimNot).GetCode());
4902 locations->SetCustomSlowPathCallerSaves(caller_saves);
4903 } else {
4904 // For non-Baker read barrier we have a temp-clobbering call.
4905 }
4906 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004907}
4908
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004909// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4910// move.
4911void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004912 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004913 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00004914 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004915 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01004916 return;
4917 }
Vladimir Marko41559982017-01-06 14:04:23 +00004918 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004919
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004920 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004921 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004922 Register bss_entry_temp;
4923 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004924
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004925 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4926 ? kWithoutReadBarrier
4927 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004928 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004929 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004930 case HLoadClass::LoadKind::kReferrersClass: {
4931 DCHECK(!cls->CanCallRuntime());
4932 DCHECK(!cls->MustGenerateClinitCheck());
4933 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4934 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004935 GenerateGcRootFieldLoad(cls,
4936 out_loc,
4937 current_method,
4938 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004939 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004940 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004941 break;
4942 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004943 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004944 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004945 // Add ADRP with its PC-relative type patch.
4946 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004947 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004948 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004949 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004950 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004951 vixl::aarch64::Label* add_label =
4952 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004953 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004954 break;
4955 }
4956 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004957 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004958 uint32_t address = dchecked_integral_cast<uint32_t>(
4959 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4960 DCHECK_NE(address, 0u);
4961 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004962 break;
4963 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004964 case HLoadClass::LoadKind::kBssEntry: {
4965 // Add ADRP with its PC-relative Class .bss entry patch.
4966 const DexFile& dex_file = cls->GetDexFile();
4967 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004968 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004969 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4970 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004971 // Add LDR with its PC-relative Class patch.
4972 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004973 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004974 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4975 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004976 out_loc,
4977 bss_entry_temp,
4978 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004979 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004980 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004981 generate_null_check = true;
4982 break;
4983 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004984 case HLoadClass::LoadKind::kJitTableAddress: {
4985 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4986 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004987 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004988 GenerateGcRootFieldLoad(cls,
4989 out_loc,
4990 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004991 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004992 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004993 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004994 break;
4995 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004996 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004997 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004998 LOG(FATAL) << "UNREACHABLE";
4999 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005000 }
5001
Vladimir Markoea4c1262017-02-06 19:59:33 +00005002 bool do_clinit = cls->MustGenerateClinitCheck();
5003 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005004 DCHECK(cls->CanCallRuntime());
5005 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00005006 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005007 codegen_->AddSlowPath(slow_path);
5008 if (generate_null_check) {
5009 __ Cbz(out, slow_path->GetEntryLabel());
5010 }
5011 if (cls->MustGenerateClinitCheck()) {
5012 GenerateClassInitializationCheck(slow_path, out);
5013 } else {
5014 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005015 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005016 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005017 }
5018}
5019
David Brazdilcb1c0552015-08-04 16:22:25 +01005020static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005021 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005022}
5023
Alexandre Rames67555f72014-11-18 10:55:16 +00005024void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5025 LocationSummary* locations =
5026 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5027 locations->SetOut(Location::RequiresRegister());
5028}
5029
5030void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005031 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5032}
5033
5034void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
5035 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5036}
5037
5038void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5039 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005040}
5041
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005042HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5043 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005044 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005045 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005046 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005047 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005048 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005049 case HLoadString::LoadKind::kJitTableAddress:
5050 DCHECK(Runtime::Current()->UseJitCompilation());
5051 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005052 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005053 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005054 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005055 }
5056 return desired_string_load_kind;
5057}
5058
Alexandre Rames67555f72014-11-18 10:55:16 +00005059void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005060 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005061 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005062 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005063 InvokeRuntimeCallingConvention calling_convention;
5064 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5065 } else {
5066 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005067 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5068 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005069 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005070 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005071 RegisterSet caller_saves = RegisterSet::Empty();
5072 InvokeRuntimeCallingConvention calling_convention;
5073 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5074 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
5075 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
5076 Primitive::kPrimNot).GetCode());
5077 locations->SetCustomSlowPathCallerSaves(caller_saves);
5078 } else {
5079 // For non-Baker read barrier we have a temp-clobbering call.
5080 }
5081 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005082 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005083}
5084
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005085// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5086// move.
5087void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005088 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005089 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005090
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005091 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005092 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005093 // Add ADRP with its PC-relative String patch.
5094 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005095 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005096 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01005097 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005098 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005099 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005100 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005101 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005102 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005103 return; // No dex cache slow path.
5104 }
5105 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005106 uint32_t address = dchecked_integral_cast<uint32_t>(
5107 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5108 DCHECK_NE(address, 0u);
5109 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005110 return; // No dex cache slow path.
5111 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005112 case HLoadString::LoadKind::kBssEntry: {
5113 // Add ADRP with its PC-relative String .bss entry patch.
5114 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005115 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005116 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005117 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005118 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005119 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005120 // Add LDR with its PC-relative String patch.
5121 vixl::aarch64::Label* ldr_label =
5122 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005123 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005124 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005125 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005126 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005127 /* offset placeholder */ 0u,
5128 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005129 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005130 SlowPathCodeARM64* slow_path =
5131 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005132 codegen_->AddSlowPath(slow_path);
5133 __ Cbz(out.X(), slow_path->GetEntryLabel());
5134 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005135 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005136 return;
5137 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005138 case HLoadString::LoadKind::kJitTableAddress: {
5139 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005140 load->GetStringIndex(),
5141 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005142 GenerateGcRootFieldLoad(load,
5143 out_loc,
5144 out.X(),
5145 /* offset */ 0,
5146 /* fixup_label */ nullptr,
5147 kCompilerReadBarrierOption);
5148 return;
5149 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005150 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005151 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005152 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005153
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005154 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005155 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005156 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005157 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005158 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5159 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005160 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005161}
5162
Alexandre Rames5319def2014-10-23 10:03:10 +01005163void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5164 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5165 locations->SetOut(Location::ConstantLocation(constant));
5166}
5167
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005168void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005169 // Will be generated at use site.
5170}
5171
Alexandre Rames67555f72014-11-18 10:55:16 +00005172void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5173 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005174 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005175 InvokeRuntimeCallingConvention calling_convention;
5176 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5177}
5178
5179void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005180 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005181 instruction,
5182 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005183 if (instruction->IsEnter()) {
5184 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5185 } else {
5186 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5187 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005188 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005189}
5190
Alexandre Rames42d641b2014-10-27 14:00:51 +00005191void LocationsBuilderARM64::VisitMul(HMul* mul) {
5192 LocationSummary* locations =
5193 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5194 switch (mul->GetResultType()) {
5195 case Primitive::kPrimInt:
5196 case Primitive::kPrimLong:
5197 locations->SetInAt(0, Location::RequiresRegister());
5198 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005199 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005200 break;
5201
5202 case Primitive::kPrimFloat:
5203 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005204 locations->SetInAt(0, Location::RequiresFpuRegister());
5205 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005206 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005207 break;
5208
5209 default:
5210 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5211 }
5212}
5213
5214void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5215 switch (mul->GetResultType()) {
5216 case Primitive::kPrimInt:
5217 case Primitive::kPrimLong:
5218 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5219 break;
5220
5221 case Primitive::kPrimFloat:
5222 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005223 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005224 break;
5225
5226 default:
5227 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5228 }
5229}
5230
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005231void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5232 LocationSummary* locations =
5233 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5234 switch (neg->GetResultType()) {
5235 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005236 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005237 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005238 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005239 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005240
5241 case Primitive::kPrimFloat:
5242 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005243 locations->SetInAt(0, Location::RequiresFpuRegister());
5244 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005245 break;
5246
5247 default:
5248 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5249 }
5250}
5251
5252void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5253 switch (neg->GetResultType()) {
5254 case Primitive::kPrimInt:
5255 case Primitive::kPrimLong:
5256 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5257 break;
5258
5259 case Primitive::kPrimFloat:
5260 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005261 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005262 break;
5263
5264 default:
5265 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5266 }
5267}
5268
5269void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5270 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005271 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005272 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005273 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005274 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5275 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005276}
5277
5278void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005279 // Note: if heap poisoning is enabled, the entry point takes cares
5280 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005281 QuickEntrypointEnum entrypoint =
5282 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5283 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005284 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005285 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005286}
5287
Alexandre Rames5319def2014-10-23 10:03:10 +01005288void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5289 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005290 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005291 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005292 if (instruction->IsStringAlloc()) {
5293 locations->AddTemp(LocationFrom(kArtMethodRegister));
5294 } else {
5295 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005296 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005297 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5298}
5299
5300void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005301 // Note: if heap poisoning is enabled, the entry point takes cares
5302 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005303 if (instruction->IsStringAlloc()) {
5304 // String is allocated through StringFactory. Call NewEmptyString entry point.
5305 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005306 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005307 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5308 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005309
5310 {
5311 // Ensure the pc position is recorded immediately after the `blr` instruction.
5312 ExactAssemblyScope eas(GetVIXLAssembler(),
5313 kInstructionSize,
5314 CodeBufferCheckScope::kExactSize);
5315 __ blr(lr);
5316 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5317 }
David Brazdil6de19382016-01-08 17:37:10 +00005318 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005319 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005320 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005321 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005322 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005323}
5324
5325void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5326 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005327 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005329}
5330
5331void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005332 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005333 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005334 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005335 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005336 break;
5337
5338 default:
5339 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5340 }
5341}
5342
David Brazdil66d126e2015-04-03 16:02:44 +01005343void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5344 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5345 locations->SetInAt(0, Location::RequiresRegister());
5346 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5347}
5348
5349void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005350 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005351}
5352
Alexandre Rames5319def2014-10-23 10:03:10 +01005353void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005354 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5355 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005356}
5357
Calin Juravle2ae48182016-03-16 14:05:09 +00005358void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5359 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005360 return;
5361 }
Artem Serov914d7a82017-02-07 14:33:49 +00005362 {
5363 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5364 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5365 Location obj = instruction->GetLocations()->InAt(0);
5366 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5367 RecordPcInfo(instruction, instruction->GetDexPc());
5368 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005369}
5370
Calin Juravle2ae48182016-03-16 14:05:09 +00005371void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005372 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005373 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005374
5375 LocationSummary* locations = instruction->GetLocations();
5376 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005377
5378 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005379}
5380
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005381void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005382 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005383}
5384
Alexandre Rames67555f72014-11-18 10:55:16 +00005385void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5386 HandleBinaryOp(instruction);
5387}
5388
5389void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5390 HandleBinaryOp(instruction);
5391}
5392
Alexandre Rames3e69f162014-12-10 10:36:50 +00005393void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5394 LOG(FATAL) << "Unreachable";
5395}
5396
5397void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5398 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5399}
5400
Alexandre Rames5319def2014-10-23 10:03:10 +01005401void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5402 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5403 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5404 if (location.IsStackSlot()) {
5405 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5406 } else if (location.IsDoubleStackSlot()) {
5407 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5408 }
5409 locations->SetOut(location);
5410}
5411
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005412void InstructionCodeGeneratorARM64::VisitParameterValue(
5413 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005414 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005415}
5416
5417void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5418 LocationSummary* locations =
5419 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005420 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005421}
5422
5423void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5424 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5425 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005426}
5427
5428void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5429 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005430 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005431 locations->SetInAt(i, Location::Any());
5432 }
5433 locations->SetOut(Location::Any());
5434}
5435
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005436void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005437 LOG(FATAL) << "Unreachable";
5438}
5439
Serban Constantinescu02164b32014-11-13 14:05:07 +00005440void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005441 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005442 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005443 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5444 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005445 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5446
5447 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005448 case Primitive::kPrimInt:
5449 case Primitive::kPrimLong:
5450 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005451 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005452 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5453 break;
5454
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005455 case Primitive::kPrimFloat:
5456 case Primitive::kPrimDouble: {
5457 InvokeRuntimeCallingConvention calling_convention;
5458 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5459 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5460 locations->SetOut(calling_convention.GetReturnLocation(type));
5461
5462 break;
5463 }
5464
Serban Constantinescu02164b32014-11-13 14:05:07 +00005465 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005466 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005467 }
5468}
5469
5470void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5471 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005472
Serban Constantinescu02164b32014-11-13 14:05:07 +00005473 switch (type) {
5474 case Primitive::kPrimInt:
5475 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005476 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005477 break;
5478 }
5479
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005480 case Primitive::kPrimFloat:
5481 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005482 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5483 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005484 if (type == Primitive::kPrimFloat) {
5485 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5486 } else {
5487 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5488 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005489 break;
5490 }
5491
Serban Constantinescu02164b32014-11-13 14:05:07 +00005492 default:
5493 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005494 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005495 }
5496}
5497
Igor Murashkind01745e2017-04-05 16:40:31 -07005498void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5499 constructor_fence->SetLocations(nullptr);
5500}
5501
5502void InstructionCodeGeneratorARM64::VisitConstructorFence(
5503 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5504 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5505}
5506
Calin Juravle27df7582015-04-17 19:12:31 +01005507void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5508 memory_barrier->SetLocations(nullptr);
5509}
5510
5511void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005512 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005513}
5514
Alexandre Rames5319def2014-10-23 10:03:10 +01005515void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5516 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5517 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005518 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005519}
5520
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005521void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005522 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005523}
5524
5525void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5526 instruction->SetLocations(nullptr);
5527}
5528
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005529void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005530 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005531}
5532
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005533void LocationsBuilderARM64::VisitRor(HRor* ror) {
5534 HandleBinaryOp(ror);
5535}
5536
5537void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5538 HandleBinaryOp(ror);
5539}
5540
Serban Constantinescu02164b32014-11-13 14:05:07 +00005541void LocationsBuilderARM64::VisitShl(HShl* shl) {
5542 HandleShift(shl);
5543}
5544
5545void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5546 HandleShift(shl);
5547}
5548
5549void LocationsBuilderARM64::VisitShr(HShr* shr) {
5550 HandleShift(shr);
5551}
5552
5553void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5554 HandleShift(shr);
5555}
5556
Alexandre Rames5319def2014-10-23 10:03:10 +01005557void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005558 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005559}
5560
5561void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005562 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005563}
5564
Alexandre Rames67555f72014-11-18 10:55:16 +00005565void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005566 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005567}
5568
5569void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005570 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005571}
5572
5573void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005574 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005575}
5576
Alexandre Rames67555f72014-11-18 10:55:16 +00005577void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005578 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005579}
5580
Calin Juravlee460d1d2015-09-29 04:52:17 +01005581void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5582 HUnresolvedInstanceFieldGet* instruction) {
5583 FieldAccessCallingConventionARM64 calling_convention;
5584 codegen_->CreateUnresolvedFieldLocationSummary(
5585 instruction, instruction->GetFieldType(), calling_convention);
5586}
5587
5588void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5589 HUnresolvedInstanceFieldGet* instruction) {
5590 FieldAccessCallingConventionARM64 calling_convention;
5591 codegen_->GenerateUnresolvedFieldAccess(instruction,
5592 instruction->GetFieldType(),
5593 instruction->GetFieldIndex(),
5594 instruction->GetDexPc(),
5595 calling_convention);
5596}
5597
5598void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5599 HUnresolvedInstanceFieldSet* instruction) {
5600 FieldAccessCallingConventionARM64 calling_convention;
5601 codegen_->CreateUnresolvedFieldLocationSummary(
5602 instruction, instruction->GetFieldType(), calling_convention);
5603}
5604
5605void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5606 HUnresolvedInstanceFieldSet* instruction) {
5607 FieldAccessCallingConventionARM64 calling_convention;
5608 codegen_->GenerateUnresolvedFieldAccess(instruction,
5609 instruction->GetFieldType(),
5610 instruction->GetFieldIndex(),
5611 instruction->GetDexPc(),
5612 calling_convention);
5613}
5614
5615void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5616 HUnresolvedStaticFieldGet* instruction) {
5617 FieldAccessCallingConventionARM64 calling_convention;
5618 codegen_->CreateUnresolvedFieldLocationSummary(
5619 instruction, instruction->GetFieldType(), calling_convention);
5620}
5621
5622void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5623 HUnresolvedStaticFieldGet* instruction) {
5624 FieldAccessCallingConventionARM64 calling_convention;
5625 codegen_->GenerateUnresolvedFieldAccess(instruction,
5626 instruction->GetFieldType(),
5627 instruction->GetFieldIndex(),
5628 instruction->GetDexPc(),
5629 calling_convention);
5630}
5631
5632void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5633 HUnresolvedStaticFieldSet* instruction) {
5634 FieldAccessCallingConventionARM64 calling_convention;
5635 codegen_->CreateUnresolvedFieldLocationSummary(
5636 instruction, instruction->GetFieldType(), calling_convention);
5637}
5638
5639void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5640 HUnresolvedStaticFieldSet* instruction) {
5641 FieldAccessCallingConventionARM64 calling_convention;
5642 codegen_->GenerateUnresolvedFieldAccess(instruction,
5643 instruction->GetFieldType(),
5644 instruction->GetFieldIndex(),
5645 instruction->GetDexPc(),
5646 calling_convention);
5647}
5648
Alexandre Rames5319def2014-10-23 10:03:10 +01005649void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005650 LocationSummary* locations =
5651 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005652 // In suspend check slow path, usually there are no caller-save registers at all.
5653 // If SIMD instructions are present, however, we force spilling all live SIMD
5654 // registers in full width (since the runtime only saves/restores lower part).
5655 locations->SetCustomSlowPathCallerSaves(
5656 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005657}
5658
5659void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005660 HBasicBlock* block = instruction->GetBlock();
5661 if (block->GetLoopInformation() != nullptr) {
5662 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5663 // The back edge will generate the suspend check.
5664 return;
5665 }
5666 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5667 // The goto will generate the suspend check.
5668 return;
5669 }
5670 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005671 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005672}
5673
Alexandre Rames67555f72014-11-18 10:55:16 +00005674void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5675 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005676 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005677 InvokeRuntimeCallingConvention calling_convention;
5678 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5679}
5680
5681void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005682 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005683 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005684}
5685
5686void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5687 LocationSummary* locations =
5688 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5689 Primitive::Type input_type = conversion->GetInputType();
5690 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005691 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005692 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5693 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5694 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5695 }
5696
Alexandre Rames542361f2015-01-29 16:57:31 +00005697 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005698 locations->SetInAt(0, Location::RequiresFpuRegister());
5699 } else {
5700 locations->SetInAt(0, Location::RequiresRegister());
5701 }
5702
Alexandre Rames542361f2015-01-29 16:57:31 +00005703 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005704 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5705 } else {
5706 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5707 }
5708}
5709
5710void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5711 Primitive::Type result_type = conversion->GetResultType();
5712 Primitive::Type input_type = conversion->GetInputType();
5713
5714 DCHECK_NE(input_type, result_type);
5715
Alexandre Rames542361f2015-01-29 16:57:31 +00005716 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005717 int result_size = Primitive::ComponentSize(result_type);
5718 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005719 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005720 Register output = OutputRegister(conversion);
5721 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005722 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005723 // 'int' values are used directly as W registers, discarding the top
5724 // bits, so we don't need to sign-extend and can just perform a move.
5725 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5726 // top 32 bits of the target register. We theoretically could leave those
5727 // bits unchanged, but we would have to make sure that no code uses a
5728 // 32bit input value as a 64bit value assuming that the top 32 bits are
5729 // zero.
5730 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005731 } else if (result_type == Primitive::kPrimChar ||
5732 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5733 __ Ubfx(output,
5734 output.IsX() ? source.X() : source.W(),
5735 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005736 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005737 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005738 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005739 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005740 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005741 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005742 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5743 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005744 } else if (Primitive::IsFloatingPointType(result_type) &&
5745 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005746 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5747 } else {
5748 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5749 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005750 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005751}
Alexandre Rames67555f72014-11-18 10:55:16 +00005752
Serban Constantinescu02164b32014-11-13 14:05:07 +00005753void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5754 HandleShift(ushr);
5755}
5756
5757void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5758 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005759}
5760
5761void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5762 HandleBinaryOp(instruction);
5763}
5764
5765void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5766 HandleBinaryOp(instruction);
5767}
5768
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005769void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005770 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005771 LOG(FATAL) << "Unreachable";
5772}
5773
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005774void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005775 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005776 LOG(FATAL) << "Unreachable";
5777}
5778
Mark Mendellfe57faa2015-09-18 09:26:15 -04005779// Simple implementation of packed switch - generate cascaded compare/jumps.
5780void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5781 LocationSummary* locations =
5782 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5783 locations->SetInAt(0, Location::RequiresRegister());
5784}
5785
5786void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5787 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005788 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005789 Register value_reg = InputRegisterAt(switch_instr, 0);
5790 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5791
Zheng Xu3927c8b2015-11-18 17:46:25 +08005792 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005793 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005794 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5795 // make sure we don't emit it if the target may run out of range.
5796 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5797 // ranges and emit the tables only as required.
5798 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005799
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005800 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005801 // Current instruction id is an upper bound of the number of HIRs in the graph.
5802 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5803 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005804 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5805 Register temp = temps.AcquireW();
5806 __ Subs(temp, value_reg, Operand(lower_bound));
5807
Zheng Xu3927c8b2015-11-18 17:46:25 +08005808 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005809 // Jump to successors[0] if value == lower_bound.
5810 __ B(eq, codegen_->GetLabelOf(successors[0]));
5811 int32_t last_index = 0;
5812 for (; num_entries - last_index > 2; last_index += 2) {
5813 __ Subs(temp, temp, Operand(2));
5814 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5815 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5816 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5817 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5818 }
5819 if (num_entries - last_index == 2) {
5820 // The last missing case_value.
5821 __ Cmp(temp, Operand(1));
5822 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005823 }
5824
5825 // And the default for any other value.
5826 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5827 __ B(codegen_->GetLabelOf(default_block));
5828 }
5829 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005830 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005831
5832 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5833
5834 // Below instructions should use at most one blocked register. Since there are two blocked
5835 // registers, we are free to block one.
5836 Register temp_w = temps.AcquireW();
5837 Register index;
5838 // Remove the bias.
5839 if (lower_bound != 0) {
5840 index = temp_w;
5841 __ Sub(index, value_reg, Operand(lower_bound));
5842 } else {
5843 index = value_reg;
5844 }
5845
5846 // Jump to default block if index is out of the range.
5847 __ Cmp(index, Operand(num_entries));
5848 __ B(hs, codegen_->GetLabelOf(default_block));
5849
5850 // In current VIXL implementation, it won't require any blocked registers to encode the
5851 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5852 // register pressure.
5853 Register table_base = temps.AcquireX();
5854 // Load jump offset from the table.
5855 __ Adr(table_base, jump_table->GetTableStartLabel());
5856 Register jump_offset = temp_w;
5857 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5858
5859 // Jump to target block by branching to table_base(pc related) + offset.
5860 Register target_address = table_base;
5861 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5862 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005863 }
5864}
5865
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005866void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5867 HInstruction* instruction,
5868 Location out,
5869 uint32_t offset,
5870 Location maybe_temp,
5871 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005872 Primitive::Type type = Primitive::kPrimNot;
5873 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005874 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005875 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005876 if (kUseBakerReadBarrier) {
5877 // Load with fast path based Baker's read barrier.
5878 // /* HeapReference<Object> */ out = *(out + offset)
5879 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5880 out,
5881 out_reg,
5882 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005883 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005884 /* needs_null_check */ false,
5885 /* use_load_acquire */ false);
5886 } else {
5887 // Load with slow path based read barrier.
5888 // Save the value of `out` into `maybe_temp` before overwriting it
5889 // in the following move operation, as we will need it for the
5890 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005891 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005892 __ Mov(temp_reg, out_reg);
5893 // /* HeapReference<Object> */ out = *(out + offset)
5894 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5895 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5896 }
5897 } else {
5898 // Plain load with no read barrier.
5899 // /* HeapReference<Object> */ out = *(out + offset)
5900 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5901 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5902 }
5903}
5904
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005905void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5906 HInstruction* instruction,
5907 Location out,
5908 Location obj,
5909 uint32_t offset,
5910 Location maybe_temp,
5911 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005912 Primitive::Type type = Primitive::kPrimNot;
5913 Register out_reg = RegisterFrom(out, type);
5914 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005915 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005916 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005917 if (kUseBakerReadBarrier) {
5918 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005919 // /* HeapReference<Object> */ out = *(obj + offset)
5920 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5921 out,
5922 obj_reg,
5923 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005924 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005925 /* needs_null_check */ false,
5926 /* use_load_acquire */ false);
5927 } else {
5928 // Load with slow path based read barrier.
5929 // /* HeapReference<Object> */ out = *(obj + offset)
5930 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5931 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5932 }
5933 } else {
5934 // Plain load with no read barrier.
5935 // /* HeapReference<Object> */ out = *(obj + offset)
5936 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5937 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5938 }
5939}
5940
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005941void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5942 HInstruction* instruction,
5943 Location root,
5944 Register obj,
5945 uint32_t offset,
5946 vixl::aarch64::Label* fixup_label,
5947 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005948 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005949 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005950 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005951 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005952 if (kUseBakerReadBarrier) {
5953 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005954 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005955 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5956 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01005957 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
5958 // the Marking Register) to decide whether we need to enter
5959 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005960 //
5961 // We use link-time generated thunks for the slow path. That thunk
5962 // checks the reference and jumps to the entrypoint if needed.
5963 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005964 // lr = &return_address;
5965 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01005966 // if (mr) { // Thread::Current()->GetIsGcMarking()
5967 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005968 // }
5969 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00005970
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005971 UseScratchRegisterScope temps(GetVIXLAssembler());
5972 DCHECK(temps.IsAvailable(ip0));
5973 DCHECK(temps.IsAvailable(ip1));
5974 temps.Exclude(ip0, ip1);
5975 uint32_t custom_data =
5976 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
5977 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00005978
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005979 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5980 vixl::aarch64::Label return_address;
5981 __ adr(lr, &return_address);
5982 if (fixup_label != nullptr) {
5983 __ Bind(fixup_label);
5984 }
5985 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
5986 "GC root LDR must be 2 instruction (8B) before the return address label.");
5987 __ ldr(root_reg, MemOperand(obj.X(), offset));
5988 __ Bind(cbnz_label);
Roland Levillain97c46462017-05-11 14:04:03 +01005989 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005990 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005991 } else {
Roland Levillain97c46462017-05-11 14:04:03 +01005992 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
5993 // the Marking Register) to decide whether we need to enter
5994 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005995 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005996 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01005997 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005998 // // Slow path.
Roland Levillain97c46462017-05-11 14:04:03 +01005999 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6000 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006001 // }
Roland Levillain44015862016-01-22 11:47:17 +00006002
Roland Levillain97c46462017-05-11 14:04:03 +01006003 // Slow path marking the GC root `root`. The entrypoint will
6004 // be loaded by the slow path code.
6005 SlowPathCodeARM64* slow_path =
6006 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006007 codegen_->AddSlowPath(slow_path);
6008
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006009 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6010 if (fixup_label == nullptr) {
6011 __ Ldr(root_reg, MemOperand(obj, offset));
6012 } else {
6013 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
6014 }
6015 static_assert(
6016 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6017 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6018 "have different sizes.");
6019 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6020 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6021 "have different sizes.");
6022
Roland Levillain97c46462017-05-11 14:04:03 +01006023 __ Cbnz(mr, slow_path->GetEntryLabel());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006024 __ Bind(slow_path->GetExitLabel());
6025 }
Roland Levillain44015862016-01-22 11:47:17 +00006026 } else {
6027 // GC root loaded through a slow path for read barriers other
6028 // than Baker's.
6029 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006030 if (fixup_label == nullptr) {
6031 __ Add(root_reg.X(), obj.X(), offset);
6032 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006033 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006034 }
Roland Levillain44015862016-01-22 11:47:17 +00006035 // /* mirror::Object* */ root = root->Read()
6036 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6037 }
6038 } else {
6039 // Plain GC root load with no read barrier.
6040 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006041 if (fixup_label == nullptr) {
6042 __ Ldr(root_reg, MemOperand(obj, offset));
6043 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006044 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006045 }
Roland Levillain44015862016-01-22 11:47:17 +00006046 // Note that GC roots are not affected by heap poisoning, thus we
6047 // do not have to unpoison `root_reg` here.
6048 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006049 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006050}
6051
6052void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6053 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006054 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006055 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006056 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006057 bool needs_null_check,
6058 bool use_load_acquire) {
6059 DCHECK(kEmitCompilerReadBarrier);
6060 DCHECK(kUseBakerReadBarrier);
6061
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006062 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6063 !use_load_acquire &&
6064 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006065 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6066 // Marking Register) to decide whether we need to enter the slow
6067 // path to mark the reference. Then, in the slow path, check the
6068 // gray bit in the lock word of the reference's holder (`obj`) to
6069 // decide whether to mark `ref` or not.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006070 //
6071 // We use link-time generated thunks for the slow path. That thunk checks
6072 // the holder and jumps to the entrypoint if needed. If the holder is not
6073 // gray, it creates a fake dependency and returns to the LDR instruction.
6074 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006075 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006076 // if (mr) { // Thread::Current()->GetIsGcMarking()
6077 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006078 // }
6079 // not_gray_return_address:
6080 // // Original reference load. If the offset is too large to fit
6081 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006082 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006083 // gray_return_address:
6084
6085 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6086 Register base = obj;
6087 if (offset >= kReferenceLoadMinFarOffset) {
6088 DCHECK(maybe_temp.IsRegister());
6089 base = WRegisterFrom(maybe_temp);
6090 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6091 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6092 offset &= (kReferenceLoadMinFarOffset - 1u);
6093 }
6094 UseScratchRegisterScope temps(GetVIXLAssembler());
6095 DCHECK(temps.IsAvailable(ip0));
6096 DCHECK(temps.IsAvailable(ip1));
6097 temps.Exclude(ip0, ip1);
6098 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6099 base.GetCode(),
6100 obj.GetCode());
6101 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6102
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006103 {
6104 EmissionCheckScope guard(GetVIXLAssembler(),
6105 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6106 vixl::aarch64::Label return_address;
6107 __ adr(lr, &return_address);
6108 __ Bind(cbnz_label);
6109 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6110 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6111 "Field LDR must be 1 instruction (4B) before the return address label; "
6112 " 2 instructions (8B) for heap poisoning.");
6113 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6114 __ ldr(ref_reg, MemOperand(base.X(), offset));
6115 if (needs_null_check) {
6116 MaybeRecordImplicitNullCheck(instruction);
6117 }
6118 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6119 __ Bind(&return_address);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006120 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006121 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006122 return;
6123 }
6124
Roland Levillain44015862016-01-22 11:47:17 +00006125 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006126 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006127 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006128 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006129 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6130 ref,
6131 obj,
6132 offset,
6133 no_index,
6134 no_scale_factor,
6135 temp,
6136 needs_null_check,
6137 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006138}
6139
6140void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6141 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006142 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006143 uint32_t data_offset,
6144 Location index,
6145 Register temp,
6146 bool needs_null_check) {
6147 DCHECK(kEmitCompilerReadBarrier);
6148 DCHECK(kUseBakerReadBarrier);
6149
Vladimir Marko66d691d2017-04-07 17:53:39 +01006150 static_assert(
6151 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6152 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6153 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
6154
6155 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6156 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006157 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6158 // Marking Register) to decide whether we need to enter the slow
6159 // path to mark the reference. Then, in the slow path, check the
6160 // gray bit in the lock word of the reference's holder (`obj`) to
6161 // decide whether to mark `ref` or not.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006162 //
6163 // We use link-time generated thunks for the slow path. That thunk checks
6164 // the holder and jumps to the entrypoint if needed. If the holder is not
6165 // gray, it creates a fake dependency and returns to the LDR instruction.
6166 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006167 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006168 // if (mr) { // Thread::Current()->GetIsGcMarking()
6169 // goto array_thunk<base_reg>(lr)
Vladimir Marko66d691d2017-04-07 17:53:39 +01006170 // }
6171 // not_gray_return_address:
6172 // // Original reference load. If the offset is too large to fit
6173 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006174 // HeapReference<mirror::Object> reference = data[index];
Vladimir Marko66d691d2017-04-07 17:53:39 +01006175 // gray_return_address:
6176
6177 DCHECK(index.IsValid());
6178 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
6179 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6180
6181 UseScratchRegisterScope temps(GetVIXLAssembler());
6182 DCHECK(temps.IsAvailable(ip0));
6183 DCHECK(temps.IsAvailable(ip1));
6184 temps.Exclude(ip0, ip1);
6185 uint32_t custom_data =
6186 linker::Arm64RelativePatcher::EncodeBakerReadBarrierArrayData(temp.GetCode());
6187 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6188
Vladimir Marko66d691d2017-04-07 17:53:39 +01006189 __ Add(temp.X(), obj.X(), Operand(data_offset));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006190 {
6191 EmissionCheckScope guard(GetVIXLAssembler(),
6192 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6193 vixl::aarch64::Label return_address;
6194 __ adr(lr, &return_address);
6195 __ Bind(cbnz_label);
6196 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6197 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6198 "Array LDR must be 1 instruction (4B) before the return address label; "
6199 " 2 instructions (8B) for heap poisoning.");
6200 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6201 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6202 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6203 __ Bind(&return_address);
6204 }
6205 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Marko66d691d2017-04-07 17:53:39 +01006206 return;
6207 }
6208
Roland Levillain44015862016-01-22 11:47:17 +00006209 // Array cells are never volatile variables, therefore array loads
6210 // never use Load-Acquire instructions on ARM64.
6211 const bool use_load_acquire = false;
6212
6213 // /* HeapReference<Object> */ ref =
6214 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006215 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6216 ref,
6217 obj,
6218 data_offset,
6219 index,
6220 scale_factor,
6221 temp,
6222 needs_null_check,
6223 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006224}
6225
6226void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6227 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006228 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006229 uint32_t offset,
6230 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006231 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006232 Register temp,
6233 bool needs_null_check,
Roland Levillainff487002017-03-07 16:50:01 +00006234 bool use_load_acquire) {
Roland Levillain44015862016-01-22 11:47:17 +00006235 DCHECK(kEmitCompilerReadBarrier);
6236 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006237 // If we are emitting an array load, we should not be using a
6238 // Load Acquire instruction. In other words:
6239 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6240 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006241
Roland Levillain97c46462017-05-11 14:04:03 +01006242 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6243 // Marking Register) to decide whether we need to enter the slow
6244 // path to mark the reference. Then, in the slow path, check the
6245 // gray bit in the lock word of the reference's holder (`obj`) to
6246 // decide whether to mark `ref` or not.
Roland Levillain44015862016-01-22 11:47:17 +00006247 //
Roland Levillain97c46462017-05-11 14:04:03 +01006248 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00006249 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006250 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6251 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6252 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6253 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6254 // if (is_gray) {
Roland Levillain97c46462017-05-11 14:04:03 +01006255 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6256 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillain54f869e2017-03-06 13:54:11 +00006257 // }
6258 // } else {
6259 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006260 // }
Roland Levillain44015862016-01-22 11:47:17 +00006261
Roland Levillainba650a42017-03-06 13:52:32 +00006262 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain97c46462017-05-11 14:04:03 +01006263 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006264 SlowPathCodeARM64* slow_path =
6265 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6266 instruction,
6267 ref,
6268 obj,
6269 offset,
6270 index,
6271 scale_factor,
6272 needs_null_check,
6273 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006274 temp);
Roland Levillainba650a42017-03-06 13:52:32 +00006275 AddSlowPath(slow_path);
6276
Roland Levillain97c46462017-05-11 14:04:03 +01006277 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006278 // Fast path: the GC is not marking: just load the reference.
Roland Levillain54f869e2017-03-06 13:54:11 +00006279 GenerateRawReferenceLoad(
6280 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006281 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006282 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainba650a42017-03-06 13:52:32 +00006283}
6284
Roland Levillainff487002017-03-07 16:50:01 +00006285void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6286 Location ref,
6287 Register obj,
6288 Location field_offset,
6289 Register temp,
6290 bool needs_null_check,
6291 bool use_load_acquire) {
6292 DCHECK(kEmitCompilerReadBarrier);
6293 DCHECK(kUseBakerReadBarrier);
6294 // If we are emitting an array load, we should not be using a
6295 // Load Acquire instruction. In other words:
6296 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6297 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6298
Roland Levillain97c46462017-05-11 14:04:03 +01006299 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6300 // Marking Register) to decide whether we need to enter the slow
6301 // path to update the reference field within `obj`. Then, in the
6302 // slow path, check the gray bit in the lock word of the reference's
6303 // holder (`obj`) to decide whether to mark `ref` and update the
6304 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00006305 //
Roland Levillain97c46462017-05-11 14:04:03 +01006306 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00006307 // // Slow path.
6308 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6309 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6310 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6311 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6312 // if (is_gray) {
6313 // old_ref = ref;
Roland Levillain97c46462017-05-11 14:04:03 +01006314 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6315 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00006316 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6317 // }
6318 // }
6319
6320 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain97c46462017-05-11 14:04:03 +01006321 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006322 SlowPathCodeARM64* slow_path =
6323 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6324 instruction,
6325 ref,
6326 obj,
6327 /* offset */ 0u,
6328 /* index */ field_offset,
6329 /* scale_factor */ 0u /* "times 1" */,
6330 needs_null_check,
6331 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006332 temp);
Roland Levillainff487002017-03-07 16:50:01 +00006333 AddSlowPath(slow_path);
6334
Roland Levillain97c46462017-05-11 14:04:03 +01006335 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006336 // Fast path: the GC is not marking: nothing to do (the field is
6337 // up-to-date, and we don't need to load the reference).
6338 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006339 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainff487002017-03-07 16:50:01 +00006340}
6341
Roland Levillainba650a42017-03-06 13:52:32 +00006342void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6343 Location ref,
6344 Register obj,
6345 uint32_t offset,
6346 Location index,
6347 size_t scale_factor,
6348 bool needs_null_check,
6349 bool use_load_acquire) {
6350 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006351 Primitive::Type type = Primitive::kPrimNot;
6352 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006353
Roland Levillainba650a42017-03-06 13:52:32 +00006354 // If needed, vixl::EmissionCheckScope guards are used to ensure
6355 // that no pools are emitted between the load (macro) instruction
6356 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006357
Roland Levillain44015862016-01-22 11:47:17 +00006358 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006359 // Load types involving an "index": ArrayGet,
6360 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6361 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006362 if (use_load_acquire) {
6363 // UnsafeGetObjectVolatile intrinsic case.
6364 // Register `index` is not an index in an object array, but an
6365 // offset to an object reference field within object `obj`.
6366 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6367 DCHECK(instruction->GetLocations()->Intrinsified());
6368 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6369 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006370 DCHECK_EQ(offset, 0u);
6371 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006372 DCHECK_EQ(needs_null_check, false);
6373 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006374 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6375 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006376 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006377 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6378 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006379 if (index.IsConstant()) {
6380 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006381 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006382 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006383 if (needs_null_check) {
6384 MaybeRecordImplicitNullCheck(instruction);
6385 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006386 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006387 UseScratchRegisterScope temps(GetVIXLAssembler());
6388 Register temp = temps.AcquireW();
6389 __ Add(temp, obj, offset);
6390 {
6391 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6392 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6393 if (needs_null_check) {
6394 MaybeRecordImplicitNullCheck(instruction);
6395 }
6396 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006397 }
Roland Levillain44015862016-01-22 11:47:17 +00006398 }
Roland Levillain44015862016-01-22 11:47:17 +00006399 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006400 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006401 MemOperand field = HeapOperand(obj, offset);
6402 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006403 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6404 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006405 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006406 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006407 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006408 if (needs_null_check) {
6409 MaybeRecordImplicitNullCheck(instruction);
6410 }
Roland Levillain44015862016-01-22 11:47:17 +00006411 }
6412 }
6413
6414 // Object* ref = ref_addr->AsMirrorPtr()
6415 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006416}
6417
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006418void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6419 // The following condition is a compile-time one, so it does not have a run-time cost.
6420 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6421 // The following condition is a run-time one; it is executed after the
6422 // previous compile-time test, to avoid penalizing non-debug builds.
6423 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6424 UseScratchRegisterScope temps(GetVIXLAssembler());
6425 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6426 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6427 }
6428 }
6429}
6430
Roland Levillain44015862016-01-22 11:47:17 +00006431void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6432 Location out,
6433 Location ref,
6434 Location obj,
6435 uint32_t offset,
6436 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006437 DCHECK(kEmitCompilerReadBarrier);
6438
Roland Levillain44015862016-01-22 11:47:17 +00006439 // Insert a slow path based read barrier *after* the reference load.
6440 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006441 // If heap poisoning is enabled, the unpoisoning of the loaded
6442 // reference will be carried out by the runtime within the slow
6443 // path.
6444 //
6445 // Note that `ref` currently does not get unpoisoned (when heap
6446 // poisoning is enabled), which is alright as the `ref` argument is
6447 // not used by the artReadBarrierSlow entry point.
6448 //
6449 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6450 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6451 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6452 AddSlowPath(slow_path);
6453
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006454 __ B(slow_path->GetEntryLabel());
6455 __ Bind(slow_path->GetExitLabel());
6456}
6457
Roland Levillain44015862016-01-22 11:47:17 +00006458void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6459 Location out,
6460 Location ref,
6461 Location obj,
6462 uint32_t offset,
6463 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006464 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006465 // Baker's read barriers shall be handled by the fast path
6466 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6467 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006468 // If heap poisoning is enabled, unpoisoning will be taken care of
6469 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006470 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006471 } else if (kPoisonHeapReferences) {
6472 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6473 }
6474}
6475
Roland Levillain44015862016-01-22 11:47:17 +00006476void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6477 Location out,
6478 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006479 DCHECK(kEmitCompilerReadBarrier);
6480
Roland Levillain44015862016-01-22 11:47:17 +00006481 // Insert a slow path based read barrier *after* the GC root load.
6482 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006483 // Note that GC roots are not affected by heap poisoning, so we do
6484 // not need to do anything special for this here.
6485 SlowPathCodeARM64* slow_path =
6486 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6487 AddSlowPath(slow_path);
6488
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006489 __ B(slow_path->GetEntryLabel());
6490 __ Bind(slow_path->GetExitLabel());
6491}
6492
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006493void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6494 LocationSummary* locations =
6495 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6496 locations->SetInAt(0, Location::RequiresRegister());
6497 locations->SetOut(Location::RequiresRegister());
6498}
6499
6500void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6501 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006502 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006503 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006504 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006505 __ Ldr(XRegisterFrom(locations->Out()),
6506 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006507 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006508 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006509 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006510 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6511 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006512 __ Ldr(XRegisterFrom(locations->Out()),
6513 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006514 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006515}
6516
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006517static void PatchJitRootUse(uint8_t* code,
6518 const uint8_t* roots_data,
6519 vixl::aarch64::Literal<uint32_t>* literal,
6520 uint64_t index_in_table) {
6521 uint32_t literal_offset = literal->GetOffset();
6522 uintptr_t address =
6523 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6524 uint8_t* data = code + literal_offset;
6525 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6526}
6527
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006528void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6529 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006530 const StringReference& string_reference = entry.first;
6531 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
6532 const auto it = jit_string_roots_.find(string_reference);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006533 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006534 uint64_t index_in_table = it->second;
6535 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006536 }
6537 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006538 const TypeReference& type_reference = entry.first;
6539 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
6540 const auto it = jit_class_roots_.find(type_reference);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006541 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006542 uint64_t index_in_table = it->second;
6543 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006544 }
6545}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006546
Alexandre Rames67555f72014-11-18 10:55:16 +00006547#undef __
6548#undef QUICK_ENTRY_POINT
6549
Alexandre Rames5319def2014-10-23 10:03:10 +01006550} // namespace arm64
6551} // namespace art