blob: 0b65b4101538c63fb345cc56ec86f1aa6c706e23 [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"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010024#include "class_table.h"
Zheng Xuc6667102015-05-15 16:08:45 +080025#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000026#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010027#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080028#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010029#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070030#include "heap_poisoning.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080031#include "intrinsics.h"
32#include "intrinsics_arm64.h"
Vladimir Markof4f2daa2017-03-20 18:26:59 +000033#include "linker/arm64/relative_patcher_arm64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010034#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010036#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070037#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000038#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010039#include "thread.h"
40#include "utils/arm64/assembler_arm64.h"
41#include "utils/assembler.h"
42#include "utils/stack_checks.h"
43
Scott Wakeling97c72b72016-06-24 16:19:36 +010044using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000045using vixl::ExactAssemblyScope;
46using vixl::CodeBufferCheckScope;
47using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010048
49#ifdef __
50#error "ARM64 Codegen VIXL macro-assembler macro already defined."
51#endif
52
Alexandre Rames5319def2014-10-23 10:03:10 +010053namespace art {
54
Roland Levillain22ccc3a2015-11-24 13:10:05 +000055template<class MirrorType>
56class GcRoot;
57
Alexandre Rames5319def2014-10-23 10:03:10 +010058namespace arm64 {
59
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::ARM64EncodableConstantOrRegister;
61using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080062using helpers::CPURegisterFrom;
63using helpers::DRegisterFrom;
64using helpers::FPRegisterFrom;
65using helpers::HeapOperand;
66using helpers::HeapOperandFrom;
67using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010068using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080070using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010071using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080072using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010073using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080074using helpers::LocationFrom;
75using helpers::OperandFromMemOperand;
76using helpers::OutputCPURegister;
77using helpers::OutputFPRegister;
78using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010079using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080080using helpers::RegisterFrom;
81using helpers::StackOperandFrom;
82using helpers::VIXLRegCodeFromART;
83using helpers::WRegisterFrom;
84using helpers::XRegisterFrom;
85
Vladimir Markof3e0ee22015-12-17 15:23:13 +000086// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080087// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
88// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000089static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010090
Vladimir Markof4f2daa2017-03-20 18:26:59 +000091// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
92// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
93// For the Baker read barrier implementation using link-generated thunks we need to split
94// the offset explicitly.
95constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
96
97// Flags controlling the use of link-time generated thunks for Baker read barriers.
Vladimir Markod1ef8732017-04-18 13:55:13 +010098constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
Vladimir Marko66d691d2017-04-07 17:53:39 +010099constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
Vladimir Markod1ef8732017-04-18 13:55:13 +0100100constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000101
102// Some instructions have special requirements for a temporary, for example
103// LoadClass/kBssEntry and LoadString/kBssEntry for Baker read barrier require
104// temp that's not an R0 (to avoid an extra move) and Baker read barrier field
105// loads with large offsets need a fixed register to limit the number of link-time
106// thunks we generate. For these and similar cases, we want to reserve a specific
107// register that's neither callee-save nor an argument register. We choose x15.
108inline Location FixedTempLocation() {
109 return Location::RegisterLocation(x15.GetCode());
110}
111
Alexandre Rames5319def2014-10-23 10:03:10 +0100112inline Condition ARM64Condition(IfCondition cond) {
113 switch (cond) {
114 case kCondEQ: return eq;
115 case kCondNE: return ne;
116 case kCondLT: return lt;
117 case kCondLE: return le;
118 case kCondGT: return gt;
119 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700120 case kCondB: return lo;
121 case kCondBE: return ls;
122 case kCondA: return hi;
123 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100124 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000125 LOG(FATAL) << "Unreachable";
126 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100127}
128
Vladimir Markod6e069b2016-01-18 11:11:01 +0000129inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
130 // The ARM64 condition codes can express all the necessary branches, see the
131 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
132 // There is no dex instruction or HIR that would need the missing conditions
133 // "equal or unordered" or "not equal".
134 switch (cond) {
135 case kCondEQ: return eq;
136 case kCondNE: return ne /* unordered */;
137 case kCondLT: return gt_bias ? cc : lt /* unordered */;
138 case kCondLE: return gt_bias ? ls : le /* unordered */;
139 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
140 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
141 default:
142 LOG(FATAL) << "UNREACHABLE";
143 UNREACHABLE();
144 }
145}
146
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100147Location ARM64ReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000148 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
149 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
150 // but we use the exact registers for clarity.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100151 if (return_type == DataType::Type::kFloat32) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000152 return LocationFrom(s0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100153 } else if (return_type == DataType::Type::kFloat64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000154 return LocationFrom(d0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100155 } else if (return_type == DataType::Type::kInt64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000156 return LocationFrom(x0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100157 } else if (return_type == DataType::Type::kVoid) {
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100158 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000159 } else {
160 return LocationFrom(w0);
161 }
162}
163
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100164Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000165 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100166}
167
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100168// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
169#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700170#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100171
Zheng Xuda403092015-04-24 17:35:39 +0800172// Calculate memory accessing operand for save/restore live registers.
173static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100174 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800175 int64_t spill_offset,
176 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100177 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
178 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
179 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800180 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100181 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800182 codegen->GetNumberOfFloatingPointRegisters()));
183
Vladimir Marko804b03f2016-09-14 16:26:36 +0100184 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100185 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
186 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800187
188 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
189 UseScratchRegisterScope temps(masm);
190
191 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100192 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
193 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800194 int64_t reg_size = kXRegSizeInBytes;
195 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
196 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100197 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800198 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
199 // If the offset does not fit in the instruction's immediate field, use an alternate register
200 // to compute the base address(float point registers spill base address).
201 Register new_base = temps.AcquireSameSizeAs(base);
202 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
203 base = new_base;
204 spill_offset = -core_spill_size;
205 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
206 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
207 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
208 }
209
210 if (is_save) {
211 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
212 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
213 } else {
214 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
215 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
216 }
217}
218
219void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800220 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100221 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
222 for (uint32_t i : LowToHighBits(core_spills)) {
223 // If the register holds an object, update the stack mask.
224 if (locations->RegisterContainsObject(i)) {
225 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800226 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100227 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
228 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
229 saved_core_stack_offsets_[i] = stack_offset;
230 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800231 }
232
Vladimir Marko804b03f2016-09-14 16:26:36 +0100233 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
234 for (uint32_t i : LowToHighBits(fp_spills)) {
235 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
236 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
237 saved_fpu_stack_offsets_[i] = stack_offset;
238 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800239 }
240
Vladimir Marko804b03f2016-09-14 16:26:36 +0100241 SaveRestoreLiveRegistersHelper(codegen,
242 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800243 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
244}
245
246void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100247 SaveRestoreLiveRegistersHelper(codegen,
248 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800249 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
250}
251
Alexandre Rames5319def2014-10-23 10:03:10 +0100252class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
253 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000254 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100255
Alexandre Rames67555f72014-11-18 10:55:16 +0000256 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100257 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000258 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100259
Alexandre Rames5319def2014-10-23 10:03:10 +0100260 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000261 if (instruction_->CanThrowIntoCatchBlock()) {
262 // Live registers will be restored in the catch block if caught.
263 SaveLiveRegisters(codegen, instruction_->GetLocations());
264 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000265 // We're moving two locations to locations that could overlap, so we need a parallel
266 // move resolver.
267 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100268 codegen->EmitParallelMoves(locations->InAt(0),
269 LocationFrom(calling_convention.GetRegisterAt(0)),
270 DataType::Type::kInt32,
271 locations->InAt(1),
272 LocationFrom(calling_convention.GetRegisterAt(1)),
273 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000274 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
275 ? kQuickThrowStringBounds
276 : kQuickThrowArrayBounds;
277 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100278 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800279 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100280 }
281
Alexandre Rames8158f282015-08-07 10:26:17 +0100282 bool IsFatal() const OVERRIDE { return true; }
283
Alexandre Rames9931f312015-06-19 14:47:01 +0100284 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
285
Alexandre Rames5319def2014-10-23 10:03:10 +0100286 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100287 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
288};
289
Alexandre Rames67555f72014-11-18 10:55:16 +0000290class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
291 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000292 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000293
294 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
295 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
296 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000297 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800298 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000299 }
300
Alexandre Rames8158f282015-08-07 10:26:17 +0100301 bool IsFatal() const OVERRIDE { return true; }
302
Alexandre Rames9931f312015-06-19 14:47:01 +0100303 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
304
Alexandre Rames67555f72014-11-18 10:55:16 +0000305 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000306 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
307};
308
309class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
310 public:
311 LoadClassSlowPathARM64(HLoadClass* cls,
312 HInstruction* at,
313 uint32_t dex_pc,
Vladimir Markoea4c1262017-02-06 19:59:33 +0000314 bool do_clinit,
315 vixl::aarch64::Register bss_entry_temp = vixl::aarch64::Register(),
316 vixl::aarch64::Label* bss_entry_adrp_label = nullptr)
317 : SlowPathCodeARM64(at),
318 cls_(cls),
319 dex_pc_(dex_pc),
320 do_clinit_(do_clinit),
321 bss_entry_temp_(bss_entry_temp),
322 bss_entry_adrp_label_(bss_entry_adrp_label) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000323 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
324 }
325
326 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000327 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000328 Location out = locations->Out();
329 constexpr bool call_saves_everything_except_r0_ip0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexandre Rames67555f72014-11-18 10:55:16 +0000330 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
331
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000332 InvokeRuntimeCallingConvention calling_convention;
333 // For HLoadClass/kBssEntry/kSaveEverything, the page address of the entry is in a temp
334 // register, make sure it's not clobbered by the call or by saving/restoring registers.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000335 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
336 bool is_load_class_bss_entry =
337 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000338 if (is_load_class_bss_entry) {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000339 DCHECK(bss_entry_temp_.IsValid());
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000340 DCHECK(!bss_entry_temp_.Is(calling_convention.GetRegisterAt(0)));
341 DCHECK(
342 !UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(bss_entry_temp_));
Vladimir Markoea4c1262017-02-06 19:59:33 +0000343 }
344
Alexandre Rames67555f72014-11-18 10:55:16 +0000345 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000346 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000347
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000348 dex::TypeIndex type_index = cls_->GetTypeIndex();
349 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000350 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
351 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000352 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800353 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100354 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800355 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100356 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800357 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000358
359 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000360 if (out.IsValid()) {
361 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100362 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000363 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000364 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000365 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000366 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000367 if (is_load_class_bss_entry) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000368 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000369 const DexFile& dex_file = cls_->GetDexFile();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000370 if (call_saves_everything_except_r0_ip0) {
371 // The class entry page address was preserved in bss_entry_temp_ thanks to kSaveEverything.
372 } else {
373 // For non-Baker read barrier, we need to re-calculate the address of the class entry page.
374 bss_entry_adrp_label_ = arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
375 arm64_codegen->EmitAdrpPlaceholder(bss_entry_adrp_label_, bss_entry_temp_);
376 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000377 vixl::aarch64::Label* strp_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +0000378 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label_);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000379 {
380 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
381 __ Bind(strp_label);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100382 __ str(RegisterFrom(locations->Out(), DataType::Type::kReference),
Vladimir Markoea4c1262017-02-06 19:59:33 +0000383 MemOperand(bss_entry_temp_, /* offset placeholder */ 0));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000384 }
385 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000386 __ B(GetExitLabel());
387 }
388
Alexandre Rames9931f312015-06-19 14:47:01 +0100389 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
390
Alexandre Rames67555f72014-11-18 10:55:16 +0000391 private:
392 // The class this slow path will load.
393 HLoadClass* const cls_;
394
Alexandre Rames67555f72014-11-18 10:55:16 +0000395 // The dex PC of `at_`.
396 const uint32_t dex_pc_;
397
398 // Whether to initialize the class.
399 const bool do_clinit_;
400
Vladimir Markoea4c1262017-02-06 19:59:33 +0000401 // For HLoadClass/kBssEntry, the temp register and the label of the ADRP where it was loaded.
402 vixl::aarch64::Register bss_entry_temp_;
403 vixl::aarch64::Label* bss_entry_adrp_label_;
404
Alexandre Rames67555f72014-11-18 10:55:16 +0000405 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
406};
407
Vladimir Markoaad75c62016-10-03 08:46:48 +0000408class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
409 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100410 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
411 : SlowPathCodeARM64(instruction),
412 temp_(temp),
413 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000414
415 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
416 LocationSummary* locations = instruction_->GetLocations();
417 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
418 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
419
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000420 InvokeRuntimeCallingConvention calling_convention;
421 // Make sure `temp_` is not clobbered by the call or by saving/restoring registers.
422 DCHECK(temp_.IsValid());
423 DCHECK(!temp_.Is(calling_convention.GetRegisterAt(0)));
424 DCHECK(!UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(temp_));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100425
Vladimir Markoaad75c62016-10-03 08:46:48 +0000426 __ Bind(GetEntryLabel());
427 SaveLiveRegisters(codegen, locations);
428
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000429 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
430 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000431 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
432 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100433 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000434 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
435
436 RestoreLiveRegisters(codegen, locations);
437
438 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000439 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100440 if (!kUseReadBarrier || kUseBakerReadBarrier) {
441 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
442 } else {
443 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100444 adrp_label_ = arm64_codegen->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100445 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
446 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000447 vixl::aarch64::Label* strp_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100448 arm64_codegen->NewStringBssEntryPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000449 {
450 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
451 __ Bind(strp_label);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100452 __ str(RegisterFrom(locations->Out(), DataType::Type::kReference),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100453 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000454 }
455
456 __ B(GetExitLabel());
457 }
458
459 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
460
461 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100462 const Register temp_;
463 vixl::aarch64::Label* adrp_label_;
464
Vladimir Markoaad75c62016-10-03 08:46:48 +0000465 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
466};
467
Alexandre Rames5319def2014-10-23 10:03:10 +0100468class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
469 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000470 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100471
Alexandre Rames67555f72014-11-18 10:55:16 +0000472 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
473 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100474 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000475 if (instruction_->CanThrowIntoCatchBlock()) {
476 // Live registers will be restored in the catch block if caught.
477 SaveLiveRegisters(codegen, instruction_->GetLocations());
478 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000479 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
480 instruction_,
481 instruction_->GetDexPc(),
482 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800483 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100484 }
485
Alexandre Rames8158f282015-08-07 10:26:17 +0100486 bool IsFatal() const OVERRIDE { return true; }
487
Alexandre Rames9931f312015-06-19 14:47:01 +0100488 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
489
Alexandre Rames5319def2014-10-23 10:03:10 +0100490 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100491 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
492};
493
494class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
495 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100496 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000497 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100498
Alexandre Rames67555f72014-11-18 10:55:16 +0000499 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100500 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000501 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100502 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100503 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000504 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800505 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100506 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000507 if (successor_ == nullptr) {
508 __ B(GetReturnLabel());
509 } else {
510 __ B(arm64_codegen->GetLabelOf(successor_));
511 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100512 }
513
Scott Wakeling97c72b72016-06-24 16:19:36 +0100514 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100515 DCHECK(successor_ == nullptr);
516 return &return_label_;
517 }
518
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100519 HBasicBlock* GetSuccessor() const {
520 return successor_;
521 }
522
Alexandre Rames9931f312015-06-19 14:47:01 +0100523 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
524
Alexandre Rames5319def2014-10-23 10:03:10 +0100525 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100526 // If not null, the block to branch to after the suspend check.
527 HBasicBlock* const successor_;
528
529 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100530 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100531
532 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
533};
534
Alexandre Rames67555f72014-11-18 10:55:16 +0000535class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
536 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000537 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000538 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000539
540 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000541 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800542
Alexandre Rames3e69f162014-12-10 10:36:50 +0000543 DCHECK(instruction_->IsCheckCast()
544 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
545 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100546 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000547
Alexandre Rames67555f72014-11-18 10:55:16 +0000548 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000549
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000550 if (!is_fatal_) {
551 SaveLiveRegisters(codegen, locations);
552 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000553
554 // We're moving two locations to locations that could overlap, so we need a parallel
555 // move resolver.
556 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800557 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800558 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100559 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800560 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800561 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100562 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000563 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000564 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800565 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100566 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000567 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
568 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
569 } else {
570 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800571 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
572 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000573 }
574
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000575 if (!is_fatal_) {
576 RestoreLiveRegisters(codegen, locations);
577 __ B(GetExitLabel());
578 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000579 }
580
Alexandre Rames9931f312015-06-19 14:47:01 +0100581 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100582 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100583
Alexandre Rames67555f72014-11-18 10:55:16 +0000584 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000585 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000586
Alexandre Rames67555f72014-11-18 10:55:16 +0000587 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
588};
589
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700590class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
591 public:
Aart Bik42249c32016-01-07 15:33:50 -0800592 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000593 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700594
595 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800596 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700597 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100598 LocationSummary* locations = instruction_->GetLocations();
599 SaveLiveRegisters(codegen, locations);
600 InvokeRuntimeCallingConvention calling_convention;
601 __ Mov(calling_convention.GetRegisterAt(0),
602 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000603 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100604 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700605 }
606
Alexandre Rames9931f312015-06-19 14:47:01 +0100607 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
608
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700609 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700610 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
611};
612
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100613class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
614 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000615 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100616
617 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
618 LocationSummary* locations = instruction_->GetLocations();
619 __ Bind(GetEntryLabel());
620 SaveLiveRegisters(codegen, locations);
621
622 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100623 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100624 parallel_move.AddMove(
625 locations->InAt(0),
626 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100627 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100628 nullptr);
629 parallel_move.AddMove(
630 locations->InAt(1),
631 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100632 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100633 nullptr);
634 parallel_move.AddMove(
635 locations->InAt(2),
636 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100637 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100638 nullptr);
639 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
640
641 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000642 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100643 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
644 RestoreLiveRegisters(codegen, locations);
645 __ B(GetExitLabel());
646 }
647
648 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
649
650 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100651 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
652};
653
Zheng Xu3927c8b2015-11-18 17:46:25 +0800654void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
655 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000656 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800657
658 // We are about to use the assembler to place literals directly. Make sure we have enough
659 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000660 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
661 num_entries * sizeof(int32_t),
662 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800663
664 __ Bind(&table_start_);
665 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
666 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100667 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800668 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100669 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800670 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
671 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
672 Literal<int32_t> literal(jump_offset);
673 __ place(&literal);
674 }
675}
676
Roland Levillain54f869e2017-03-06 13:54:11 +0000677// Abstract base class for read barrier slow paths marking a reference
678// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000679//
Roland Levillain54f869e2017-03-06 13:54:11 +0000680// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100681// barrier marking runtime entry point to be invoked or an empty
682// location; in the latter case, the read barrier marking runtime
683// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000684class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
685 protected:
686 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
687 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000688 DCHECK(kEmitCompilerReadBarrier);
689 }
690
Roland Levillain54f869e2017-03-06 13:54:11 +0000691 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000692
Roland Levillain54f869e2017-03-06 13:54:11 +0000693 // Generate assembly code calling the read barrier marking runtime
694 // entry point (ReadBarrierMarkRegX).
695 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000696 // No need to save live registers; it's taken care of by the
697 // entrypoint. Also, there is no need to update the stack mask,
698 // as this runtime call will not trigger a garbage collection.
699 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
700 DCHECK_NE(ref_.reg(), LR);
701 DCHECK_NE(ref_.reg(), WSP);
702 DCHECK_NE(ref_.reg(), WZR);
703 // IP0 is used internally by the ReadBarrierMarkRegX entry point
704 // as a temporary, it cannot be the entry point's input/output.
705 DCHECK_NE(ref_.reg(), IP0);
706 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
707 // "Compact" slow path, saving two moves.
708 //
709 // Instead of using the standard runtime calling convention (input
710 // and output in W0):
711 //
712 // W0 <- ref
713 // W0 <- ReadBarrierMark(W0)
714 // ref <- W0
715 //
716 // we just use rX (the register containing `ref`) as input and output
717 // of a dedicated entrypoint:
718 //
719 // rX <- ReadBarrierMarkRegX(rX)
720 //
721 if (entrypoint_.IsValid()) {
722 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
723 __ Blr(XRegisterFrom(entrypoint_));
724 } else {
725 // Entrypoint is not already loaded, load from the thread.
726 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100727 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000728 // This runtime call does not require a stack map.
729 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
730 }
731 }
732
733 // The location (register) of the marked object reference.
734 const Location ref_;
735
736 // The location of the entrypoint if it is already loaded.
737 const Location entrypoint_;
738
Roland Levillain54f869e2017-03-06 13:54:11 +0000739 private:
740 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
741};
742
Alexandre Rames5319def2014-10-23 10:03:10 +0100743// Slow path marking an object reference `ref` during a read
744// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000745// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100746//
747// This means that after the execution of this slow path, `ref` will
748// always be up-to-date, but `obj.field` may not; i.e., after the
749// flip, `ref` will be a to-space reference, but `obj.field` will
750// probably still be a from-space reference (unless it gets updated by
751// another thread, or if another thread installed another object
752// reference (different from `ref`) in `obj.field`).
753//
Roland Levillain97c46462017-05-11 14:04:03 +0100754// Argument `entrypoint` must be a register location holding the read
755// barrier marking runtime entry point to be invoked or an empty
756// location; in the latter case, the read barrier marking runtime
757// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000758class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100759 public:
760 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
761 Location ref,
762 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000763 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100764 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100765 }
766
767 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
768
769 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000770 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100771 DCHECK(locations->CanCall());
772 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000773 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000774 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
775 << "Unexpected instruction in read barrier marking slow path: "
776 << instruction_->DebugName();
777
778 __ Bind(GetEntryLabel());
779 GenerateReadBarrierMarkRuntimeCall(codegen);
780 __ B(GetExitLabel());
781 }
782
783 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000784 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
785};
786
Roland Levillain54f869e2017-03-06 13:54:11 +0000787// Slow path loading `obj`'s lock word, loading a reference from
788// object `*(obj + offset + (index << scale_factor))` into `ref`, and
789// marking `ref` if `obj` is gray according to the lock word (Baker
790// read barrier). The field `obj.field` in the object `obj` holding
791// this reference does not get updated by this slow path after marking
792// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
793// below for that).
794//
795// This means that after the execution of this slow path, `ref` will
796// always be up-to-date, but `obj.field` may not; i.e., after the
797// flip, `ref` will be a to-space reference, but `obj.field` will
798// probably still be a from-space reference (unless it gets updated by
799// another thread, or if another thread installed another object
800// reference (different from `ref`) in `obj.field`).
801//
802// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100803// barrier marking runtime entry point to be invoked or an empty
804// location; in the latter case, the read barrier marking runtime
805// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000806class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
807 public:
808 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
809 Location ref,
810 Register obj,
811 uint32_t offset,
812 Location index,
813 size_t scale_factor,
814 bool needs_null_check,
815 bool use_load_acquire,
816 Register temp,
Roland Levillain97c46462017-05-11 14:04:03 +0100817 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000818 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
819 obj_(obj),
820 offset_(offset),
821 index_(index),
822 scale_factor_(scale_factor),
823 needs_null_check_(needs_null_check),
824 use_load_acquire_(use_load_acquire),
825 temp_(temp) {
826 DCHECK(kEmitCompilerReadBarrier);
827 DCHECK(kUseBakerReadBarrier);
828 }
829
830 const char* GetDescription() const OVERRIDE {
831 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
832 }
833
834 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
835 LocationSummary* locations = instruction_->GetLocations();
836 DCHECK(locations->CanCall());
837 DCHECK(ref_.IsRegister()) << ref_;
838 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
839 DCHECK(obj_.IsW());
840 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100841 DCHECK(instruction_->IsInstanceFieldGet() ||
842 instruction_->IsStaticFieldGet() ||
843 instruction_->IsArrayGet() ||
844 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100845 instruction_->IsInstanceOf() ||
846 instruction_->IsCheckCast() ||
847 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
848 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
849 << "Unexpected instruction in read barrier marking slow path: "
850 << instruction_->DebugName();
851 // The read barrier instrumentation of object ArrayGet
852 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000853 // instruction.
854 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000855 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
856
Roland Levillain54f869e2017-03-06 13:54:11 +0000857 // Temporary register `temp_`, used to store the lock word, must
858 // not be IP0 nor IP1, as we may use them to emit the reference
859 // load (in the call to GenerateRawReferenceLoad below), and we
860 // need the lock word to still be in `temp_` after the reference
861 // load.
862 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
863 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
864
Alexandre Rames5319def2014-10-23 10:03:10 +0100865 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000866
867 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
868 // inserted after the original load. However, in fast path based
869 // Baker's read barriers, we need to perform the load of
870 // mirror::Object::monitor_ *before* the original reference load.
871 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000872 // The slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100873 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000874 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
875 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
876 // HeapReference<mirror::Object> ref = *src; // Original reference load.
877 // bool is_gray = (rb_state == ReadBarrier::GrayState());
878 // if (is_gray) {
879 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
880 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000881 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000882 // Note: the original implementation in ReadBarrier::Barrier is
883 // slightly more complex as it performs additional checks that we do
884 // not do here for performance reasons.
885
886 // /* int32_t */ monitor = obj->monitor_
887 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
888 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
889 if (needs_null_check_) {
890 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100891 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000892 // /* LockWord */ lock_word = LockWord(monitor)
893 static_assert(sizeof(LockWord) == sizeof(int32_t),
894 "art::LockWord and int32_t have different sizes.");
895
896 // Introduce a dependency on the lock_word including rb_state,
897 // to prevent load-load reordering, and without using
898 // a memory barrier (which would be more expensive).
899 // `obj` is unchanged by this operation, but its value now depends
900 // on `temp`.
901 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
902
903 // The actual reference load.
904 // A possible implicit null check has already been handled above.
905 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
906 arm64_codegen->GenerateRawReferenceLoad(instruction_,
907 ref_,
908 obj_,
909 offset_,
910 index_,
911 scale_factor_,
912 /* needs_null_check */ false,
913 use_load_acquire_);
914
915 // Mark the object `ref` when `obj` is gray.
916 //
917 // if (rb_state == ReadBarrier::GrayState())
918 // ref = ReadBarrier::Mark(ref);
919 //
920 // Given the numeric representation, it's enough to check the low bit of the rb_state.
921 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
922 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
923 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
924 GenerateReadBarrierMarkRuntimeCall(codegen);
925
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000926 __ B(GetExitLabel());
927 }
928
929 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000930 // The register containing the object holding the marked object reference field.
931 Register obj_;
932 // The offset, index and scale factor to access the reference in `obj_`.
933 uint32_t offset_;
934 Location index_;
935 size_t scale_factor_;
936 // Is a null check required?
937 bool needs_null_check_;
938 // Should this reference load use Load-Acquire semantics?
939 bool use_load_acquire_;
940 // A temporary register used to hold the lock word of `obj_`.
941 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000942
Roland Levillain54f869e2017-03-06 13:54:11 +0000943 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000944};
945
Roland Levillain54f869e2017-03-06 13:54:11 +0000946// Slow path loading `obj`'s lock word, loading a reference from
947// object `*(obj + offset + (index << scale_factor))` into `ref`, and
948// marking `ref` if `obj` is gray according to the lock word (Baker
949// read barrier). If needed, this slow path also atomically updates
950// the field `obj.field` in the object `obj` holding this reference
951// after marking (contrary to
952// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
953// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100954//
955// This means that after the execution of this slow path, both `ref`
956// and `obj.field` will be up-to-date; i.e., after the flip, both will
957// hold the same to-space reference (unless another thread installed
958// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000959//
Roland Levillain54f869e2017-03-06 13:54:11 +0000960// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100961// barrier marking runtime entry point to be invoked or an empty
962// location; in the latter case, the read barrier marking runtime
963// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000964class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
965 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100966 public:
Roland Levillain97c46462017-05-11 14:04:03 +0100967 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
968 HInstruction* instruction,
969 Location ref,
970 Register obj,
971 uint32_t offset,
972 Location index,
973 size_t scale_factor,
974 bool needs_null_check,
975 bool use_load_acquire,
976 Register temp,
977 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000978 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100979 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000980 offset_(offset),
981 index_(index),
982 scale_factor_(scale_factor),
983 needs_null_check_(needs_null_check),
984 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000985 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100986 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000987 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100988 }
989
990 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000991 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100992 }
993
994 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
995 LocationSummary* locations = instruction_->GetLocations();
996 Register ref_reg = WRegisterFrom(ref_);
997 DCHECK(locations->CanCall());
998 DCHECK(ref_.IsRegister()) << ref_;
999 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +00001000 DCHECK(obj_.IsW());
1001 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
1002
1003 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001004 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
1005 << "Unexpected instruction in read barrier marking and field updating slow path: "
1006 << instruction_->DebugName();
1007 DCHECK(instruction_->GetLocations()->Intrinsified());
1008 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +00001009 DCHECK_EQ(offset_, 0u);
1010 DCHECK_EQ(scale_factor_, 0u);
1011 DCHECK_EQ(use_load_acquire_, false);
1012 // The location of the offset of the marked reference field within `obj_`.
1013 Location field_offset = index_;
1014 DCHECK(field_offset.IsRegister()) << field_offset;
1015
1016 // Temporary register `temp_`, used to store the lock word, must
1017 // not be IP0 nor IP1, as we may use them to emit the reference
1018 // load (in the call to GenerateRawReferenceLoad below), and we
1019 // need the lock word to still be in `temp_` after the reference
1020 // load.
1021 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1022 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001023
1024 __ Bind(GetEntryLabel());
1025
Roland Levillainff487002017-03-07 16:50:01 +00001026 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM64's:
1027 //
1028 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1029 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1030 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1031 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1032 // if (is_gray) {
1033 // old_ref = ref;
1034 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1035 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1036 // }
1037
Roland Levillain54f869e2017-03-06 13:54:11 +00001038 // /* int32_t */ monitor = obj->monitor_
1039 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1040 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
1041 if (needs_null_check_) {
1042 codegen->MaybeRecordImplicitNullCheck(instruction_);
1043 }
1044 // /* LockWord */ lock_word = LockWord(monitor)
1045 static_assert(sizeof(LockWord) == sizeof(int32_t),
1046 "art::LockWord and int32_t have different sizes.");
1047
1048 // Introduce a dependency on the lock_word including rb_state,
1049 // to prevent load-load reordering, and without using
1050 // a memory barrier (which would be more expensive).
1051 // `obj` is unchanged by this operation, but its value now depends
1052 // on `temp`.
1053 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
1054
1055 // The actual reference load.
1056 // A possible implicit null check has already been handled above.
1057 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1058 arm64_codegen->GenerateRawReferenceLoad(instruction_,
1059 ref_,
1060 obj_,
1061 offset_,
1062 index_,
1063 scale_factor_,
1064 /* needs_null_check */ false,
1065 use_load_acquire_);
1066
1067 // Mark the object `ref` when `obj` is gray.
1068 //
1069 // if (rb_state == ReadBarrier::GrayState())
1070 // ref = ReadBarrier::Mark(ref);
1071 //
1072 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1073 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1074 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1075 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1076
1077 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001078 // Note that we cannot use IP to save the old reference, as IP is
1079 // used internally by the ReadBarrierMarkRegX entry point, and we
1080 // need the old reference after the call to that entry point.
1081 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1082 __ Mov(temp_.W(), ref_reg);
1083
Roland Levillain54f869e2017-03-06 13:54:11 +00001084 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001085
1086 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001087 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001088 //
1089 // Note that this field could also hold a different object, if
1090 // another thread had concurrently changed it. In that case, the
1091 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1092 // (CAS) operation below would abort the CAS, leaving the field
1093 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001094 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001095 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001096
1097 // Update the the holder's field atomically. This may fail if
1098 // mutator updates before us, but it's OK. This is achieved
1099 // using a strong compare-and-set (CAS) operation with relaxed
1100 // memory synchronization ordering, where the expected value is
1101 // the old reference and the desired value is the new reference.
1102
1103 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1104 UseScratchRegisterScope temps(masm);
1105
1106 // Convenience aliases.
1107 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001108 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001109 Register expected = temp_.W();
1110 Register value = ref_reg;
1111 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1112 Register tmp_value = temps.AcquireW(); // Value in memory.
1113
1114 __ Add(tmp_ptr, base.X(), Operand(offset));
1115
1116 if (kPoisonHeapReferences) {
1117 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1118 if (value.Is(expected)) {
1119 // Do not poison `value`, as it is the same register as
1120 // `expected`, which has just been poisoned.
1121 } else {
1122 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1123 }
1124 }
1125
1126 // do {
1127 // tmp_value = [tmp_ptr] - expected;
1128 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1129
Roland Levillain24a4d112016-10-26 13:10:46 +01001130 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001131 __ Bind(&loop_head);
1132 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1133 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001134 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001135 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1136 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001137 __ B(&exit_loop);
1138 __ Bind(&comparison_failed);
1139 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001140 __ Bind(&exit_loop);
1141
1142 if (kPoisonHeapReferences) {
1143 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1144 if (value.Is(expected)) {
1145 // Do not unpoison `value`, as it is the same register as
1146 // `expected`, which has just been unpoisoned.
1147 } else {
1148 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1149 }
1150 }
1151
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001152 __ B(GetExitLabel());
1153 }
1154
1155 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001156 // The register containing the object holding the marked object reference field.
1157 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001158 // The offset, index and scale factor to access the reference in `obj_`.
1159 uint32_t offset_;
1160 Location index_;
1161 size_t scale_factor_;
1162 // Is a null check required?
1163 bool needs_null_check_;
1164 // Should this reference load use Load-Acquire semantics?
1165 bool use_load_acquire_;
1166 // A temporary register used to hold the lock word of `obj_`; and
1167 // also to hold the original reference value, when the reference is
1168 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001169 const Register temp_;
1170
Roland Levillain54f869e2017-03-06 13:54:11 +00001171 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001172};
1173
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001174// Slow path generating a read barrier for a heap reference.
1175class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1176 public:
1177 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1178 Location out,
1179 Location ref,
1180 Location obj,
1181 uint32_t offset,
1182 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001183 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001184 out_(out),
1185 ref_(ref),
1186 obj_(obj),
1187 offset_(offset),
1188 index_(index) {
1189 DCHECK(kEmitCompilerReadBarrier);
1190 // If `obj` is equal to `out` or `ref`, it means the initial object
1191 // has been overwritten by (or after) the heap object reference load
1192 // to be instrumented, e.g.:
1193 //
1194 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001195 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001196 //
1197 // In that case, we have lost the information about the original
1198 // object, and the emitted read barrier cannot work properly.
1199 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1200 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1201 }
1202
1203 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1204 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1205 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001206 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001207 DCHECK(locations->CanCall());
1208 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001209 DCHECK(instruction_->IsInstanceFieldGet() ||
1210 instruction_->IsStaticFieldGet() ||
1211 instruction_->IsArrayGet() ||
1212 instruction_->IsInstanceOf() ||
1213 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001214 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001215 << "Unexpected instruction in read barrier for heap reference slow path: "
1216 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001217 // The read barrier instrumentation of object ArrayGet
1218 // instructions does not support the HIntermediateAddress
1219 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001220 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001221 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001222
1223 __ Bind(GetEntryLabel());
1224
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001225 SaveLiveRegisters(codegen, locations);
1226
1227 // We may have to change the index's value, but as `index_` is a
1228 // constant member (like other "inputs" of this slow path),
1229 // introduce a copy of it, `index`.
1230 Location index = index_;
1231 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001232 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001233 if (instruction_->IsArrayGet()) {
1234 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001235 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001236 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1237 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1238 // We are about to change the value of `index_reg` (see the
1239 // calls to vixl::MacroAssembler::Lsl and
1240 // vixl::MacroAssembler::Mov below), but it has
1241 // not been saved by the previous call to
1242 // art::SlowPathCode::SaveLiveRegisters, as it is a
1243 // callee-save register --
1244 // art::SlowPathCode::SaveLiveRegisters does not consider
1245 // callee-save registers, as it has been designed with the
1246 // assumption that callee-save registers are supposed to be
1247 // handled by the called function. So, as a callee-save
1248 // register, `index_reg` _would_ eventually be saved onto
1249 // the stack, but it would be too late: we would have
1250 // changed its value earlier. Therefore, we manually save
1251 // it here into another freely available register,
1252 // `free_reg`, chosen of course among the caller-save
1253 // registers (as a callee-save `free_reg` register would
1254 // exhibit the same problem).
1255 //
1256 // Note we could have requested a temporary register from
1257 // the register allocator instead; but we prefer not to, as
1258 // this is a slow path, and we know we can find a
1259 // caller-save register that is available.
1260 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1261 __ Mov(free_reg.W(), index_reg);
1262 index_reg = free_reg;
1263 index = LocationFrom(index_reg);
1264 } else {
1265 // The initial register stored in `index_` has already been
1266 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1267 // (as it is not a callee-save register), so we can freely
1268 // use it.
1269 }
1270 // Shifting the index value contained in `index_reg` by the scale
1271 // factor (2) cannot overflow in practice, as the runtime is
1272 // unable to allocate object arrays with a size larger than
1273 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001274 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001275 static_assert(
1276 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1277 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1278 __ Add(index_reg, index_reg, Operand(offset_));
1279 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001280 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1281 // intrinsics, `index_` is not shifted by a scale factor of 2
1282 // (as in the case of ArrayGet), as it is actually an offset
1283 // to an object field within an object.
1284 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001285 DCHECK(instruction_->GetLocations()->Intrinsified());
1286 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1287 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1288 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001289 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001290 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001291 }
1292 }
1293
1294 // We're moving two or three locations to locations that could
1295 // overlap, so we need a parallel move resolver.
1296 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001297 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001298 parallel_move.AddMove(ref_,
1299 LocationFrom(calling_convention.GetRegisterAt(0)),
1300 type,
1301 nullptr);
1302 parallel_move.AddMove(obj_,
1303 LocationFrom(calling_convention.GetRegisterAt(1)),
1304 type,
1305 nullptr);
1306 if (index.IsValid()) {
1307 parallel_move.AddMove(index,
1308 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001309 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001310 nullptr);
1311 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1312 } else {
1313 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1314 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1315 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001316 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001317 instruction_,
1318 instruction_->GetDexPc(),
1319 this);
1320 CheckEntrypointTypes<
1321 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1322 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1323
1324 RestoreLiveRegisters(codegen, locations);
1325
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001326 __ B(GetExitLabel());
1327 }
1328
1329 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1330
1331 private:
1332 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001333 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1334 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001335 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1336 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1337 return Register(VIXLRegCodeFromART(i), kXRegSize);
1338 }
1339 }
1340 // We shall never fail to find a free caller-save register, as
1341 // there are more than two core caller-save registers on ARM64
1342 // (meaning it is possible to find one which is different from
1343 // `ref` and `obj`).
1344 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1345 LOG(FATAL) << "Could not find a free register";
1346 UNREACHABLE();
1347 }
1348
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001349 const Location out_;
1350 const Location ref_;
1351 const Location obj_;
1352 const uint32_t offset_;
1353 // An additional location containing an index to an array.
1354 // Only used for HArrayGet and the UnsafeGetObject &
1355 // UnsafeGetObjectVolatile intrinsics.
1356 const Location index_;
1357
1358 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1359};
1360
1361// Slow path generating a read barrier for a GC root.
1362class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1363 public:
1364 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001365 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001366 DCHECK(kEmitCompilerReadBarrier);
1367 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001368
1369 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1370 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001371 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001372 DCHECK(locations->CanCall());
1373 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001374 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1375 << "Unexpected instruction in read barrier for GC root slow path: "
1376 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001377
1378 __ Bind(GetEntryLabel());
1379 SaveLiveRegisters(codegen, locations);
1380
1381 InvokeRuntimeCallingConvention calling_convention;
1382 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1383 // The argument of the ReadBarrierForRootSlow is not a managed
1384 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1385 // thus we need a 64-bit move here, and we cannot use
1386 //
1387 // arm64_codegen->MoveLocation(
1388 // LocationFrom(calling_convention.GetRegisterAt(0)),
1389 // root_,
1390 // type);
1391 //
1392 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001393 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001394 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001395 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001396 instruction_,
1397 instruction_->GetDexPc(),
1398 this);
1399 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1400 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1401
1402 RestoreLiveRegisters(codegen, locations);
1403 __ B(GetExitLabel());
1404 }
1405
1406 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1407
1408 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001409 const Location out_;
1410 const Location root_;
1411
1412 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1413};
1414
Alexandre Rames5319def2014-10-23 10:03:10 +01001415#undef __
1416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001417Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001418 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001419 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001420 LOG(FATAL) << "Unreachable type " << type;
1421 }
1422
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001423 if (DataType::IsFloatingPointType(type) &&
Alexandre Rames5319def2014-10-23 10:03:10 +01001424 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001425 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001426 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001427 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1428 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1429 } else {
1430 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001431 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1432 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001433 }
1434
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001435 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001436 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001437 return next_location;
1438}
1439
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001440Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001441 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001442}
1443
Serban Constantinescu579885a2015-02-22 20:51:33 +00001444CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1445 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001446 const CompilerOptions& compiler_options,
1447 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001448 : CodeGenerator(graph,
1449 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001450 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001451 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001452 callee_saved_core_registers.GetList(),
1453 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001454 compiler_options,
1455 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001456 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1457 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001458 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001459 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001460 move_resolver_(graph->GetAllocator(), this),
1461 assembler_(graph->GetAllocator()),
Vladimir Marko58155012015-08-19 12:49:41 +00001462 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001463 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001464 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001465 uint64_literals_(std::less<uint64_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001466 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1467 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1468 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1469 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1470 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1471 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1472 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1473 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001474 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001475 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001476 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001477 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001478 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001479 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001480}
Alexandre Rames5319def2014-10-23 10:03:10 +01001481
Alexandre Rames67555f72014-11-18 10:55:16 +00001482#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001483
Zheng Xu3927c8b2015-11-18 17:46:25 +08001484void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001485 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001486 jump_table->EmitTable(this);
1487 }
1488}
1489
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001490void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001491 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001492 // Ensure we emit the literal pool.
1493 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001494
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001495 CodeGenerator::Finalize(allocator);
1496}
1497
Zheng Xuad4450e2015-04-17 18:48:56 +08001498void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1499 // Note: There are 6 kinds of moves:
1500 // 1. constant -> GPR/FPR (non-cycle)
1501 // 2. constant -> stack (non-cycle)
1502 // 3. GPR/FPR -> GPR/FPR
1503 // 4. GPR/FPR -> stack
1504 // 5. stack -> GPR/FPR
1505 // 6. stack -> stack (non-cycle)
1506 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1507 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1508 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1509 // dependency.
1510 vixl_temps_.Open(GetVIXLAssembler());
1511}
1512
1513void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1514 vixl_temps_.Close();
1515}
1516
1517Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001518 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1519 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1520 || kind == Location::kSIMDStackSlot);
1521 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1522 ? Location::kFpuRegister
1523 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001524 Location scratch = GetScratchLocation(kind);
1525 if (!scratch.Equals(Location::NoLocation())) {
1526 return scratch;
1527 }
1528 // Allocate from VIXL temp registers.
1529 if (kind == Location::kRegister) {
1530 scratch = LocationFrom(vixl_temps_.AcquireX());
1531 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001532 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001533 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1534 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1535 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001536 }
1537 AddScratchLocation(scratch);
1538 return scratch;
1539}
1540
1541void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1542 if (loc.IsRegister()) {
1543 vixl_temps_.Release(XRegisterFrom(loc));
1544 } else {
1545 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001546 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001547 }
1548 RemoveScratchLocation(loc);
1549}
1550
Alexandre Rames3e69f162014-12-10 10:36:50 +00001551void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001552 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001553 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001554}
1555
Alexandre Rames5319def2014-10-23 10:03:10 +01001556void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001557 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001558 __ Bind(&frame_entry_label_);
1559
Serban Constantinescu02164b32014-11-13 14:05:07 +00001560 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1561 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001562 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001563 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001564 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001565 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001566 {
1567 // Ensure that between load and RecordPcInfo there are no pools emitted.
1568 ExactAssemblyScope eas(GetVIXLAssembler(),
1569 kInstructionSize,
1570 CodeBufferCheckScope::kExactSize);
1571 __ ldr(wzr, MemOperand(temp, 0));
1572 RecordPcInfo(nullptr, 0);
1573 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001574 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001575
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001576 if (!HasEmptyFrame()) {
1577 int frame_size = GetFrameSize();
1578 // Stack layout:
1579 // sp[frame_size - 8] : lr.
1580 // ... : other preserved core registers.
1581 // ... : other preserved fp registers.
1582 // ... : reserved frame space.
1583 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001584
1585 // Save the current method if we need it. Note that we do not
1586 // do this in HCurrentMethod, as the instruction might have been removed
1587 // in the SSA graph.
1588 if (RequiresCurrentMethod()) {
1589 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001590 } else {
1591 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001592 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001593 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001594 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1595 frame_size - GetCoreSpillSize());
1596 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1597 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001598
1599 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1600 // Initialize should_deoptimize flag to 0.
1601 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1602 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1603 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001604 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01001605
1606 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001607}
1608
1609void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001610 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001611 if (!HasEmptyFrame()) {
1612 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001613 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1614 frame_size - FrameEntrySpillSize());
1615 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1616 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001617 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001618 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001619 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001620 __ Ret();
1621 GetAssembler()->cfi().RestoreState();
1622 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001623}
1624
Scott Wakeling97c72b72016-06-24 16:19:36 +01001625CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001626 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001627 return CPURegList(CPURegister::kRegister, kXRegSize,
1628 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001629}
1630
Scott Wakeling97c72b72016-06-24 16:19:36 +01001631CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001632 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1633 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001634 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1635 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001636}
1637
Alexandre Rames5319def2014-10-23 10:03:10 +01001638void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1639 __ Bind(GetLabelOf(block));
1640}
1641
Calin Juravle175dc732015-08-25 15:42:32 +01001642void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1643 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001644 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001645}
1646
Calin Juravlee460d1d2015-09-29 04:52:17 +01001647void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1648 if (location.IsRegister()) {
1649 locations->AddTemp(location);
1650 } else {
1651 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1652 }
1653}
1654
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001655void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001656 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001657 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001658 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001659 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001660 if (value_can_be_null) {
1661 __ Cbz(value, &done);
1662 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001663 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001664 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001665 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001666 if (value_can_be_null) {
1667 __ Bind(&done);
1668 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001669}
1670
David Brazdil58282f42016-01-14 12:45:10 +00001671void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001672 // Blocked core registers:
1673 // lr : Runtime reserved.
1674 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001675 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001676 // ip1 : VIXL core temp.
1677 // ip0 : VIXL core temp.
1678 //
1679 // Blocked fp registers:
1680 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001681 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1682 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001683 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001684 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001685 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001686
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001687 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001688 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001689 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001690 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001691
David Brazdil58282f42016-01-14 12:45:10 +00001692 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001693 // Stubs do not save callee-save floating point registers. If the graph
1694 // is debuggable, we need to deal with these registers differently. For
1695 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001696 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1697 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001698 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001699 }
1700 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001701}
1702
Alexandre Rames3e69f162014-12-10 10:36:50 +00001703size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1704 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1705 __ Str(reg, MemOperand(sp, stack_index));
1706 return kArm64WordSize;
1707}
1708
1709size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1710 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1711 __ Ldr(reg, MemOperand(sp, stack_index));
1712 return kArm64WordSize;
1713}
1714
1715size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1716 FPRegister reg = FPRegister(reg_id, kDRegSize);
1717 __ Str(reg, MemOperand(sp, stack_index));
1718 return kArm64WordSize;
1719}
1720
1721size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1722 FPRegister reg = FPRegister(reg_id, kDRegSize);
1723 __ Ldr(reg, MemOperand(sp, stack_index));
1724 return kArm64WordSize;
1725}
1726
Alexandre Rames5319def2014-10-23 10:03:10 +01001727void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001728 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001729}
1730
1731void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001732 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001733}
1734
Alexandre Rames67555f72014-11-18 10:55:16 +00001735void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001736 if (constant->IsIntConstant()) {
1737 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1738 } else if (constant->IsLongConstant()) {
1739 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1740 } else if (constant->IsNullConstant()) {
1741 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001742 } else if (constant->IsFloatConstant()) {
1743 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1744 } else {
1745 DCHECK(constant->IsDoubleConstant());
1746 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1747 }
1748}
1749
Alexandre Rames3e69f162014-12-10 10:36:50 +00001750
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001751static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001752 DCHECK(constant.IsConstant());
1753 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001754 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001755 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001756 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1757 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1758 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1759 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001760}
1761
Roland Levillain952b2352017-05-03 19:49:14 +01001762// Allocate a scratch register from the VIXL pool, querying first
1763// the floating-point register pool, and then the core register
1764// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001765// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1766// using a different allocation strategy.
1767static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1768 vixl::aarch64::UseScratchRegisterScope* temps,
1769 int size_in_bits) {
1770 return masm->GetScratchFPRegisterList()->IsEmpty()
1771 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1772 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1773}
1774
Calin Juravlee460d1d2015-09-29 04:52:17 +01001775void CodeGeneratorARM64::MoveLocation(Location destination,
1776 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001777 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001778 if (source.Equals(destination)) {
1779 return;
1780 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001781
1782 // A valid move can always be inferred from the destination and source
1783 // locations. When moving from and to a register, the argument type can be
1784 // used to generate 32bit instead of 64bit moves. In debug mode we also
1785 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001786 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001787
1788 if (destination.IsRegister() || destination.IsFpuRegister()) {
1789 if (unspecified_type) {
1790 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1791 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001792 (src_cst != nullptr && (src_cst->IsIntConstant()
1793 || src_cst->IsFloatConstant()
1794 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001795 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001796 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001797 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001798 // If the source is a double stack slot or a 64bit constant, a 64bit
1799 // type is appropriate. Else the source is a register, and since the
1800 // type has not been specified, we chose a 64bit type to force a 64bit
1801 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001802 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001803 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001804 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001805 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1806 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001807 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001808 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1809 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1810 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001811 } else if (source.IsSIMDStackSlot()) {
1812 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001813 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001814 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001815 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001816 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001817 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001818 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001819 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001820 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001821 DataType::Type source_type = DataType::Is64BitType(dst_type)
1822 ? DataType::Type::kInt64
1823 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001824 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1825 }
1826 } else {
1827 DCHECK(source.IsFpuRegister());
1828 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001829 DataType::Type source_type = DataType::Is64BitType(dst_type)
1830 ? DataType::Type::kFloat64
1831 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001832 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1833 } else {
1834 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001835 if (GetGraph()->HasSIMD()) {
1836 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1837 } else {
1838 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1839 }
1840 }
1841 }
1842 } else if (destination.IsSIMDStackSlot()) {
1843 if (source.IsFpuRegister()) {
1844 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1845 } else {
1846 DCHECK(source.IsSIMDStackSlot());
1847 UseScratchRegisterScope temps(GetVIXLAssembler());
1848 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1849 Register temp = temps.AcquireX();
1850 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1851 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1852 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1853 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1854 } else {
1855 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1856 __ Ldr(temp, StackOperandFrom(source));
1857 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001858 }
1859 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001860 } else { // The destination is not a register. It must be a stack slot.
1861 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1862 if (source.IsRegister() || source.IsFpuRegister()) {
1863 if (unspecified_type) {
1864 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001865 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001866 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001867 dst_type =
1868 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001869 }
1870 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001871 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1872 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001873 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001874 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001875 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1876 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001877 UseScratchRegisterScope temps(GetVIXLAssembler());
1878 HConstant* src_cst = source.GetConstant();
1879 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001880 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001881 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1882 ? Register(xzr)
1883 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001884 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001885 if (src_cst->IsIntConstant()) {
1886 temp = temps.AcquireW();
1887 } else if (src_cst->IsLongConstant()) {
1888 temp = temps.AcquireX();
1889 } else if (src_cst->IsFloatConstant()) {
1890 temp = temps.AcquireS();
1891 } else {
1892 DCHECK(src_cst->IsDoubleConstant());
1893 temp = temps.AcquireD();
1894 }
1895 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001896 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001897 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001898 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001899 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001900 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001901 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001902 // Use any scratch register (a core or a floating-point one)
1903 // from VIXL scratch register pools as a temporary.
1904 //
1905 // We used to only use the FP scratch register pool, but in some
1906 // rare cases the only register from this pool (D31) would
1907 // already be used (e.g. within a ParallelMove instruction, when
1908 // a move is blocked by a another move requiring a scratch FP
1909 // register, which would reserve D31). To prevent this issue, we
1910 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001911 //
1912 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001913 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001914 // use AcquireFPOrCoreCPURegisterOfSize instead of
1915 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1916 // allocates core scratch registers first.
1917 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1918 GetVIXLAssembler(),
1919 &temps,
1920 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001921 __ Ldr(temp, StackOperandFrom(source));
1922 __ Str(temp, StackOperandFrom(destination));
1923 }
1924 }
1925}
1926
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001927void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001928 CPURegister dst,
1929 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001930 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001931 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001932 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001933 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001934 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001935 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001936 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001937 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001938 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001939 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001940 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001941 case DataType::Type::kInt16:
1942 __ Ldrsh(Register(dst), src);
1943 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001944 case DataType::Type::kInt32:
1945 case DataType::Type::kReference:
1946 case DataType::Type::kInt64:
1947 case DataType::Type::kFloat32:
1948 case DataType::Type::kFloat64:
1949 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001950 __ Ldr(dst, src);
1951 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001952 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001953 LOG(FATAL) << "Unreachable type " << type;
1954 }
1955}
1956
Calin Juravle77520bc2015-01-12 18:45:46 +00001957void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001958 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001959 const MemOperand& src,
1960 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001961 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001962 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001963 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001964 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001965
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001966 DCHECK(!src.IsPreIndex());
1967 DCHECK(!src.IsPostIndex());
1968
1969 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001970 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001971 {
1972 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1973 MemOperand base = MemOperand(temp_base);
1974 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001975 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001976 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001977 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001978 {
1979 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1980 __ ldarb(Register(dst), base);
1981 if (needs_null_check) {
1982 MaybeRecordImplicitNullCheck(instruction);
1983 }
1984 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001985 if (type == DataType::Type::kInt8) {
1986 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001987 }
1988 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001989 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001990 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001991 {
1992 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1993 __ ldarh(Register(dst), base);
1994 if (needs_null_check) {
1995 MaybeRecordImplicitNullCheck(instruction);
1996 }
1997 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001998 if (type == DataType::Type::kInt16) {
1999 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
2000 }
Artem Serov914d7a82017-02-07 14:33:49 +00002001 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002002 case DataType::Type::kInt32:
2003 case DataType::Type::kReference:
2004 case DataType::Type::kInt64:
2005 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002006 {
2007 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2008 __ ldar(Register(dst), base);
2009 if (needs_null_check) {
2010 MaybeRecordImplicitNullCheck(instruction);
2011 }
2012 }
2013 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002014 case DataType::Type::kFloat32:
2015 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00002016 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002017 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002018
Artem Serov914d7a82017-02-07 14:33:49 +00002019 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2020 {
2021 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2022 __ ldar(temp, base);
2023 if (needs_null_check) {
2024 MaybeRecordImplicitNullCheck(instruction);
2025 }
2026 }
2027 __ Fmov(FPRegister(dst), temp);
2028 break;
Roland Levillain44015862016-01-22 11:47:17 +00002029 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002030 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00002031 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002032 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002033 }
2034}
2035
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002036void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002037 CPURegister src,
2038 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002039 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002040 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002041 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002042 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002043 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002044 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002045 case DataType::Type::kUint16:
2046 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002047 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002048 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002049 case DataType::Type::kInt32:
2050 case DataType::Type::kReference:
2051 case DataType::Type::kInt64:
2052 case DataType::Type::kFloat32:
2053 case DataType::Type::kFloat64:
2054 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002055 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002056 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002057 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002058 LOG(FATAL) << "Unreachable type " << type;
2059 }
2060}
2061
Artem Serov914d7a82017-02-07 14:33:49 +00002062void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002063 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002064 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002065 const MemOperand& dst,
2066 bool needs_null_check) {
2067 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002068 UseScratchRegisterScope temps(GetVIXLAssembler());
2069 Register temp_base = temps.AcquireX();
2070
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002071 DCHECK(!dst.IsPreIndex());
2072 DCHECK(!dst.IsPostIndex());
2073
2074 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002075 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002076 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002077 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002078 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002079 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002080 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002081 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002082 case DataType::Type::kInt8:
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;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002091 case DataType::Type::kUint16:
2092 case DataType::Type::kInt16:
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;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002101 case DataType::Type::kInt32:
2102 case DataType::Type::kReference:
2103 case DataType::Type::kInt64:
2104 DCHECK_EQ(src.Is64Bits(), DataType::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;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002113 case DataType::Type::kFloat32:
2114 case DataType::Type::kFloat64: {
2115 DCHECK_EQ(src.Is64Bits(), DataType::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 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002134 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002135 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) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002207 slow_path =
2208 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002209 instruction->SetSlowPath(slow_path);
2210 codegen_->AddSlowPath(slow_path);
2211 if (successor != nullptr) {
2212 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002213 }
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
Alexandre Rames67555f72014-11-18 10:55:16 +00002238void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002239 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002240 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002241 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002242 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002243 case DataType::Type::kInt32:
2244 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01002245 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002246 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002247 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002248 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002249
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002250 case DataType::Type::kFloat32:
2251 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002252 locations->SetInAt(0, Location::RequiresFpuRegister());
2253 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002254 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002255 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002256
Alexandre Rames5319def2014-10-23 10:03:10 +01002257 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002258 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002259 }
2260}
2261
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002262void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2263 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002264 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2265
2266 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002267 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002268 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002269 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2270 object_field_get_with_read_barrier
2271 ? LocationSummary::kCallOnSlowPath
2272 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002273 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002274 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002275 // We need a temporary register for the read barrier marking slow
2276 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002277 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2278 !Runtime::Current()->UseJitCompilation() &&
2279 !field_info.IsVolatile()) {
2280 // If link-time thunks for the Baker read barrier are enabled, for AOT
2281 // non-volatile loads we need a temporary only if the offset is too big.
2282 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2283 locations->AddTemp(FixedTempLocation());
2284 }
2285 } else {
2286 locations->AddTemp(Location::RequiresRegister());
2287 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002288 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002289 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002290 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002291 locations->SetOut(Location::RequiresFpuRegister());
2292 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002293 // The output overlaps for an object field get when read barriers
2294 // are enabled: we do not want the load to overwrite the object's
2295 // location, as we need it to emit the read barrier.
2296 locations->SetOut(
2297 Location::RequiresRegister(),
2298 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002299 }
2300}
2301
2302void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2303 const FieldInfo& field_info) {
2304 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002305 LocationSummary* locations = instruction->GetLocations();
2306 Location base_loc = locations->InAt(0);
2307 Location out = locations->Out();
2308 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002309 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002310 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002311
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002312 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
2313 field_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002314 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002315 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002316 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002317 Location maybe_temp =
2318 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002319 // Note that potential implicit null checks are handled in this
2320 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2321 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2322 instruction,
2323 out,
2324 base,
2325 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002326 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002327 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002328 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002329 } else {
2330 // General case.
2331 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002332 // Note that a potential implicit null check is handled in this
2333 // CodeGeneratorARM64::LoadAcquire call.
2334 // NB: LoadAcquire will record the pc info if needed.
2335 codegen_->LoadAcquire(
2336 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002337 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002338 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2339 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002340 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002341 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002342 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002343 if (field_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002344 // If read barriers are enabled, emit read barriers other than
2345 // Baker's using a slow path (and also unpoison the loaded
2346 // reference, if heap poisoning is enabled).
2347 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2348 }
Roland Levillain4d027112015-07-01 15:41:14 +01002349 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002350}
2351
2352void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2353 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002354 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002355 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002356 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2357 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002358 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002359 locations->SetInAt(1, Location::RequiresFpuRegister());
2360 } else {
2361 locations->SetInAt(1, Location::RequiresRegister());
2362 }
2363}
2364
2365void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002366 const FieldInfo& field_info,
2367 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002368 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2369
2370 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002371 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002372 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002373 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002374 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002375
Roland Levillain4d027112015-07-01 15:41:14 +01002376 {
2377 // We use a block to end the scratch scope before the write barrier, thus
2378 // freeing the temporary registers so they can be used in `MarkGCCard`.
2379 UseScratchRegisterScope temps(GetVIXLAssembler());
2380
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002381 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002382 DCHECK(value.IsW());
2383 Register temp = temps.AcquireW();
2384 __ Mov(temp, value.W());
2385 GetAssembler()->PoisonHeapReference(temp.W());
2386 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002387 }
Roland Levillain4d027112015-07-01 15:41:14 +01002388
2389 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002390 codegen_->StoreRelease(
2391 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002392 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002393 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2394 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002395 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2396 codegen_->MaybeRecordImplicitNullCheck(instruction);
2397 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002398 }
2399
2400 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002401 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002402 }
2403}
2404
Alexandre Rames67555f72014-11-18 10:55:16 +00002405void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002406 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002407
2408 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002409 case DataType::Type::kInt32:
2410 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002411 Register dst = OutputRegister(instr);
2412 Register lhs = InputRegisterAt(instr, 0);
2413 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002414 if (instr->IsAdd()) {
2415 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002416 } else if (instr->IsAnd()) {
2417 __ And(dst, lhs, rhs);
2418 } else if (instr->IsOr()) {
2419 __ Orr(dst, lhs, rhs);
2420 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002421 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002422 } else if (instr->IsRor()) {
2423 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002424 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002425 __ Ror(dst, lhs, shift);
2426 } else {
2427 // Ensure shift distance is in the same size register as the result. If
2428 // we are rotating a long and the shift comes in a w register originally,
2429 // we don't need to sxtw for use as an x since the shift distances are
2430 // all & reg_bits - 1.
2431 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2432 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002433 } else {
2434 DCHECK(instr->IsXor());
2435 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002436 }
2437 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002438 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002439 case DataType::Type::kFloat32:
2440 case DataType::Type::kFloat64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002441 FPRegister dst = OutputFPRegister(instr);
2442 FPRegister lhs = InputFPRegisterAt(instr, 0);
2443 FPRegister rhs = InputFPRegisterAt(instr, 1);
2444 if (instr->IsAdd()) {
2445 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002446 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002447 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002448 } else {
2449 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002450 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002451 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002452 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002453 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002454 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002455 }
2456}
2457
Serban Constantinescu02164b32014-11-13 14:05:07 +00002458void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2459 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2460
Vladimir Markoca6fff82017-10-03 14:49:14 +01002461 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002462 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002463 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002464 case DataType::Type::kInt32:
2465 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002466 locations->SetInAt(0, Location::RequiresRegister());
2467 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002469 break;
2470 }
2471 default:
2472 LOG(FATAL) << "Unexpected shift type " << type;
2473 }
2474}
2475
2476void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2477 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2478
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002479 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002480 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002481 case DataType::Type::kInt32:
2482 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002483 Register dst = OutputRegister(instr);
2484 Register lhs = InputRegisterAt(instr, 0);
2485 Operand rhs = InputOperandAt(instr, 1);
2486 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002487 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002488 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002489 if (instr->IsShl()) {
2490 __ Lsl(dst, lhs, shift_value);
2491 } else if (instr->IsShr()) {
2492 __ Asr(dst, lhs, shift_value);
2493 } else {
2494 __ Lsr(dst, lhs, shift_value);
2495 }
2496 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002497 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002498
2499 if (instr->IsShl()) {
2500 __ Lsl(dst, lhs, rhs_reg);
2501 } else if (instr->IsShr()) {
2502 __ Asr(dst, lhs, rhs_reg);
2503 } else {
2504 __ Lsr(dst, lhs, rhs_reg);
2505 }
2506 }
2507 break;
2508 }
2509 default:
2510 LOG(FATAL) << "Unexpected shift operation type " << type;
2511 }
2512}
2513
Alexandre Rames5319def2014-10-23 10:03:10 +01002514void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002515 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002516}
2517
2518void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002519 HandleBinaryOp(instruction);
2520}
2521
2522void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2523 HandleBinaryOp(instruction);
2524}
2525
2526void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2527 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002528}
2529
Artem Serov7fc63502016-02-09 17:15:29 +00002530void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002531 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002532 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002533 locations->SetInAt(0, Location::RequiresRegister());
2534 // There is no immediate variant of negated bitwise instructions in AArch64.
2535 locations->SetInAt(1, Location::RequiresRegister());
2536 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2537}
2538
Artem Serov7fc63502016-02-09 17:15:29 +00002539void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002540 Register dst = OutputRegister(instr);
2541 Register lhs = InputRegisterAt(instr, 0);
2542 Register rhs = InputRegisterAt(instr, 1);
2543
2544 switch (instr->GetOpKind()) {
2545 case HInstruction::kAnd:
2546 __ Bic(dst, lhs, rhs);
2547 break;
2548 case HInstruction::kOr:
2549 __ Orn(dst, lhs, rhs);
2550 break;
2551 case HInstruction::kXor:
2552 __ Eon(dst, lhs, rhs);
2553 break;
2554 default:
2555 LOG(FATAL) << "Unreachable";
2556 }
2557}
2558
Anton Kirilov74234da2017-01-13 14:42:47 +00002559void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2560 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002561 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2562 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002563 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002564 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002565 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2566 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2567 } else {
2568 locations->SetInAt(0, Location::RequiresRegister());
2569 }
2570 locations->SetInAt(1, Location::RequiresRegister());
2571 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2572}
2573
Anton Kirilov74234da2017-01-13 14:42:47 +00002574void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2575 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002576 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002577 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002578 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002579 Register out = OutputRegister(instruction);
2580 Register left;
2581 if (kind != HInstruction::kNeg) {
2582 left = InputRegisterAt(instruction, 0);
2583 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002584 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002585 // shifter operand operation, the IR generating `right_reg` (input to the type
2586 // conversion) can have a different type from the current instruction's type,
2587 // so we manually indicate the type.
2588 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002589 Operand right_operand(0);
2590
Anton Kirilov74234da2017-01-13 14:42:47 +00002591 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2592 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002593 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2594 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002595 right_operand = Operand(right_reg,
2596 helpers::ShiftFromOpKind(op_kind),
2597 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002598 }
2599
2600 // Logical binary operations do not support extension operations in the
2601 // operand. Note that VIXL would still manage if it was passed by generating
2602 // the extension as a separate instruction.
2603 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2604 DCHECK(!right_operand.IsExtendedRegister() ||
2605 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2606 kind != HInstruction::kNeg));
2607 switch (kind) {
2608 case HInstruction::kAdd:
2609 __ Add(out, left, right_operand);
2610 break;
2611 case HInstruction::kAnd:
2612 __ And(out, left, right_operand);
2613 break;
2614 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002615 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002616 __ Neg(out, right_operand);
2617 break;
2618 case HInstruction::kOr:
2619 __ Orr(out, left, right_operand);
2620 break;
2621 case HInstruction::kSub:
2622 __ Sub(out, left, right_operand);
2623 break;
2624 case HInstruction::kXor:
2625 __ Eor(out, left, right_operand);
2626 break;
2627 default:
2628 LOG(FATAL) << "Unexpected operation kind: " << kind;
2629 UNREACHABLE();
2630 }
2631}
2632
Artem Serov328429f2016-07-06 16:23:04 +01002633void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002634 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002635 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002636 locations->SetInAt(0, Location::RequiresRegister());
2637 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002638 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002639}
2640
Roland Levillain19c54192016-11-04 13:44:09 +00002641void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002642 __ Add(OutputRegister(instruction),
2643 InputRegisterAt(instruction, 0),
2644 Operand(InputOperandAt(instruction, 1)));
2645}
2646
Artem Serove1811ed2017-04-27 16:50:47 +01002647void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2648 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002649 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002650
2651 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2652
2653 locations->SetInAt(0, Location::RequiresRegister());
2654 // For byte case we don't need to shift the index variable so we can encode the data offset into
2655 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2656 // data offset constant generation out of the loop and reduce the critical path length in the
2657 // loop.
2658 locations->SetInAt(1, shift->GetValue() == 0
2659 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2660 : Location::RequiresRegister());
2661 locations->SetInAt(2, Location::ConstantLocation(shift));
2662 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2663}
2664
2665void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2666 HIntermediateAddressIndex* instruction) {
2667 Register index_reg = InputRegisterAt(instruction, 0);
2668 uint32_t shift = Int64ConstantFrom(instruction->GetLocations()->InAt(2));
2669 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2670
2671 if (shift == 0) {
2672 __ Add(OutputRegister(instruction), index_reg, offset);
2673 } else {
2674 Register offset_reg = InputRegisterAt(instruction, 1);
2675 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2676 }
2677}
2678
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002679void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002680 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002681 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002682 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2683 if (instr->GetOpKind() == HInstruction::kSub &&
2684 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002685 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002686 // Don't allocate register for Mneg instruction.
2687 } else {
2688 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2689 Location::RequiresRegister());
2690 }
2691 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2692 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002693 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2694}
2695
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002696void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002697 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002698 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2699 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002700
2701 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2702 // This fixup should be carried out for all multiply-accumulate instructions:
2703 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002704 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002705 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2706 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002707 vixl::aarch64::Instruction* prev =
2708 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002709 if (prev->IsLoadOrStore()) {
2710 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002711 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002712 __ nop();
2713 }
2714 }
2715
2716 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002717 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002718 __ Madd(res, mul_left, mul_right, accumulator);
2719 } else {
2720 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002721 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002722 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002723 __ Mneg(res, mul_left, mul_right);
2724 } else {
2725 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2726 __ Msub(res, mul_left, mul_right, accumulator);
2727 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002728 }
2729}
2730
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002731void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002732 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002733 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002734 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002735 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2736 object_array_get_with_read_barrier
2737 ? LocationSummary::kCallOnSlowPath
2738 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002739 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002740 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002741 // We need a temporary register for the read barrier marking slow
2742 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002743 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2744 !Runtime::Current()->UseJitCompilation() &&
2745 instruction->GetIndex()->IsConstant()) {
2746 // Array loads with constant index are treated as field loads.
2747 // If link-time thunks for the Baker read barrier are enabled, for AOT
2748 // constant index loads we need a temporary only if the offset is too big.
2749 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2750 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002751 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002752 if (offset >= kReferenceLoadMinFarOffset) {
2753 locations->AddTemp(FixedTempLocation());
2754 }
2755 } else {
2756 locations->AddTemp(Location::RequiresRegister());
2757 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002758 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002759 locations->SetInAt(0, Location::RequiresRegister());
2760 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002761 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002762 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2763 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002764 // The output overlaps in the case of an object array get with
2765 // read barriers enabled: we do not want the move to overwrite the
2766 // array's location, as we need it to emit the read barrier.
2767 locations->SetOut(
2768 Location::RequiresRegister(),
2769 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002770 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002771}
2772
2773void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002774 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002775 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002776 LocationSummary* locations = instruction->GetLocations();
2777 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002778 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002779 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002780 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2781 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002782 MacroAssembler* masm = GetVIXLAssembler();
2783 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002784
Roland Levillain19c54192016-11-04 13:44:09 +00002785 // The read barrier instrumentation of object ArrayGet instructions
2786 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002787 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002788 instruction->GetArray()->IsIntermediateAddress() &&
2789 kEmitCompilerReadBarrier));
2790
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002791 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002792 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002793 // Note that a potential implicit null check is handled in the
2794 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002795 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002796 if (index.IsConstant()) {
2797 // Array load with a constant index can be treated as a field load.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002798 offset += Int64ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002799 Location maybe_temp =
2800 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2801 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2802 out,
2803 obj.W(),
2804 offset,
2805 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002806 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002807 /* use_load_acquire */ false);
2808 } else {
2809 Register temp = WRegisterFrom(locations->GetTemp(0));
2810 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002811 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002812 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002813 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002814 // General case.
2815 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002816 Register length;
2817 if (maybe_compressed_char_at) {
2818 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2819 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002820 {
2821 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2822 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2823
2824 if (instruction->GetArray()->IsIntermediateAddress()) {
2825 DCHECK_LT(count_offset, offset);
2826 int64_t adjusted_offset =
2827 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2828 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2829 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2830 } else {
2831 __ Ldr(length, HeapOperand(obj, count_offset));
2832 }
2833 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002834 }
jessicahandojo05765752016-09-09 19:01:32 -07002835 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002836 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002837 if (maybe_compressed_char_at) {
2838 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002839 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2840 "Expecting 0=compressed, 1=uncompressed");
2841 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002842 __ Ldrb(Register(OutputCPURegister(instruction)),
2843 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2844 __ B(&done);
2845 __ Bind(&uncompressed_load);
2846 __ Ldrh(Register(OutputCPURegister(instruction)),
2847 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2848 __ Bind(&done);
2849 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002850 offset += Int64ConstantFrom(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002851 source = HeapOperand(obj, offset);
2852 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002853 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002854 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002855 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002856 // We do not need to compute the intermediate address from the array: the
2857 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002858 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002859 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002860 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002861 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2862 }
2863 temp = obj;
2864 } else {
2865 __ Add(temp, obj, offset);
2866 }
jessicahandojo05765752016-09-09 19:01:32 -07002867 if (maybe_compressed_char_at) {
2868 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002869 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2870 "Expecting 0=compressed, 1=uncompressed");
2871 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002872 __ Ldrb(Register(OutputCPURegister(instruction)),
2873 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2874 __ B(&done);
2875 __ Bind(&uncompressed_load);
2876 __ Ldrh(Register(OutputCPURegister(instruction)),
2877 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2878 __ Bind(&done);
2879 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002880 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002881 }
Roland Levillain44015862016-01-22 11:47:17 +00002882 }
jessicahandojo05765752016-09-09 19:01:32 -07002883 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002884 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2885 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002886 codegen_->Load(type, OutputCPURegister(instruction), source);
2887 codegen_->MaybeRecordImplicitNullCheck(instruction);
2888 }
Roland Levillain44015862016-01-22 11:47:17 +00002889
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002890 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002891 static_assert(
2892 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2893 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2894 Location obj_loc = locations->InAt(0);
2895 if (index.IsConstant()) {
2896 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2897 } else {
2898 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2899 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002900 }
Roland Levillain4d027112015-07-01 15:41:14 +01002901 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002902}
2903
Alexandre Rames5319def2014-10-23 10:03:10 +01002904void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002905 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002906 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002907 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002908}
2909
2910void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002911 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002912 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002913 {
2914 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2915 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2916 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2917 codegen_->MaybeRecordImplicitNullCheck(instruction);
2918 }
jessicahandojo05765752016-09-09 19:01:32 -07002919 // Mask out compression flag from String's array length.
2920 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002921 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002922 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002923}
2924
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002925void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002926 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002927
2928 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002929 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002930 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002931 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002932 LocationSummary::kCallOnSlowPath :
2933 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002934 locations->SetInAt(0, Location::RequiresRegister());
2935 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002936 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2937 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002938 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002939 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002940 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002941 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002942 }
2943}
2944
2945void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002946 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002947 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002948 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002949 bool needs_write_barrier =
2950 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002951
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002952 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002953 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002954 CPURegister source = value;
2955 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002956 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002957 MemOperand destination = HeapOperand(array);
2958 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002959
2960 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002961 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002962 if (index.IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002963 offset += Int64ConstantFrom(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002964 destination = HeapOperand(array, offset);
2965 } else {
2966 UseScratchRegisterScope temps(masm);
2967 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002968 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002969 // We do not need to compute the intermediate address from the array: the
2970 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002971 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002972 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002973 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002974 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2975 }
2976 temp = array;
2977 } else {
2978 __ Add(temp, array, offset);
2979 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002980 destination = HeapOperand(temp,
2981 XRegisterFrom(index),
2982 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002983 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002984 }
Artem Serov914d7a82017-02-07 14:33:49 +00002985 {
2986 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2987 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2988 codegen_->Store(value_type, value, destination);
2989 codegen_->MaybeRecordImplicitNullCheck(instruction);
2990 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002991 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002992 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002993 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002994 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002995 {
2996 // We use a block to end the scratch scope before the write barrier, thus
2997 // freeing the temporary registers so they can be used in `MarkGCCard`.
2998 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002999 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01003000 if (index.IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003001 offset += Int64ConstantFrom(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003002 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01003003 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01003004 destination = HeapOperand(temp,
3005 XRegisterFrom(index),
3006 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003007 DataType::SizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01003008 }
3009
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003010 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3011 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3012 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3013
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003014 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003015 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003016 codegen_->AddSlowPath(slow_path);
3017 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003018 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003019 __ Cbnz(Register(value), &non_zero);
3020 if (!index.IsConstant()) {
3021 __ Add(temp, array, offset);
3022 }
Artem Serov914d7a82017-02-07 14:33:49 +00003023 {
3024 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
3025 // emitted.
3026 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3027 __ Str(wzr, destination);
3028 codegen_->MaybeRecordImplicitNullCheck(instruction);
3029 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003030 __ B(&done);
3031 __ Bind(&non_zero);
3032 }
3033
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003034 // Note that when Baker read barriers are enabled, the type
3035 // checks are performed without read barriers. This is fine,
3036 // even in the case where a class object is in the from-space
3037 // after the flip, as a comparison involving such a type would
3038 // not produce a false positive; it may of course produce a
3039 // false negative, in which case we would take the ArraySet
3040 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003041
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003042 Register temp2 = temps.AcquireSameSizeAs(array);
3043 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003044 {
3045 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3046 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3047 __ Ldr(temp, HeapOperand(array, class_offset));
3048 codegen_->MaybeRecordImplicitNullCheck(instruction);
3049 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003050 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003051
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003052 // /* HeapReference<Class> */ temp = temp->component_type_
3053 __ Ldr(temp, HeapOperand(temp, component_offset));
3054 // /* HeapReference<Class> */ temp2 = value->klass_
3055 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3056 // If heap poisoning is enabled, no need to unpoison `temp`
3057 // nor `temp2`, as we are comparing two poisoned references.
3058 __ Cmp(temp, temp2);
3059 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003060
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003061 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3062 vixl::aarch64::Label do_put;
3063 __ B(eq, &do_put);
3064 // If heap poisoning is enabled, the `temp` reference has
3065 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003066 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3067
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003068 // /* HeapReference<Class> */ temp = temp->super_class_
3069 __ Ldr(temp, HeapOperand(temp, super_offset));
3070 // If heap poisoning is enabled, no need to unpoison
3071 // `temp`, as we are comparing against null below.
3072 __ Cbnz(temp, slow_path->GetEntryLabel());
3073 __ Bind(&do_put);
3074 } else {
3075 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003076 }
3077 }
3078
3079 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003080 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003081 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003082 __ Mov(temp2, value.W());
3083 GetAssembler()->PoisonHeapReference(temp2);
3084 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003085 }
3086
3087 if (!index.IsConstant()) {
3088 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003089 } else {
3090 // We no longer need the `temp` here so release it as the store below may
3091 // need a scratch register (if the constant index makes the offset too large)
3092 // and the poisoned `source` could be using the other scratch register.
3093 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003094 }
Artem Serov914d7a82017-02-07 14:33:49 +00003095 {
3096 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3097 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3098 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003099
Artem Serov914d7a82017-02-07 14:33:49 +00003100 if (!may_need_runtime_call_for_type_check) {
3101 codegen_->MaybeRecordImplicitNullCheck(instruction);
3102 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003103 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003104 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003105
3106 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3107
3108 if (done.IsLinked()) {
3109 __ Bind(&done);
3110 }
3111
3112 if (slow_path != nullptr) {
3113 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003114 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003115 }
3116}
3117
Alexandre Rames67555f72014-11-18 10:55:16 +00003118void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003119 RegisterSet caller_saves = RegisterSet::Empty();
3120 InvokeRuntimeCallingConvention calling_convention;
3121 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3122 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3123 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003124 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003125 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003126}
3127
3128void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003129 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003130 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003131 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003132 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3133 __ B(slow_path->GetEntryLabel(), hs);
3134}
3135
Alexandre Rames67555f72014-11-18 10:55:16 +00003136void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3137 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003138 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00003139 locations->SetInAt(0, Location::RequiresRegister());
3140 if (check->HasUses()) {
3141 locations->SetOut(Location::SameAsFirstInput());
3142 }
3143}
3144
3145void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3146 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003147 SlowPathCodeARM64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(
Alexandre Rames67555f72014-11-18 10:55:16 +00003148 check->GetLoadClass(), check, check->GetDexPc(), true);
3149 codegen_->AddSlowPath(slow_path);
3150 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3151}
3152
Roland Levillain1a653882016-03-18 18:05:57 +00003153static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3154 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3155 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3156}
3157
3158void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3159 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3160 Location rhs_loc = instruction->GetLocations()->InAt(1);
3161 if (rhs_loc.IsConstant()) {
3162 // 0.0 is the only immediate that can be encoded directly in
3163 // an FCMP instruction.
3164 //
3165 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3166 // specify that in a floating-point comparison, positive zero
3167 // and negative zero are considered equal, so we can use the
3168 // literal 0.0 for both cases here.
3169 //
3170 // Note however that some methods (Float.equal, Float.compare,
3171 // Float.compareTo, Double.equal, Double.compare,
3172 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3173 // StrictMath.min) consider 0.0 to be (strictly) greater than
3174 // -0.0. So if we ever translate calls to these methods into a
3175 // HCompare instruction, we must handle the -0.0 case with
3176 // care here.
3177 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3178 __ Fcmp(lhs_reg, 0.0);
3179 } else {
3180 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3181 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003182}
3183
Serban Constantinescu02164b32014-11-13 14:05:07 +00003184void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003185 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003186 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003187 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003188 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003189 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003190 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003191 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003192 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003193 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003194 case DataType::Type::kInt32:
3195 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003196 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003197 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003198 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3199 break;
3200 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003201 case DataType::Type::kFloat32:
3202 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003203 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003204 locations->SetInAt(1,
3205 IsFloatingPointZeroConstant(compare->InputAt(1))
3206 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3207 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003208 locations->SetOut(Location::RequiresRegister());
3209 break;
3210 }
3211 default:
3212 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3213 }
3214}
3215
3216void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003217 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00003218
3219 // 0 if: left == right
3220 // 1 if: left > right
3221 // -1 if: left < right
3222 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003223 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003224 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003225 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003226 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003227 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003228 case DataType::Type::kInt32:
3229 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003230 Register result = OutputRegister(compare);
3231 Register left = InputRegisterAt(compare, 0);
3232 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003233 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003234 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3235 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003236 break;
3237 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003238 case DataType::Type::kFloat32:
3239 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003240 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003241 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003242 __ Cset(result, ne);
3243 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003244 break;
3245 }
3246 default:
3247 LOG(FATAL) << "Unimplemented compare type " << in_type;
3248 }
3249}
3250
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003251void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003252 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003253
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003254 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003255 locations->SetInAt(0, Location::RequiresFpuRegister());
3256 locations->SetInAt(1,
3257 IsFloatingPointZeroConstant(instruction->InputAt(1))
3258 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3259 : Location::RequiresFpuRegister());
3260 } else {
3261 // Integer cases.
3262 locations->SetInAt(0, Location::RequiresRegister());
3263 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3264 }
3265
David Brazdilb3e773e2016-01-26 11:28:37 +00003266 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003267 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003268 }
3269}
3270
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003271void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003272 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003273 return;
3274 }
3275
3276 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003277 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003278 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003279
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003280 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003281 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003282 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003283 } else {
3284 // Integer cases.
3285 Register lhs = InputRegisterAt(instruction, 0);
3286 Operand rhs = InputOperandAt(instruction, 1);
3287 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003288 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003289 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003290}
3291
3292#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3293 M(Equal) \
3294 M(NotEqual) \
3295 M(LessThan) \
3296 M(LessThanOrEqual) \
3297 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003298 M(GreaterThanOrEqual) \
3299 M(Below) \
3300 M(BelowOrEqual) \
3301 M(Above) \
3302 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003303#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003304void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3305void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003306FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003307#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003308#undef FOR_EACH_CONDITION_INSTRUCTION
3309
Zheng Xuc6667102015-05-15 16:08:45 +08003310void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3311 DCHECK(instruction->IsDiv() || instruction->IsRem());
3312
3313 LocationSummary* locations = instruction->GetLocations();
3314 Location second = locations->InAt(1);
3315 DCHECK(second.IsConstant());
3316
3317 Register out = OutputRegister(instruction);
3318 Register dividend = InputRegisterAt(instruction, 0);
3319 int64_t imm = Int64FromConstant(second.GetConstant());
3320 DCHECK(imm == 1 || imm == -1);
3321
3322 if (instruction->IsRem()) {
3323 __ Mov(out, 0);
3324 } else {
3325 if (imm == 1) {
3326 __ Mov(out, dividend);
3327 } else {
3328 __ Neg(out, dividend);
3329 }
3330 }
3331}
3332
3333void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3334 DCHECK(instruction->IsDiv() || instruction->IsRem());
3335
3336 LocationSummary* locations = instruction->GetLocations();
3337 Location second = locations->InAt(1);
3338 DCHECK(second.IsConstant());
3339
3340 Register out = OutputRegister(instruction);
3341 Register dividend = InputRegisterAt(instruction, 0);
3342 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003343 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003344 int ctz_imm = CTZ(abs_imm);
3345
3346 UseScratchRegisterScope temps(GetVIXLAssembler());
3347 Register temp = temps.AcquireSameSizeAs(out);
3348
3349 if (instruction->IsDiv()) {
3350 __ Add(temp, dividend, abs_imm - 1);
3351 __ Cmp(dividend, 0);
3352 __ Csel(out, temp, dividend, lt);
3353 if (imm > 0) {
3354 __ Asr(out, out, ctz_imm);
3355 } else {
3356 __ Neg(out, Operand(out, ASR, ctz_imm));
3357 }
3358 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003359 int bits = instruction->GetResultType() == DataType::Type::kInt32 ? 32 : 64;
Zheng Xuc6667102015-05-15 16:08:45 +08003360 __ Asr(temp, dividend, bits - 1);
3361 __ Lsr(temp, temp, bits - ctz_imm);
3362 __ Add(out, dividend, temp);
3363 __ And(out, out, abs_imm - 1);
3364 __ Sub(out, out, temp);
3365 }
3366}
3367
3368void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3369 DCHECK(instruction->IsDiv() || instruction->IsRem());
3370
3371 LocationSummary* locations = instruction->GetLocations();
3372 Location second = locations->InAt(1);
3373 DCHECK(second.IsConstant());
3374
3375 Register out = OutputRegister(instruction);
3376 Register dividend = InputRegisterAt(instruction, 0);
3377 int64_t imm = Int64FromConstant(second.GetConstant());
3378
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003379 DataType::Type type = instruction->GetResultType();
3380 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003381
3382 int64_t magic;
3383 int shift;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003384 CalculateMagicAndShiftForDivRem(
3385 imm, type == DataType::Type::kInt64 /* is_long */, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003386
3387 UseScratchRegisterScope temps(GetVIXLAssembler());
3388 Register temp = temps.AcquireSameSizeAs(out);
3389
3390 // temp = get_high(dividend * magic)
3391 __ Mov(temp, magic);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003392 if (type == DataType::Type::kInt64) {
Zheng Xuc6667102015-05-15 16:08:45 +08003393 __ Smulh(temp, dividend, temp);
3394 } else {
3395 __ Smull(temp.X(), dividend, temp);
3396 __ Lsr(temp.X(), temp.X(), 32);
3397 }
3398
3399 if (imm > 0 && magic < 0) {
3400 __ Add(temp, temp, dividend);
3401 } else if (imm < 0 && magic > 0) {
3402 __ Sub(temp, temp, dividend);
3403 }
3404
3405 if (shift != 0) {
3406 __ Asr(temp, temp, shift);
3407 }
3408
3409 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003410 __ Sub(out, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003411 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003412 __ Sub(temp, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003413 // TODO: Strength reduction for msub.
3414 Register temp_imm = temps.AcquireSameSizeAs(out);
3415 __ Mov(temp_imm, imm);
3416 __ Msub(out, temp, temp_imm, dividend);
3417 }
3418}
3419
3420void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3421 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003422 DataType::Type type = instruction->GetResultType();
3423 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003424
3425 LocationSummary* locations = instruction->GetLocations();
3426 Register out = OutputRegister(instruction);
3427 Location second = locations->InAt(1);
3428
3429 if (second.IsConstant()) {
3430 int64_t imm = Int64FromConstant(second.GetConstant());
3431
3432 if (imm == 0) {
3433 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3434 } else if (imm == 1 || imm == -1) {
3435 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003436 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003437 DivRemByPowerOfTwo(instruction);
3438 } else {
3439 DCHECK(imm <= -2 || imm >= 2);
3440 GenerateDivRemWithAnyConstant(instruction);
3441 }
3442 } else {
3443 Register dividend = InputRegisterAt(instruction, 0);
3444 Register divisor = InputRegisterAt(instruction, 1);
3445 if (instruction->IsDiv()) {
3446 __ Sdiv(out, dividend, divisor);
3447 } else {
3448 UseScratchRegisterScope temps(GetVIXLAssembler());
3449 Register temp = temps.AcquireSameSizeAs(out);
3450 __ Sdiv(temp, dividend, divisor);
3451 __ Msub(out, temp, divisor, dividend);
3452 }
3453 }
3454}
3455
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003456void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3457 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003458 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003459 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003460 case DataType::Type::kInt32:
3461 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003462 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003463 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003464 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3465 break;
3466
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003467 case DataType::Type::kFloat32:
3468 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003469 locations->SetInAt(0, Location::RequiresFpuRegister());
3470 locations->SetInAt(1, Location::RequiresFpuRegister());
3471 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3472 break;
3473
3474 default:
3475 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3476 }
3477}
3478
3479void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003480 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003481 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003482 case DataType::Type::kInt32:
3483 case DataType::Type::kInt64:
Zheng Xuc6667102015-05-15 16:08:45 +08003484 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003485 break;
3486
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003487 case DataType::Type::kFloat32:
3488 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003489 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3490 break;
3491
3492 default:
3493 LOG(FATAL) << "Unexpected div type " << type;
3494 }
3495}
3496
Alexandre Rames67555f72014-11-18 10:55:16 +00003497void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003498 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003499 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003500}
3501
3502void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3503 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003504 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003505 codegen_->AddSlowPath(slow_path);
3506 Location value = instruction->GetLocations()->InAt(0);
3507
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003508 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003509
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003510 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003511 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003512 return;
3513 }
3514
Alexandre Rames67555f72014-11-18 10:55:16 +00003515 if (value.IsConstant()) {
3516 int64_t divisor = Int64ConstantFrom(value);
3517 if (divisor == 0) {
3518 __ B(slow_path->GetEntryLabel());
3519 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003520 // A division by a non-null constant is valid. We don't need to perform
3521 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003522 }
3523 } else {
3524 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3525 }
3526}
3527
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003528void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3529 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003530 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003531 locations->SetOut(Location::ConstantLocation(constant));
3532}
3533
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003534void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3535 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003536 // Will be generated at use site.
3537}
3538
Alexandre Rames5319def2014-10-23 10:03:10 +01003539void LocationsBuilderARM64::VisitExit(HExit* exit) {
3540 exit->SetLocations(nullptr);
3541}
3542
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003543void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003544}
3545
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003546void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3547 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003548 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003549 locations->SetOut(Location::ConstantLocation(constant));
3550}
3551
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003552void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003553 // Will be generated at use site.
3554}
3555
David Brazdilfc6a86a2015-06-26 10:33:45 +00003556void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003557 DCHECK(!successor->IsExitBlock());
3558 HBasicBlock* block = got->GetBlock();
3559 HInstruction* previous = got->GetPrevious();
3560 HLoopInformation* info = block->GetLoopInformation();
3561
David Brazdil46e2a392015-03-16 17:31:52 +00003562 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003563 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3564 return;
3565 }
3566 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3567 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01003568 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003569 }
3570 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003571 __ B(codegen_->GetLabelOf(successor));
3572 }
3573}
3574
David Brazdilfc6a86a2015-06-26 10:33:45 +00003575void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3576 got->SetLocations(nullptr);
3577}
3578
3579void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3580 HandleGoto(got, got->GetSuccessor());
3581}
3582
3583void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3584 try_boundary->SetLocations(nullptr);
3585}
3586
3587void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3588 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3589 if (!successor->IsExitBlock()) {
3590 HandleGoto(try_boundary, successor);
3591 }
3592}
3593
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003594void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003595 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003596 vixl::aarch64::Label* true_target,
3597 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003598 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003599
David Brazdil0debae72015-11-12 18:37:00 +00003600 if (true_target == nullptr && false_target == nullptr) {
3601 // Nothing to do. The code always falls through.
3602 return;
3603 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003604 // Constant condition, statically compared against "true" (integer value 1).
3605 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003606 if (true_target != nullptr) {
3607 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003608 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003609 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003610 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003611 if (false_target != nullptr) {
3612 __ B(false_target);
3613 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003614 }
David Brazdil0debae72015-11-12 18:37:00 +00003615 return;
3616 }
3617
3618 // The following code generates these patterns:
3619 // (1) true_target == nullptr && false_target != nullptr
3620 // - opposite condition true => branch to false_target
3621 // (2) true_target != nullptr && false_target == nullptr
3622 // - condition true => branch to true_target
3623 // (3) true_target != nullptr && false_target != nullptr
3624 // - condition true => branch to true_target
3625 // - branch to false_target
3626 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003627 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003628 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003629 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003630 if (true_target == nullptr) {
3631 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3632 } else {
3633 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3634 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003635 } else {
3636 // The condition instruction has not been materialized, use its inputs as
3637 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003638 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003639
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003640 DataType::Type type = condition->InputAt(0)->GetType();
3641 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003642 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003643 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003644 IfCondition opposite_condition = condition->GetOppositeCondition();
3645 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003646 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003647 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003648 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003649 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003650 // Integer cases.
3651 Register lhs = InputRegisterAt(condition, 0);
3652 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003653
3654 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003655 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003656 if (true_target == nullptr) {
3657 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3658 non_fallthrough_target = false_target;
3659 } else {
3660 arm64_cond = ARM64Condition(condition->GetCondition());
3661 non_fallthrough_target = true_target;
3662 }
3663
Aart Bik086d27e2016-01-20 17:02:00 -08003664 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003665 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003666 switch (arm64_cond) {
3667 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003668 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003669 break;
3670 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003671 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003672 break;
3673 case lt:
3674 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003675 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003676 break;
3677 case ge:
3678 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003679 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003680 break;
3681 default:
3682 // Without the `static_cast` the compiler throws an error for
3683 // `-Werror=sign-promo`.
3684 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3685 }
3686 } else {
3687 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003688 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003689 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003690 }
3691 }
David Brazdil0debae72015-11-12 18:37:00 +00003692
3693 // If neither branch falls through (case 3), the conditional branch to `true_target`
3694 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3695 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003696 __ B(false_target);
3697 }
3698}
3699
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003700void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003701 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003702 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003703 locations->SetInAt(0, Location::RequiresRegister());
3704 }
3705}
3706
3707void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003708 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3709 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003710 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3711 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3712 true_target = nullptr;
3713 }
3714 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3715 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3716 false_target = nullptr;
3717 }
David Brazdil0debae72015-11-12 18:37:00 +00003718 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003719}
3720
3721void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003722 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003723 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003724 InvokeRuntimeCallingConvention calling_convention;
3725 RegisterSet caller_saves = RegisterSet::Empty();
3726 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3727 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003728 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003729 locations->SetInAt(0, Location::RequiresRegister());
3730 }
3731}
3732
3733void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003734 SlowPathCodeARM64* slow_path =
3735 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003736 GenerateTestAndBranch(deoptimize,
3737 /* condition_input_index */ 0,
3738 slow_path->GetEntryLabel(),
3739 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003740}
3741
Mingyao Yang063fc772016-08-02 11:02:54 -07003742void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003743 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003744 LocationSummary(flag, LocationSummary::kNoCall);
3745 locations->SetOut(Location::RequiresRegister());
3746}
3747
3748void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3749 __ Ldr(OutputRegister(flag),
3750 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3751}
3752
David Brazdilc0b601b2016-02-08 14:20:45 +00003753static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3754 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003755 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003756}
3757
Alexandre Rames880f1192016-06-13 16:04:50 +01003758static inline Condition GetConditionForSelect(HCondition* condition) {
3759 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003760 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3761 : ARM64Condition(cond);
3762}
3763
David Brazdil74eb1b22015-12-14 11:44:01 +00003764void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003765 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003766 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003767 locations->SetInAt(0, Location::RequiresFpuRegister());
3768 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003769 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003770 } else {
3771 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3772 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3773 bool is_true_value_constant = cst_true_value != nullptr;
3774 bool is_false_value_constant = cst_false_value != nullptr;
3775 // Ask VIXL whether we should synthesize constants in registers.
3776 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3777 Operand true_op = is_true_value_constant ?
3778 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3779 Operand false_op = is_false_value_constant ?
3780 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3781 bool true_value_in_register = false;
3782 bool false_value_in_register = false;
3783 MacroAssembler::GetCselSynthesisInformation(
3784 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3785 true_value_in_register |= !is_true_value_constant;
3786 false_value_in_register |= !is_false_value_constant;
3787
3788 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3789 : Location::ConstantLocation(cst_true_value));
3790 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3791 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003792 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003793 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003794
David Brazdil74eb1b22015-12-14 11:44:01 +00003795 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3796 locations->SetInAt(2, Location::RequiresRegister());
3797 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003798}
3799
3800void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003801 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003802 Condition csel_cond;
3803
3804 if (IsBooleanValueOrMaterializedCondition(cond)) {
3805 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003806 // Use the condition flags set by the previous instruction.
3807 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003808 } else {
3809 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003810 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003811 }
3812 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003813 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003814 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003815 } else {
3816 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003817 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003818 }
3819
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003820 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003821 __ Fcsel(OutputFPRegister(select),
3822 InputFPRegisterAt(select, 1),
3823 InputFPRegisterAt(select, 0),
3824 csel_cond);
3825 } else {
3826 __ Csel(OutputRegister(select),
3827 InputOperandAt(select, 1),
3828 InputOperandAt(select, 0),
3829 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003830 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003831}
3832
David Srbecky0cf44932015-12-09 14:09:59 +00003833void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003834 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003835}
3836
David Srbeckyd28f4a02016-03-14 17:14:24 +00003837void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3838 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003839}
3840
3841void CodeGeneratorARM64::GenerateNop() {
3842 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003843}
3844
Alexandre Rames5319def2014-10-23 10:03:10 +01003845void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003846 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003847}
3848
3849void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003850 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003851}
3852
3853void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003854 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003855}
3856
3857void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003858 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003859}
3860
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003861// Temp is used for read barrier.
3862static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3863 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003864 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003865 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3866 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3867 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3868 return 1;
3869 }
3870 return 0;
3871}
3872
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003873// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003874// interface pointer, one for loading the current interface.
3875// The other checks have one temp for loading the object's class.
3876static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3877 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3878 return 3;
3879 }
3880 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003881}
3882
Alexandre Rames67555f72014-11-18 10:55:16 +00003883void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003884 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003885 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003886 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003887 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003888 case TypeCheckKind::kExactCheck:
3889 case TypeCheckKind::kAbstractClassCheck:
3890 case TypeCheckKind::kClassHierarchyCheck:
3891 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003892 call_kind =
3893 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003894 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003895 break;
3896 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003897 case TypeCheckKind::kUnresolvedCheck:
3898 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003899 call_kind = LocationSummary::kCallOnSlowPath;
3900 break;
3901 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003902
Vladimir Markoca6fff82017-10-03 14:49:14 +01003903 LocationSummary* locations =
3904 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003905 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003906 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003907 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003908 locations->SetInAt(0, Location::RequiresRegister());
3909 locations->SetInAt(1, Location::RequiresRegister());
3910 // The "out" register is used as a temporary, so it overlaps with the inputs.
3911 // Note that TypeCheckSlowPathARM64 uses this register too.
3912 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003913 // Add temps if necessary for read barriers.
3914 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003915}
3916
3917void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003918 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003919 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003920 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003921 Register obj = InputRegisterAt(instruction, 0);
3922 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003923 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003924 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003925 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3926 DCHECK_LE(num_temps, 1u);
3927 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003928 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3929 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3930 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3931 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003932
Scott Wakeling97c72b72016-06-24 16:19:36 +01003933 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003934 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003935
3936 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003937 // Avoid null check if we know `obj` is not null.
3938 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003939 __ Cbz(obj, &zero);
3940 }
3941
Roland Levillain44015862016-01-22 11:47:17 +00003942 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003943 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003944 // /* HeapReference<Class> */ out = obj->klass_
3945 GenerateReferenceLoadTwoRegisters(instruction,
3946 out_loc,
3947 obj_loc,
3948 class_offset,
3949 maybe_temp_loc,
3950 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003951 __ Cmp(out, cls);
3952 __ Cset(out, eq);
3953 if (zero.IsLinked()) {
3954 __ B(&done);
3955 }
3956 break;
3957 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003958
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003959 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003960 // /* HeapReference<Class> */ out = obj->klass_
3961 GenerateReferenceLoadTwoRegisters(instruction,
3962 out_loc,
3963 obj_loc,
3964 class_offset,
3965 maybe_temp_loc,
3966 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003967 // If the class is abstract, we eagerly fetch the super class of the
3968 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003969 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003970 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003971 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003972 GenerateReferenceLoadOneRegister(instruction,
3973 out_loc,
3974 super_offset,
3975 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003976 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003977 // If `out` is null, we use it for the result, and jump to `done`.
3978 __ Cbz(out, &done);
3979 __ Cmp(out, cls);
3980 __ B(ne, &loop);
3981 __ Mov(out, 1);
3982 if (zero.IsLinked()) {
3983 __ B(&done);
3984 }
3985 break;
3986 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003987
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003988 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003989 // /* HeapReference<Class> */ out = obj->klass_
3990 GenerateReferenceLoadTwoRegisters(instruction,
3991 out_loc,
3992 obj_loc,
3993 class_offset,
3994 maybe_temp_loc,
3995 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003996 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003997 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003998 __ Bind(&loop);
3999 __ Cmp(out, cls);
4000 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004001 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004002 GenerateReferenceLoadOneRegister(instruction,
4003 out_loc,
4004 super_offset,
4005 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004006 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004007 __ Cbnz(out, &loop);
4008 // If `out` is null, we use it for the result, and jump to `done`.
4009 __ B(&done);
4010 __ Bind(&success);
4011 __ Mov(out, 1);
4012 if (zero.IsLinked()) {
4013 __ B(&done);
4014 }
4015 break;
4016 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004017
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004018 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004019 // /* HeapReference<Class> */ out = obj->klass_
4020 GenerateReferenceLoadTwoRegisters(instruction,
4021 out_loc,
4022 obj_loc,
4023 class_offset,
4024 maybe_temp_loc,
4025 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004026 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004027 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004028 __ Cmp(out, cls);
4029 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004030 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004031 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004032 GenerateReferenceLoadOneRegister(instruction,
4033 out_loc,
4034 component_offset,
4035 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004036 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004037 // If `out` is null, we use it for the result, and jump to `done`.
4038 __ Cbz(out, &done);
4039 __ Ldrh(out, HeapOperand(out, primitive_offset));
4040 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4041 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004042 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004043 __ Mov(out, 1);
4044 __ B(&done);
4045 break;
4046 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004047
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004048 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004049 // No read barrier since the slow path will retry upon failure.
4050 // /* HeapReference<Class> */ out = obj->klass_
4051 GenerateReferenceLoadTwoRegisters(instruction,
4052 out_loc,
4053 obj_loc,
4054 class_offset,
4055 maybe_temp_loc,
4056 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004057 __ Cmp(out, cls);
4058 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004059 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4060 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004061 codegen_->AddSlowPath(slow_path);
4062 __ B(ne, slow_path->GetEntryLabel());
4063 __ Mov(out, 1);
4064 if (zero.IsLinked()) {
4065 __ B(&done);
4066 }
4067 break;
4068 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004069
Calin Juravle98893e12015-10-02 21:05:03 +01004070 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004071 case TypeCheckKind::kInterfaceCheck: {
4072 // Note that we indeed only call on slow path, but we always go
4073 // into the slow path for the unresolved and interface check
4074 // cases.
4075 //
4076 // We cannot directly call the InstanceofNonTrivial runtime
4077 // entry point without resorting to a type checking slow path
4078 // here (i.e. by calling InvokeRuntime directly), as it would
4079 // require to assign fixed registers for the inputs of this
4080 // HInstanceOf instruction (following the runtime calling
4081 // convention), which might be cluttered by the potential first
4082 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004083 //
4084 // TODO: Introduce a new runtime entry point taking the object
4085 // to test (instead of its class) as argument, and let it deal
4086 // with the read barrier issues. This will let us refactor this
4087 // case of the `switch` code as it was previously (with a direct
4088 // call to the runtime not using a type checking slow path).
4089 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004090 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004091 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4092 instruction, /* is_fatal */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004093 codegen_->AddSlowPath(slow_path);
4094 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004095 if (zero.IsLinked()) {
4096 __ B(&done);
4097 }
4098 break;
4099 }
4100 }
4101
4102 if (zero.IsLinked()) {
4103 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004104 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004105 }
4106
4107 if (done.IsLinked()) {
4108 __ Bind(&done);
4109 }
4110
4111 if (slow_path != nullptr) {
4112 __ Bind(slow_path->GetExitLabel());
4113 }
4114}
4115
4116void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4117 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4118 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4119
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004120 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4121 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004122 case TypeCheckKind::kExactCheck:
4123 case TypeCheckKind::kAbstractClassCheck:
4124 case TypeCheckKind::kClassHierarchyCheck:
4125 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004126 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4127 LocationSummary::kCallOnSlowPath :
4128 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004129 break;
4130 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004131 case TypeCheckKind::kUnresolvedCheck:
4132 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004133 call_kind = LocationSummary::kCallOnSlowPath;
4134 break;
4135 }
4136
Vladimir Markoca6fff82017-10-03 14:49:14 +01004137 LocationSummary* locations =
4138 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004139 locations->SetInAt(0, Location::RequiresRegister());
4140 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004141 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4142 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004143}
4144
4145void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004146 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004147 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004148 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004149 Register obj = InputRegisterAt(instruction, 0);
4150 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004151 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4152 DCHECK_GE(num_temps, 1u);
4153 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004154 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004155 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4156 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004157 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004158 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4159 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4160 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4161 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4162 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4163 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4164 const uint32_t object_array_data_offset =
4165 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004166
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004167 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004168 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4169 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4170 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004171 if (!kEmitCompilerReadBarrier) {
4172 is_type_check_slow_path_fatal =
4173 (type_check_kind == TypeCheckKind::kExactCheck ||
4174 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4175 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4176 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4177 !instruction->CanThrowIntoCatchBlock();
4178 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004179 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004180 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4181 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004182 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004183
Scott Wakeling97c72b72016-06-24 16:19:36 +01004184 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004185 // Avoid null check if we know obj is not null.
4186 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004187 __ Cbz(obj, &done);
4188 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004189
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004190 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004191 case TypeCheckKind::kExactCheck:
4192 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004193 // /* HeapReference<Class> */ temp = obj->klass_
4194 GenerateReferenceLoadTwoRegisters(instruction,
4195 temp_loc,
4196 obj_loc,
4197 class_offset,
4198 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004199 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004200
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004201 __ Cmp(temp, cls);
4202 // Jump to slow path for throwing the exception or doing a
4203 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004204 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004205 break;
4206 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004207
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004208 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004209 // /* HeapReference<Class> */ temp = obj->klass_
4210 GenerateReferenceLoadTwoRegisters(instruction,
4211 temp_loc,
4212 obj_loc,
4213 class_offset,
4214 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004215 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004216
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004217 // If the class is abstract, we eagerly fetch the super class of the
4218 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004219 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004220 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004221 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004222 GenerateReferenceLoadOneRegister(instruction,
4223 temp_loc,
4224 super_offset,
4225 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004226 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004227
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004228 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4229 // exception.
4230 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4231 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004232 __ Cmp(temp, cls);
4233 __ B(ne, &loop);
4234 break;
4235 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004236
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004237 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004238 // /* HeapReference<Class> */ temp = obj->klass_
4239 GenerateReferenceLoadTwoRegisters(instruction,
4240 temp_loc,
4241 obj_loc,
4242 class_offset,
4243 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004244 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004245
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004246 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004247 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004248 __ Bind(&loop);
4249 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004250 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004251
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004252 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004253 GenerateReferenceLoadOneRegister(instruction,
4254 temp_loc,
4255 super_offset,
4256 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004257 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004258
4259 // If the class reference currently in `temp` is not null, jump
4260 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004261 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004262 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004263 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004264 break;
4265 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004266
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004267 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004268 // /* HeapReference<Class> */ temp = obj->klass_
4269 GenerateReferenceLoadTwoRegisters(instruction,
4270 temp_loc,
4271 obj_loc,
4272 class_offset,
4273 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004274 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004275
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004276 // Do an exact check.
4277 __ Cmp(temp, cls);
4278 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004279
4280 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004281 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004282 GenerateReferenceLoadOneRegister(instruction,
4283 temp_loc,
4284 component_offset,
4285 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004286 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004287
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004288 // If the component type is null, jump to the slow path to throw the exception.
4289 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4290 // Otherwise, the object is indeed an array. Further check that this component type is not a
4291 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004292 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4293 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004294 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004295 break;
4296 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004297
Calin Juravle98893e12015-10-02 21:05:03 +01004298 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004299 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004300 //
4301 // We cannot directly call the CheckCast runtime entry point
4302 // without resorting to a type checking slow path here (i.e. by
4303 // calling InvokeRuntime directly), as it would require to
4304 // assign fixed registers for the inputs of this HInstanceOf
4305 // instruction (following the runtime calling convention), which
4306 // might be cluttered by the potential first read barrier
4307 // emission at the beginning of this method.
4308 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004309 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004310 case TypeCheckKind::kInterfaceCheck: {
4311 // /* HeapReference<Class> */ temp = obj->klass_
4312 GenerateReferenceLoadTwoRegisters(instruction,
4313 temp_loc,
4314 obj_loc,
4315 class_offset,
4316 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004317 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004318
4319 // /* HeapReference<Class> */ temp = temp->iftable_
4320 GenerateReferenceLoadTwoRegisters(instruction,
4321 temp_loc,
4322 temp_loc,
4323 iftable_offset,
4324 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004325 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004326 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004327 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004328 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004329 vixl::aarch64::Label start_loop;
4330 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004331 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004332 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4333 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004334 // Go to next interface.
4335 __ Add(temp, temp, 2 * kHeapReferenceSize);
4336 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004337 // Compare the classes and continue the loop if they do not match.
4338 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4339 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004340 break;
4341 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004342 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004343 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004344
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004345 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004346}
4347
Alexandre Rames5319def2014-10-23 10:03:10 +01004348void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004349 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004350 locations->SetOut(Location::ConstantLocation(constant));
4351}
4352
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004353void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004354 // Will be generated at use site.
4355}
4356
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004357void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004358 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004359 locations->SetOut(Location::ConstantLocation(constant));
4360}
4361
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004362void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004363 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004364}
4365
Calin Juravle175dc732015-08-25 15:42:32 +01004366void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4367 // The trampoline uses the same calling convention as dex calling conventions,
4368 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4369 // the method_idx.
4370 HandleInvoke(invoke);
4371}
4372
4373void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4374 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004375 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004376}
4377
Alexandre Rames5319def2014-10-23 10:03:10 +01004378void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004379 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004380 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004381}
4382
Alexandre Rames67555f72014-11-18 10:55:16 +00004383void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4384 HandleInvoke(invoke);
4385}
4386
4387void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4388 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004389 LocationSummary* locations = invoke->GetLocations();
4390 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004391 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004392 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004393 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004394
4395 // The register ip1 is required to be used for the hidden argument in
4396 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004397 MacroAssembler* masm = GetVIXLAssembler();
4398 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004399 scratch_scope.Exclude(ip1);
4400 __ Mov(ip1, invoke->GetDexMethodIndex());
4401
Artem Serov914d7a82017-02-07 14:33:49 +00004402 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004403 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004404 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004405 {
4406 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4407 // /* HeapReference<Class> */ temp = temp->klass_
4408 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4409 codegen_->MaybeRecordImplicitNullCheck(invoke);
4410 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004411 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004412 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004413 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004414 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004415 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004416 }
Artem Serov914d7a82017-02-07 14:33:49 +00004417
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004418 // Instead of simply (possibly) unpoisoning `temp` here, we should
4419 // emit a read barrier for the previous class reference load.
4420 // However this is not required in practice, as this is an
4421 // intermediate/temporary reference and because the current
4422 // concurrent copying collector keeps the from-space memory
4423 // intact/accessible until the end of the marking phase (the
4424 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004425 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004426 __ Ldr(temp,
4427 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4428 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004429 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004430 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004431 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004432 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004433 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004434
4435 {
4436 // Ensure the pc position is recorded immediately after the `blr` instruction.
4437 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4438
4439 // lr();
4440 __ blr(lr);
4441 DCHECK(!codegen_->IsLeafMethod());
4442 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4443 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004444
4445 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004446}
4447
4448void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004449 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004450 if (intrinsic.TryDispatch(invoke)) {
4451 return;
4452 }
4453
Alexandre Rames67555f72014-11-18 10:55:16 +00004454 HandleInvoke(invoke);
4455}
4456
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004457void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004458 // Explicit clinit checks triggered by static invokes must have been pruned by
4459 // art::PrepareForRegisterAllocation.
4460 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004461
Vladimir Markoca6fff82017-10-03 14:49:14 +01004462 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004463 if (intrinsic.TryDispatch(invoke)) {
4464 return;
4465 }
4466
Alexandre Rames67555f72014-11-18 10:55:16 +00004467 HandleInvoke(invoke);
4468}
4469
Andreas Gampe878d58c2015-01-15 23:24:00 -08004470static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4471 if (invoke->GetLocations()->Intrinsified()) {
4472 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4473 intrinsic.Dispatch(invoke);
4474 return true;
4475 }
4476 return false;
4477}
4478
Vladimir Markodc151b22015-10-15 18:02:30 +01004479HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4480 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004481 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004482 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004483 return desired_dispatch_info;
4484}
4485
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004486void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4487 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004488 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004489 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4490 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004491 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4492 uint32_t offset =
4493 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004494 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004495 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004496 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004497 }
Vladimir Marko58155012015-08-19 12:49:41 +00004498 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004499 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004500 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004501 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4502 DCHECK(GetCompilerOptions().IsBootImage());
4503 // Add ADRP with its PC-relative method patch.
4504 vixl::aarch64::Label* adrp_label = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
4505 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4506 // Add ADD with its PC-relative method patch.
4507 vixl::aarch64::Label* add_label =
4508 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), adrp_label);
4509 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4510 break;
4511 }
Vladimir Marko58155012015-08-19 12:49:41 +00004512 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4513 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004514 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004515 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004516 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko58155012015-08-19 12:49:41 +00004517 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004518 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4519 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004520 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004521 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004522 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004523 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004524 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004525 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004526 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004527 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4528 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4529 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004530 }
4531 }
4532
4533 switch (invoke->GetCodePtrLocation()) {
4534 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004535 {
4536 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4537 ExactAssemblyScope eas(GetVIXLAssembler(),
4538 kInstructionSize,
4539 CodeBufferCheckScope::kExactSize);
4540 __ bl(&frame_entry_label_);
4541 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4542 }
Vladimir Marko58155012015-08-19 12:49:41 +00004543 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004544 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4545 // LR = callee_method->entry_point_from_quick_compiled_code_;
4546 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004547 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004548 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004549 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004550 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004551 ExactAssemblyScope eas(GetVIXLAssembler(),
4552 kInstructionSize,
4553 CodeBufferCheckScope::kExactSize);
4554 // lr()
4555 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004556 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004557 }
Vladimir Marko58155012015-08-19 12:49:41 +00004558 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004559 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004560
Andreas Gampe878d58c2015-01-15 23:24:00 -08004561 DCHECK(!IsLeafMethod());
4562}
4563
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004564void CodeGeneratorARM64::GenerateVirtualCall(
4565 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004566 // Use the calling convention instead of the location of the receiver, as
4567 // intrinsics may have put the receiver in a different register. In the intrinsics
4568 // slow path, the arguments have been moved to the right place, so here we are
4569 // guaranteed that the receiver is the first register of the calling convention.
4570 InvokeDexCallingConvention calling_convention;
4571 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004572 Register temp = XRegisterFrom(temp_in);
4573 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4574 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4575 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004576 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004577
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004578 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004579
4580 {
4581 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4582 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4583 // /* HeapReference<Class> */ temp = receiver->klass_
4584 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4585 MaybeRecordImplicitNullCheck(invoke);
4586 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004587 // Instead of simply (possibly) unpoisoning `temp` here, we should
4588 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004589 // intermediate/temporary reference and because the current
4590 // concurrent copying collector keeps the from-space memory
4591 // intact/accessible until the end of the marking phase (the
4592 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004593 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4594 // temp = temp->GetMethodAt(method_offset);
4595 __ Ldr(temp, MemOperand(temp, method_offset));
4596 // lr = temp->GetEntryPoint();
4597 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004598 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004599 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004600 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4601 // lr();
4602 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004603 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004604 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004605}
4606
Orion Hodsonac141392017-01-13 11:53:47 +00004607void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4608 HandleInvoke(invoke);
4609}
4610
4611void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4612 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004613 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004614}
4615
Vladimir Marko65979462017-05-19 17:25:12 +01004616vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeMethodPatch(
4617 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004618 vixl::aarch64::Label* adrp_label) {
Vladimir Marko65979462017-05-19 17:25:12 +01004619 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004620 target_method.index,
Vladimir Marko65979462017-05-19 17:25:12 +01004621 adrp_label,
4622 &pc_relative_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004623}
4624
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004625vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4626 MethodReference target_method,
4627 vixl::aarch64::Label* adrp_label) {
4628 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004629 target_method.index,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004630 adrp_label,
4631 &method_bss_entry_patches_);
4632}
4633
Scott Wakeling97c72b72016-06-24 16:19:36 +01004634vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4635 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004636 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004637 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004638 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004639}
4640
Vladimir Marko1998cd02017-01-13 13:02:58 +00004641vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4642 const DexFile& dex_file,
4643 dex::TypeIndex type_index,
4644 vixl::aarch64::Label* adrp_label) {
4645 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4646}
4647
Vladimir Marko65979462017-05-19 17:25:12 +01004648vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4649 const DexFile& dex_file,
4650 dex::StringIndex string_index,
4651 vixl::aarch64::Label* adrp_label) {
4652 return
4653 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
4654}
4655
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004656vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4657 const DexFile& dex_file,
4658 dex::StringIndex string_index,
4659 vixl::aarch64::Label* adrp_label) {
4660 return NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
4661}
4662
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004663vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4664 baker_read_barrier_patches_.emplace_back(custom_data);
4665 return &baker_read_barrier_patches_.back().label;
4666}
4667
Scott Wakeling97c72b72016-06-24 16:19:36 +01004668vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4669 const DexFile& dex_file,
4670 uint32_t offset_or_index,
4671 vixl::aarch64::Label* adrp_label,
4672 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004673 // Add a patch entry and return the label.
4674 patches->emplace_back(dex_file, offset_or_index);
4675 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004676 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004677 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4678 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4679 return label;
4680}
4681
Scott Wakeling97c72b72016-06-24 16:19:36 +01004682vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4683 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004684 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004685}
4686
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004687vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004688 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004689 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004690 return jit_string_patches_.GetOrCreate(
4691 StringReference(&dex_file, string_index),
4692 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4693}
4694
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004695vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004696 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004697 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004698 return jit_class_patches_.GetOrCreate(
4699 TypeReference(&dex_file, type_index),
4700 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4701}
4702
Vladimir Markoaad75c62016-10-03 08:46:48 +00004703void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4704 vixl::aarch64::Register reg) {
4705 DCHECK(reg.IsX());
4706 SingleEmissionCheckScope guard(GetVIXLAssembler());
4707 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004708 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004709}
4710
4711void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4712 vixl::aarch64::Register out,
4713 vixl::aarch64::Register base) {
4714 DCHECK(out.IsX());
4715 DCHECK(base.IsX());
4716 SingleEmissionCheckScope guard(GetVIXLAssembler());
4717 __ Bind(fixup_label);
4718 __ add(out, base, Operand(/* offset placeholder */ 0));
4719}
4720
4721void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4722 vixl::aarch64::Register out,
4723 vixl::aarch64::Register base) {
4724 DCHECK(base.IsX());
4725 SingleEmissionCheckScope guard(GetVIXLAssembler());
4726 __ Bind(fixup_label);
4727 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4728}
4729
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004730template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004731inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4732 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004733 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004734 for (const PcRelativePatchInfo& info : infos) {
4735 linker_patches->push_back(Factory(info.label.GetLocation(),
4736 &info.target_dex_file,
4737 info.pc_insn_label->GetLocation(),
4738 info.offset_or_index));
4739 }
4740}
4741
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004742void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004743 DCHECK(linker_patches->empty());
4744 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004745 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004746 method_bss_entry_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004747 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004748 type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004749 pc_relative_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004750 string_bss_entry_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004751 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004752 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01004753 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004754 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
4755 pc_relative_method_patches_, linker_patches);
4756 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
4757 pc_relative_type_patches_, linker_patches);
4758 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
4759 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004760 } else {
4761 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004762 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
4763 pc_relative_type_patches_, linker_patches);
4764 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
4765 pc_relative_string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004766 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004767 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4768 method_bss_entry_patches_, linker_patches);
4769 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4770 type_bss_entry_patches_, linker_patches);
4771 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4772 string_bss_entry_patches_, linker_patches);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004773 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004774 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
4775 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004776 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004777 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004778}
4779
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004780vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4781 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004782 value,
4783 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4784}
4785
Scott Wakeling97c72b72016-06-24 16:19:36 +01004786vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004787 return uint64_literals_.GetOrCreate(
4788 value,
4789 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004790}
4791
Andreas Gampe878d58c2015-01-15 23:24:00 -08004792void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004793 // Explicit clinit checks triggered by static invokes must have been pruned by
4794 // art::PrepareForRegisterAllocation.
4795 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004796
Andreas Gampe878d58c2015-01-15 23:24:00 -08004797 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004798 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004799 return;
4800 }
4801
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004802 {
4803 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4804 // are no pools emitted.
4805 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4806 LocationSummary* locations = invoke->GetLocations();
4807 codegen_->GenerateStaticOrDirectCall(
4808 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
4809 }
4810
4811 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004812}
4813
4814void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004815 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004816 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004817 return;
4818 }
4819
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004820 {
4821 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4822 // are no pools emitted.
4823 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4824 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
4825 DCHECK(!codegen_->IsLeafMethod());
4826 }
4827
4828 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004829}
4830
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004831HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4832 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004833 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004834 case HLoadClass::LoadKind::kInvalid:
4835 LOG(FATAL) << "UNREACHABLE";
4836 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004837 case HLoadClass::LoadKind::kReferrersClass:
4838 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004839 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004840 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004841 case HLoadClass::LoadKind::kBssEntry:
4842 DCHECK(!Runtime::Current()->UseJitCompilation());
4843 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004844 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004845 DCHECK(Runtime::Current()->UseJitCompilation());
4846 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004847 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004848 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004849 break;
4850 }
4851 return desired_class_load_kind;
4852}
4853
Alexandre Rames67555f72014-11-18 10:55:16 +00004854void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004855 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004856 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004857 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004858 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004859 cls,
4860 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004861 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004862 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004863 return;
4864 }
Vladimir Marko41559982017-01-06 14:04:23 +00004865 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004866
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004867 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4868 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004869 ? LocationSummary::kCallOnSlowPath
4870 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004871 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004872 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004873 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004874 }
4875
Vladimir Marko41559982017-01-06 14:04:23 +00004876 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004877 locations->SetInAt(0, Location::RequiresRegister());
4878 }
4879 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004880 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4881 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4882 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004883 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004884 RegisterSet caller_saves = RegisterSet::Empty();
4885 InvokeRuntimeCallingConvention calling_convention;
4886 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4887 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004888 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
4889 DataType::Type::kReference).GetCode());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004890 locations->SetCustomSlowPathCallerSaves(caller_saves);
4891 } else {
4892 // For non-Baker read barrier we have a temp-clobbering call.
4893 }
4894 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004895}
4896
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004897// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4898// move.
4899void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004900 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004901 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00004902 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004903 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01004904 return;
4905 }
Vladimir Marko41559982017-01-06 14:04:23 +00004906 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004907
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004908 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004909 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004910 Register bss_entry_temp;
4911 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004912
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004913 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4914 ? kWithoutReadBarrier
4915 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004916 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004917 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004918 case HLoadClass::LoadKind::kReferrersClass: {
4919 DCHECK(!cls->CanCallRuntime());
4920 DCHECK(!cls->MustGenerateClinitCheck());
4921 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4922 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004923 GenerateGcRootFieldLoad(cls,
4924 out_loc,
4925 current_method,
4926 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004927 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004928 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004929 break;
4930 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004931 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004932 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004933 // Add ADRP with its PC-relative type patch.
4934 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004935 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004936 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004937 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004938 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004939 vixl::aarch64::Label* add_label =
4940 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004941 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004942 break;
4943 }
4944 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004945 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004946 uint32_t address = dchecked_integral_cast<uint32_t>(
4947 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4948 DCHECK_NE(address, 0u);
4949 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004950 break;
4951 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004952 case HLoadClass::LoadKind::kBootImageClassTable: {
4953 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
4954 // Add ADRP with its PC-relative type patch.
4955 const DexFile& dex_file = cls->GetDexFile();
4956 dex::TypeIndex type_index = cls->GetTypeIndex();
4957 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
4958 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
4959 // Add LDR with its PC-relative type patch.
4960 vixl::aarch64::Label* ldr_label =
4961 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
4962 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
4963 // Extract the reference from the slot data, i.e. clear the hash bits.
4964 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
4965 ComputeModifiedUtf8Hash(dex_file.StringByTypeIdx(type_index)));
4966 if (masked_hash != 0) {
4967 __ Sub(out.W(), out.W(), Operand(masked_hash));
4968 }
4969 break;
4970 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004971 case HLoadClass::LoadKind::kBssEntry: {
4972 // Add ADRP with its PC-relative Class .bss entry patch.
4973 const DexFile& dex_file = cls->GetDexFile();
4974 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004975 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004976 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4977 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004978 // Add LDR with its PC-relative Class patch.
4979 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004980 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004981 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4982 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004983 out_loc,
4984 bss_entry_temp,
4985 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004986 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004987 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004988 generate_null_check = true;
4989 break;
4990 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004991 case HLoadClass::LoadKind::kJitTableAddress: {
4992 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4993 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004994 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004995 GenerateGcRootFieldLoad(cls,
4996 out_loc,
4997 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004998 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004999 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005000 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005001 break;
5002 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005003 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005004 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005005 LOG(FATAL) << "UNREACHABLE";
5006 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005007 }
5008
Vladimir Markoea4c1262017-02-06 19:59:33 +00005009 bool do_clinit = cls->MustGenerateClinitCheck();
5010 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005011 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005012 SlowPathCodeARM64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00005013 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005014 codegen_->AddSlowPath(slow_path);
5015 if (generate_null_check) {
5016 __ Cbz(out, slow_path->GetEntryLabel());
5017 }
5018 if (cls->MustGenerateClinitCheck()) {
5019 GenerateClassInitializationCheck(slow_path, out);
5020 } else {
5021 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005022 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005023 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005024 }
5025}
5026
David Brazdilcb1c0552015-08-04 16:22:25 +01005027static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005028 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005029}
5030
Alexandre Rames67555f72014-11-18 10:55:16 +00005031void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5032 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005033 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005034 locations->SetOut(Location::RequiresRegister());
5035}
5036
5037void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005038 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5039}
5040
5041void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005042 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005043}
5044
5045void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5046 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005047}
5048
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005049HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5050 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005051 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005052 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005053 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005054 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005055 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005056 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005057 case HLoadString::LoadKind::kJitTableAddress:
5058 DCHECK(Runtime::Current()->UseJitCompilation());
5059 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005060 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005061 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005062 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005063 }
5064 return desired_string_load_kind;
5065}
5066
Alexandre Rames67555f72014-11-18 10:55:16 +00005067void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005068 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005069 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005070 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005071 InvokeRuntimeCallingConvention calling_convention;
5072 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5073 } else {
5074 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005075 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5076 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005077 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005078 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005079 RegisterSet caller_saves = RegisterSet::Empty();
5080 InvokeRuntimeCallingConvention calling_convention;
5081 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5082 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005083 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
5084 DataType::Type::kReference).GetCode());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005085 locations->SetCustomSlowPathCallerSaves(caller_saves);
5086 } else {
5087 // For non-Baker read barrier we have a temp-clobbering call.
5088 }
5089 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005090 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005091}
5092
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005093// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5094// move.
5095void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005096 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005097 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005098
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005099 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005100 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005101 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005102 // Add ADRP with its PC-relative String patch.
5103 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005104 const dex::StringIndex string_index = load->GetStringIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01005105 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005106 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005107 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005108 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005109 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005110 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005111 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005112 }
5113 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005114 uint32_t address = dchecked_integral_cast<uint32_t>(
5115 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5116 DCHECK_NE(address, 0u);
5117 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005118 return;
5119 }
5120 case HLoadString::LoadKind::kBootImageInternTable: {
5121 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5122 // Add ADRP with its PC-relative String patch.
5123 const DexFile& dex_file = load->GetDexFile();
5124 const dex::StringIndex string_index = load->GetStringIndex();
5125 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
5126 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
5127 // Add LDR with its PC-relative String patch.
5128 vixl::aarch64::Label* ldr_label =
5129 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
5130 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5131 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005132 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005133 case HLoadString::LoadKind::kBssEntry: {
5134 // Add ADRP with its PC-relative String .bss entry patch.
5135 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005136 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005137 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005138 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005139 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005140 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005141 // Add LDR with its .bss entry String patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005142 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005143 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005144 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005145 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005146 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005147 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005148 /* offset placeholder */ 0u,
5149 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005150 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005151 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005152 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005153 codegen_->AddSlowPath(slow_path);
5154 __ Cbz(out.X(), slow_path->GetEntryLabel());
5155 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005156 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005157 return;
5158 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005159 case HLoadString::LoadKind::kJitTableAddress: {
5160 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005161 load->GetStringIndex(),
5162 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005163 GenerateGcRootFieldLoad(load,
5164 out_loc,
5165 out.X(),
5166 /* offset */ 0,
5167 /* fixup_label */ nullptr,
5168 kCompilerReadBarrierOption);
5169 return;
5170 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005171 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005172 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005173 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005174
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005175 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005176 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005177 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005178 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005179 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5180 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005181 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005182}
5183
Alexandre Rames5319def2014-10-23 10:03:10 +01005184void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005185 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005186 locations->SetOut(Location::ConstantLocation(constant));
5187}
5188
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005189void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005190 // Will be generated at use site.
5191}
5192
Alexandre Rames67555f72014-11-18 10:55:16 +00005193void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005194 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5195 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005196 InvokeRuntimeCallingConvention calling_convention;
5197 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5198}
5199
5200void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005201 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005202 instruction,
5203 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005204 if (instruction->IsEnter()) {
5205 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5206 } else {
5207 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5208 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005209 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005210}
5211
Alexandre Rames42d641b2014-10-27 14:00:51 +00005212void LocationsBuilderARM64::VisitMul(HMul* mul) {
5213 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005214 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005215 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005216 case DataType::Type::kInt32:
5217 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005218 locations->SetInAt(0, Location::RequiresRegister());
5219 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005220 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005221 break;
5222
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005223 case DataType::Type::kFloat32:
5224 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005225 locations->SetInAt(0, Location::RequiresFpuRegister());
5226 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005227 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005228 break;
5229
5230 default:
5231 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5232 }
5233}
5234
5235void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5236 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005237 case DataType::Type::kInt32:
5238 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005239 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5240 break;
5241
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005242 case DataType::Type::kFloat32:
5243 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005244 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005245 break;
5246
5247 default:
5248 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5249 }
5250}
5251
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005252void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5253 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005254 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005255 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005256 case DataType::Type::kInt32:
5257 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005258 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005259 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005260 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005261
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005262 case DataType::Type::kFloat32:
5263 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005264 locations->SetInAt(0, Location::RequiresFpuRegister());
5265 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005266 break;
5267
5268 default:
5269 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5270 }
5271}
5272
5273void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5274 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005275 case DataType::Type::kInt32:
5276 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005277 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5278 break;
5279
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005280 case DataType::Type::kFloat32:
5281 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005282 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005283 break;
5284
5285 default:
5286 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5287 }
5288}
5289
5290void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005291 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5292 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005293 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005294 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005295 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5296 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005297}
5298
5299void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005300 // Note: if heap poisoning is enabled, the entry point takes cares
5301 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005302 QuickEntrypointEnum entrypoint =
5303 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5304 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005305 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005306 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005307}
5308
Alexandre Rames5319def2014-10-23 10:03:10 +01005309void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005310 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5311 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005312 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005313 if (instruction->IsStringAlloc()) {
5314 locations->AddTemp(LocationFrom(kArtMethodRegister));
5315 } else {
5316 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005317 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005318 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005319}
5320
5321void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005322 // Note: if heap poisoning is enabled, the entry point takes cares
5323 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005324 if (instruction->IsStringAlloc()) {
5325 // String is allocated through StringFactory. Call NewEmptyString entry point.
5326 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005327 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005328 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5329 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005330
5331 {
5332 // Ensure the pc position is recorded immediately after the `blr` instruction.
5333 ExactAssemblyScope eas(GetVIXLAssembler(),
5334 kInstructionSize,
5335 CodeBufferCheckScope::kExactSize);
5336 __ blr(lr);
5337 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5338 }
David Brazdil6de19382016-01-08 17:37:10 +00005339 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005340 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005341 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005342 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005343 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005344}
5345
5346void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005347 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005348 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005349 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005350}
5351
5352void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005353 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005354 case DataType::Type::kInt32:
5355 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005356 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005357 break;
5358
5359 default:
5360 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5361 }
5362}
5363
David Brazdil66d126e2015-04-03 16:02:44 +01005364void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005365 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005366 locations->SetInAt(0, Location::RequiresRegister());
5367 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5368}
5369
5370void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005371 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005372}
5373
Alexandre Rames5319def2014-10-23 10:03:10 +01005374void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005375 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5376 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005377}
5378
Calin Juravle2ae48182016-03-16 14:05:09 +00005379void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5380 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005381 return;
5382 }
Artem Serov914d7a82017-02-07 14:33:49 +00005383 {
5384 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5385 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5386 Location obj = instruction->GetLocations()->InAt(0);
5387 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5388 RecordPcInfo(instruction, instruction->GetDexPc());
5389 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005390}
5391
Calin Juravle2ae48182016-03-16 14:05:09 +00005392void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005393 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005394 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005395
5396 LocationSummary* locations = instruction->GetLocations();
5397 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005398
5399 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005400}
5401
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005402void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005403 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005404}
5405
Alexandre Rames67555f72014-11-18 10:55:16 +00005406void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5407 HandleBinaryOp(instruction);
5408}
5409
5410void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5411 HandleBinaryOp(instruction);
5412}
5413
Alexandre Rames3e69f162014-12-10 10:36:50 +00005414void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5415 LOG(FATAL) << "Unreachable";
5416}
5417
5418void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005419 if (instruction->GetNext()->IsSuspendCheck() &&
5420 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5421 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5422 // The back edge will generate the suspend check.
5423 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5424 }
5425
Alexandre Rames3e69f162014-12-10 10:36:50 +00005426 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5427}
5428
Alexandre Rames5319def2014-10-23 10:03:10 +01005429void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005430 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005431 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5432 if (location.IsStackSlot()) {
5433 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5434 } else if (location.IsDoubleStackSlot()) {
5435 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5436 }
5437 locations->SetOut(location);
5438}
5439
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005440void InstructionCodeGeneratorARM64::VisitParameterValue(
5441 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005442 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005443}
5444
5445void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5446 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005447 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005448 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005449}
5450
5451void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5452 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5453 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005454}
5455
5456void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005457 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005458 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005459 locations->SetInAt(i, Location::Any());
5460 }
5461 locations->SetOut(Location::Any());
5462}
5463
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005464void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005465 LOG(FATAL) << "Unreachable";
5466}
5467
Serban Constantinescu02164b32014-11-13 14:05:07 +00005468void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005469 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005470 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005471 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005472 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005473 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005474
5475 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005476 case DataType::Type::kInt32:
5477 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005478 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005479 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005480 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5481 break;
5482
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005483 case DataType::Type::kFloat32:
5484 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005485 InvokeRuntimeCallingConvention calling_convention;
5486 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5487 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5488 locations->SetOut(calling_convention.GetReturnLocation(type));
5489
5490 break;
5491 }
5492
Serban Constantinescu02164b32014-11-13 14:05:07 +00005493 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005494 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005495 }
5496}
5497
5498void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005499 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005500
Serban Constantinescu02164b32014-11-13 14:05:07 +00005501 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005502 case DataType::Type::kInt32:
5503 case DataType::Type::kInt64: {
Zheng Xuc6667102015-05-15 16:08:45 +08005504 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005505 break;
5506 }
5507
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005508 case DataType::Type::kFloat32:
5509 case DataType::Type::kFloat64: {
5510 QuickEntrypointEnum entrypoint =
5511 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005512 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005513 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005514 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5515 } else {
5516 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5517 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005518 break;
5519 }
5520
Serban Constantinescu02164b32014-11-13 14:05:07 +00005521 default:
5522 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005523 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005524 }
5525}
5526
Igor Murashkind01745e2017-04-05 16:40:31 -07005527void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5528 constructor_fence->SetLocations(nullptr);
5529}
5530
5531void InstructionCodeGeneratorARM64::VisitConstructorFence(
5532 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5533 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5534}
5535
Calin Juravle27df7582015-04-17 19:12:31 +01005536void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5537 memory_barrier->SetLocations(nullptr);
5538}
5539
5540void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005541 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005542}
5543
Alexandre Rames5319def2014-10-23 10:03:10 +01005544void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005545 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005546 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005547 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005548}
5549
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005550void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005551 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005552}
5553
5554void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5555 instruction->SetLocations(nullptr);
5556}
5557
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005558void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005559 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005560}
5561
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005562void LocationsBuilderARM64::VisitRor(HRor* ror) {
5563 HandleBinaryOp(ror);
5564}
5565
5566void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5567 HandleBinaryOp(ror);
5568}
5569
Serban Constantinescu02164b32014-11-13 14:05:07 +00005570void LocationsBuilderARM64::VisitShl(HShl* shl) {
5571 HandleShift(shl);
5572}
5573
5574void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5575 HandleShift(shl);
5576}
5577
5578void LocationsBuilderARM64::VisitShr(HShr* shr) {
5579 HandleShift(shr);
5580}
5581
5582void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5583 HandleShift(shr);
5584}
5585
Alexandre Rames5319def2014-10-23 10:03:10 +01005586void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005587 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005588}
5589
5590void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005591 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005592}
5593
Alexandre Rames67555f72014-11-18 10:55:16 +00005594void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005595 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005596}
5597
5598void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005599 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005600}
5601
5602void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005603 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005604}
5605
Alexandre Rames67555f72014-11-18 10:55:16 +00005606void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005607 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005608}
5609
Calin Juravlee460d1d2015-09-29 04:52:17 +01005610void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5611 HUnresolvedInstanceFieldGet* instruction) {
5612 FieldAccessCallingConventionARM64 calling_convention;
5613 codegen_->CreateUnresolvedFieldLocationSummary(
5614 instruction, instruction->GetFieldType(), calling_convention);
5615}
5616
5617void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5618 HUnresolvedInstanceFieldGet* instruction) {
5619 FieldAccessCallingConventionARM64 calling_convention;
5620 codegen_->GenerateUnresolvedFieldAccess(instruction,
5621 instruction->GetFieldType(),
5622 instruction->GetFieldIndex(),
5623 instruction->GetDexPc(),
5624 calling_convention);
5625}
5626
5627void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5628 HUnresolvedInstanceFieldSet* instruction) {
5629 FieldAccessCallingConventionARM64 calling_convention;
5630 codegen_->CreateUnresolvedFieldLocationSummary(
5631 instruction, instruction->GetFieldType(), calling_convention);
5632}
5633
5634void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5635 HUnresolvedInstanceFieldSet* instruction) {
5636 FieldAccessCallingConventionARM64 calling_convention;
5637 codegen_->GenerateUnresolvedFieldAccess(instruction,
5638 instruction->GetFieldType(),
5639 instruction->GetFieldIndex(),
5640 instruction->GetDexPc(),
5641 calling_convention);
5642}
5643
5644void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5645 HUnresolvedStaticFieldGet* instruction) {
5646 FieldAccessCallingConventionARM64 calling_convention;
5647 codegen_->CreateUnresolvedFieldLocationSummary(
5648 instruction, instruction->GetFieldType(), calling_convention);
5649}
5650
5651void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5652 HUnresolvedStaticFieldGet* instruction) {
5653 FieldAccessCallingConventionARM64 calling_convention;
5654 codegen_->GenerateUnresolvedFieldAccess(instruction,
5655 instruction->GetFieldType(),
5656 instruction->GetFieldIndex(),
5657 instruction->GetDexPc(),
5658 calling_convention);
5659}
5660
5661void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5662 HUnresolvedStaticFieldSet* instruction) {
5663 FieldAccessCallingConventionARM64 calling_convention;
5664 codegen_->CreateUnresolvedFieldLocationSummary(
5665 instruction, instruction->GetFieldType(), calling_convention);
5666}
5667
5668void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5669 HUnresolvedStaticFieldSet* instruction) {
5670 FieldAccessCallingConventionARM64 calling_convention;
5671 codegen_->GenerateUnresolvedFieldAccess(instruction,
5672 instruction->GetFieldType(),
5673 instruction->GetFieldIndex(),
5674 instruction->GetDexPc(),
5675 calling_convention);
5676}
5677
Alexandre Rames5319def2014-10-23 10:03:10 +01005678void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005679 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5680 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005681 // In suspend check slow path, usually there are no caller-save registers at all.
5682 // If SIMD instructions are present, however, we force spilling all live SIMD
5683 // registers in full width (since the runtime only saves/restores lower part).
5684 locations->SetCustomSlowPathCallerSaves(
5685 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005686}
5687
5688void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005689 HBasicBlock* block = instruction->GetBlock();
5690 if (block->GetLoopInformation() != nullptr) {
5691 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5692 // The back edge will generate the suspend check.
5693 return;
5694 }
5695 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5696 // The goto will generate the suspend check.
5697 return;
5698 }
5699 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005700 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005701}
5702
Alexandre Rames67555f72014-11-18 10:55:16 +00005703void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005704 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5705 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005706 InvokeRuntimeCallingConvention calling_convention;
5707 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5708}
5709
5710void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005711 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005712 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005713}
5714
5715void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5716 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005717 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005718 DataType::Type input_type = conversion->GetInputType();
5719 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005720 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5721 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005722 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
5723 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005724 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5725 }
5726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005727 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005728 locations->SetInAt(0, Location::RequiresFpuRegister());
5729 } else {
5730 locations->SetInAt(0, Location::RequiresRegister());
5731 }
5732
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005733 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005734 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5735 } else {
5736 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5737 }
5738}
5739
5740void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005741 DataType::Type result_type = conversion->GetResultType();
5742 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00005743
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005744 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5745 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005746
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005747 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
5748 int result_size = DataType::Size(result_type);
5749 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005750 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005751 Register output = OutputRegister(conversion);
5752 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005753 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005754 // 'int' values are used directly as W registers, discarding the top
5755 // bits, so we don't need to sign-extend and can just perform a move.
5756 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5757 // top 32 bits of the target register. We theoretically could leave those
5758 // bits unchanged, but we would have to make sure that no code uses a
5759 // 32bit input value as a 64bit value assuming that the top 32 bits are
5760 // zero.
5761 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005762 } else if (DataType::IsUnsignedType(result_type) ||
5763 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
5764 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005765 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005766 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005767 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005768 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005769 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005770 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
5771 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005772 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005773 } else if (DataType::IsFloatingPointType(result_type) &&
5774 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005775 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5776 } else {
5777 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5778 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005779 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005780}
Alexandre Rames67555f72014-11-18 10:55:16 +00005781
Serban Constantinescu02164b32014-11-13 14:05:07 +00005782void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5783 HandleShift(ushr);
5784}
5785
5786void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5787 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005788}
5789
5790void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5791 HandleBinaryOp(instruction);
5792}
5793
5794void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5795 HandleBinaryOp(instruction);
5796}
5797
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005798void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005799 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005800 LOG(FATAL) << "Unreachable";
5801}
5802
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005803void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005804 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005805 LOG(FATAL) << "Unreachable";
5806}
5807
Mark Mendellfe57faa2015-09-18 09:26:15 -04005808// Simple implementation of packed switch - generate cascaded compare/jumps.
5809void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5810 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005811 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005812 locations->SetInAt(0, Location::RequiresRegister());
5813}
5814
5815void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5816 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005817 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005818 Register value_reg = InputRegisterAt(switch_instr, 0);
5819 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5820
Zheng Xu3927c8b2015-11-18 17:46:25 +08005821 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005822 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005823 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5824 // make sure we don't emit it if the target may run out of range.
5825 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5826 // ranges and emit the tables only as required.
5827 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005828
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005829 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005830 // Current instruction id is an upper bound of the number of HIRs in the graph.
5831 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5832 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005833 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5834 Register temp = temps.AcquireW();
5835 __ Subs(temp, value_reg, Operand(lower_bound));
5836
Zheng Xu3927c8b2015-11-18 17:46:25 +08005837 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005838 // Jump to successors[0] if value == lower_bound.
5839 __ B(eq, codegen_->GetLabelOf(successors[0]));
5840 int32_t last_index = 0;
5841 for (; num_entries - last_index > 2; last_index += 2) {
5842 __ Subs(temp, temp, Operand(2));
5843 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5844 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5845 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5846 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5847 }
5848 if (num_entries - last_index == 2) {
5849 // The last missing case_value.
5850 __ Cmp(temp, Operand(1));
5851 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005852 }
5853
5854 // And the default for any other value.
5855 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5856 __ B(codegen_->GetLabelOf(default_block));
5857 }
5858 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005859 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005860
5861 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5862
5863 // Below instructions should use at most one blocked register. Since there are two blocked
5864 // registers, we are free to block one.
5865 Register temp_w = temps.AcquireW();
5866 Register index;
5867 // Remove the bias.
5868 if (lower_bound != 0) {
5869 index = temp_w;
5870 __ Sub(index, value_reg, Operand(lower_bound));
5871 } else {
5872 index = value_reg;
5873 }
5874
5875 // Jump to default block if index is out of the range.
5876 __ Cmp(index, Operand(num_entries));
5877 __ B(hs, codegen_->GetLabelOf(default_block));
5878
5879 // In current VIXL implementation, it won't require any blocked registers to encode the
5880 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5881 // register pressure.
5882 Register table_base = temps.AcquireX();
5883 // Load jump offset from the table.
5884 __ Adr(table_base, jump_table->GetTableStartLabel());
5885 Register jump_offset = temp_w;
5886 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5887
5888 // Jump to target block by branching to table_base(pc related) + offset.
5889 Register target_address = table_base;
5890 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5891 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005892 }
5893}
5894
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005895void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5896 HInstruction* instruction,
5897 Location out,
5898 uint32_t offset,
5899 Location maybe_temp,
5900 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005901 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00005902 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005903 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005904 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005905 if (kUseBakerReadBarrier) {
5906 // Load with fast path based Baker's read barrier.
5907 // /* HeapReference<Object> */ out = *(out + offset)
5908 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5909 out,
5910 out_reg,
5911 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005912 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005913 /* needs_null_check */ false,
5914 /* use_load_acquire */ false);
5915 } else {
5916 // Load with slow path based read barrier.
5917 // Save the value of `out` into `maybe_temp` before overwriting it
5918 // in the following move operation, as we will need it for the
5919 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005920 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005921 __ Mov(temp_reg, out_reg);
5922 // /* HeapReference<Object> */ out = *(out + offset)
5923 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5924 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5925 }
5926 } else {
5927 // Plain load with no read barrier.
5928 // /* HeapReference<Object> */ out = *(out + offset)
5929 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5930 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5931 }
5932}
5933
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005934void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5935 HInstruction* instruction,
5936 Location out,
5937 Location obj,
5938 uint32_t offset,
5939 Location maybe_temp,
5940 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005941 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00005942 Register out_reg = RegisterFrom(out, type);
5943 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005944 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005945 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005946 if (kUseBakerReadBarrier) {
5947 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005948 // /* HeapReference<Object> */ out = *(obj + offset)
5949 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5950 out,
5951 obj_reg,
5952 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005953 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005954 /* needs_null_check */ false,
5955 /* use_load_acquire */ false);
5956 } else {
5957 // Load with slow path based read barrier.
5958 // /* HeapReference<Object> */ out = *(obj + offset)
5959 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5960 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5961 }
5962 } else {
5963 // Plain load with no read barrier.
5964 // /* HeapReference<Object> */ out = *(obj + offset)
5965 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5966 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5967 }
5968}
5969
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005970void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5971 HInstruction* instruction,
5972 Location root,
5973 Register obj,
5974 uint32_t offset,
5975 vixl::aarch64::Label* fixup_label,
5976 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005977 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005978 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005979 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005980 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005981 if (kUseBakerReadBarrier) {
5982 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005983 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005984 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5985 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01005986 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
5987 // the Marking Register) to decide whether we need to enter
5988 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005989 //
5990 // We use link-time generated thunks for the slow path. That thunk
5991 // checks the reference and jumps to the entrypoint if needed.
5992 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005993 // lr = &return_address;
5994 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01005995 // if (mr) { // Thread::Current()->GetIsGcMarking()
5996 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005997 // }
5998 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00005999
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006000 UseScratchRegisterScope temps(GetVIXLAssembler());
6001 DCHECK(temps.IsAvailable(ip0));
6002 DCHECK(temps.IsAvailable(ip1));
6003 temps.Exclude(ip0, ip1);
6004 uint32_t custom_data =
6005 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
6006 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00006007
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006008 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6009 vixl::aarch64::Label return_address;
6010 __ adr(lr, &return_address);
6011 if (fixup_label != nullptr) {
6012 __ Bind(fixup_label);
6013 }
6014 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6015 "GC root LDR must be 2 instruction (8B) before the return address label.");
6016 __ ldr(root_reg, MemOperand(obj.X(), offset));
6017 __ Bind(cbnz_label);
Roland Levillain97c46462017-05-11 14:04:03 +01006018 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006019 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006020 } else {
Roland Levillain97c46462017-05-11 14:04:03 +01006021 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6022 // the Marking Register) to decide whether we need to enter
6023 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006024 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006025 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01006026 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006027 // // Slow path.
Roland Levillain97c46462017-05-11 14:04:03 +01006028 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6029 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006030 // }
Roland Levillain44015862016-01-22 11:47:17 +00006031
Roland Levillain97c46462017-05-11 14:04:03 +01006032 // Slow path marking the GC root `root`. The entrypoint will
6033 // be loaded by the slow path code.
6034 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006035 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathARM64(instruction, root);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006036 codegen_->AddSlowPath(slow_path);
6037
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006038 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6039 if (fixup_label == nullptr) {
6040 __ Ldr(root_reg, MemOperand(obj, offset));
6041 } else {
6042 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
6043 }
6044 static_assert(
6045 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6046 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6047 "have different sizes.");
6048 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6049 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6050 "have different sizes.");
6051
Roland Levillain97c46462017-05-11 14:04:03 +01006052 __ Cbnz(mr, slow_path->GetEntryLabel());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006053 __ Bind(slow_path->GetExitLabel());
6054 }
Roland Levillain44015862016-01-22 11:47:17 +00006055 } else {
6056 // GC root loaded through a slow path for read barriers other
6057 // than Baker's.
6058 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006059 if (fixup_label == nullptr) {
6060 __ Add(root_reg.X(), obj.X(), offset);
6061 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006062 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006063 }
Roland Levillain44015862016-01-22 11:47:17 +00006064 // /* mirror::Object* */ root = root->Read()
6065 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6066 }
6067 } else {
6068 // Plain GC root load with no read barrier.
6069 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006070 if (fixup_label == nullptr) {
6071 __ Ldr(root_reg, MemOperand(obj, offset));
6072 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006073 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006074 }
Roland Levillain44015862016-01-22 11:47:17 +00006075 // Note that GC roots are not affected by heap poisoning, thus we
6076 // do not have to unpoison `root_reg` here.
6077 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006078 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006079}
6080
6081void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6082 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006083 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006084 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006085 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006086 bool needs_null_check,
6087 bool use_load_acquire) {
6088 DCHECK(kEmitCompilerReadBarrier);
6089 DCHECK(kUseBakerReadBarrier);
6090
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006091 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6092 !use_load_acquire &&
6093 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006094 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6095 // Marking Register) to decide whether we need to enter the slow
6096 // path to mark the reference. Then, in the slow path, check the
6097 // gray bit in the lock word of the reference's holder (`obj`) to
6098 // decide whether to mark `ref` or not.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006099 //
6100 // We use link-time generated thunks for the slow path. That thunk checks
6101 // the holder and jumps to the entrypoint if needed. If the holder is not
6102 // gray, it creates a fake dependency and returns to the LDR instruction.
6103 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006104 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006105 // if (mr) { // Thread::Current()->GetIsGcMarking()
6106 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006107 // }
6108 // not_gray_return_address:
6109 // // Original reference load. If the offset is too large to fit
6110 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006111 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006112 // gray_return_address:
6113
6114 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6115 Register base = obj;
6116 if (offset >= kReferenceLoadMinFarOffset) {
6117 DCHECK(maybe_temp.IsRegister());
6118 base = WRegisterFrom(maybe_temp);
6119 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6120 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6121 offset &= (kReferenceLoadMinFarOffset - 1u);
6122 }
6123 UseScratchRegisterScope temps(GetVIXLAssembler());
6124 DCHECK(temps.IsAvailable(ip0));
6125 DCHECK(temps.IsAvailable(ip1));
6126 temps.Exclude(ip0, ip1);
6127 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6128 base.GetCode(),
6129 obj.GetCode());
6130 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6131
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006132 {
6133 EmissionCheckScope guard(GetVIXLAssembler(),
6134 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6135 vixl::aarch64::Label return_address;
6136 __ adr(lr, &return_address);
6137 __ Bind(cbnz_label);
6138 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6139 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6140 "Field LDR must be 1 instruction (4B) before the return address label; "
6141 " 2 instructions (8B) for heap poisoning.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006142 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006143 __ ldr(ref_reg, MemOperand(base.X(), offset));
6144 if (needs_null_check) {
6145 MaybeRecordImplicitNullCheck(instruction);
6146 }
6147 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6148 __ Bind(&return_address);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006149 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006150 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006151 return;
6152 }
6153
Roland Levillain44015862016-01-22 11:47:17 +00006154 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006155 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006156 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006157 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006158 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6159 ref,
6160 obj,
6161 offset,
6162 no_index,
6163 no_scale_factor,
6164 temp,
6165 needs_null_check,
6166 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006167}
6168
6169void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6170 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006171 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006172 uint32_t data_offset,
6173 Location index,
6174 Register temp,
6175 bool needs_null_check) {
6176 DCHECK(kEmitCompilerReadBarrier);
6177 DCHECK(kUseBakerReadBarrier);
6178
Vladimir Marko66d691d2017-04-07 17:53:39 +01006179 static_assert(
6180 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6181 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006182 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006183
6184 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6185 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006186 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6187 // Marking Register) to decide whether we need to enter the slow
6188 // path to mark the reference. Then, in the slow path, check the
6189 // gray bit in the lock word of the reference's holder (`obj`) to
6190 // decide whether to mark `ref` or not.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006191 //
6192 // We use link-time generated thunks for the slow path. That thunk checks
6193 // the holder and jumps to the entrypoint if needed. If the holder is not
6194 // gray, it creates a fake dependency and returns to the LDR instruction.
6195 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006196 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006197 // if (mr) { // Thread::Current()->GetIsGcMarking()
6198 // goto array_thunk<base_reg>(lr)
Vladimir Marko66d691d2017-04-07 17:53:39 +01006199 // }
6200 // not_gray_return_address:
6201 // // Original reference load. If the offset is too large to fit
6202 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006203 // HeapReference<mirror::Object> reference = data[index];
Vladimir Marko66d691d2017-04-07 17:53:39 +01006204 // gray_return_address:
6205
6206 DCHECK(index.IsValid());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006207 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6208 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006209
6210 UseScratchRegisterScope temps(GetVIXLAssembler());
6211 DCHECK(temps.IsAvailable(ip0));
6212 DCHECK(temps.IsAvailable(ip1));
6213 temps.Exclude(ip0, ip1);
6214 uint32_t custom_data =
6215 linker::Arm64RelativePatcher::EncodeBakerReadBarrierArrayData(temp.GetCode());
6216 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6217
Vladimir Marko66d691d2017-04-07 17:53:39 +01006218 __ Add(temp.X(), obj.X(), Operand(data_offset));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006219 {
6220 EmissionCheckScope guard(GetVIXLAssembler(),
6221 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6222 vixl::aarch64::Label return_address;
6223 __ adr(lr, &return_address);
6224 __ Bind(cbnz_label);
6225 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6226 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6227 "Array LDR must be 1 instruction (4B) before the return address label; "
6228 " 2 instructions (8B) for heap poisoning.");
6229 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6230 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6231 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6232 __ Bind(&return_address);
6233 }
6234 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Marko66d691d2017-04-07 17:53:39 +01006235 return;
6236 }
6237
Roland Levillain44015862016-01-22 11:47:17 +00006238 // Array cells are never volatile variables, therefore array loads
6239 // never use Load-Acquire instructions on ARM64.
6240 const bool use_load_acquire = false;
6241
6242 // /* HeapReference<Object> */ ref =
6243 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006244 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6245 ref,
6246 obj,
6247 data_offset,
6248 index,
6249 scale_factor,
6250 temp,
6251 needs_null_check,
6252 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006253}
6254
6255void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6256 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006257 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006258 uint32_t offset,
6259 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006260 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006261 Register temp,
6262 bool needs_null_check,
Roland Levillainff487002017-03-07 16:50:01 +00006263 bool use_load_acquire) {
Roland Levillain44015862016-01-22 11:47:17 +00006264 DCHECK(kEmitCompilerReadBarrier);
6265 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006266 // If we are emitting an array load, we should not be using a
6267 // Load Acquire instruction. In other words:
6268 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6269 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006270
Roland Levillain97c46462017-05-11 14:04:03 +01006271 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6272 // Marking Register) to decide whether we need to enter the slow
6273 // path to mark the reference. Then, in the slow path, check the
6274 // gray bit in the lock word of the reference's holder (`obj`) to
6275 // decide whether to mark `ref` or not.
Roland Levillain44015862016-01-22 11:47:17 +00006276 //
Roland Levillain97c46462017-05-11 14:04:03 +01006277 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00006278 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006279 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6280 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6281 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6282 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6283 // if (is_gray) {
Roland Levillain97c46462017-05-11 14:04:03 +01006284 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6285 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillain54f869e2017-03-06 13:54:11 +00006286 // }
6287 // } else {
6288 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006289 // }
Roland Levillain44015862016-01-22 11:47:17 +00006290
Roland Levillainba650a42017-03-06 13:52:32 +00006291 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain97c46462017-05-11 14:04:03 +01006292 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006293 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006294 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
Roland Levillainff487002017-03-07 16:50:01 +00006295 instruction,
6296 ref,
6297 obj,
6298 offset,
6299 index,
6300 scale_factor,
6301 needs_null_check,
6302 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006303 temp);
Roland Levillainba650a42017-03-06 13:52:32 +00006304 AddSlowPath(slow_path);
6305
Roland Levillain97c46462017-05-11 14:04:03 +01006306 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006307 // Fast path: the GC is not marking: just load the reference.
Roland Levillain54f869e2017-03-06 13:54:11 +00006308 GenerateRawReferenceLoad(
6309 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006310 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006311 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainba650a42017-03-06 13:52:32 +00006312}
6313
Roland Levillainff487002017-03-07 16:50:01 +00006314void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6315 Location ref,
6316 Register obj,
6317 Location field_offset,
6318 Register temp,
6319 bool needs_null_check,
6320 bool use_load_acquire) {
6321 DCHECK(kEmitCompilerReadBarrier);
6322 DCHECK(kUseBakerReadBarrier);
6323 // If we are emitting an array load, we should not be using a
6324 // Load Acquire instruction. In other words:
6325 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6326 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6327
Roland Levillain97c46462017-05-11 14:04:03 +01006328 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6329 // Marking Register) to decide whether we need to enter the slow
6330 // path to update the reference field within `obj`. Then, in the
6331 // slow path, check the gray bit in the lock word of the reference's
6332 // holder (`obj`) to decide whether to mark `ref` and update the
6333 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00006334 //
Roland Levillain97c46462017-05-11 14:04:03 +01006335 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00006336 // // Slow path.
6337 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6338 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6339 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6340 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6341 // if (is_gray) {
6342 // old_ref = ref;
Roland Levillain97c46462017-05-11 14:04:03 +01006343 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6344 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00006345 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6346 // }
6347 // }
6348
6349 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain97c46462017-05-11 14:04:03 +01006350 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006351 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006352 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
Roland Levillainff487002017-03-07 16:50:01 +00006353 instruction,
6354 ref,
6355 obj,
6356 /* offset */ 0u,
6357 /* index */ field_offset,
6358 /* scale_factor */ 0u /* "times 1" */,
6359 needs_null_check,
6360 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006361 temp);
Roland Levillainff487002017-03-07 16:50:01 +00006362 AddSlowPath(slow_path);
6363
Roland Levillain97c46462017-05-11 14:04:03 +01006364 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006365 // Fast path: the GC is not marking: nothing to do (the field is
6366 // up-to-date, and we don't need to load the reference).
6367 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006368 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainff487002017-03-07 16:50:01 +00006369}
6370
Roland Levillainba650a42017-03-06 13:52:32 +00006371void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6372 Location ref,
6373 Register obj,
6374 uint32_t offset,
6375 Location index,
6376 size_t scale_factor,
6377 bool needs_null_check,
6378 bool use_load_acquire) {
6379 DCHECK(obj.IsW());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006380 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006381 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006382
Roland Levillainba650a42017-03-06 13:52:32 +00006383 // If needed, vixl::EmissionCheckScope guards are used to ensure
6384 // that no pools are emitted between the load (macro) instruction
6385 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006386
Roland Levillain44015862016-01-22 11:47:17 +00006387 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006388 // Load types involving an "index": ArrayGet,
6389 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6390 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006391 if (use_load_acquire) {
6392 // UnsafeGetObjectVolatile intrinsic case.
6393 // Register `index` is not an index in an object array, but an
6394 // offset to an object reference field within object `obj`.
6395 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6396 DCHECK(instruction->GetLocations()->Intrinsified());
6397 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6398 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006399 DCHECK_EQ(offset, 0u);
6400 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006401 DCHECK_EQ(needs_null_check, false);
6402 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006403 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6404 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006405 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006406 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6407 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006408 if (index.IsConstant()) {
6409 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006410 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006411 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006412 if (needs_null_check) {
6413 MaybeRecordImplicitNullCheck(instruction);
6414 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006415 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006416 UseScratchRegisterScope temps(GetVIXLAssembler());
6417 Register temp = temps.AcquireW();
6418 __ Add(temp, obj, offset);
6419 {
6420 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6421 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6422 if (needs_null_check) {
6423 MaybeRecordImplicitNullCheck(instruction);
6424 }
6425 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006426 }
Roland Levillain44015862016-01-22 11:47:17 +00006427 }
Roland Levillain44015862016-01-22 11:47:17 +00006428 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006429 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006430 MemOperand field = HeapOperand(obj, offset);
6431 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006432 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6433 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006434 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006435 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006436 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006437 if (needs_null_check) {
6438 MaybeRecordImplicitNullCheck(instruction);
6439 }
Roland Levillain44015862016-01-22 11:47:17 +00006440 }
6441 }
6442
6443 // Object* ref = ref_addr->AsMirrorPtr()
6444 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006445}
6446
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006447void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6448 // The following condition is a compile-time one, so it does not have a run-time cost.
6449 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6450 // The following condition is a run-time one; it is executed after the
6451 // previous compile-time test, to avoid penalizing non-debug builds.
6452 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6453 UseScratchRegisterScope temps(GetVIXLAssembler());
6454 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6455 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6456 }
6457 }
6458}
6459
Roland Levillain44015862016-01-22 11:47:17 +00006460void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6461 Location out,
6462 Location ref,
6463 Location obj,
6464 uint32_t offset,
6465 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006466 DCHECK(kEmitCompilerReadBarrier);
6467
Roland Levillain44015862016-01-22 11:47:17 +00006468 // Insert a slow path based read barrier *after* the reference load.
6469 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006470 // If heap poisoning is enabled, the unpoisoning of the loaded
6471 // reference will be carried out by the runtime within the slow
6472 // path.
6473 //
6474 // Note that `ref` currently does not get unpoisoned (when heap
6475 // poisoning is enabled), which is alright as the `ref` argument is
6476 // not used by the artReadBarrierSlow entry point.
6477 //
6478 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006479 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006480 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6481 AddSlowPath(slow_path);
6482
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006483 __ B(slow_path->GetEntryLabel());
6484 __ Bind(slow_path->GetExitLabel());
6485}
6486
Roland Levillain44015862016-01-22 11:47:17 +00006487void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6488 Location out,
6489 Location ref,
6490 Location obj,
6491 uint32_t offset,
6492 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006493 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006494 // Baker's read barriers shall be handled by the fast path
6495 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6496 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006497 // If heap poisoning is enabled, unpoisoning will be taken care of
6498 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006499 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006500 } else if (kPoisonHeapReferences) {
6501 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6502 }
6503}
6504
Roland Levillain44015862016-01-22 11:47:17 +00006505void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6506 Location out,
6507 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006508 DCHECK(kEmitCompilerReadBarrier);
6509
Roland Levillain44015862016-01-22 11:47:17 +00006510 // Insert a slow path based read barrier *after* the GC root load.
6511 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006512 // Note that GC roots are not affected by heap poisoning, so we do
6513 // not need to do anything special for this here.
6514 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006515 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006516 AddSlowPath(slow_path);
6517
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006518 __ B(slow_path->GetEntryLabel());
6519 __ Bind(slow_path->GetExitLabel());
6520}
6521
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006522void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6523 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006524 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006525 locations->SetInAt(0, Location::RequiresRegister());
6526 locations->SetOut(Location::RequiresRegister());
6527}
6528
6529void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6530 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006531 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006532 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006533 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006534 __ Ldr(XRegisterFrom(locations->Out()),
6535 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006536 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006537 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006538 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006539 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6540 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006541 __ Ldr(XRegisterFrom(locations->Out()),
6542 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006543 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006544}
6545
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006546static void PatchJitRootUse(uint8_t* code,
6547 const uint8_t* roots_data,
6548 vixl::aarch64::Literal<uint32_t>* literal,
6549 uint64_t index_in_table) {
6550 uint32_t literal_offset = literal->GetOffset();
6551 uintptr_t address =
6552 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6553 uint8_t* data = code + literal_offset;
6554 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6555}
6556
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006557void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6558 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006559 const StringReference& string_reference = entry.first;
6560 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006561 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006562 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006563 }
6564 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006565 const TypeReference& type_reference = entry.first;
6566 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006567 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006568 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006569 }
6570}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006571
Alexandre Rames67555f72014-11-18 10:55:16 +00006572#undef __
6573#undef QUICK_ENTRY_POINT
6574
Alexandre Rames5319def2014-10-23 10:03:10 +01006575} // namespace arm64
6576} // namespace art