blob: 4999950600106c219415344f21beb3e22903da32 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Vladimir Markof4f2daa2017-03-20 18:26:59 +000019#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000020#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070022#include "base/bit_utils.h"
23#include "base/bit_utils_iterator.h"
Zheng Xuc6667102015-05-15 16:08:45 +080024#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000025#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010026#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080027#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080029#include "intrinsics.h"
30#include "intrinsics_arm64.h"
Vladimir Markof4f2daa2017-03-20 18:26:59 +000031#include "linker/arm64/relative_patcher_arm64.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070032#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010033#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070034#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000035#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010036#include "thread.h"
37#include "utils/arm64/assembler_arm64.h"
38#include "utils/assembler.h"
39#include "utils/stack_checks.h"
40
Scott Wakeling97c72b72016-06-24 16:19:36 +010041using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000042using vixl::ExactAssemblyScope;
43using vixl::CodeBufferCheckScope;
44using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010045
46#ifdef __
47#error "ARM64 Codegen VIXL macro-assembler macro already defined."
48#endif
49
Alexandre Rames5319def2014-10-23 10:03:10 +010050namespace art {
51
Roland Levillain22ccc3a2015-11-24 13:10:05 +000052template<class MirrorType>
53class GcRoot;
54
Alexandre Rames5319def2014-10-23 10:03:10 +010055namespace arm64 {
56
Alexandre Ramesbe919d92016-08-23 18:33:36 +010057using helpers::ARM64EncodableConstantOrRegister;
58using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080059using helpers::CPURegisterFrom;
60using helpers::DRegisterFrom;
61using helpers::FPRegisterFrom;
62using helpers::HeapOperand;
63using helpers::HeapOperandFrom;
64using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010065using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080066using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080067using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010068using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010070using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080071using helpers::LocationFrom;
72using helpers::OperandFromMemOperand;
73using helpers::OutputCPURegister;
74using helpers::OutputFPRegister;
75using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010076using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080077using helpers::RegisterFrom;
78using helpers::StackOperandFrom;
79using helpers::VIXLRegCodeFromART;
80using helpers::WRegisterFrom;
81using helpers::XRegisterFrom;
82
Vladimir Markof3e0ee22015-12-17 15:23:13 +000083// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080084// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
85// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000086static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010087
Vladimir Markof4f2daa2017-03-20 18:26:59 +000088// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
89// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
90// For the Baker read barrier implementation using link-generated thunks we need to split
91// the offset explicitly.
92constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
93
94// Flags controlling the use of link-time generated thunks for Baker read barriers.
Vladimir Markod1ef8732017-04-18 13:55:13 +010095constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
Vladimir Marko66d691d2017-04-07 17:53:39 +010096constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
Vladimir Markod1ef8732017-04-18 13:55:13 +010097constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
Vladimir Markof4f2daa2017-03-20 18:26:59 +000098
99// Some instructions have special requirements for a temporary, for example
100// LoadClass/kBssEntry and LoadString/kBssEntry for Baker read barrier require
101// temp that's not an R0 (to avoid an extra move) and Baker read barrier field
102// loads with large offsets need a fixed register to limit the number of link-time
103// thunks we generate. For these and similar cases, we want to reserve a specific
104// register that's neither callee-save nor an argument register. We choose x15.
105inline Location FixedTempLocation() {
106 return Location::RegisterLocation(x15.GetCode());
107}
108
Alexandre Rames5319def2014-10-23 10:03:10 +0100109inline Condition ARM64Condition(IfCondition cond) {
110 switch (cond) {
111 case kCondEQ: return eq;
112 case kCondNE: return ne;
113 case kCondLT: return lt;
114 case kCondLE: return le;
115 case kCondGT: return gt;
116 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700117 case kCondB: return lo;
118 case kCondBE: return ls;
119 case kCondA: return hi;
120 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100121 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000122 LOG(FATAL) << "Unreachable";
123 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100124}
125
Vladimir Markod6e069b2016-01-18 11:11:01 +0000126inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
127 // The ARM64 condition codes can express all the necessary branches, see the
128 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
129 // There is no dex instruction or HIR that would need the missing conditions
130 // "equal or unordered" or "not equal".
131 switch (cond) {
132 case kCondEQ: return eq;
133 case kCondNE: return ne /* unordered */;
134 case kCondLT: return gt_bias ? cc : lt /* unordered */;
135 case kCondLE: return gt_bias ? ls : le /* unordered */;
136 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
137 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
138 default:
139 LOG(FATAL) << "UNREACHABLE";
140 UNREACHABLE();
141 }
142}
143
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000144Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000145 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
146 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
147 // but we use the exact registers for clarity.
148 if (return_type == Primitive::kPrimFloat) {
149 return LocationFrom(s0);
150 } else if (return_type == Primitive::kPrimDouble) {
151 return LocationFrom(d0);
152 } else if (return_type == Primitive::kPrimLong) {
153 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100154 } else if (return_type == Primitive::kPrimVoid) {
155 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000156 } else {
157 return LocationFrom(w0);
158 }
159}
160
Alexandre Rames5319def2014-10-23 10:03:10 +0100161Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000162 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100163}
164
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100165// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
166#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700167#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100168
Zheng Xuda403092015-04-24 17:35:39 +0800169// Calculate memory accessing operand for save/restore live registers.
170static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100171 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800172 int64_t spill_offset,
173 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100174 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
175 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
176 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800177 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100178 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800179 codegen->GetNumberOfFloatingPointRegisters()));
180
Vladimir Marko804b03f2016-09-14 16:26:36 +0100181 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100182 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
183 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800184
185 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
186 UseScratchRegisterScope temps(masm);
187
188 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100189 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
190 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800191 int64_t reg_size = kXRegSizeInBytes;
192 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
193 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100194 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800195 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
196 // If the offset does not fit in the instruction's immediate field, use an alternate register
197 // to compute the base address(float point registers spill base address).
198 Register new_base = temps.AcquireSameSizeAs(base);
199 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
200 base = new_base;
201 spill_offset = -core_spill_size;
202 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
203 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
204 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
205 }
206
207 if (is_save) {
208 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
209 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
210 } else {
211 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
212 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
213 }
214}
215
216void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800217 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100218 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
219 for (uint32_t i : LowToHighBits(core_spills)) {
220 // If the register holds an object, update the stack mask.
221 if (locations->RegisterContainsObject(i)) {
222 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800223 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100224 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
225 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
226 saved_core_stack_offsets_[i] = stack_offset;
227 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800228 }
229
Vladimir Marko804b03f2016-09-14 16:26:36 +0100230 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
231 for (uint32_t i : LowToHighBits(fp_spills)) {
232 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
233 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
234 saved_fpu_stack_offsets_[i] = stack_offset;
235 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800236 }
237
Vladimir Marko804b03f2016-09-14 16:26:36 +0100238 SaveRestoreLiveRegistersHelper(codegen,
239 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800240 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
241}
242
243void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100244 SaveRestoreLiveRegistersHelper(codegen,
245 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800246 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
247}
248
Alexandre Rames5319def2014-10-23 10:03:10 +0100249class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
250 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000251 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100252
Alexandre Rames67555f72014-11-18 10:55:16 +0000253 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100254 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000255 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100256
Alexandre Rames5319def2014-10-23 10:03:10 +0100257 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000258 if (instruction_->CanThrowIntoCatchBlock()) {
259 // Live registers will be restored in the catch block if caught.
260 SaveLiveRegisters(codegen, instruction_->GetLocations());
261 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000262 // We're moving two locations to locations that could overlap, so we need a parallel
263 // move resolver.
264 InvokeRuntimeCallingConvention calling_convention;
265 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100266 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
267 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000268 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
269 ? kQuickThrowStringBounds
270 : kQuickThrowArrayBounds;
271 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100272 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800273 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100274 }
275
Alexandre Rames8158f282015-08-07 10:26:17 +0100276 bool IsFatal() const OVERRIDE { return true; }
277
Alexandre Rames9931f312015-06-19 14:47:01 +0100278 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
279
Alexandre Rames5319def2014-10-23 10:03:10 +0100280 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100281 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
282};
283
Alexandre Rames67555f72014-11-18 10:55:16 +0000284class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
285 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000286 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000287
288 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
289 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
290 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000291 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800292 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000293 }
294
Alexandre Rames8158f282015-08-07 10:26:17 +0100295 bool IsFatal() const OVERRIDE { return true; }
296
Alexandre Rames9931f312015-06-19 14:47:01 +0100297 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
298
Alexandre Rames67555f72014-11-18 10:55:16 +0000299 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000300 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
301};
302
303class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
304 public:
305 LoadClassSlowPathARM64(HLoadClass* cls,
306 HInstruction* at,
307 uint32_t dex_pc,
Vladimir Markoea4c1262017-02-06 19:59:33 +0000308 bool do_clinit,
309 vixl::aarch64::Register bss_entry_temp = vixl::aarch64::Register(),
310 vixl::aarch64::Label* bss_entry_adrp_label = nullptr)
311 : SlowPathCodeARM64(at),
312 cls_(cls),
313 dex_pc_(dex_pc),
314 do_clinit_(do_clinit),
315 bss_entry_temp_(bss_entry_temp),
316 bss_entry_adrp_label_(bss_entry_adrp_label) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000317 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
318 }
319
320 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000321 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000322 Location out = locations->Out();
323 constexpr bool call_saves_everything_except_r0_ip0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexandre Rames67555f72014-11-18 10:55:16 +0000324 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
325
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000326 InvokeRuntimeCallingConvention calling_convention;
327 // For HLoadClass/kBssEntry/kSaveEverything, the page address of the entry is in a temp
328 // register, make sure it's not clobbered by the call or by saving/restoring registers.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000329 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
330 bool is_load_class_bss_entry =
331 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000332 if (is_load_class_bss_entry) {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000333 DCHECK(bss_entry_temp_.IsValid());
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000334 DCHECK(!bss_entry_temp_.Is(calling_convention.GetRegisterAt(0)));
335 DCHECK(
336 !UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(bss_entry_temp_));
Vladimir Markoea4c1262017-02-06 19:59:33 +0000337 }
338
Alexandre Rames67555f72014-11-18 10:55:16 +0000339 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000340 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000341
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000342 dex::TypeIndex type_index = cls_->GetTypeIndex();
343 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000344 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
345 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000346 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800347 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100348 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800349 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100350 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800351 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000352
353 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000354 if (out.IsValid()) {
355 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000356 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000357 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000358 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000359 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000360 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000361 if (is_load_class_bss_entry) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000362 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000363 const DexFile& dex_file = cls_->GetDexFile();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000364 if (call_saves_everything_except_r0_ip0) {
365 // The class entry page address was preserved in bss_entry_temp_ thanks to kSaveEverything.
366 } else {
367 // For non-Baker read barrier, we need to re-calculate the address of the class entry page.
368 bss_entry_adrp_label_ = arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
369 arm64_codegen->EmitAdrpPlaceholder(bss_entry_adrp_label_, bss_entry_temp_);
370 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000371 vixl::aarch64::Label* strp_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +0000372 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label_);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000373 {
374 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
375 __ Bind(strp_label);
376 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Markoea4c1262017-02-06 19:59:33 +0000377 MemOperand(bss_entry_temp_, /* offset placeholder */ 0));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000378 }
379 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000380 __ B(GetExitLabel());
381 }
382
Alexandre Rames9931f312015-06-19 14:47:01 +0100383 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
384
Alexandre Rames67555f72014-11-18 10:55:16 +0000385 private:
386 // The class this slow path will load.
387 HLoadClass* const cls_;
388
Alexandre Rames67555f72014-11-18 10:55:16 +0000389 // The dex PC of `at_`.
390 const uint32_t dex_pc_;
391
392 // Whether to initialize the class.
393 const bool do_clinit_;
394
Vladimir Markoea4c1262017-02-06 19:59:33 +0000395 // For HLoadClass/kBssEntry, the temp register and the label of the ADRP where it was loaded.
396 vixl::aarch64::Register bss_entry_temp_;
397 vixl::aarch64::Label* bss_entry_adrp_label_;
398
Alexandre Rames67555f72014-11-18 10:55:16 +0000399 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
400};
401
Vladimir Markoaad75c62016-10-03 08:46:48 +0000402class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
403 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100404 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
405 : SlowPathCodeARM64(instruction),
406 temp_(temp),
407 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000408
409 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
410 LocationSummary* locations = instruction_->GetLocations();
411 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
412 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
413
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000414 InvokeRuntimeCallingConvention calling_convention;
415 // Make sure `temp_` is not clobbered by the call or by saving/restoring registers.
416 DCHECK(temp_.IsValid());
417 DCHECK(!temp_.Is(calling_convention.GetRegisterAt(0)));
418 DCHECK(!UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(temp_));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100419
Vladimir Markoaad75c62016-10-03 08:46:48 +0000420 __ Bind(GetEntryLabel());
421 SaveLiveRegisters(codegen, locations);
422
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000423 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
424 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000425 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
426 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
427 Primitive::Type type = instruction_->GetType();
428 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
429
430 RestoreLiveRegisters(codegen, locations);
431
432 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000433 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100434 if (!kUseReadBarrier || kUseBakerReadBarrier) {
435 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
436 } else {
437 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
438 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
439 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
440 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000441 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100442 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000443 {
444 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
445 __ Bind(strp_label);
446 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100447 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000448 }
449
450 __ B(GetExitLabel());
451 }
452
453 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
454
455 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100456 const Register temp_;
457 vixl::aarch64::Label* adrp_label_;
458
Vladimir Markoaad75c62016-10-03 08:46:48 +0000459 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
460};
461
Alexandre Rames5319def2014-10-23 10:03:10 +0100462class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
463 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000464 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100465
Alexandre Rames67555f72014-11-18 10:55:16 +0000466 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
467 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100468 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000469 if (instruction_->CanThrowIntoCatchBlock()) {
470 // Live registers will be restored in the catch block if caught.
471 SaveLiveRegisters(codegen, instruction_->GetLocations());
472 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000473 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
474 instruction_,
475 instruction_->GetDexPc(),
476 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800477 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100478 }
479
Alexandre Rames8158f282015-08-07 10:26:17 +0100480 bool IsFatal() const OVERRIDE { return true; }
481
Alexandre Rames9931f312015-06-19 14:47:01 +0100482 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
483
Alexandre Rames5319def2014-10-23 10:03:10 +0100484 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100485 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
486};
487
488class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
489 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100490 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000491 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100492
Alexandre Rames67555f72014-11-18 10:55:16 +0000493 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100494 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000495 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100496 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100497 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000498 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800499 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100500 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000501 if (successor_ == nullptr) {
502 __ B(GetReturnLabel());
503 } else {
504 __ B(arm64_codegen->GetLabelOf(successor_));
505 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100506 }
507
Scott Wakeling97c72b72016-06-24 16:19:36 +0100508 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100509 DCHECK(successor_ == nullptr);
510 return &return_label_;
511 }
512
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100513 HBasicBlock* GetSuccessor() const {
514 return successor_;
515 }
516
Alexandre Rames9931f312015-06-19 14:47:01 +0100517 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
518
Alexandre Rames5319def2014-10-23 10:03:10 +0100519 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100520 // If not null, the block to branch to after the suspend check.
521 HBasicBlock* const successor_;
522
523 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100524 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100525
526 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
527};
528
Alexandre Rames67555f72014-11-18 10:55:16 +0000529class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
530 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000531 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000532 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000533
534 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000535 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800536
Alexandre Rames3e69f162014-12-10 10:36:50 +0000537 DCHECK(instruction_->IsCheckCast()
538 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
539 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100540 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000541
Alexandre Rames67555f72014-11-18 10:55:16 +0000542 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000543
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000544 if (!is_fatal_) {
545 SaveLiveRegisters(codegen, locations);
546 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000547
548 // We're moving two locations to locations that could overlap, so we need a parallel
549 // move resolver.
550 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800551 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800552 LocationFrom(calling_convention.GetRegisterAt(0)),
553 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800554 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800555 LocationFrom(calling_convention.GetRegisterAt(1)),
556 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000557 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000558 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800559 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000560 Primitive::Type ret_type = instruction_->GetType();
561 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
562 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
563 } else {
564 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800565 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
566 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000567 }
568
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000569 if (!is_fatal_) {
570 RestoreLiveRegisters(codegen, locations);
571 __ B(GetExitLabel());
572 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000573 }
574
Alexandre Rames9931f312015-06-19 14:47:01 +0100575 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100576 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100577
Alexandre Rames67555f72014-11-18 10:55:16 +0000578 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000579 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000580
Alexandre Rames67555f72014-11-18 10:55:16 +0000581 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
582};
583
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700584class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
585 public:
Aart Bik42249c32016-01-07 15:33:50 -0800586 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000587 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700588
589 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800590 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700591 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100592 LocationSummary* locations = instruction_->GetLocations();
593 SaveLiveRegisters(codegen, locations);
594 InvokeRuntimeCallingConvention calling_convention;
595 __ Mov(calling_convention.GetRegisterAt(0),
596 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000597 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100598 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700599 }
600
Alexandre Rames9931f312015-06-19 14:47:01 +0100601 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
602
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700603 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700604 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
605};
606
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100607class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
608 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000609 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100610
611 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
612 LocationSummary* locations = instruction_->GetLocations();
613 __ Bind(GetEntryLabel());
614 SaveLiveRegisters(codegen, locations);
615
616 InvokeRuntimeCallingConvention calling_convention;
617 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
618 parallel_move.AddMove(
619 locations->InAt(0),
620 LocationFrom(calling_convention.GetRegisterAt(0)),
621 Primitive::kPrimNot,
622 nullptr);
623 parallel_move.AddMove(
624 locations->InAt(1),
625 LocationFrom(calling_convention.GetRegisterAt(1)),
626 Primitive::kPrimInt,
627 nullptr);
628 parallel_move.AddMove(
629 locations->InAt(2),
630 LocationFrom(calling_convention.GetRegisterAt(2)),
631 Primitive::kPrimNot,
632 nullptr);
633 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
634
635 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000636 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100637 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
638 RestoreLiveRegisters(codegen, locations);
639 __ B(GetExitLabel());
640 }
641
642 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
643
644 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100645 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
646};
647
Zheng Xu3927c8b2015-11-18 17:46:25 +0800648void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
649 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000650 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800651
652 // We are about to use the assembler to place literals directly. Make sure we have enough
653 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000654 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
655 num_entries * sizeof(int32_t),
656 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800657
658 __ Bind(&table_start_);
659 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
660 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100661 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800662 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100663 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800664 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
665 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
666 Literal<int32_t> literal(jump_offset);
667 __ place(&literal);
668 }
669}
670
Roland Levillain54f869e2017-03-06 13:54:11 +0000671// Abstract base class for read barrier slow paths marking a reference
672// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000673//
Roland Levillain54f869e2017-03-06 13:54:11 +0000674// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100675// barrier marking runtime entry point to be invoked or an empty
676// location; in the latter case, the read barrier marking runtime
677// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000678class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
679 protected:
680 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
681 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000682 DCHECK(kEmitCompilerReadBarrier);
683 }
684
Roland Levillain54f869e2017-03-06 13:54:11 +0000685 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000686
Roland Levillain54f869e2017-03-06 13:54:11 +0000687 // Generate assembly code calling the read barrier marking runtime
688 // entry point (ReadBarrierMarkRegX).
689 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000690 // No need to save live registers; it's taken care of by the
691 // entrypoint. Also, there is no need to update the stack mask,
692 // as this runtime call will not trigger a garbage collection.
693 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
694 DCHECK_NE(ref_.reg(), LR);
695 DCHECK_NE(ref_.reg(), WSP);
696 DCHECK_NE(ref_.reg(), WZR);
697 // IP0 is used internally by the ReadBarrierMarkRegX entry point
698 // as a temporary, it cannot be the entry point's input/output.
699 DCHECK_NE(ref_.reg(), IP0);
700 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
701 // "Compact" slow path, saving two moves.
702 //
703 // Instead of using the standard runtime calling convention (input
704 // and output in W0):
705 //
706 // W0 <- ref
707 // W0 <- ReadBarrierMark(W0)
708 // ref <- W0
709 //
710 // we just use rX (the register containing `ref`) as input and output
711 // of a dedicated entrypoint:
712 //
713 // rX <- ReadBarrierMarkRegX(rX)
714 //
715 if (entrypoint_.IsValid()) {
716 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
717 __ Blr(XRegisterFrom(entrypoint_));
718 } else {
719 // Entrypoint is not already loaded, load from the thread.
720 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100721 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000722 // This runtime call does not require a stack map.
723 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
724 }
725 }
726
727 // The location (register) of the marked object reference.
728 const Location ref_;
729
730 // The location of the entrypoint if it is already loaded.
731 const Location entrypoint_;
732
Roland Levillain54f869e2017-03-06 13:54:11 +0000733 private:
734 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
735};
736
Alexandre Rames5319def2014-10-23 10:03:10 +0100737// Slow path marking an object reference `ref` during a read
738// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000739// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100740//
741// This means that after the execution of this slow path, `ref` will
742// always be up-to-date, but `obj.field` may not; i.e., after the
743// flip, `ref` will be a to-space reference, but `obj.field` will
744// probably still be a from-space reference (unless it gets updated by
745// another thread, or if another thread installed another object
746// reference (different from `ref`) in `obj.field`).
747//
Roland Levillain97c46462017-05-11 14:04:03 +0100748// Argument `entrypoint` must be a register location holding the read
749// barrier marking runtime entry point to be invoked or an empty
750// location; in the latter case, the read barrier marking runtime
751// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000752class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100753 public:
754 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
755 Location ref,
756 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000757 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100758 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100759 }
760
761 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
762
763 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000764 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100765 DCHECK(locations->CanCall());
766 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000767 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000768 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
769 << "Unexpected instruction in read barrier marking slow path: "
770 << instruction_->DebugName();
771
772 __ Bind(GetEntryLabel());
773 GenerateReadBarrierMarkRuntimeCall(codegen);
774 __ B(GetExitLabel());
775 }
776
777 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000778 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
779};
780
Roland Levillain54f869e2017-03-06 13:54:11 +0000781// Slow path loading `obj`'s lock word, loading a reference from
782// object `*(obj + offset + (index << scale_factor))` into `ref`, and
783// marking `ref` if `obj` is gray according to the lock word (Baker
784// read barrier). The field `obj.field` in the object `obj` holding
785// this reference does not get updated by this slow path after marking
786// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
787// below for that).
788//
789// This means that after the execution of this slow path, `ref` will
790// always be up-to-date, but `obj.field` may not; i.e., after the
791// flip, `ref` will be a to-space reference, but `obj.field` will
792// probably still be a from-space reference (unless it gets updated by
793// another thread, or if another thread installed another object
794// reference (different from `ref`) in `obj.field`).
795//
796// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100797// barrier marking runtime entry point to be invoked or an empty
798// location; in the latter case, the read barrier marking runtime
799// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000800class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
801 public:
802 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
803 Location ref,
804 Register obj,
805 uint32_t offset,
806 Location index,
807 size_t scale_factor,
808 bool needs_null_check,
809 bool use_load_acquire,
810 Register temp,
Roland Levillain97c46462017-05-11 14:04:03 +0100811 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000812 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
813 obj_(obj),
814 offset_(offset),
815 index_(index),
816 scale_factor_(scale_factor),
817 needs_null_check_(needs_null_check),
818 use_load_acquire_(use_load_acquire),
819 temp_(temp) {
820 DCHECK(kEmitCompilerReadBarrier);
821 DCHECK(kUseBakerReadBarrier);
822 }
823
824 const char* GetDescription() const OVERRIDE {
825 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
826 }
827
828 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
829 LocationSummary* locations = instruction_->GetLocations();
830 DCHECK(locations->CanCall());
831 DCHECK(ref_.IsRegister()) << ref_;
832 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
833 DCHECK(obj_.IsW());
834 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100835 DCHECK(instruction_->IsInstanceFieldGet() ||
836 instruction_->IsStaticFieldGet() ||
837 instruction_->IsArrayGet() ||
838 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100839 instruction_->IsInstanceOf() ||
840 instruction_->IsCheckCast() ||
841 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
842 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
843 << "Unexpected instruction in read barrier marking slow path: "
844 << instruction_->DebugName();
845 // The read barrier instrumentation of object ArrayGet
846 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000847 // instruction.
848 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000849 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
850
Roland Levillain54f869e2017-03-06 13:54:11 +0000851 // Temporary register `temp_`, used to store the lock word, must
852 // not be IP0 nor IP1, as we may use them to emit the reference
853 // load (in the call to GenerateRawReferenceLoad below), and we
854 // need the lock word to still be in `temp_` after the reference
855 // load.
856 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
857 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
858
Alexandre Rames5319def2014-10-23 10:03:10 +0100859 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000860
861 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
862 // inserted after the original load. However, in fast path based
863 // Baker's read barriers, we need to perform the load of
864 // mirror::Object::monitor_ *before* the original reference load.
865 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000866 // The slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100867 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000868 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
869 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
870 // HeapReference<mirror::Object> ref = *src; // Original reference load.
871 // bool is_gray = (rb_state == ReadBarrier::GrayState());
872 // if (is_gray) {
873 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
874 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000875 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000876 // Note: the original implementation in ReadBarrier::Barrier is
877 // slightly more complex as it performs additional checks that we do
878 // not do here for performance reasons.
879
880 // /* int32_t */ monitor = obj->monitor_
881 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
882 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
883 if (needs_null_check_) {
884 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100885 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000886 // /* LockWord */ lock_word = LockWord(monitor)
887 static_assert(sizeof(LockWord) == sizeof(int32_t),
888 "art::LockWord and int32_t have different sizes.");
889
890 // Introduce a dependency on the lock_word including rb_state,
891 // to prevent load-load reordering, and without using
892 // a memory barrier (which would be more expensive).
893 // `obj` is unchanged by this operation, but its value now depends
894 // on `temp`.
895 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
896
897 // The actual reference load.
898 // A possible implicit null check has already been handled above.
899 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
900 arm64_codegen->GenerateRawReferenceLoad(instruction_,
901 ref_,
902 obj_,
903 offset_,
904 index_,
905 scale_factor_,
906 /* needs_null_check */ false,
907 use_load_acquire_);
908
909 // Mark the object `ref` when `obj` is gray.
910 //
911 // if (rb_state == ReadBarrier::GrayState())
912 // ref = ReadBarrier::Mark(ref);
913 //
914 // Given the numeric representation, it's enough to check the low bit of the rb_state.
915 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
916 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
917 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
918 GenerateReadBarrierMarkRuntimeCall(codegen);
919
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000920 __ B(GetExitLabel());
921 }
922
923 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000924 // The register containing the object holding the marked object reference field.
925 Register obj_;
926 // The offset, index and scale factor to access the reference in `obj_`.
927 uint32_t offset_;
928 Location index_;
929 size_t scale_factor_;
930 // Is a null check required?
931 bool needs_null_check_;
932 // Should this reference load use Load-Acquire semantics?
933 bool use_load_acquire_;
934 // A temporary register used to hold the lock word of `obj_`.
935 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000936
Roland Levillain54f869e2017-03-06 13:54:11 +0000937 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000938};
939
Roland Levillain54f869e2017-03-06 13:54:11 +0000940// Slow path loading `obj`'s lock word, loading a reference from
941// object `*(obj + offset + (index << scale_factor))` into `ref`, and
942// marking `ref` if `obj` is gray according to the lock word (Baker
943// read barrier). If needed, this slow path also atomically updates
944// the field `obj.field` in the object `obj` holding this reference
945// after marking (contrary to
946// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
947// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100948//
949// This means that after the execution of this slow path, both `ref`
950// and `obj.field` will be up-to-date; i.e., after the flip, both will
951// hold the same to-space reference (unless another thread installed
952// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000953//
Roland Levillain54f869e2017-03-06 13:54:11 +0000954// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100955// barrier marking runtime entry point to be invoked or an empty
956// location; in the latter case, the read barrier marking runtime
957// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000958class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
959 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100960 public:
Roland Levillain97c46462017-05-11 14:04:03 +0100961 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
962 HInstruction* instruction,
963 Location ref,
964 Register obj,
965 uint32_t offset,
966 Location index,
967 size_t scale_factor,
968 bool needs_null_check,
969 bool use_load_acquire,
970 Register temp,
971 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000972 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100973 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000974 offset_(offset),
975 index_(index),
976 scale_factor_(scale_factor),
977 needs_null_check_(needs_null_check),
978 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000979 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100980 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000981 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100982 }
983
984 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000985 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100986 }
987
988 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
989 LocationSummary* locations = instruction_->GetLocations();
990 Register ref_reg = WRegisterFrom(ref_);
991 DCHECK(locations->CanCall());
992 DCHECK(ref_.IsRegister()) << ref_;
993 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000994 DCHECK(obj_.IsW());
995 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
996
997 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100998 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
999 << "Unexpected instruction in read barrier marking and field updating slow path: "
1000 << instruction_->DebugName();
1001 DCHECK(instruction_->GetLocations()->Intrinsified());
1002 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +00001003 DCHECK_EQ(offset_, 0u);
1004 DCHECK_EQ(scale_factor_, 0u);
1005 DCHECK_EQ(use_load_acquire_, false);
1006 // The location of the offset of the marked reference field within `obj_`.
1007 Location field_offset = index_;
1008 DCHECK(field_offset.IsRegister()) << field_offset;
1009
1010 // Temporary register `temp_`, used to store the lock word, must
1011 // not be IP0 nor IP1, as we may use them to emit the reference
1012 // load (in the call to GenerateRawReferenceLoad below), and we
1013 // need the lock word to still be in `temp_` after the reference
1014 // load.
1015 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1016 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001017
1018 __ Bind(GetEntryLabel());
1019
Roland Levillainff487002017-03-07 16:50:01 +00001020 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM64's:
1021 //
1022 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1023 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1024 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1025 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1026 // if (is_gray) {
1027 // old_ref = ref;
1028 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1029 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1030 // }
1031
Roland Levillain54f869e2017-03-06 13:54:11 +00001032 // /* int32_t */ monitor = obj->monitor_
1033 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1034 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
1035 if (needs_null_check_) {
1036 codegen->MaybeRecordImplicitNullCheck(instruction_);
1037 }
1038 // /* LockWord */ lock_word = LockWord(monitor)
1039 static_assert(sizeof(LockWord) == sizeof(int32_t),
1040 "art::LockWord and int32_t have different sizes.");
1041
1042 // Introduce a dependency on the lock_word including rb_state,
1043 // to prevent load-load reordering, and without using
1044 // a memory barrier (which would be more expensive).
1045 // `obj` is unchanged by this operation, but its value now depends
1046 // on `temp`.
1047 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
1048
1049 // The actual reference load.
1050 // A possible implicit null check has already been handled above.
1051 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1052 arm64_codegen->GenerateRawReferenceLoad(instruction_,
1053 ref_,
1054 obj_,
1055 offset_,
1056 index_,
1057 scale_factor_,
1058 /* needs_null_check */ false,
1059 use_load_acquire_);
1060
1061 // Mark the object `ref` when `obj` is gray.
1062 //
1063 // if (rb_state == ReadBarrier::GrayState())
1064 // ref = ReadBarrier::Mark(ref);
1065 //
1066 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1067 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1068 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1069 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1070
1071 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001072 // Note that we cannot use IP to save the old reference, as IP is
1073 // used internally by the ReadBarrierMarkRegX entry point, and we
1074 // need the old reference after the call to that entry point.
1075 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1076 __ Mov(temp_.W(), ref_reg);
1077
Roland Levillain54f869e2017-03-06 13:54:11 +00001078 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001079
1080 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001081 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001082 //
1083 // Note that this field could also hold a different object, if
1084 // another thread had concurrently changed it. In that case, the
1085 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1086 // (CAS) operation below would abort the CAS, leaving the field
1087 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001088 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001089 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001090
1091 // Update the the holder's field atomically. This may fail if
1092 // mutator updates before us, but it's OK. This is achieved
1093 // using a strong compare-and-set (CAS) operation with relaxed
1094 // memory synchronization ordering, where the expected value is
1095 // the old reference and the desired value is the new reference.
1096
1097 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1098 UseScratchRegisterScope temps(masm);
1099
1100 // Convenience aliases.
1101 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001102 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001103 Register expected = temp_.W();
1104 Register value = ref_reg;
1105 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1106 Register tmp_value = temps.AcquireW(); // Value in memory.
1107
1108 __ Add(tmp_ptr, base.X(), Operand(offset));
1109
1110 if (kPoisonHeapReferences) {
1111 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1112 if (value.Is(expected)) {
1113 // Do not poison `value`, as it is the same register as
1114 // `expected`, which has just been poisoned.
1115 } else {
1116 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1117 }
1118 }
1119
1120 // do {
1121 // tmp_value = [tmp_ptr] - expected;
1122 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1123
Roland Levillain24a4d112016-10-26 13:10:46 +01001124 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001125 __ Bind(&loop_head);
1126 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1127 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001128 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001129 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1130 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001131 __ B(&exit_loop);
1132 __ Bind(&comparison_failed);
1133 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001134 __ Bind(&exit_loop);
1135
1136 if (kPoisonHeapReferences) {
1137 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1138 if (value.Is(expected)) {
1139 // Do not unpoison `value`, as it is the same register as
1140 // `expected`, which has just been unpoisoned.
1141 } else {
1142 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1143 }
1144 }
1145
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001146 __ B(GetExitLabel());
1147 }
1148
1149 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001150 // The register containing the object holding the marked object reference field.
1151 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001152 // The offset, index and scale factor to access the reference in `obj_`.
1153 uint32_t offset_;
1154 Location index_;
1155 size_t scale_factor_;
1156 // Is a null check required?
1157 bool needs_null_check_;
1158 // Should this reference load use Load-Acquire semantics?
1159 bool use_load_acquire_;
1160 // A temporary register used to hold the lock word of `obj_`; and
1161 // also to hold the original reference value, when the reference is
1162 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001163 const Register temp_;
1164
Roland Levillain54f869e2017-03-06 13:54:11 +00001165 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001166};
1167
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001168// Slow path generating a read barrier for a heap reference.
1169class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1170 public:
1171 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1172 Location out,
1173 Location ref,
1174 Location obj,
1175 uint32_t offset,
1176 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001177 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001178 out_(out),
1179 ref_(ref),
1180 obj_(obj),
1181 offset_(offset),
1182 index_(index) {
1183 DCHECK(kEmitCompilerReadBarrier);
1184 // If `obj` is equal to `out` or `ref`, it means the initial object
1185 // has been overwritten by (or after) the heap object reference load
1186 // to be instrumented, e.g.:
1187 //
1188 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001189 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001190 //
1191 // In that case, we have lost the information about the original
1192 // object, and the emitted read barrier cannot work properly.
1193 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1194 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1195 }
1196
1197 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1198 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1199 LocationSummary* locations = instruction_->GetLocations();
1200 Primitive::Type type = Primitive::kPrimNot;
1201 DCHECK(locations->CanCall());
1202 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001203 DCHECK(instruction_->IsInstanceFieldGet() ||
1204 instruction_->IsStaticFieldGet() ||
1205 instruction_->IsArrayGet() ||
1206 instruction_->IsInstanceOf() ||
1207 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001208 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001209 << "Unexpected instruction in read barrier for heap reference slow path: "
1210 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001211 // The read barrier instrumentation of object ArrayGet
1212 // instructions does not support the HIntermediateAddress
1213 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001214 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001215 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001216
1217 __ Bind(GetEntryLabel());
1218
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001219 SaveLiveRegisters(codegen, locations);
1220
1221 // We may have to change the index's value, but as `index_` is a
1222 // constant member (like other "inputs" of this slow path),
1223 // introduce a copy of it, `index`.
1224 Location index = index_;
1225 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001226 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001227 if (instruction_->IsArrayGet()) {
1228 // Compute the actual memory offset and store it in `index`.
1229 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
1230 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1231 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1232 // We are about to change the value of `index_reg` (see the
1233 // calls to vixl::MacroAssembler::Lsl and
1234 // vixl::MacroAssembler::Mov below), but it has
1235 // not been saved by the previous call to
1236 // art::SlowPathCode::SaveLiveRegisters, as it is a
1237 // callee-save register --
1238 // art::SlowPathCode::SaveLiveRegisters does not consider
1239 // callee-save registers, as it has been designed with the
1240 // assumption that callee-save registers are supposed to be
1241 // handled by the called function. So, as a callee-save
1242 // register, `index_reg` _would_ eventually be saved onto
1243 // the stack, but it would be too late: we would have
1244 // changed its value earlier. Therefore, we manually save
1245 // it here into another freely available register,
1246 // `free_reg`, chosen of course among the caller-save
1247 // registers (as a callee-save `free_reg` register would
1248 // exhibit the same problem).
1249 //
1250 // Note we could have requested a temporary register from
1251 // the register allocator instead; but we prefer not to, as
1252 // this is a slow path, and we know we can find a
1253 // caller-save register that is available.
1254 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1255 __ Mov(free_reg.W(), index_reg);
1256 index_reg = free_reg;
1257 index = LocationFrom(index_reg);
1258 } else {
1259 // The initial register stored in `index_` has already been
1260 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1261 // (as it is not a callee-save register), so we can freely
1262 // use it.
1263 }
1264 // Shifting the index value contained in `index_reg` by the scale
1265 // factor (2) cannot overflow in practice, as the runtime is
1266 // unable to allocate object arrays with a size larger than
1267 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1268 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1269 static_assert(
1270 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1271 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1272 __ Add(index_reg, index_reg, Operand(offset_));
1273 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001274 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1275 // intrinsics, `index_` is not shifted by a scale factor of 2
1276 // (as in the case of ArrayGet), as it is actually an offset
1277 // to an object field within an object.
1278 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001279 DCHECK(instruction_->GetLocations()->Intrinsified());
1280 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1281 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1282 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001283 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001284 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001285 }
1286 }
1287
1288 // We're moving two or three locations to locations that could
1289 // overlap, so we need a parallel move resolver.
1290 InvokeRuntimeCallingConvention calling_convention;
1291 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1292 parallel_move.AddMove(ref_,
1293 LocationFrom(calling_convention.GetRegisterAt(0)),
1294 type,
1295 nullptr);
1296 parallel_move.AddMove(obj_,
1297 LocationFrom(calling_convention.GetRegisterAt(1)),
1298 type,
1299 nullptr);
1300 if (index.IsValid()) {
1301 parallel_move.AddMove(index,
1302 LocationFrom(calling_convention.GetRegisterAt(2)),
1303 Primitive::kPrimInt,
1304 nullptr);
1305 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1306 } else {
1307 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1308 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1309 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001310 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001311 instruction_,
1312 instruction_->GetDexPc(),
1313 this);
1314 CheckEntrypointTypes<
1315 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1316 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1317
1318 RestoreLiveRegisters(codegen, locations);
1319
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001320 __ B(GetExitLabel());
1321 }
1322
1323 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1324
1325 private:
1326 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001327 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1328 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001329 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1330 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1331 return Register(VIXLRegCodeFromART(i), kXRegSize);
1332 }
1333 }
1334 // We shall never fail to find a free caller-save register, as
1335 // there are more than two core caller-save registers on ARM64
1336 // (meaning it is possible to find one which is different from
1337 // `ref` and `obj`).
1338 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1339 LOG(FATAL) << "Could not find a free register";
1340 UNREACHABLE();
1341 }
1342
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001343 const Location out_;
1344 const Location ref_;
1345 const Location obj_;
1346 const uint32_t offset_;
1347 // An additional location containing an index to an array.
1348 // Only used for HArrayGet and the UnsafeGetObject &
1349 // UnsafeGetObjectVolatile intrinsics.
1350 const Location index_;
1351
1352 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1353};
1354
1355// Slow path generating a read barrier for a GC root.
1356class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1357 public:
1358 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001359 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001360 DCHECK(kEmitCompilerReadBarrier);
1361 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001362
1363 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1364 LocationSummary* locations = instruction_->GetLocations();
1365 Primitive::Type type = Primitive::kPrimNot;
1366 DCHECK(locations->CanCall());
1367 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001368 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1369 << "Unexpected instruction in read barrier for GC root slow path: "
1370 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001371
1372 __ Bind(GetEntryLabel());
1373 SaveLiveRegisters(codegen, locations);
1374
1375 InvokeRuntimeCallingConvention calling_convention;
1376 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1377 // The argument of the ReadBarrierForRootSlow is not a managed
1378 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1379 // thus we need a 64-bit move here, and we cannot use
1380 //
1381 // arm64_codegen->MoveLocation(
1382 // LocationFrom(calling_convention.GetRegisterAt(0)),
1383 // root_,
1384 // type);
1385 //
1386 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1387 // reference type (`Primitive::kPrimNot`).
1388 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001389 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001390 instruction_,
1391 instruction_->GetDexPc(),
1392 this);
1393 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1394 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1395
1396 RestoreLiveRegisters(codegen, locations);
1397 __ B(GetExitLabel());
1398 }
1399
1400 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1401
1402 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001403 const Location out_;
1404 const Location root_;
1405
1406 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1407};
1408
Alexandre Rames5319def2014-10-23 10:03:10 +01001409#undef __
1410
1411Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
1412 Location next_location;
1413 if (type == Primitive::kPrimVoid) {
1414 LOG(FATAL) << "Unreachable type " << type;
1415 }
1416
1417 if (Primitive::IsFloatingPointType(type) &&
1418 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001419 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1420 } else if (!Primitive::IsFloatingPointType(type) &&
1421 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1422 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1423 } else {
1424 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001425 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1426 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001427 }
1428
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001429 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001430 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001431 return next_location;
1432}
1433
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001434Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001435 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001436}
1437
Serban Constantinescu579885a2015-02-22 20:51:33 +00001438CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1439 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001440 const CompilerOptions& compiler_options,
1441 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001442 : CodeGenerator(graph,
1443 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001444 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001445 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001446 callee_saved_core_registers.GetList(),
1447 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001448 compiler_options,
1449 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001450 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001451 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001452 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001453 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001454 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001455 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001456 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001457 uint32_literals_(std::less<uint32_t>(),
1458 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001459 uint64_literals_(std::less<uint64_t>(),
1460 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001461 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001462 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001463 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001464 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001465 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001466 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001467 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001468 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1469 jit_class_patches_(TypeReferenceValueComparator(),
1470 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001471 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001472 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001473}
Alexandre Rames5319def2014-10-23 10:03:10 +01001474
Alexandre Rames67555f72014-11-18 10:55:16 +00001475#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001476
Zheng Xu3927c8b2015-11-18 17:46:25 +08001477void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001478 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001479 jump_table->EmitTable(this);
1480 }
1481}
1482
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001483void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001484 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001485 // Ensure we emit the literal pool.
1486 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001487
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001488 CodeGenerator::Finalize(allocator);
1489}
1490
Zheng Xuad4450e2015-04-17 18:48:56 +08001491void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1492 // Note: There are 6 kinds of moves:
1493 // 1. constant -> GPR/FPR (non-cycle)
1494 // 2. constant -> stack (non-cycle)
1495 // 3. GPR/FPR -> GPR/FPR
1496 // 4. GPR/FPR -> stack
1497 // 5. stack -> GPR/FPR
1498 // 6. stack -> stack (non-cycle)
1499 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1500 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1501 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1502 // dependency.
1503 vixl_temps_.Open(GetVIXLAssembler());
1504}
1505
1506void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1507 vixl_temps_.Close();
1508}
1509
1510Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001511 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1512 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1513 || kind == Location::kSIMDStackSlot);
1514 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1515 ? Location::kFpuRegister
1516 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001517 Location scratch = GetScratchLocation(kind);
1518 if (!scratch.Equals(Location::NoLocation())) {
1519 return scratch;
1520 }
1521 // Allocate from VIXL temp registers.
1522 if (kind == Location::kRegister) {
1523 scratch = LocationFrom(vixl_temps_.AcquireX());
1524 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001525 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001526 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1527 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1528 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001529 }
1530 AddScratchLocation(scratch);
1531 return scratch;
1532}
1533
1534void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1535 if (loc.IsRegister()) {
1536 vixl_temps_.Release(XRegisterFrom(loc));
1537 } else {
1538 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001539 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001540 }
1541 RemoveScratchLocation(loc);
1542}
1543
Alexandre Rames3e69f162014-12-10 10:36:50 +00001544void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001545 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001546 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001547}
1548
Alexandre Rames5319def2014-10-23 10:03:10 +01001549void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001550 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001551 __ Bind(&frame_entry_label_);
1552
Serban Constantinescu02164b32014-11-13 14:05:07 +00001553 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1554 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001555 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001556 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001557 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001558 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001559 {
1560 // Ensure that between load and RecordPcInfo there are no pools emitted.
1561 ExactAssemblyScope eas(GetVIXLAssembler(),
1562 kInstructionSize,
1563 CodeBufferCheckScope::kExactSize);
1564 __ ldr(wzr, MemOperand(temp, 0));
1565 RecordPcInfo(nullptr, 0);
1566 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001567 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001568
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001569 if (!HasEmptyFrame()) {
1570 int frame_size = GetFrameSize();
1571 // Stack layout:
1572 // sp[frame_size - 8] : lr.
1573 // ... : other preserved core registers.
1574 // ... : other preserved fp registers.
1575 // ... : reserved frame space.
1576 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001577
1578 // Save the current method if we need it. Note that we do not
1579 // do this in HCurrentMethod, as the instruction might have been removed
1580 // in the SSA graph.
1581 if (RequiresCurrentMethod()) {
1582 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001583 } else {
1584 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001585 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001586 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001587 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1588 frame_size - GetCoreSpillSize());
1589 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1590 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001591
1592 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1593 // Initialize should_deoptimize flag to 0.
1594 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1595 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1596 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001597 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001598}
1599
1600void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001601 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001602 if (!HasEmptyFrame()) {
1603 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001604 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1605 frame_size - FrameEntrySpillSize());
1606 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1607 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001608 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001609 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001610 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001611 __ Ret();
1612 GetAssembler()->cfi().RestoreState();
1613 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001614}
1615
Scott Wakeling97c72b72016-06-24 16:19:36 +01001616CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001617 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001618 return CPURegList(CPURegister::kRegister, kXRegSize,
1619 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001620}
1621
Scott Wakeling97c72b72016-06-24 16:19:36 +01001622CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001623 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1624 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001625 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1626 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001627}
1628
Alexandre Rames5319def2014-10-23 10:03:10 +01001629void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1630 __ Bind(GetLabelOf(block));
1631}
1632
Calin Juravle175dc732015-08-25 15:42:32 +01001633void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1634 DCHECK(location.IsRegister());
1635 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1636}
1637
Calin Juravlee460d1d2015-09-29 04:52:17 +01001638void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1639 if (location.IsRegister()) {
1640 locations->AddTemp(location);
1641 } else {
1642 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1643 }
1644}
1645
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001646void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001647 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001648 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001649 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001650 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001651 if (value_can_be_null) {
1652 __ Cbz(value, &done);
1653 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001654 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001655 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001656 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001657 if (value_can_be_null) {
1658 __ Bind(&done);
1659 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001660}
1661
David Brazdil58282f42016-01-14 12:45:10 +00001662void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001663 // Blocked core registers:
1664 // lr : Runtime reserved.
1665 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001666 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001667 // ip1 : VIXL core temp.
1668 // ip0 : VIXL core temp.
1669 //
1670 // Blocked fp registers:
1671 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001672 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1673 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001674 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001675 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001676 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001677
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001678 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001679 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001680 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001681 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001682
David Brazdil58282f42016-01-14 12:45:10 +00001683 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001684 // Stubs do not save callee-save floating point registers. If the graph
1685 // is debuggable, we need to deal with these registers differently. For
1686 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001687 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1688 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001689 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001690 }
1691 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001692}
1693
Alexandre Rames3e69f162014-12-10 10:36:50 +00001694size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1695 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1696 __ Str(reg, MemOperand(sp, stack_index));
1697 return kArm64WordSize;
1698}
1699
1700size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1701 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1702 __ Ldr(reg, MemOperand(sp, stack_index));
1703 return kArm64WordSize;
1704}
1705
1706size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1707 FPRegister reg = FPRegister(reg_id, kDRegSize);
1708 __ Str(reg, MemOperand(sp, stack_index));
1709 return kArm64WordSize;
1710}
1711
1712size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1713 FPRegister reg = FPRegister(reg_id, kDRegSize);
1714 __ Ldr(reg, MemOperand(sp, stack_index));
1715 return kArm64WordSize;
1716}
1717
Alexandre Rames5319def2014-10-23 10:03:10 +01001718void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001719 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001720}
1721
1722void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001723 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001724}
1725
Alexandre Rames67555f72014-11-18 10:55:16 +00001726void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001727 if (constant->IsIntConstant()) {
1728 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1729 } else if (constant->IsLongConstant()) {
1730 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1731 } else if (constant->IsNullConstant()) {
1732 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001733 } else if (constant->IsFloatConstant()) {
1734 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1735 } else {
1736 DCHECK(constant->IsDoubleConstant());
1737 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1738 }
1739}
1740
Alexandre Rames3e69f162014-12-10 10:36:50 +00001741
1742static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1743 DCHECK(constant.IsConstant());
1744 HConstant* cst = constant.GetConstant();
1745 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001746 // Null is mapped to a core W register, which we associate with kPrimInt.
1747 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001748 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1749 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1750 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1751}
1752
Roland Levillain952b2352017-05-03 19:49:14 +01001753// Allocate a scratch register from the VIXL pool, querying first
1754// the floating-point register pool, and then the core register
1755// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001756// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1757// using a different allocation strategy.
1758static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1759 vixl::aarch64::UseScratchRegisterScope* temps,
1760 int size_in_bits) {
1761 return masm->GetScratchFPRegisterList()->IsEmpty()
1762 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1763 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1764}
1765
Calin Juravlee460d1d2015-09-29 04:52:17 +01001766void CodeGeneratorARM64::MoveLocation(Location destination,
1767 Location source,
1768 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001769 if (source.Equals(destination)) {
1770 return;
1771 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001772
1773 // A valid move can always be inferred from the destination and source
1774 // locations. When moving from and to a register, the argument type can be
1775 // used to generate 32bit instead of 64bit moves. In debug mode we also
1776 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001777 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001778
1779 if (destination.IsRegister() || destination.IsFpuRegister()) {
1780 if (unspecified_type) {
1781 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1782 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001783 (src_cst != nullptr && (src_cst->IsIntConstant()
1784 || src_cst->IsFloatConstant()
1785 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001786 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001787 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001788 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001789 // If the source is a double stack slot or a 64bit constant, a 64bit
1790 // type is appropriate. Else the source is a register, and since the
1791 // type has not been specified, we chose a 64bit type to force a 64bit
1792 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001793 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001794 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001795 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001796 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1797 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1798 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001799 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1800 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1801 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001802 } else if (source.IsSIMDStackSlot()) {
1803 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001804 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001805 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001806 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001807 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001808 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001809 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001810 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001811 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001812 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1813 ? Primitive::kPrimLong
1814 : Primitive::kPrimInt;
1815 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1816 }
1817 } else {
1818 DCHECK(source.IsFpuRegister());
1819 if (destination.IsRegister()) {
1820 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1821 ? Primitive::kPrimDouble
1822 : Primitive::kPrimFloat;
1823 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1824 } else {
1825 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001826 if (GetGraph()->HasSIMD()) {
1827 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1828 } else {
1829 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1830 }
1831 }
1832 }
1833 } else if (destination.IsSIMDStackSlot()) {
1834 if (source.IsFpuRegister()) {
1835 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1836 } else {
1837 DCHECK(source.IsSIMDStackSlot());
1838 UseScratchRegisterScope temps(GetVIXLAssembler());
1839 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1840 Register temp = temps.AcquireX();
1841 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1842 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1843 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1844 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1845 } else {
1846 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1847 __ Ldr(temp, StackOperandFrom(source));
1848 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001849 }
1850 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001851 } else { // The destination is not a register. It must be a stack slot.
1852 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1853 if (source.IsRegister() || source.IsFpuRegister()) {
1854 if (unspecified_type) {
1855 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001856 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001857 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001858 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001859 }
1860 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001861 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1862 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1863 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001864 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001865 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1866 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001867 UseScratchRegisterScope temps(GetVIXLAssembler());
1868 HConstant* src_cst = source.GetConstant();
1869 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001870 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001871 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1872 ? Register(xzr)
1873 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001874 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001875 if (src_cst->IsIntConstant()) {
1876 temp = temps.AcquireW();
1877 } else if (src_cst->IsLongConstant()) {
1878 temp = temps.AcquireX();
1879 } else if (src_cst->IsFloatConstant()) {
1880 temp = temps.AcquireS();
1881 } else {
1882 DCHECK(src_cst->IsDoubleConstant());
1883 temp = temps.AcquireD();
1884 }
1885 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001886 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001887 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001888 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001889 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001890 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001891 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001892 // Use any scratch register (a core or a floating-point one)
1893 // from VIXL scratch register pools as a temporary.
1894 //
1895 // We used to only use the FP scratch register pool, but in some
1896 // rare cases the only register from this pool (D31) would
1897 // already be used (e.g. within a ParallelMove instruction, when
1898 // a move is blocked by a another move requiring a scratch FP
1899 // register, which would reserve D31). To prevent this issue, we
1900 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001901 //
1902 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001903 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001904 // use AcquireFPOrCoreCPURegisterOfSize instead of
1905 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1906 // allocates core scratch registers first.
1907 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1908 GetVIXLAssembler(),
1909 &temps,
1910 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001911 __ Ldr(temp, StackOperandFrom(source));
1912 __ Str(temp, StackOperandFrom(destination));
1913 }
1914 }
1915}
1916
1917void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001918 CPURegister dst,
1919 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001920 switch (type) {
1921 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001922 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001923 break;
1924 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001925 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001926 break;
1927 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001928 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001929 break;
1930 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001931 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001932 break;
1933 case Primitive::kPrimInt:
1934 case Primitive::kPrimNot:
1935 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001936 case Primitive::kPrimFloat:
1937 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001938 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001939 __ Ldr(dst, src);
1940 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001941 case Primitive::kPrimVoid:
1942 LOG(FATAL) << "Unreachable type " << type;
1943 }
1944}
1945
Calin Juravle77520bc2015-01-12 18:45:46 +00001946void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001947 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001948 const MemOperand& src,
1949 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001950 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001951 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001952 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001953 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001954
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001955 DCHECK(!src.IsPreIndex());
1956 DCHECK(!src.IsPostIndex());
1957
1958 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001959 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001960 {
1961 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1962 MemOperand base = MemOperand(temp_base);
1963 switch (type) {
1964 case Primitive::kPrimBoolean:
1965 {
1966 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1967 __ ldarb(Register(dst), base);
1968 if (needs_null_check) {
1969 MaybeRecordImplicitNullCheck(instruction);
1970 }
1971 }
1972 break;
1973 case Primitive::kPrimByte:
1974 {
1975 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1976 __ ldarb(Register(dst), base);
1977 if (needs_null_check) {
1978 MaybeRecordImplicitNullCheck(instruction);
1979 }
1980 }
1981 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1982 break;
1983 case Primitive::kPrimChar:
1984 {
1985 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1986 __ ldarh(Register(dst), base);
1987 if (needs_null_check) {
1988 MaybeRecordImplicitNullCheck(instruction);
1989 }
1990 }
1991 break;
1992 case Primitive::kPrimShort:
1993 {
1994 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1995 __ ldarh(Register(dst), base);
1996 if (needs_null_check) {
1997 MaybeRecordImplicitNullCheck(instruction);
1998 }
1999 }
2000 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
2001 break;
2002 case Primitive::kPrimInt:
2003 case Primitive::kPrimNot:
2004 case Primitive::kPrimLong:
2005 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
2006 {
2007 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2008 __ ldar(Register(dst), base);
2009 if (needs_null_check) {
2010 MaybeRecordImplicitNullCheck(instruction);
2011 }
2012 }
2013 break;
2014 case Primitive::kPrimFloat:
2015 case Primitive::kPrimDouble: {
2016 DCHECK(dst.IsFPRegister());
2017 DCHECK_EQ(dst.Is64Bits(), Primitive::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 }
Artem Serov914d7a82017-02-07 14:33:49 +00002030 case Primitive::kPrimVoid:
2031 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002032 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002033 }
2034}
2035
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002036void CodeGeneratorARM64::Store(Primitive::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) {
2040 case Primitive::kPrimBoolean:
2041 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002042 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002043 break;
2044 case Primitive::kPrimChar:
2045 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002046 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002047 break;
2048 case Primitive::kPrimInt:
2049 case Primitive::kPrimNot:
2050 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002051 case Primitive::kPrimFloat:
2052 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00002053 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002054 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002055 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002056 case Primitive::kPrimVoid:
2057 LOG(FATAL) << "Unreachable type " << type;
2058 }
2059}
2060
Artem Serov914d7a82017-02-07 14:33:49 +00002061void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
2062 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002063 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002064 const MemOperand& dst,
2065 bool needs_null_check) {
2066 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002067 UseScratchRegisterScope temps(GetVIXLAssembler());
2068 Register temp_base = temps.AcquireX();
2069
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002070 DCHECK(!dst.IsPreIndex());
2071 DCHECK(!dst.IsPostIndex());
2072
2073 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002074 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002075 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002076 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002077 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002078 switch (type) {
2079 case Primitive::kPrimBoolean:
2080 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00002081 {
2082 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2083 __ stlrb(Register(src), base);
2084 if (needs_null_check) {
2085 MaybeRecordImplicitNullCheck(instruction);
2086 }
2087 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002088 break;
2089 case Primitive::kPrimChar:
2090 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00002091 {
2092 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2093 __ stlrh(Register(src), base);
2094 if (needs_null_check) {
2095 MaybeRecordImplicitNullCheck(instruction);
2096 }
2097 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002098 break;
2099 case Primitive::kPrimInt:
2100 case Primitive::kPrimNot:
2101 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002102 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002103 {
2104 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2105 __ stlr(Register(src), base);
2106 if (needs_null_check) {
2107 MaybeRecordImplicitNullCheck(instruction);
2108 }
2109 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002110 break;
2111 case Primitive::kPrimFloat:
2112 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002113 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002114 Register temp_src;
2115 if (src.IsZero()) {
2116 // The zero register is used to avoid synthesizing zero constants.
2117 temp_src = Register(src);
2118 } else {
2119 DCHECK(src.IsFPRegister());
2120 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2121 __ Fmov(temp_src, FPRegister(src));
2122 }
Artem Serov914d7a82017-02-07 14:33:49 +00002123 {
2124 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2125 __ stlr(temp_src, base);
2126 if (needs_null_check) {
2127 MaybeRecordImplicitNullCheck(instruction);
2128 }
2129 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002130 break;
2131 }
2132 case Primitive::kPrimVoid:
2133 LOG(FATAL) << "Unreachable type " << type;
2134 }
2135}
2136
Calin Juravle175dc732015-08-25 15:42:32 +01002137void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2138 HInstruction* instruction,
2139 uint32_t dex_pc,
2140 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002141 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002142
2143 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2144 {
2145 // Ensure the pc position is recorded immediately after the `blr` instruction.
2146 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2147 __ blr(lr);
2148 if (EntrypointRequiresStackMap(entrypoint)) {
2149 RecordPcInfo(instruction, dex_pc, slow_path);
2150 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002151 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002152}
2153
Roland Levillaindec8f632016-07-22 17:10:06 +01002154void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2155 HInstruction* instruction,
2156 SlowPathCode* slow_path) {
2157 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002158 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2159 __ Blr(lr);
2160}
2161
Alexandre Rames67555f72014-11-18 10:55:16 +00002162void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002163 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002164 UseScratchRegisterScope temps(GetVIXLAssembler());
2165 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002166 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2167
Serban Constantinescu02164b32014-11-13 14:05:07 +00002168 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002169 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2170 __ Add(temp, class_reg, status_offset);
2171 __ Ldar(temp, HeapOperand(temp));
2172 __ Cmp(temp, mirror::Class::kStatusInitialized);
2173 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002174 __ Bind(slow_path->GetExitLabel());
2175}
Alexandre Rames5319def2014-10-23 10:03:10 +01002176
Roland Levillain44015862016-01-22 11:47:17 +00002177void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002178 BarrierType type = BarrierAll;
2179
2180 switch (kind) {
2181 case MemBarrierKind::kAnyAny:
2182 case MemBarrierKind::kAnyStore: {
2183 type = BarrierAll;
2184 break;
2185 }
2186 case MemBarrierKind::kLoadAny: {
2187 type = BarrierReads;
2188 break;
2189 }
2190 case MemBarrierKind::kStoreStore: {
2191 type = BarrierWrites;
2192 break;
2193 }
2194 default:
2195 LOG(FATAL) << "Unexpected memory barrier " << kind;
2196 }
2197 __ Dmb(InnerShareable, type);
2198}
2199
Serban Constantinescu02164b32014-11-13 14:05:07 +00002200void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2201 HBasicBlock* successor) {
2202 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002203 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2204 if (slow_path == nullptr) {
2205 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2206 instruction->SetSlowPath(slow_path);
2207 codegen_->AddSlowPath(slow_path);
2208 if (successor != nullptr) {
2209 DCHECK(successor->IsLoopHeader());
2210 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2211 }
2212 } else {
2213 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2214 }
2215
Serban Constantinescu02164b32014-11-13 14:05:07 +00002216 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2217 Register temp = temps.AcquireW();
2218
Andreas Gampe542451c2016-07-26 09:02:02 -07002219 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002220 if (successor == nullptr) {
2221 __ Cbnz(temp, slow_path->GetEntryLabel());
2222 __ Bind(slow_path->GetReturnLabel());
2223 } else {
2224 __ Cbz(temp, codegen_->GetLabelOf(successor));
2225 __ B(slow_path->GetEntryLabel());
2226 // slow_path will return to GetLabelOf(successor).
2227 }
2228}
2229
Alexandre Rames5319def2014-10-23 10:03:10 +01002230InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2231 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002232 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002233 assembler_(codegen->GetAssembler()),
2234 codegen_(codegen) {}
2235
2236#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002237 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002238
2239#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2240
2241enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002242 // Using a base helps identify when we hit such breakpoints.
2243 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002244#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2245 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2246#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2247};
2248
2249#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002250 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002251 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2252 } \
2253 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2254 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2255 locations->SetOut(Location::Any()); \
2256 }
2257 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2258#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2259
2260#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002261#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002262
Alexandre Rames67555f72014-11-18 10:55:16 +00002263void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002264 DCHECK_EQ(instr->InputCount(), 2U);
2265 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2266 Primitive::Type type = instr->GetResultType();
2267 switch (type) {
2268 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002269 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002270 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002271 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002272 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002273 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002274
2275 case Primitive::kPrimFloat:
2276 case Primitive::kPrimDouble:
2277 locations->SetInAt(0, Location::RequiresFpuRegister());
2278 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002279 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002280 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002281
Alexandre Rames5319def2014-10-23 10:03:10 +01002282 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002283 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002284 }
2285}
2286
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002287void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2288 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002289 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2290
2291 bool object_field_get_with_read_barrier =
2292 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002293 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002294 new (GetGraph()->GetArena()) LocationSummary(instruction,
2295 object_field_get_with_read_barrier ?
2296 LocationSummary::kCallOnSlowPath :
2297 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002298 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002299 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002300 // We need a temporary register for the read barrier marking slow
2301 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002302 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2303 !Runtime::Current()->UseJitCompilation() &&
2304 !field_info.IsVolatile()) {
2305 // If link-time thunks for the Baker read barrier are enabled, for AOT
2306 // non-volatile loads we need a temporary only if the offset is too big.
2307 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2308 locations->AddTemp(FixedTempLocation());
2309 }
2310 } else {
2311 locations->AddTemp(Location::RequiresRegister());
2312 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002313 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002314 locations->SetInAt(0, Location::RequiresRegister());
2315 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2316 locations->SetOut(Location::RequiresFpuRegister());
2317 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002318 // The output overlaps for an object field get when read barriers
2319 // are enabled: we do not want the load to overwrite the object's
2320 // location, as we need it to emit the read barrier.
2321 locations->SetOut(
2322 Location::RequiresRegister(),
2323 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002324 }
2325}
2326
2327void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2328 const FieldInfo& field_info) {
2329 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002330 LocationSummary* locations = instruction->GetLocations();
2331 Location base_loc = locations->InAt(0);
2332 Location out = locations->Out();
2333 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002334 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002335 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002336
Roland Levillain44015862016-01-22 11:47:17 +00002337 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2338 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002339 // /* HeapReference<Object> */ out = *(base + offset)
2340 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002341 Location maybe_temp =
2342 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002343 // Note that potential implicit null checks are handled in this
2344 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2345 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2346 instruction,
2347 out,
2348 base,
2349 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002350 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002351 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002352 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002353 } else {
2354 // General case.
2355 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002356 // Note that a potential implicit null check is handled in this
2357 // CodeGeneratorARM64::LoadAcquire call.
2358 // NB: LoadAcquire will record the pc info if needed.
2359 codegen_->LoadAcquire(
2360 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002361 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002362 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2363 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002364 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002365 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002366 }
Roland Levillain44015862016-01-22 11:47:17 +00002367 if (field_type == Primitive::kPrimNot) {
2368 // If read barriers are enabled, emit read barriers other than
2369 // Baker's using a slow path (and also unpoison the loaded
2370 // reference, if heap poisoning is enabled).
2371 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2372 }
Roland Levillain4d027112015-07-01 15:41:14 +01002373 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002374}
2375
2376void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2377 LocationSummary* locations =
2378 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2379 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002380 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2381 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2382 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002383 locations->SetInAt(1, Location::RequiresFpuRegister());
2384 } else {
2385 locations->SetInAt(1, Location::RequiresRegister());
2386 }
2387}
2388
2389void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002390 const FieldInfo& field_info,
2391 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002392 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2393
2394 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002395 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002396 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002397 Offset offset = field_info.GetFieldOffset();
2398 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002399
Roland Levillain4d027112015-07-01 15:41:14 +01002400 {
2401 // We use a block to end the scratch scope before the write barrier, thus
2402 // freeing the temporary registers so they can be used in `MarkGCCard`.
2403 UseScratchRegisterScope temps(GetVIXLAssembler());
2404
2405 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2406 DCHECK(value.IsW());
2407 Register temp = temps.AcquireW();
2408 __ Mov(temp, value.W());
2409 GetAssembler()->PoisonHeapReference(temp.W());
2410 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002411 }
Roland Levillain4d027112015-07-01 15:41:14 +01002412
2413 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002414 codegen_->StoreRelease(
2415 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002416 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002417 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2418 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002419 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2420 codegen_->MaybeRecordImplicitNullCheck(instruction);
2421 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002422 }
2423
2424 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002425 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002426 }
2427}
2428
Alexandre Rames67555f72014-11-18 10:55:16 +00002429void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002430 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002431
2432 switch (type) {
2433 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002434 case Primitive::kPrimLong: {
2435 Register dst = OutputRegister(instr);
2436 Register lhs = InputRegisterAt(instr, 0);
2437 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002438 if (instr->IsAdd()) {
2439 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002440 } else if (instr->IsAnd()) {
2441 __ And(dst, lhs, rhs);
2442 } else if (instr->IsOr()) {
2443 __ Orr(dst, lhs, rhs);
2444 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002445 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002446 } else if (instr->IsRor()) {
2447 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002448 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002449 __ Ror(dst, lhs, shift);
2450 } else {
2451 // Ensure shift distance is in the same size register as the result. If
2452 // we are rotating a long and the shift comes in a w register originally,
2453 // we don't need to sxtw for use as an x since the shift distances are
2454 // all & reg_bits - 1.
2455 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2456 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002457 } else {
2458 DCHECK(instr->IsXor());
2459 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002460 }
2461 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002462 }
2463 case Primitive::kPrimFloat:
2464 case Primitive::kPrimDouble: {
2465 FPRegister dst = OutputFPRegister(instr);
2466 FPRegister lhs = InputFPRegisterAt(instr, 0);
2467 FPRegister rhs = InputFPRegisterAt(instr, 1);
2468 if (instr->IsAdd()) {
2469 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002470 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002471 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002472 } else {
2473 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002474 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002475 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002476 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002477 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002478 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002479 }
2480}
2481
Serban Constantinescu02164b32014-11-13 14:05:07 +00002482void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2483 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2484
2485 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2486 Primitive::Type type = instr->GetResultType();
2487 switch (type) {
2488 case Primitive::kPrimInt:
2489 case Primitive::kPrimLong: {
2490 locations->SetInAt(0, Location::RequiresRegister());
2491 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002492 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002493 break;
2494 }
2495 default:
2496 LOG(FATAL) << "Unexpected shift type " << type;
2497 }
2498}
2499
2500void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2501 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2502
2503 Primitive::Type type = instr->GetType();
2504 switch (type) {
2505 case Primitive::kPrimInt:
2506 case Primitive::kPrimLong: {
2507 Register dst = OutputRegister(instr);
2508 Register lhs = InputRegisterAt(instr, 0);
2509 Operand rhs = InputOperandAt(instr, 1);
2510 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002511 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002512 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002513 if (instr->IsShl()) {
2514 __ Lsl(dst, lhs, shift_value);
2515 } else if (instr->IsShr()) {
2516 __ Asr(dst, lhs, shift_value);
2517 } else {
2518 __ Lsr(dst, lhs, shift_value);
2519 }
2520 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002521 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002522
2523 if (instr->IsShl()) {
2524 __ Lsl(dst, lhs, rhs_reg);
2525 } else if (instr->IsShr()) {
2526 __ Asr(dst, lhs, rhs_reg);
2527 } else {
2528 __ Lsr(dst, lhs, rhs_reg);
2529 }
2530 }
2531 break;
2532 }
2533 default:
2534 LOG(FATAL) << "Unexpected shift operation type " << type;
2535 }
2536}
2537
Alexandre Rames5319def2014-10-23 10:03:10 +01002538void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002539 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002540}
2541
2542void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002543 HandleBinaryOp(instruction);
2544}
2545
2546void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2547 HandleBinaryOp(instruction);
2548}
2549
2550void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2551 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002552}
2553
Artem Serov7fc63502016-02-09 17:15:29 +00002554void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002555 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2556 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2557 locations->SetInAt(0, Location::RequiresRegister());
2558 // There is no immediate variant of negated bitwise instructions in AArch64.
2559 locations->SetInAt(1, Location::RequiresRegister());
2560 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2561}
2562
Artem Serov7fc63502016-02-09 17:15:29 +00002563void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002564 Register dst = OutputRegister(instr);
2565 Register lhs = InputRegisterAt(instr, 0);
2566 Register rhs = InputRegisterAt(instr, 1);
2567
2568 switch (instr->GetOpKind()) {
2569 case HInstruction::kAnd:
2570 __ Bic(dst, lhs, rhs);
2571 break;
2572 case HInstruction::kOr:
2573 __ Orn(dst, lhs, rhs);
2574 break;
2575 case HInstruction::kXor:
2576 __ Eon(dst, lhs, rhs);
2577 break;
2578 default:
2579 LOG(FATAL) << "Unreachable";
2580 }
2581}
2582
Anton Kirilov74234da2017-01-13 14:42:47 +00002583void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2584 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002585 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2586 instruction->GetType() == Primitive::kPrimLong);
2587 LocationSummary* locations =
2588 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2589 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2590 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2591 } else {
2592 locations->SetInAt(0, Location::RequiresRegister());
2593 }
2594 locations->SetInAt(1, Location::RequiresRegister());
2595 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2596}
2597
Anton Kirilov74234da2017-01-13 14:42:47 +00002598void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2599 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002600 Primitive::Type type = instruction->GetType();
2601 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2602 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2603 Register out = OutputRegister(instruction);
2604 Register left;
2605 if (kind != HInstruction::kNeg) {
2606 left = InputRegisterAt(instruction, 0);
2607 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002608 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002609 // shifter operand operation, the IR generating `right_reg` (input to the type
2610 // conversion) can have a different type from the current instruction's type,
2611 // so we manually indicate the type.
2612 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002613 Operand right_operand(0);
2614
Anton Kirilov74234da2017-01-13 14:42:47 +00002615 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2616 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002617 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2618 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002619 right_operand = Operand(right_reg,
2620 helpers::ShiftFromOpKind(op_kind),
2621 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002622 }
2623
2624 // Logical binary operations do not support extension operations in the
2625 // operand. Note that VIXL would still manage if it was passed by generating
2626 // the extension as a separate instruction.
2627 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2628 DCHECK(!right_operand.IsExtendedRegister() ||
2629 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2630 kind != HInstruction::kNeg));
2631 switch (kind) {
2632 case HInstruction::kAdd:
2633 __ Add(out, left, right_operand);
2634 break;
2635 case HInstruction::kAnd:
2636 __ And(out, left, right_operand);
2637 break;
2638 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002639 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002640 __ Neg(out, right_operand);
2641 break;
2642 case HInstruction::kOr:
2643 __ Orr(out, left, right_operand);
2644 break;
2645 case HInstruction::kSub:
2646 __ Sub(out, left, right_operand);
2647 break;
2648 case HInstruction::kXor:
2649 __ Eor(out, left, right_operand);
2650 break;
2651 default:
2652 LOG(FATAL) << "Unexpected operation kind: " << kind;
2653 UNREACHABLE();
2654 }
2655}
2656
Artem Serov328429f2016-07-06 16:23:04 +01002657void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002658 LocationSummary* locations =
2659 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2660 locations->SetInAt(0, Location::RequiresRegister());
2661 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002662 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002663}
2664
Roland Levillain19c54192016-11-04 13:44:09 +00002665void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002666 __ Add(OutputRegister(instruction),
2667 InputRegisterAt(instruction, 0),
2668 Operand(InputOperandAt(instruction, 1)));
2669}
2670
Artem Serove1811ed2017-04-27 16:50:47 +01002671void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2672 LocationSummary* locations =
2673 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2674
2675 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2676
2677 locations->SetInAt(0, Location::RequiresRegister());
2678 // For byte case we don't need to shift the index variable so we can encode the data offset into
2679 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2680 // data offset constant generation out of the loop and reduce the critical path length in the
2681 // loop.
2682 locations->SetInAt(1, shift->GetValue() == 0
2683 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2684 : Location::RequiresRegister());
2685 locations->SetInAt(2, Location::ConstantLocation(shift));
2686 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2687}
2688
2689void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2690 HIntermediateAddressIndex* instruction) {
2691 Register index_reg = InputRegisterAt(instruction, 0);
2692 uint32_t shift = Int64ConstantFrom(instruction->GetLocations()->InAt(2));
2693 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2694
2695 if (shift == 0) {
2696 __ Add(OutputRegister(instruction), index_reg, offset);
2697 } else {
2698 Register offset_reg = InputRegisterAt(instruction, 1);
2699 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2700 }
2701}
2702
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002703void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002704 LocationSummary* locations =
2705 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002706 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2707 if (instr->GetOpKind() == HInstruction::kSub &&
2708 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002709 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002710 // Don't allocate register for Mneg instruction.
2711 } else {
2712 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2713 Location::RequiresRegister());
2714 }
2715 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2716 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002717 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2718}
2719
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002720void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002721 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002722 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2723 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002724
2725 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2726 // This fixup should be carried out for all multiply-accumulate instructions:
2727 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2728 if (instr->GetType() == Primitive::kPrimLong &&
2729 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2730 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002731 vixl::aarch64::Instruction* prev =
2732 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002733 if (prev->IsLoadOrStore()) {
2734 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002735 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002736 __ nop();
2737 }
2738 }
2739
2740 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002741 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002742 __ Madd(res, mul_left, mul_right, accumulator);
2743 } else {
2744 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002745 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002746 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002747 __ Mneg(res, mul_left, mul_right);
2748 } else {
2749 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2750 __ Msub(res, mul_left, mul_right, accumulator);
2751 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002752 }
2753}
2754
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002755void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002756 bool object_array_get_with_read_barrier =
2757 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002758 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002759 new (GetGraph()->GetArena()) LocationSummary(instruction,
2760 object_array_get_with_read_barrier ?
2761 LocationSummary::kCallOnSlowPath :
2762 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002763 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002764 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002765 // We need a temporary register for the read barrier marking slow
2766 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002767 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2768 !Runtime::Current()->UseJitCompilation() &&
2769 instruction->GetIndex()->IsConstant()) {
2770 // Array loads with constant index are treated as field loads.
2771 // If link-time thunks for the Baker read barrier are enabled, for AOT
2772 // constant index loads we need a temporary only if the offset is too big.
2773 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2774 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
2775 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
2776 if (offset >= kReferenceLoadMinFarOffset) {
2777 locations->AddTemp(FixedTempLocation());
2778 }
2779 } else {
2780 locations->AddTemp(Location::RequiresRegister());
2781 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002782 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002783 locations->SetInAt(0, Location::RequiresRegister());
2784 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002785 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2786 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2787 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002788 // The output overlaps in the case of an object array get with
2789 // read barriers enabled: we do not want the move to overwrite the
2790 // array's location, as we need it to emit the read barrier.
2791 locations->SetOut(
2792 Location::RequiresRegister(),
2793 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002794 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002795}
2796
2797void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002798 Primitive::Type type = instruction->GetType();
2799 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002800 LocationSummary* locations = instruction->GetLocations();
2801 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002802 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002803 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002804 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2805 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002806 MacroAssembler* masm = GetVIXLAssembler();
2807 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002808
Roland Levillain19c54192016-11-04 13:44:09 +00002809 // The read barrier instrumentation of object ArrayGet instructions
2810 // does not support the HIntermediateAddress instruction.
2811 DCHECK(!((type == Primitive::kPrimNot) &&
2812 instruction->GetArray()->IsIntermediateAddress() &&
2813 kEmitCompilerReadBarrier));
2814
Roland Levillain44015862016-01-22 11:47:17 +00002815 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2816 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002817 // Note that a potential implicit null check is handled in the
2818 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002819 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002820 if (index.IsConstant()) {
2821 // Array load with a constant index can be treated as a field load.
2822 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2823 Location maybe_temp =
2824 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2825 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2826 out,
2827 obj.W(),
2828 offset,
2829 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002830 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002831 /* use_load_acquire */ false);
2832 } else {
2833 Register temp = WRegisterFrom(locations->GetTemp(0));
2834 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002835 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002836 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002837 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002838 // General case.
2839 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002840 Register length;
2841 if (maybe_compressed_char_at) {
2842 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2843 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002844 {
2845 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2846 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2847
2848 if (instruction->GetArray()->IsIntermediateAddress()) {
2849 DCHECK_LT(count_offset, offset);
2850 int64_t adjusted_offset =
2851 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2852 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2853 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2854 } else {
2855 __ Ldr(length, HeapOperand(obj, count_offset));
2856 }
2857 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002858 }
jessicahandojo05765752016-09-09 19:01:32 -07002859 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002860 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002861 if (maybe_compressed_char_at) {
2862 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002863 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2864 "Expecting 0=compressed, 1=uncompressed");
2865 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002866 __ Ldrb(Register(OutputCPURegister(instruction)),
2867 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2868 __ B(&done);
2869 __ Bind(&uncompressed_load);
2870 __ Ldrh(Register(OutputCPURegister(instruction)),
2871 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2872 __ Bind(&done);
2873 } else {
2874 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2875 source = HeapOperand(obj, offset);
2876 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002877 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002878 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002879 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002880 // We do not need to compute the intermediate address from the array: the
2881 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002882 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002883 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002884 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002885 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2886 }
2887 temp = obj;
2888 } else {
2889 __ Add(temp, obj, offset);
2890 }
jessicahandojo05765752016-09-09 19:01:32 -07002891 if (maybe_compressed_char_at) {
2892 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002893 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2894 "Expecting 0=compressed, 1=uncompressed");
2895 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002896 __ Ldrb(Register(OutputCPURegister(instruction)),
2897 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2898 __ B(&done);
2899 __ Bind(&uncompressed_load);
2900 __ Ldrh(Register(OutputCPURegister(instruction)),
2901 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2902 __ Bind(&done);
2903 } else {
2904 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2905 }
Roland Levillain44015862016-01-22 11:47:17 +00002906 }
jessicahandojo05765752016-09-09 19:01:32 -07002907 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002908 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2909 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002910 codegen_->Load(type, OutputCPURegister(instruction), source);
2911 codegen_->MaybeRecordImplicitNullCheck(instruction);
2912 }
Roland Levillain44015862016-01-22 11:47:17 +00002913
2914 if (type == Primitive::kPrimNot) {
2915 static_assert(
2916 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2917 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2918 Location obj_loc = locations->InAt(0);
2919 if (index.IsConstant()) {
2920 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2921 } else {
2922 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2923 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002924 }
Roland Levillain4d027112015-07-01 15:41:14 +01002925 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002926}
2927
Alexandre Rames5319def2014-10-23 10:03:10 +01002928void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2929 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2930 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002931 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002932}
2933
2934void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002935 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002936 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002937 {
2938 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2939 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2940 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2941 codegen_->MaybeRecordImplicitNullCheck(instruction);
2942 }
jessicahandojo05765752016-09-09 19:01:32 -07002943 // Mask out compression flag from String's array length.
2944 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002945 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002946 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002947}
2948
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002949void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002950 Primitive::Type value_type = instruction->GetComponentType();
2951
2952 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002953 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2954 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002955 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002956 LocationSummary::kCallOnSlowPath :
2957 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002958 locations->SetInAt(0, Location::RequiresRegister());
2959 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002960 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2961 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2962 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002963 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002964 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002965 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002966 }
2967}
2968
2969void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2970 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002971 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002972 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002973 bool needs_write_barrier =
2974 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002975
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002976 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002977 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002978 CPURegister source = value;
2979 Location index = locations->InAt(1);
2980 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2981 MemOperand destination = HeapOperand(array);
2982 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002983
2984 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002985 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002986 if (index.IsConstant()) {
2987 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2988 destination = HeapOperand(array, offset);
2989 } else {
2990 UseScratchRegisterScope temps(masm);
2991 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002992 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002993 // We do not need to compute the intermediate address from the array: the
2994 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002995 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002996 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002997 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002998 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2999 }
3000 temp = array;
3001 } else {
3002 __ Add(temp, array, offset);
3003 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003004 destination = HeapOperand(temp,
3005 XRegisterFrom(index),
3006 LSL,
3007 Primitive::ComponentSizeShift(value_type));
3008 }
Artem Serov914d7a82017-02-07 14:33:49 +00003009 {
3010 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3011 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3012 codegen_->Store(value_type, value, destination);
3013 codegen_->MaybeRecordImplicitNullCheck(instruction);
3014 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003015 } else {
Artem Serov328429f2016-07-06 16:23:04 +01003016 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003017 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003018 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01003019 {
3020 // We use a block to end the scratch scope before the write barrier, thus
3021 // freeing the temporary registers so they can be used in `MarkGCCard`.
3022 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003023 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01003024 if (index.IsConstant()) {
3025 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003026 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01003027 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01003028 destination = HeapOperand(temp,
3029 XRegisterFrom(index),
3030 LSL,
3031 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01003032 }
3033
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003034 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3035 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3036 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3037
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003038 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003039 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
3040 codegen_->AddSlowPath(slow_path);
3041 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003042 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003043 __ Cbnz(Register(value), &non_zero);
3044 if (!index.IsConstant()) {
3045 __ Add(temp, array, offset);
3046 }
Artem Serov914d7a82017-02-07 14:33:49 +00003047 {
3048 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
3049 // emitted.
3050 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3051 __ Str(wzr, destination);
3052 codegen_->MaybeRecordImplicitNullCheck(instruction);
3053 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003054 __ B(&done);
3055 __ Bind(&non_zero);
3056 }
3057
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003058 // Note that when Baker read barriers are enabled, the type
3059 // checks are performed without read barriers. This is fine,
3060 // even in the case where a class object is in the from-space
3061 // after the flip, as a comparison involving such a type would
3062 // not produce a false positive; it may of course produce a
3063 // false negative, in which case we would take the ArraySet
3064 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003065
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003066 Register temp2 = temps.AcquireSameSizeAs(array);
3067 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003068 {
3069 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3070 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3071 __ Ldr(temp, HeapOperand(array, class_offset));
3072 codegen_->MaybeRecordImplicitNullCheck(instruction);
3073 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003074 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003075
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003076 // /* HeapReference<Class> */ temp = temp->component_type_
3077 __ Ldr(temp, HeapOperand(temp, component_offset));
3078 // /* HeapReference<Class> */ temp2 = value->klass_
3079 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3080 // If heap poisoning is enabled, no need to unpoison `temp`
3081 // nor `temp2`, as we are comparing two poisoned references.
3082 __ Cmp(temp, temp2);
3083 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003084
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003085 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3086 vixl::aarch64::Label do_put;
3087 __ B(eq, &do_put);
3088 // If heap poisoning is enabled, the `temp` reference has
3089 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003090 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3091
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003092 // /* HeapReference<Class> */ temp = temp->super_class_
3093 __ Ldr(temp, HeapOperand(temp, super_offset));
3094 // If heap poisoning is enabled, no need to unpoison
3095 // `temp`, as we are comparing against null below.
3096 __ Cbnz(temp, slow_path->GetEntryLabel());
3097 __ Bind(&do_put);
3098 } else {
3099 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003100 }
3101 }
3102
3103 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003104 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003105 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003106 __ Mov(temp2, value.W());
3107 GetAssembler()->PoisonHeapReference(temp2);
3108 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003109 }
3110
3111 if (!index.IsConstant()) {
3112 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003113 } else {
3114 // We no longer need the `temp` here so release it as the store below may
3115 // need a scratch register (if the constant index makes the offset too large)
3116 // and the poisoned `source` could be using the other scratch register.
3117 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003118 }
Artem Serov914d7a82017-02-07 14:33:49 +00003119 {
3120 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3121 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3122 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003123
Artem Serov914d7a82017-02-07 14:33:49 +00003124 if (!may_need_runtime_call_for_type_check) {
3125 codegen_->MaybeRecordImplicitNullCheck(instruction);
3126 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003127 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003128 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003129
3130 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3131
3132 if (done.IsLinked()) {
3133 __ Bind(&done);
3134 }
3135
3136 if (slow_path != nullptr) {
3137 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003138 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003139 }
3140}
3141
Alexandre Rames67555f72014-11-18 10:55:16 +00003142void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003143 RegisterSet caller_saves = RegisterSet::Empty();
3144 InvokeRuntimeCallingConvention calling_convention;
3145 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3146 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3147 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003148 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003149 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003150}
3151
3152void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003153 BoundsCheckSlowPathARM64* slow_path =
3154 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003155 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003156 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3157 __ B(slow_path->GetEntryLabel(), hs);
3158}
3159
Alexandre Rames67555f72014-11-18 10:55:16 +00003160void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3161 LocationSummary* locations =
3162 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3163 locations->SetInAt(0, Location::RequiresRegister());
3164 if (check->HasUses()) {
3165 locations->SetOut(Location::SameAsFirstInput());
3166 }
3167}
3168
3169void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3170 // We assume the class is not null.
3171 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3172 check->GetLoadClass(), check, check->GetDexPc(), true);
3173 codegen_->AddSlowPath(slow_path);
3174 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3175}
3176
Roland Levillain1a653882016-03-18 18:05:57 +00003177static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3178 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3179 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3180}
3181
3182void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3183 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3184 Location rhs_loc = instruction->GetLocations()->InAt(1);
3185 if (rhs_loc.IsConstant()) {
3186 // 0.0 is the only immediate that can be encoded directly in
3187 // an FCMP instruction.
3188 //
3189 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3190 // specify that in a floating-point comparison, positive zero
3191 // and negative zero are considered equal, so we can use the
3192 // literal 0.0 for both cases here.
3193 //
3194 // Note however that some methods (Float.equal, Float.compare,
3195 // Float.compareTo, Double.equal, Double.compare,
3196 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3197 // StrictMath.min) consider 0.0 to be (strictly) greater than
3198 // -0.0. So if we ever translate calls to these methods into a
3199 // HCompare instruction, we must handle the -0.0 case with
3200 // care here.
3201 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3202 __ Fcmp(lhs_reg, 0.0);
3203 } else {
3204 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3205 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003206}
3207
Serban Constantinescu02164b32014-11-13 14:05:07 +00003208void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003209 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003210 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3211 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003212 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003213 case Primitive::kPrimBoolean:
3214 case Primitive::kPrimByte:
3215 case Primitive::kPrimShort:
3216 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003217 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003218 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003219 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003220 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003221 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3222 break;
3223 }
3224 case Primitive::kPrimFloat:
3225 case Primitive::kPrimDouble: {
3226 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003227 locations->SetInAt(1,
3228 IsFloatingPointZeroConstant(compare->InputAt(1))
3229 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3230 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003231 locations->SetOut(Location::RequiresRegister());
3232 break;
3233 }
3234 default:
3235 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3236 }
3237}
3238
3239void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3240 Primitive::Type in_type = compare->InputAt(0)->GetType();
3241
3242 // 0 if: left == right
3243 // 1 if: left > right
3244 // -1 if: left < right
3245 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003246 case Primitive::kPrimBoolean:
3247 case Primitive::kPrimByte:
3248 case Primitive::kPrimShort:
3249 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003250 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003251 case Primitive::kPrimLong: {
3252 Register result = OutputRegister(compare);
3253 Register left = InputRegisterAt(compare, 0);
3254 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003255 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003256 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3257 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003258 break;
3259 }
3260 case Primitive::kPrimFloat:
3261 case Primitive::kPrimDouble: {
3262 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003263 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003264 __ Cset(result, ne);
3265 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003266 break;
3267 }
3268 default:
3269 LOG(FATAL) << "Unimplemented compare type " << in_type;
3270 }
3271}
3272
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003273void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003274 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003275
3276 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3277 locations->SetInAt(0, Location::RequiresFpuRegister());
3278 locations->SetInAt(1,
3279 IsFloatingPointZeroConstant(instruction->InputAt(1))
3280 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3281 : Location::RequiresFpuRegister());
3282 } else {
3283 // Integer cases.
3284 locations->SetInAt(0, Location::RequiresRegister());
3285 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3286 }
3287
David Brazdilb3e773e2016-01-26 11:28:37 +00003288 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003289 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003290 }
3291}
3292
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003293void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003294 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003295 return;
3296 }
3297
3298 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003299 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003300 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003301
Roland Levillain7f63c522015-07-13 15:54:55 +00003302 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003303 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003304 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003305 } else {
3306 // Integer cases.
3307 Register lhs = InputRegisterAt(instruction, 0);
3308 Operand rhs = InputOperandAt(instruction, 1);
3309 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003310 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003311 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003312}
3313
3314#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3315 M(Equal) \
3316 M(NotEqual) \
3317 M(LessThan) \
3318 M(LessThanOrEqual) \
3319 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003320 M(GreaterThanOrEqual) \
3321 M(Below) \
3322 M(BelowOrEqual) \
3323 M(Above) \
3324 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003325#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003326void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3327void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003328FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003329#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003330#undef FOR_EACH_CONDITION_INSTRUCTION
3331
Zheng Xuc6667102015-05-15 16:08:45 +08003332void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3333 DCHECK(instruction->IsDiv() || instruction->IsRem());
3334
3335 LocationSummary* locations = instruction->GetLocations();
3336 Location second = locations->InAt(1);
3337 DCHECK(second.IsConstant());
3338
3339 Register out = OutputRegister(instruction);
3340 Register dividend = InputRegisterAt(instruction, 0);
3341 int64_t imm = Int64FromConstant(second.GetConstant());
3342 DCHECK(imm == 1 || imm == -1);
3343
3344 if (instruction->IsRem()) {
3345 __ Mov(out, 0);
3346 } else {
3347 if (imm == 1) {
3348 __ Mov(out, dividend);
3349 } else {
3350 __ Neg(out, dividend);
3351 }
3352 }
3353}
3354
3355void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3356 DCHECK(instruction->IsDiv() || instruction->IsRem());
3357
3358 LocationSummary* locations = instruction->GetLocations();
3359 Location second = locations->InAt(1);
3360 DCHECK(second.IsConstant());
3361
3362 Register out = OutputRegister(instruction);
3363 Register dividend = InputRegisterAt(instruction, 0);
3364 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003365 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003366 int ctz_imm = CTZ(abs_imm);
3367
3368 UseScratchRegisterScope temps(GetVIXLAssembler());
3369 Register temp = temps.AcquireSameSizeAs(out);
3370
3371 if (instruction->IsDiv()) {
3372 __ Add(temp, dividend, abs_imm - 1);
3373 __ Cmp(dividend, 0);
3374 __ Csel(out, temp, dividend, lt);
3375 if (imm > 0) {
3376 __ Asr(out, out, ctz_imm);
3377 } else {
3378 __ Neg(out, Operand(out, ASR, ctz_imm));
3379 }
3380 } else {
3381 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3382 __ Asr(temp, dividend, bits - 1);
3383 __ Lsr(temp, temp, bits - ctz_imm);
3384 __ Add(out, dividend, temp);
3385 __ And(out, out, abs_imm - 1);
3386 __ Sub(out, out, temp);
3387 }
3388}
3389
3390void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3391 DCHECK(instruction->IsDiv() || instruction->IsRem());
3392
3393 LocationSummary* locations = instruction->GetLocations();
3394 Location second = locations->InAt(1);
3395 DCHECK(second.IsConstant());
3396
3397 Register out = OutputRegister(instruction);
3398 Register dividend = InputRegisterAt(instruction, 0);
3399 int64_t imm = Int64FromConstant(second.GetConstant());
3400
3401 Primitive::Type type = instruction->GetResultType();
3402 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3403
3404 int64_t magic;
3405 int shift;
3406 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3407
3408 UseScratchRegisterScope temps(GetVIXLAssembler());
3409 Register temp = temps.AcquireSameSizeAs(out);
3410
3411 // temp = get_high(dividend * magic)
3412 __ Mov(temp, magic);
3413 if (type == Primitive::kPrimLong) {
3414 __ Smulh(temp, dividend, temp);
3415 } else {
3416 __ Smull(temp.X(), dividend, temp);
3417 __ Lsr(temp.X(), temp.X(), 32);
3418 }
3419
3420 if (imm > 0 && magic < 0) {
3421 __ Add(temp, temp, dividend);
3422 } else if (imm < 0 && magic > 0) {
3423 __ Sub(temp, temp, dividend);
3424 }
3425
3426 if (shift != 0) {
3427 __ Asr(temp, temp, shift);
3428 }
3429
3430 if (instruction->IsDiv()) {
3431 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3432 } else {
3433 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3434 // TODO: Strength reduction for msub.
3435 Register temp_imm = temps.AcquireSameSizeAs(out);
3436 __ Mov(temp_imm, imm);
3437 __ Msub(out, temp, temp_imm, dividend);
3438 }
3439}
3440
3441void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3442 DCHECK(instruction->IsDiv() || instruction->IsRem());
3443 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003444 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003445
3446 LocationSummary* locations = instruction->GetLocations();
3447 Register out = OutputRegister(instruction);
3448 Location second = locations->InAt(1);
3449
3450 if (second.IsConstant()) {
3451 int64_t imm = Int64FromConstant(second.GetConstant());
3452
3453 if (imm == 0) {
3454 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3455 } else if (imm == 1 || imm == -1) {
3456 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003457 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003458 DivRemByPowerOfTwo(instruction);
3459 } else {
3460 DCHECK(imm <= -2 || imm >= 2);
3461 GenerateDivRemWithAnyConstant(instruction);
3462 }
3463 } else {
3464 Register dividend = InputRegisterAt(instruction, 0);
3465 Register divisor = InputRegisterAt(instruction, 1);
3466 if (instruction->IsDiv()) {
3467 __ Sdiv(out, dividend, divisor);
3468 } else {
3469 UseScratchRegisterScope temps(GetVIXLAssembler());
3470 Register temp = temps.AcquireSameSizeAs(out);
3471 __ Sdiv(temp, dividend, divisor);
3472 __ Msub(out, temp, divisor, dividend);
3473 }
3474 }
3475}
3476
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003477void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3478 LocationSummary* locations =
3479 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3480 switch (div->GetResultType()) {
3481 case Primitive::kPrimInt:
3482 case Primitive::kPrimLong:
3483 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003484 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003485 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3486 break;
3487
3488 case Primitive::kPrimFloat:
3489 case Primitive::kPrimDouble:
3490 locations->SetInAt(0, Location::RequiresFpuRegister());
3491 locations->SetInAt(1, Location::RequiresFpuRegister());
3492 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3493 break;
3494
3495 default:
3496 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3497 }
3498}
3499
3500void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3501 Primitive::Type type = div->GetResultType();
3502 switch (type) {
3503 case Primitive::kPrimInt:
3504 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003505 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003506 break;
3507
3508 case Primitive::kPrimFloat:
3509 case Primitive::kPrimDouble:
3510 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3511 break;
3512
3513 default:
3514 LOG(FATAL) << "Unexpected div type " << type;
3515 }
3516}
3517
Alexandre Rames67555f72014-11-18 10:55:16 +00003518void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003519 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003520 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003521}
3522
3523void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3524 SlowPathCodeARM64* slow_path =
3525 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3526 codegen_->AddSlowPath(slow_path);
3527 Location value = instruction->GetLocations()->InAt(0);
3528
Alexandre Rames3e69f162014-12-10 10:36:50 +00003529 Primitive::Type type = instruction->GetType();
3530
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003531 if (!Primitive::IsIntegralType(type)) {
3532 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003533 return;
3534 }
3535
Alexandre Rames67555f72014-11-18 10:55:16 +00003536 if (value.IsConstant()) {
3537 int64_t divisor = Int64ConstantFrom(value);
3538 if (divisor == 0) {
3539 __ B(slow_path->GetEntryLabel());
3540 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003541 // A division by a non-null constant is valid. We don't need to perform
3542 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003543 }
3544 } else {
3545 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3546 }
3547}
3548
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003549void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3550 LocationSummary* locations =
3551 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3552 locations->SetOut(Location::ConstantLocation(constant));
3553}
3554
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003555void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3556 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003557 // Will be generated at use site.
3558}
3559
Alexandre Rames5319def2014-10-23 10:03:10 +01003560void LocationsBuilderARM64::VisitExit(HExit* exit) {
3561 exit->SetLocations(nullptr);
3562}
3563
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003564void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003565}
3566
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003567void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3568 LocationSummary* locations =
3569 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3570 locations->SetOut(Location::ConstantLocation(constant));
3571}
3572
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003573void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003574 // Will be generated at use site.
3575}
3576
David Brazdilfc6a86a2015-06-26 10:33:45 +00003577void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003578 DCHECK(!successor->IsExitBlock());
3579 HBasicBlock* block = got->GetBlock();
3580 HInstruction* previous = got->GetPrevious();
3581 HLoopInformation* info = block->GetLoopInformation();
3582
David Brazdil46e2a392015-03-16 17:31:52 +00003583 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003584 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3585 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3586 return;
3587 }
3588 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3589 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3590 }
3591 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003592 __ B(codegen_->GetLabelOf(successor));
3593 }
3594}
3595
David Brazdilfc6a86a2015-06-26 10:33:45 +00003596void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3597 got->SetLocations(nullptr);
3598}
3599
3600void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3601 HandleGoto(got, got->GetSuccessor());
3602}
3603
3604void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3605 try_boundary->SetLocations(nullptr);
3606}
3607
3608void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3609 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3610 if (!successor->IsExitBlock()) {
3611 HandleGoto(try_boundary, successor);
3612 }
3613}
3614
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003615void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003616 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003617 vixl::aarch64::Label* true_target,
3618 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003619 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003620
David Brazdil0debae72015-11-12 18:37:00 +00003621 if (true_target == nullptr && false_target == nullptr) {
3622 // Nothing to do. The code always falls through.
3623 return;
3624 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003625 // Constant condition, statically compared against "true" (integer value 1).
3626 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003627 if (true_target != nullptr) {
3628 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003629 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003630 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003631 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003632 if (false_target != nullptr) {
3633 __ B(false_target);
3634 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003635 }
David Brazdil0debae72015-11-12 18:37:00 +00003636 return;
3637 }
3638
3639 // The following code generates these patterns:
3640 // (1) true_target == nullptr && false_target != nullptr
3641 // - opposite condition true => branch to false_target
3642 // (2) true_target != nullptr && false_target == nullptr
3643 // - condition true => branch to true_target
3644 // (3) true_target != nullptr && false_target != nullptr
3645 // - condition true => branch to true_target
3646 // - branch to false_target
3647 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003648 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003649 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003650 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003651 if (true_target == nullptr) {
3652 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3653 } else {
3654 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3655 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003656 } else {
3657 // The condition instruction has not been materialized, use its inputs as
3658 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003659 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003660
David Brazdil0debae72015-11-12 18:37:00 +00003661 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003662 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003663 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003664 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003665 IfCondition opposite_condition = condition->GetOppositeCondition();
3666 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003667 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003668 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003669 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003670 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003671 // Integer cases.
3672 Register lhs = InputRegisterAt(condition, 0);
3673 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003674
3675 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003676 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003677 if (true_target == nullptr) {
3678 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3679 non_fallthrough_target = false_target;
3680 } else {
3681 arm64_cond = ARM64Condition(condition->GetCondition());
3682 non_fallthrough_target = true_target;
3683 }
3684
Aart Bik086d27e2016-01-20 17:02:00 -08003685 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003686 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003687 switch (arm64_cond) {
3688 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003689 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003690 break;
3691 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003692 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003693 break;
3694 case lt:
3695 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003696 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003697 break;
3698 case ge:
3699 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003700 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003701 break;
3702 default:
3703 // Without the `static_cast` the compiler throws an error for
3704 // `-Werror=sign-promo`.
3705 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3706 }
3707 } else {
3708 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003709 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003710 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003711 }
3712 }
David Brazdil0debae72015-11-12 18:37:00 +00003713
3714 // If neither branch falls through (case 3), the conditional branch to `true_target`
3715 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3716 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003717 __ B(false_target);
3718 }
3719}
3720
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003721void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3722 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003723 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003724 locations->SetInAt(0, Location::RequiresRegister());
3725 }
3726}
3727
3728void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003729 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3730 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003731 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3732 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3733 true_target = nullptr;
3734 }
3735 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3736 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3737 false_target = nullptr;
3738 }
David Brazdil0debae72015-11-12 18:37:00 +00003739 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003740}
3741
3742void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3743 LocationSummary* locations = new (GetGraph()->GetArena())
3744 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003745 InvokeRuntimeCallingConvention calling_convention;
3746 RegisterSet caller_saves = RegisterSet::Empty();
3747 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3748 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003749 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003750 locations->SetInAt(0, Location::RequiresRegister());
3751 }
3752}
3753
3754void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003755 SlowPathCodeARM64* slow_path =
3756 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003757 GenerateTestAndBranch(deoptimize,
3758 /* condition_input_index */ 0,
3759 slow_path->GetEntryLabel(),
3760 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003761}
3762
Mingyao Yang063fc772016-08-02 11:02:54 -07003763void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3764 LocationSummary* locations = new (GetGraph()->GetArena())
3765 LocationSummary(flag, LocationSummary::kNoCall);
3766 locations->SetOut(Location::RequiresRegister());
3767}
3768
3769void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3770 __ Ldr(OutputRegister(flag),
3771 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3772}
3773
David Brazdilc0b601b2016-02-08 14:20:45 +00003774static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3775 return condition->IsCondition() &&
3776 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3777}
3778
Alexandre Rames880f1192016-06-13 16:04:50 +01003779static inline Condition GetConditionForSelect(HCondition* condition) {
3780 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003781 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3782 : ARM64Condition(cond);
3783}
3784
David Brazdil74eb1b22015-12-14 11:44:01 +00003785void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3786 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003787 if (Primitive::IsFloatingPointType(select->GetType())) {
3788 locations->SetInAt(0, Location::RequiresFpuRegister());
3789 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003790 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003791 } else {
3792 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3793 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3794 bool is_true_value_constant = cst_true_value != nullptr;
3795 bool is_false_value_constant = cst_false_value != nullptr;
3796 // Ask VIXL whether we should synthesize constants in registers.
3797 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3798 Operand true_op = is_true_value_constant ?
3799 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3800 Operand false_op = is_false_value_constant ?
3801 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3802 bool true_value_in_register = false;
3803 bool false_value_in_register = false;
3804 MacroAssembler::GetCselSynthesisInformation(
3805 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3806 true_value_in_register |= !is_true_value_constant;
3807 false_value_in_register |= !is_false_value_constant;
3808
3809 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3810 : Location::ConstantLocation(cst_true_value));
3811 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3812 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003813 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003814 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003815
David Brazdil74eb1b22015-12-14 11:44:01 +00003816 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3817 locations->SetInAt(2, Location::RequiresRegister());
3818 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003819}
3820
3821void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003822 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003823 Condition csel_cond;
3824
3825 if (IsBooleanValueOrMaterializedCondition(cond)) {
3826 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003827 // Use the condition flags set by the previous instruction.
3828 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003829 } else {
3830 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003831 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003832 }
3833 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003834 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003835 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003836 } else {
3837 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003838 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003839 }
3840
Alexandre Rames880f1192016-06-13 16:04:50 +01003841 if (Primitive::IsFloatingPointType(select->GetType())) {
3842 __ Fcsel(OutputFPRegister(select),
3843 InputFPRegisterAt(select, 1),
3844 InputFPRegisterAt(select, 0),
3845 csel_cond);
3846 } else {
3847 __ Csel(OutputRegister(select),
3848 InputOperandAt(select, 1),
3849 InputOperandAt(select, 0),
3850 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003851 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003852}
3853
David Srbecky0cf44932015-12-09 14:09:59 +00003854void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3855 new (GetGraph()->GetArena()) LocationSummary(info);
3856}
3857
David Srbeckyd28f4a02016-03-14 17:14:24 +00003858void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3859 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003860}
3861
3862void CodeGeneratorARM64::GenerateNop() {
3863 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003864}
3865
Alexandre Rames5319def2014-10-23 10:03:10 +01003866void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003867 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003868}
3869
3870void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003871 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003872}
3873
3874void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003875 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003876}
3877
3878void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003879 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003880}
3881
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003882// Temp is used for read barrier.
3883static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3884 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003885 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003886 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3887 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3888 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3889 return 1;
3890 }
3891 return 0;
3892}
3893
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003894// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003895// interface pointer, one for loading the current interface.
3896// The other checks have one temp for loading the object's class.
3897static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3898 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3899 return 3;
3900 }
3901 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003902}
3903
Alexandre Rames67555f72014-11-18 10:55:16 +00003904void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003905 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003906 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003907 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003908 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003909 case TypeCheckKind::kExactCheck:
3910 case TypeCheckKind::kAbstractClassCheck:
3911 case TypeCheckKind::kClassHierarchyCheck:
3912 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003913 call_kind =
3914 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003915 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003916 break;
3917 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003918 case TypeCheckKind::kUnresolvedCheck:
3919 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003920 call_kind = LocationSummary::kCallOnSlowPath;
3921 break;
3922 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003923
Alexandre Rames67555f72014-11-18 10:55:16 +00003924 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003925 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003926 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003927 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003928 locations->SetInAt(0, Location::RequiresRegister());
3929 locations->SetInAt(1, Location::RequiresRegister());
3930 // The "out" register is used as a temporary, so it overlaps with the inputs.
3931 // Note that TypeCheckSlowPathARM64 uses this register too.
3932 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003933 // Add temps if necessary for read barriers.
3934 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003935}
3936
3937void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003938 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003939 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003940 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003941 Register obj = InputRegisterAt(instruction, 0);
3942 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003943 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003944 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003945 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3946 DCHECK_LE(num_temps, 1u);
3947 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003948 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3949 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3950 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3951 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003952
Scott Wakeling97c72b72016-06-24 16:19:36 +01003953 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003954 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003955
3956 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003957 // Avoid null check if we know `obj` is not null.
3958 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003959 __ Cbz(obj, &zero);
3960 }
3961
Roland Levillain44015862016-01-22 11:47:17 +00003962 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003963 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003964 // /* HeapReference<Class> */ out = obj->klass_
3965 GenerateReferenceLoadTwoRegisters(instruction,
3966 out_loc,
3967 obj_loc,
3968 class_offset,
3969 maybe_temp_loc,
3970 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003971 __ Cmp(out, cls);
3972 __ Cset(out, eq);
3973 if (zero.IsLinked()) {
3974 __ B(&done);
3975 }
3976 break;
3977 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003978
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003979 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003980 // /* HeapReference<Class> */ out = obj->klass_
3981 GenerateReferenceLoadTwoRegisters(instruction,
3982 out_loc,
3983 obj_loc,
3984 class_offset,
3985 maybe_temp_loc,
3986 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003987 // If the class is abstract, we eagerly fetch the super class of the
3988 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003989 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003990 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003991 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003992 GenerateReferenceLoadOneRegister(instruction,
3993 out_loc,
3994 super_offset,
3995 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003996 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003997 // If `out` is null, we use it for the result, and jump to `done`.
3998 __ Cbz(out, &done);
3999 __ Cmp(out, cls);
4000 __ B(ne, &loop);
4001 __ Mov(out, 1);
4002 if (zero.IsLinked()) {
4003 __ B(&done);
4004 }
4005 break;
4006 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004007
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004008 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004009 // /* HeapReference<Class> */ out = obj->klass_
4010 GenerateReferenceLoadTwoRegisters(instruction,
4011 out_loc,
4012 obj_loc,
4013 class_offset,
4014 maybe_temp_loc,
4015 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004016 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004017 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004018 __ Bind(&loop);
4019 __ Cmp(out, cls);
4020 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004021 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004022 GenerateReferenceLoadOneRegister(instruction,
4023 out_loc,
4024 super_offset,
4025 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004026 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004027 __ Cbnz(out, &loop);
4028 // If `out` is null, we use it for the result, and jump to `done`.
4029 __ B(&done);
4030 __ Bind(&success);
4031 __ Mov(out, 1);
4032 if (zero.IsLinked()) {
4033 __ B(&done);
4034 }
4035 break;
4036 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004037
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004038 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004039 // /* HeapReference<Class> */ out = obj->klass_
4040 GenerateReferenceLoadTwoRegisters(instruction,
4041 out_loc,
4042 obj_loc,
4043 class_offset,
4044 maybe_temp_loc,
4045 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004046 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004047 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004048 __ Cmp(out, cls);
4049 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004050 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004051 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004052 GenerateReferenceLoadOneRegister(instruction,
4053 out_loc,
4054 component_offset,
4055 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004056 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004057 // If `out` is null, we use it for the result, and jump to `done`.
4058 __ Cbz(out, &done);
4059 __ Ldrh(out, HeapOperand(out, primitive_offset));
4060 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4061 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004062 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004063 __ Mov(out, 1);
4064 __ B(&done);
4065 break;
4066 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004067
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004068 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004069 // No read barrier since the slow path will retry upon failure.
4070 // /* HeapReference<Class> */ out = obj->klass_
4071 GenerateReferenceLoadTwoRegisters(instruction,
4072 out_loc,
4073 obj_loc,
4074 class_offset,
4075 maybe_temp_loc,
4076 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004077 __ Cmp(out, cls);
4078 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004079 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4080 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004081 codegen_->AddSlowPath(slow_path);
4082 __ B(ne, slow_path->GetEntryLabel());
4083 __ Mov(out, 1);
4084 if (zero.IsLinked()) {
4085 __ B(&done);
4086 }
4087 break;
4088 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004089
Calin Juravle98893e12015-10-02 21:05:03 +01004090 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004091 case TypeCheckKind::kInterfaceCheck: {
4092 // Note that we indeed only call on slow path, but we always go
4093 // into the slow path for the unresolved and interface check
4094 // cases.
4095 //
4096 // We cannot directly call the InstanceofNonTrivial runtime
4097 // entry point without resorting to a type checking slow path
4098 // here (i.e. by calling InvokeRuntime directly), as it would
4099 // require to assign fixed registers for the inputs of this
4100 // HInstanceOf instruction (following the runtime calling
4101 // convention), which might be cluttered by the potential first
4102 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004103 //
4104 // TODO: Introduce a new runtime entry point taking the object
4105 // to test (instead of its class) as argument, and let it deal
4106 // with the read barrier issues. This will let us refactor this
4107 // case of the `switch` code as it was previously (with a direct
4108 // call to the runtime not using a type checking slow path).
4109 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004110 DCHECK(locations->OnlyCallsOnSlowPath());
4111 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4112 /* is_fatal */ false);
4113 codegen_->AddSlowPath(slow_path);
4114 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004115 if (zero.IsLinked()) {
4116 __ B(&done);
4117 }
4118 break;
4119 }
4120 }
4121
4122 if (zero.IsLinked()) {
4123 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004124 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004125 }
4126
4127 if (done.IsLinked()) {
4128 __ Bind(&done);
4129 }
4130
4131 if (slow_path != nullptr) {
4132 __ Bind(slow_path->GetExitLabel());
4133 }
4134}
4135
4136void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4137 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4138 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4139
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004140 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4141 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004142 case TypeCheckKind::kExactCheck:
4143 case TypeCheckKind::kAbstractClassCheck:
4144 case TypeCheckKind::kClassHierarchyCheck:
4145 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004146 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4147 LocationSummary::kCallOnSlowPath :
4148 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004149 break;
4150 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004151 case TypeCheckKind::kUnresolvedCheck:
4152 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004153 call_kind = LocationSummary::kCallOnSlowPath;
4154 break;
4155 }
4156
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004157 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4158 locations->SetInAt(0, Location::RequiresRegister());
4159 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004160 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4161 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004162}
4163
4164void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004165 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004166 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004167 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004168 Register obj = InputRegisterAt(instruction, 0);
4169 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004170 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4171 DCHECK_GE(num_temps, 1u);
4172 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004173 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004174 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4175 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004176 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004177 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4178 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4179 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4180 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4181 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4182 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4183 const uint32_t object_array_data_offset =
4184 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004185
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004186 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004187 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4188 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4189 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004190 if (!kEmitCompilerReadBarrier) {
4191 is_type_check_slow_path_fatal =
4192 (type_check_kind == TypeCheckKind::kExactCheck ||
4193 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4194 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4195 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4196 !instruction->CanThrowIntoCatchBlock();
4197 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004198 SlowPathCodeARM64* type_check_slow_path =
4199 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4200 is_type_check_slow_path_fatal);
4201 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004202
Scott Wakeling97c72b72016-06-24 16:19:36 +01004203 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004204 // Avoid null check if we know obj is not null.
4205 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004206 __ Cbz(obj, &done);
4207 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004208
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004209 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004210 case TypeCheckKind::kExactCheck:
4211 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004212 // /* HeapReference<Class> */ temp = obj->klass_
4213 GenerateReferenceLoadTwoRegisters(instruction,
4214 temp_loc,
4215 obj_loc,
4216 class_offset,
4217 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004218 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004219
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004220 __ Cmp(temp, cls);
4221 // Jump to slow path for throwing the exception or doing a
4222 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004223 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004224 break;
4225 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004226
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004227 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004228 // /* HeapReference<Class> */ temp = obj->klass_
4229 GenerateReferenceLoadTwoRegisters(instruction,
4230 temp_loc,
4231 obj_loc,
4232 class_offset,
4233 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004234 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004235
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004236 // If the class is abstract, we eagerly fetch the super class of the
4237 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004238 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004239 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004240 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004241 GenerateReferenceLoadOneRegister(instruction,
4242 temp_loc,
4243 super_offset,
4244 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004245 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004246
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004247 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4248 // exception.
4249 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4250 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004251 __ Cmp(temp, cls);
4252 __ B(ne, &loop);
4253 break;
4254 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004255
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004256 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004257 // /* HeapReference<Class> */ temp = obj->klass_
4258 GenerateReferenceLoadTwoRegisters(instruction,
4259 temp_loc,
4260 obj_loc,
4261 class_offset,
4262 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004263 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004264
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004265 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004266 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004267 __ Bind(&loop);
4268 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004269 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004270
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004271 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004272 GenerateReferenceLoadOneRegister(instruction,
4273 temp_loc,
4274 super_offset,
4275 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004276 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004277
4278 // If the class reference currently in `temp` is not null, jump
4279 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004280 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004281 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004282 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004283 break;
4284 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004285
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004286 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004287 // /* HeapReference<Class> */ temp = obj->klass_
4288 GenerateReferenceLoadTwoRegisters(instruction,
4289 temp_loc,
4290 obj_loc,
4291 class_offset,
4292 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004293 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004294
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004295 // Do an exact check.
4296 __ Cmp(temp, cls);
4297 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004298
4299 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004300 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004301 GenerateReferenceLoadOneRegister(instruction,
4302 temp_loc,
4303 component_offset,
4304 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004305 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004306
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004307 // If the component type is null, jump to the slow path to throw the exception.
4308 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4309 // Otherwise, the object is indeed an array. Further check that this component type is not a
4310 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004311 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4312 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004313 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004314 break;
4315 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004316
Calin Juravle98893e12015-10-02 21:05:03 +01004317 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004318 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004319 //
4320 // We cannot directly call the CheckCast runtime entry point
4321 // without resorting to a type checking slow path here (i.e. by
4322 // calling InvokeRuntime directly), as it would require to
4323 // assign fixed registers for the inputs of this HInstanceOf
4324 // instruction (following the runtime calling convention), which
4325 // might be cluttered by the potential first read barrier
4326 // emission at the beginning of this method.
4327 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004328 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004329 case TypeCheckKind::kInterfaceCheck: {
4330 // /* HeapReference<Class> */ temp = obj->klass_
4331 GenerateReferenceLoadTwoRegisters(instruction,
4332 temp_loc,
4333 obj_loc,
4334 class_offset,
4335 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004336 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004337
4338 // /* HeapReference<Class> */ temp = temp->iftable_
4339 GenerateReferenceLoadTwoRegisters(instruction,
4340 temp_loc,
4341 temp_loc,
4342 iftable_offset,
4343 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004344 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004345 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004346 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004347 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004348 vixl::aarch64::Label start_loop;
4349 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004350 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004351 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4352 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004353 // Go to next interface.
4354 __ Add(temp, temp, 2 * kHeapReferenceSize);
4355 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004356 // Compare the classes and continue the loop if they do not match.
4357 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4358 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004359 break;
4360 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004361 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004362 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004363
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004364 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004365}
4366
Alexandre Rames5319def2014-10-23 10:03:10 +01004367void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4368 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4369 locations->SetOut(Location::ConstantLocation(constant));
4370}
4371
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004372void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004373 // Will be generated at use site.
4374}
4375
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004376void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4377 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4378 locations->SetOut(Location::ConstantLocation(constant));
4379}
4380
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004381void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004382 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004383}
4384
Calin Juravle175dc732015-08-25 15:42:32 +01004385void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4386 // The trampoline uses the same calling convention as dex calling conventions,
4387 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4388 // the method_idx.
4389 HandleInvoke(invoke);
4390}
4391
4392void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4393 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4394}
4395
Alexandre Rames5319def2014-10-23 10:03:10 +01004396void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004397 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004398 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004399}
4400
Alexandre Rames67555f72014-11-18 10:55:16 +00004401void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4402 HandleInvoke(invoke);
4403}
4404
4405void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4406 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004407 LocationSummary* locations = invoke->GetLocations();
4408 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004409 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004410 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004411 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004412
4413 // The register ip1 is required to be used for the hidden argument in
4414 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004415 MacroAssembler* masm = GetVIXLAssembler();
4416 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004417 scratch_scope.Exclude(ip1);
4418 __ Mov(ip1, invoke->GetDexMethodIndex());
4419
Artem Serov914d7a82017-02-07 14:33:49 +00004420 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004421 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004422 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004423 {
4424 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4425 // /* HeapReference<Class> */ temp = temp->klass_
4426 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4427 codegen_->MaybeRecordImplicitNullCheck(invoke);
4428 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004429 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004430 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004431 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004432 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004433 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004434 }
Artem Serov914d7a82017-02-07 14:33:49 +00004435
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004436 // Instead of simply (possibly) unpoisoning `temp` here, we should
4437 // emit a read barrier for the previous class reference load.
4438 // However this is not required in practice, as this is an
4439 // intermediate/temporary reference and because the current
4440 // concurrent copying collector keeps the from-space memory
4441 // intact/accessible until the end of the marking phase (the
4442 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004443 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004444 __ Ldr(temp,
4445 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4446 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004447 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004448 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004449 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004450 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004451 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004452
4453 {
4454 // Ensure the pc position is recorded immediately after the `blr` instruction.
4455 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4456
4457 // lr();
4458 __ blr(lr);
4459 DCHECK(!codegen_->IsLeafMethod());
4460 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4461 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004462}
4463
4464void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004465 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004466 if (intrinsic.TryDispatch(invoke)) {
4467 return;
4468 }
4469
Alexandre Rames67555f72014-11-18 10:55:16 +00004470 HandleInvoke(invoke);
4471}
4472
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004473void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004474 // Explicit clinit checks triggered by static invokes must have been pruned by
4475 // art::PrepareForRegisterAllocation.
4476 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004477
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004478 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004479 if (intrinsic.TryDispatch(invoke)) {
4480 return;
4481 }
4482
Alexandre Rames67555f72014-11-18 10:55:16 +00004483 HandleInvoke(invoke);
4484}
4485
Andreas Gampe878d58c2015-01-15 23:24:00 -08004486static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4487 if (invoke->GetLocations()->Intrinsified()) {
4488 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4489 intrinsic.Dispatch(invoke);
4490 return true;
4491 }
4492 return false;
4493}
4494
Vladimir Markodc151b22015-10-15 18:02:30 +01004495HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4496 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004497 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004498 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004499 return desired_dispatch_info;
4500}
4501
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004502void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4503 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004504 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004505 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4506 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004507 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4508 uint32_t offset =
4509 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004510 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004511 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004512 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004513 }
Vladimir Marko58155012015-08-19 12:49:41 +00004514 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004515 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004516 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004517 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4518 DCHECK(GetCompilerOptions().IsBootImage());
4519 // Add ADRP with its PC-relative method patch.
4520 vixl::aarch64::Label* adrp_label = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
4521 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4522 // Add ADD with its PC-relative method patch.
4523 vixl::aarch64::Label* add_label =
4524 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), adrp_label);
4525 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4526 break;
4527 }
Vladimir Marko58155012015-08-19 12:49:41 +00004528 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4529 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004530 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004531 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004532 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko58155012015-08-19 12:49:41 +00004533 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004534 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4535 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004536 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004537 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004538 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004539 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004540 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004541 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004542 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004543 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4544 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4545 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004546 }
4547 }
4548
4549 switch (invoke->GetCodePtrLocation()) {
4550 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004551 {
4552 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4553 ExactAssemblyScope eas(GetVIXLAssembler(),
4554 kInstructionSize,
4555 CodeBufferCheckScope::kExactSize);
4556 __ bl(&frame_entry_label_);
4557 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4558 }
Vladimir Marko58155012015-08-19 12:49:41 +00004559 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004560 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4561 // LR = callee_method->entry_point_from_quick_compiled_code_;
4562 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004563 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004564 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004565 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004566 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004567 ExactAssemblyScope eas(GetVIXLAssembler(),
4568 kInstructionSize,
4569 CodeBufferCheckScope::kExactSize);
4570 // lr()
4571 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004572 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004573 }
Vladimir Marko58155012015-08-19 12:49:41 +00004574 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004575 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004576
Andreas Gampe878d58c2015-01-15 23:24:00 -08004577 DCHECK(!IsLeafMethod());
4578}
4579
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004580void CodeGeneratorARM64::GenerateVirtualCall(
4581 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004582 // Use the calling convention instead of the location of the receiver, as
4583 // intrinsics may have put the receiver in a different register. In the intrinsics
4584 // slow path, the arguments have been moved to the right place, so here we are
4585 // guaranteed that the receiver is the first register of the calling convention.
4586 InvokeDexCallingConvention calling_convention;
4587 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004588 Register temp = XRegisterFrom(temp_in);
4589 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4590 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4591 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004592 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004593
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004594 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004595
4596 {
4597 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4598 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4599 // /* HeapReference<Class> */ temp = receiver->klass_
4600 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4601 MaybeRecordImplicitNullCheck(invoke);
4602 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004603 // Instead of simply (possibly) unpoisoning `temp` here, we should
4604 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004605 // intermediate/temporary reference and because the current
4606 // concurrent copying collector keeps the from-space memory
4607 // intact/accessible until the end of the marking phase (the
4608 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004609 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4610 // temp = temp->GetMethodAt(method_offset);
4611 __ Ldr(temp, MemOperand(temp, method_offset));
4612 // lr = temp->GetEntryPoint();
4613 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004614 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004615 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004616 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4617 // lr();
4618 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004619 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004620 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004621}
4622
Orion Hodsonac141392017-01-13 11:53:47 +00004623void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4624 HandleInvoke(invoke);
4625}
4626
4627void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4628 codegen_->GenerateInvokePolymorphicCall(invoke);
4629}
4630
Vladimir Marko65979462017-05-19 17:25:12 +01004631vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeMethodPatch(
4632 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004633 vixl::aarch64::Label* adrp_label) {
Vladimir Marko65979462017-05-19 17:25:12 +01004634 return NewPcRelativePatch(*target_method.dex_file,
4635 target_method.dex_method_index,
4636 adrp_label,
4637 &pc_relative_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004638}
4639
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004640vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4641 MethodReference target_method,
4642 vixl::aarch64::Label* adrp_label) {
4643 return NewPcRelativePatch(*target_method.dex_file,
4644 target_method.dex_method_index,
4645 adrp_label,
4646 &method_bss_entry_patches_);
4647}
4648
Scott Wakeling97c72b72016-06-24 16:19:36 +01004649vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4650 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004651 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004652 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004653 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004654}
4655
Vladimir Marko1998cd02017-01-13 13:02:58 +00004656vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4657 const DexFile& dex_file,
4658 dex::TypeIndex type_index,
4659 vixl::aarch64::Label* adrp_label) {
4660 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4661}
4662
Vladimir Marko65979462017-05-19 17:25:12 +01004663vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4664 const DexFile& dex_file,
4665 dex::StringIndex string_index,
4666 vixl::aarch64::Label* adrp_label) {
4667 return
4668 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
4669}
4670
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004671vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4672 baker_read_barrier_patches_.emplace_back(custom_data);
4673 return &baker_read_barrier_patches_.back().label;
4674}
4675
Scott Wakeling97c72b72016-06-24 16:19:36 +01004676vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4677 const DexFile& dex_file,
4678 uint32_t offset_or_index,
4679 vixl::aarch64::Label* adrp_label,
4680 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004681 // Add a patch entry and return the label.
4682 patches->emplace_back(dex_file, offset_or_index);
4683 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004684 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004685 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4686 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4687 return label;
4688}
4689
Scott Wakeling97c72b72016-06-24 16:19:36 +01004690vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4691 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004692 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004693}
4694
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004695vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004696 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4697 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4698 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004699 return jit_string_patches_.GetOrCreate(
4700 StringReference(&dex_file, string_index),
4701 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4702}
4703
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004704vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004705 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4706 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4707 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004708 return jit_class_patches_.GetOrCreate(
4709 TypeReference(&dex_file, type_index),
4710 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4711}
4712
Vladimir Markoaad75c62016-10-03 08:46:48 +00004713void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4714 vixl::aarch64::Register reg) {
4715 DCHECK(reg.IsX());
4716 SingleEmissionCheckScope guard(GetVIXLAssembler());
4717 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004718 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004719}
4720
4721void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4722 vixl::aarch64::Register out,
4723 vixl::aarch64::Register base) {
4724 DCHECK(out.IsX());
4725 DCHECK(base.IsX());
4726 SingleEmissionCheckScope guard(GetVIXLAssembler());
4727 __ Bind(fixup_label);
4728 __ add(out, base, Operand(/* offset placeholder */ 0));
4729}
4730
4731void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4732 vixl::aarch64::Register out,
4733 vixl::aarch64::Register base) {
4734 DCHECK(base.IsX());
4735 SingleEmissionCheckScope guard(GetVIXLAssembler());
4736 __ Bind(fixup_label);
4737 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4738}
4739
4740template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4741inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4742 const ArenaDeque<PcRelativePatchInfo>& infos,
4743 ArenaVector<LinkerPatch>* linker_patches) {
4744 for (const PcRelativePatchInfo& info : infos) {
4745 linker_patches->push_back(Factory(info.label.GetLocation(),
4746 &info.target_dex_file,
4747 info.pc_insn_label->GetLocation(),
4748 info.offset_or_index));
4749 }
4750}
4751
Vladimir Marko58155012015-08-19 12:49:41 +00004752void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4753 DCHECK(linker_patches->empty());
4754 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004755 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004756 method_bss_entry_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004757 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004758 type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004759 pc_relative_string_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004760 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004761 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01004762 if (GetCompilerOptions().IsBootImage()) {
4763 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004764 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004765 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4766 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004767 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4768 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004769 } else {
4770 DCHECK(pc_relative_method_patches_.empty());
4771 DCHECK(pc_relative_type_patches_.empty());
4772 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4773 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004774 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004775 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
4776 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004777 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4778 linker_patches);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004779 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
4780 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
4781 info.custom_data));
4782 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004783 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004784}
4785
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004786vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4787 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004788 value,
4789 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4790}
4791
Scott Wakeling97c72b72016-06-24 16:19:36 +01004792vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004793 return uint64_literals_.GetOrCreate(
4794 value,
4795 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004796}
4797
Andreas Gampe878d58c2015-01-15 23:24:00 -08004798void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004799 // Explicit clinit checks triggered by static invokes must have been pruned by
4800 // art::PrepareForRegisterAllocation.
4801 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004802
Andreas Gampe878d58c2015-01-15 23:24:00 -08004803 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4804 return;
4805 }
4806
Artem Serov914d7a82017-02-07 14:33:49 +00004807 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4808 // are no pools emitted.
4809 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004810 LocationSummary* locations = invoke->GetLocations();
4811 codegen_->GenerateStaticOrDirectCall(
4812 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Alexandre Rames5319def2014-10-23 10:03:10 +01004813}
4814
4815void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004816 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4817 return;
4818 }
4819
Artem Serov914d7a82017-02-07 14:33:49 +00004820 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4821 // are no pools emitted.
4822 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004823 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004824 DCHECK(!codegen_->IsLeafMethod());
Alexandre Rames5319def2014-10-23 10:03:10 +01004825}
4826
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004827HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4828 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004829 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004830 case HLoadClass::LoadKind::kInvalid:
4831 LOG(FATAL) << "UNREACHABLE";
4832 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004833 case HLoadClass::LoadKind::kReferrersClass:
4834 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004835 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004836 case HLoadClass::LoadKind::kBssEntry:
4837 DCHECK(!Runtime::Current()->UseJitCompilation());
4838 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004839 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004840 DCHECK(Runtime::Current()->UseJitCompilation());
4841 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004842 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004843 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004844 break;
4845 }
4846 return desired_class_load_kind;
4847}
4848
Alexandre Rames67555f72014-11-18 10:55:16 +00004849void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004850 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004851 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004852 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004853 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004854 cls,
4855 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004856 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004857 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004858 return;
4859 }
Vladimir Marko41559982017-01-06 14:04:23 +00004860 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004861
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004862 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4863 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004864 ? LocationSummary::kCallOnSlowPath
4865 : LocationSummary::kNoCall;
4866 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004867 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004868 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004869 }
4870
Vladimir Marko41559982017-01-06 14:04:23 +00004871 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004872 locations->SetInAt(0, Location::RequiresRegister());
4873 }
4874 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004875 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4876 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4877 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004878 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004879 RegisterSet caller_saves = RegisterSet::Empty();
4880 InvokeRuntimeCallingConvention calling_convention;
4881 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4882 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4883 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4884 Primitive::kPrimNot).GetCode());
4885 locations->SetCustomSlowPathCallerSaves(caller_saves);
4886 } else {
4887 // For non-Baker read barrier we have a temp-clobbering call.
4888 }
4889 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004890}
4891
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004892// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4893// move.
4894void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004895 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004896 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00004897 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004898 return;
4899 }
Vladimir Marko41559982017-01-06 14:04:23 +00004900 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004901
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004902 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004903 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004904 Register bss_entry_temp;
4905 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004906
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004907 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4908 ? kWithoutReadBarrier
4909 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004910 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004911 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004912 case HLoadClass::LoadKind::kReferrersClass: {
4913 DCHECK(!cls->CanCallRuntime());
4914 DCHECK(!cls->MustGenerateClinitCheck());
4915 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4916 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004917 GenerateGcRootFieldLoad(cls,
4918 out_loc,
4919 current_method,
4920 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004921 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004922 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004923 break;
4924 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004925 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004926 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004927 // Add ADRP with its PC-relative type patch.
4928 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004929 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004930 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004931 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004932 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004933 vixl::aarch64::Label* add_label =
4934 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004935 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004936 break;
4937 }
4938 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004939 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004940 uint32_t address = dchecked_integral_cast<uint32_t>(
4941 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4942 DCHECK_NE(address, 0u);
4943 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004944 break;
4945 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004946 case HLoadClass::LoadKind::kBssEntry: {
4947 // Add ADRP with its PC-relative Class .bss entry patch.
4948 const DexFile& dex_file = cls->GetDexFile();
4949 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004950 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004951 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4952 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004953 // Add LDR with its PC-relative Class patch.
4954 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004955 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004956 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4957 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004958 out_loc,
4959 bss_entry_temp,
4960 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004961 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004962 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004963 generate_null_check = true;
4964 break;
4965 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004966 case HLoadClass::LoadKind::kJitTableAddress: {
4967 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4968 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004969 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004970 GenerateGcRootFieldLoad(cls,
4971 out_loc,
4972 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004973 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004974 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004975 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004976 break;
4977 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004978 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004979 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004980 LOG(FATAL) << "UNREACHABLE";
4981 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004982 }
4983
Vladimir Markoea4c1262017-02-06 19:59:33 +00004984 bool do_clinit = cls->MustGenerateClinitCheck();
4985 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004986 DCHECK(cls->CanCallRuntime());
4987 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00004988 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004989 codegen_->AddSlowPath(slow_path);
4990 if (generate_null_check) {
4991 __ Cbz(out, slow_path->GetEntryLabel());
4992 }
4993 if (cls->MustGenerateClinitCheck()) {
4994 GenerateClassInitializationCheck(slow_path, out);
4995 } else {
4996 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004997 }
4998 }
4999}
5000
David Brazdilcb1c0552015-08-04 16:22:25 +01005001static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005002 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005003}
5004
Alexandre Rames67555f72014-11-18 10:55:16 +00005005void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5006 LocationSummary* locations =
5007 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5008 locations->SetOut(Location::RequiresRegister());
5009}
5010
5011void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005012 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5013}
5014
5015void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
5016 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5017}
5018
5019void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5020 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005021}
5022
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005023HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5024 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005025 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005026 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005027 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005028 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005029 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005030 case HLoadString::LoadKind::kJitTableAddress:
5031 DCHECK(Runtime::Current()->UseJitCompilation());
5032 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005033 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005034 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005035 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005036 }
5037 return desired_string_load_kind;
5038}
5039
Alexandre Rames67555f72014-11-18 10:55:16 +00005040void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005041 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005042 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005043 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005044 InvokeRuntimeCallingConvention calling_convention;
5045 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5046 } else {
5047 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005048 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5049 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005050 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005051 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005052 RegisterSet caller_saves = RegisterSet::Empty();
5053 InvokeRuntimeCallingConvention calling_convention;
5054 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5055 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
5056 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
5057 Primitive::kPrimNot).GetCode());
5058 locations->SetCustomSlowPathCallerSaves(caller_saves);
5059 } else {
5060 // For non-Baker read barrier we have a temp-clobbering call.
5061 }
5062 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005063 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005064}
5065
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005066// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5067// move.
5068void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005069 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005070 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005071
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005072 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005073 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005074 // Add ADRP with its PC-relative String patch.
5075 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005076 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005077 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01005078 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005079 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005080 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005081 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005082 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005083 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005084 return; // No dex cache slow path.
5085 }
5086 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005087 uint32_t address = dchecked_integral_cast<uint32_t>(
5088 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5089 DCHECK_NE(address, 0u);
5090 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005091 return; // No dex cache slow path.
5092 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005093 case HLoadString::LoadKind::kBssEntry: {
5094 // Add ADRP with its PC-relative String .bss entry patch.
5095 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005096 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005097 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005098 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005099 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005100 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005101 // Add LDR with its PC-relative String patch.
5102 vixl::aarch64::Label* ldr_label =
5103 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005104 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005105 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005106 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005107 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005108 /* offset placeholder */ 0u,
5109 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005110 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005111 SlowPathCodeARM64* slow_path =
5112 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005113 codegen_->AddSlowPath(slow_path);
5114 __ Cbz(out.X(), slow_path->GetEntryLabel());
5115 __ Bind(slow_path->GetExitLabel());
5116 return;
5117 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005118 case HLoadString::LoadKind::kJitTableAddress: {
5119 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005120 load->GetStringIndex(),
5121 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005122 GenerateGcRootFieldLoad(load,
5123 out_loc,
5124 out.X(),
5125 /* offset */ 0,
5126 /* fixup_label */ nullptr,
5127 kCompilerReadBarrierOption);
5128 return;
5129 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005130 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005131 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005132 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005133
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005134 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005135 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005136 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005137 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005138 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5139 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005140}
5141
Alexandre Rames5319def2014-10-23 10:03:10 +01005142void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5143 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5144 locations->SetOut(Location::ConstantLocation(constant));
5145}
5146
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005147void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005148 // Will be generated at use site.
5149}
5150
Alexandre Rames67555f72014-11-18 10:55:16 +00005151void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5152 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005153 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005154 InvokeRuntimeCallingConvention calling_convention;
5155 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5156}
5157
5158void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005159 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005160 instruction,
5161 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005162 if (instruction->IsEnter()) {
5163 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5164 } else {
5165 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5166 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005167}
5168
Alexandre Rames42d641b2014-10-27 14:00:51 +00005169void LocationsBuilderARM64::VisitMul(HMul* mul) {
5170 LocationSummary* locations =
5171 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5172 switch (mul->GetResultType()) {
5173 case Primitive::kPrimInt:
5174 case Primitive::kPrimLong:
5175 locations->SetInAt(0, Location::RequiresRegister());
5176 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005177 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005178 break;
5179
5180 case Primitive::kPrimFloat:
5181 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005182 locations->SetInAt(0, Location::RequiresFpuRegister());
5183 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005184 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005185 break;
5186
5187 default:
5188 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5189 }
5190}
5191
5192void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5193 switch (mul->GetResultType()) {
5194 case Primitive::kPrimInt:
5195 case Primitive::kPrimLong:
5196 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5197 break;
5198
5199 case Primitive::kPrimFloat:
5200 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005201 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005202 break;
5203
5204 default:
5205 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5206 }
5207}
5208
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005209void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5210 LocationSummary* locations =
5211 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5212 switch (neg->GetResultType()) {
5213 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005214 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005215 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005216 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005217 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005218
5219 case Primitive::kPrimFloat:
5220 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005221 locations->SetInAt(0, Location::RequiresFpuRegister());
5222 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005223 break;
5224
5225 default:
5226 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5227 }
5228}
5229
5230void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5231 switch (neg->GetResultType()) {
5232 case Primitive::kPrimInt:
5233 case Primitive::kPrimLong:
5234 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5235 break;
5236
5237 case Primitive::kPrimFloat:
5238 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005239 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005240 break;
5241
5242 default:
5243 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5244 }
5245}
5246
5247void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5248 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005249 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005250 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005251 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005252 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5253 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005254}
5255
5256void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005257 // Note: if heap poisoning is enabled, the entry point takes cares
5258 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005259 QuickEntrypointEnum entrypoint =
5260 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5261 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005262 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005263}
5264
Alexandre Rames5319def2014-10-23 10:03:10 +01005265void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5266 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005267 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005268 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005269 if (instruction->IsStringAlloc()) {
5270 locations->AddTemp(LocationFrom(kArtMethodRegister));
5271 } else {
5272 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005273 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005274 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5275}
5276
5277void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005278 // Note: if heap poisoning is enabled, the entry point takes cares
5279 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005280 if (instruction->IsStringAlloc()) {
5281 // String is allocated through StringFactory. Call NewEmptyString entry point.
5282 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005283 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005284 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5285 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005286
5287 {
5288 // Ensure the pc position is recorded immediately after the `blr` instruction.
5289 ExactAssemblyScope eas(GetVIXLAssembler(),
5290 kInstructionSize,
5291 CodeBufferCheckScope::kExactSize);
5292 __ blr(lr);
5293 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5294 }
David Brazdil6de19382016-01-08 17:37:10 +00005295 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005296 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005297 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005298 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005299}
5300
5301void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5302 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005303 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005304 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005305}
5306
5307void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005308 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005309 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005310 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005311 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005312 break;
5313
5314 default:
5315 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5316 }
5317}
5318
David Brazdil66d126e2015-04-03 16:02:44 +01005319void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5320 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5321 locations->SetInAt(0, Location::RequiresRegister());
5322 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5323}
5324
5325void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005326 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005327}
5328
Alexandre Rames5319def2014-10-23 10:03:10 +01005329void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005330 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5331 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005332}
5333
Calin Juravle2ae48182016-03-16 14:05:09 +00005334void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5335 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005336 return;
5337 }
Artem Serov914d7a82017-02-07 14:33:49 +00005338 {
5339 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5340 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5341 Location obj = instruction->GetLocations()->InAt(0);
5342 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5343 RecordPcInfo(instruction, instruction->GetDexPc());
5344 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005345}
5346
Calin Juravle2ae48182016-03-16 14:05:09 +00005347void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005348 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005349 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005350
5351 LocationSummary* locations = instruction->GetLocations();
5352 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005353
5354 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005355}
5356
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005357void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005358 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005359}
5360
Alexandre Rames67555f72014-11-18 10:55:16 +00005361void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5362 HandleBinaryOp(instruction);
5363}
5364
5365void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5366 HandleBinaryOp(instruction);
5367}
5368
Alexandre Rames3e69f162014-12-10 10:36:50 +00005369void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5370 LOG(FATAL) << "Unreachable";
5371}
5372
5373void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5374 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5375}
5376
Alexandre Rames5319def2014-10-23 10:03:10 +01005377void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5378 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5379 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5380 if (location.IsStackSlot()) {
5381 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5382 } else if (location.IsDoubleStackSlot()) {
5383 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5384 }
5385 locations->SetOut(location);
5386}
5387
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005388void InstructionCodeGeneratorARM64::VisitParameterValue(
5389 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005390 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005391}
5392
5393void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5394 LocationSummary* locations =
5395 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005396 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005397}
5398
5399void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5400 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5401 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005402}
5403
5404void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5405 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005406 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005407 locations->SetInAt(i, Location::Any());
5408 }
5409 locations->SetOut(Location::Any());
5410}
5411
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005412void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005413 LOG(FATAL) << "Unreachable";
5414}
5415
Serban Constantinescu02164b32014-11-13 14:05:07 +00005416void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005417 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005418 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005419 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5420 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005421 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5422
5423 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005424 case Primitive::kPrimInt:
5425 case Primitive::kPrimLong:
5426 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005427 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005428 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5429 break;
5430
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005431 case Primitive::kPrimFloat:
5432 case Primitive::kPrimDouble: {
5433 InvokeRuntimeCallingConvention calling_convention;
5434 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5435 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5436 locations->SetOut(calling_convention.GetReturnLocation(type));
5437
5438 break;
5439 }
5440
Serban Constantinescu02164b32014-11-13 14:05:07 +00005441 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005442 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005443 }
5444}
5445
5446void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5447 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005448
Serban Constantinescu02164b32014-11-13 14:05:07 +00005449 switch (type) {
5450 case Primitive::kPrimInt:
5451 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005452 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005453 break;
5454 }
5455
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005456 case Primitive::kPrimFloat:
5457 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005458 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5459 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005460 if (type == Primitive::kPrimFloat) {
5461 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5462 } else {
5463 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5464 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005465 break;
5466 }
5467
Serban Constantinescu02164b32014-11-13 14:05:07 +00005468 default:
5469 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005470 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005471 }
5472}
5473
Igor Murashkind01745e2017-04-05 16:40:31 -07005474void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5475 constructor_fence->SetLocations(nullptr);
5476}
5477
5478void InstructionCodeGeneratorARM64::VisitConstructorFence(
5479 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5480 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5481}
5482
Calin Juravle27df7582015-04-17 19:12:31 +01005483void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5484 memory_barrier->SetLocations(nullptr);
5485}
5486
5487void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005488 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005489}
5490
Alexandre Rames5319def2014-10-23 10:03:10 +01005491void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5492 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5493 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005494 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005495}
5496
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005497void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005498 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005499}
5500
5501void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5502 instruction->SetLocations(nullptr);
5503}
5504
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005505void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005506 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005507}
5508
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005509void LocationsBuilderARM64::VisitRor(HRor* ror) {
5510 HandleBinaryOp(ror);
5511}
5512
5513void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5514 HandleBinaryOp(ror);
5515}
5516
Serban Constantinescu02164b32014-11-13 14:05:07 +00005517void LocationsBuilderARM64::VisitShl(HShl* shl) {
5518 HandleShift(shl);
5519}
5520
5521void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5522 HandleShift(shl);
5523}
5524
5525void LocationsBuilderARM64::VisitShr(HShr* shr) {
5526 HandleShift(shr);
5527}
5528
5529void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5530 HandleShift(shr);
5531}
5532
Alexandre Rames5319def2014-10-23 10:03:10 +01005533void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005534 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005535}
5536
5537void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005538 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005539}
5540
Alexandre Rames67555f72014-11-18 10:55:16 +00005541void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005542 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005543}
5544
5545void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005546 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005547}
5548
5549void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005550 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005551}
5552
Alexandre Rames67555f72014-11-18 10:55:16 +00005553void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005554 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005555}
5556
Calin Juravlee460d1d2015-09-29 04:52:17 +01005557void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5558 HUnresolvedInstanceFieldGet* instruction) {
5559 FieldAccessCallingConventionARM64 calling_convention;
5560 codegen_->CreateUnresolvedFieldLocationSummary(
5561 instruction, instruction->GetFieldType(), calling_convention);
5562}
5563
5564void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5565 HUnresolvedInstanceFieldGet* instruction) {
5566 FieldAccessCallingConventionARM64 calling_convention;
5567 codegen_->GenerateUnresolvedFieldAccess(instruction,
5568 instruction->GetFieldType(),
5569 instruction->GetFieldIndex(),
5570 instruction->GetDexPc(),
5571 calling_convention);
5572}
5573
5574void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5575 HUnresolvedInstanceFieldSet* instruction) {
5576 FieldAccessCallingConventionARM64 calling_convention;
5577 codegen_->CreateUnresolvedFieldLocationSummary(
5578 instruction, instruction->GetFieldType(), calling_convention);
5579}
5580
5581void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5582 HUnresolvedInstanceFieldSet* instruction) {
5583 FieldAccessCallingConventionARM64 calling_convention;
5584 codegen_->GenerateUnresolvedFieldAccess(instruction,
5585 instruction->GetFieldType(),
5586 instruction->GetFieldIndex(),
5587 instruction->GetDexPc(),
5588 calling_convention);
5589}
5590
5591void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5592 HUnresolvedStaticFieldGet* instruction) {
5593 FieldAccessCallingConventionARM64 calling_convention;
5594 codegen_->CreateUnresolvedFieldLocationSummary(
5595 instruction, instruction->GetFieldType(), calling_convention);
5596}
5597
5598void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5599 HUnresolvedStaticFieldGet* instruction) {
5600 FieldAccessCallingConventionARM64 calling_convention;
5601 codegen_->GenerateUnresolvedFieldAccess(instruction,
5602 instruction->GetFieldType(),
5603 instruction->GetFieldIndex(),
5604 instruction->GetDexPc(),
5605 calling_convention);
5606}
5607
5608void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5609 HUnresolvedStaticFieldSet* instruction) {
5610 FieldAccessCallingConventionARM64 calling_convention;
5611 codegen_->CreateUnresolvedFieldLocationSummary(
5612 instruction, instruction->GetFieldType(), calling_convention);
5613}
5614
5615void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5616 HUnresolvedStaticFieldSet* instruction) {
5617 FieldAccessCallingConventionARM64 calling_convention;
5618 codegen_->GenerateUnresolvedFieldAccess(instruction,
5619 instruction->GetFieldType(),
5620 instruction->GetFieldIndex(),
5621 instruction->GetDexPc(),
5622 calling_convention);
5623}
5624
Alexandre Rames5319def2014-10-23 10:03:10 +01005625void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005626 LocationSummary* locations =
5627 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005628 // In suspend check slow path, usually there are no caller-save registers at all.
5629 // If SIMD instructions are present, however, we force spilling all live SIMD
5630 // registers in full width (since the runtime only saves/restores lower part).
5631 locations->SetCustomSlowPathCallerSaves(
5632 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005633}
5634
5635void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005636 HBasicBlock* block = instruction->GetBlock();
5637 if (block->GetLoopInformation() != nullptr) {
5638 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5639 // The back edge will generate the suspend check.
5640 return;
5641 }
5642 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5643 // The goto will generate the suspend check.
5644 return;
5645 }
5646 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005647}
5648
Alexandre Rames67555f72014-11-18 10:55:16 +00005649void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5650 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005651 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005652 InvokeRuntimeCallingConvention calling_convention;
5653 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5654}
5655
5656void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005657 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005658 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005659}
5660
5661void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5662 LocationSummary* locations =
5663 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5664 Primitive::Type input_type = conversion->GetInputType();
5665 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005666 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005667 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5668 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5669 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5670 }
5671
Alexandre Rames542361f2015-01-29 16:57:31 +00005672 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005673 locations->SetInAt(0, Location::RequiresFpuRegister());
5674 } else {
5675 locations->SetInAt(0, Location::RequiresRegister());
5676 }
5677
Alexandre Rames542361f2015-01-29 16:57:31 +00005678 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005679 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5680 } else {
5681 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5682 }
5683}
5684
5685void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5686 Primitive::Type result_type = conversion->GetResultType();
5687 Primitive::Type input_type = conversion->GetInputType();
5688
5689 DCHECK_NE(input_type, result_type);
5690
Alexandre Rames542361f2015-01-29 16:57:31 +00005691 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005692 int result_size = Primitive::ComponentSize(result_type);
5693 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005694 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005695 Register output = OutputRegister(conversion);
5696 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005697 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005698 // 'int' values are used directly as W registers, discarding the top
5699 // bits, so we don't need to sign-extend and can just perform a move.
5700 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5701 // top 32 bits of the target register. We theoretically could leave those
5702 // bits unchanged, but we would have to make sure that no code uses a
5703 // 32bit input value as a 64bit value assuming that the top 32 bits are
5704 // zero.
5705 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005706 } else if (result_type == Primitive::kPrimChar ||
5707 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5708 __ Ubfx(output,
5709 output.IsX() ? source.X() : source.W(),
5710 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005711 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005712 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005713 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005714 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005715 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005716 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005717 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5718 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005719 } else if (Primitive::IsFloatingPointType(result_type) &&
5720 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005721 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5722 } else {
5723 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5724 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005725 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005726}
Alexandre Rames67555f72014-11-18 10:55:16 +00005727
Serban Constantinescu02164b32014-11-13 14:05:07 +00005728void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5729 HandleShift(ushr);
5730}
5731
5732void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5733 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005734}
5735
5736void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5737 HandleBinaryOp(instruction);
5738}
5739
5740void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5741 HandleBinaryOp(instruction);
5742}
5743
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005744void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005745 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005746 LOG(FATAL) << "Unreachable";
5747}
5748
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005749void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005750 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005751 LOG(FATAL) << "Unreachable";
5752}
5753
Mark Mendellfe57faa2015-09-18 09:26:15 -04005754// Simple implementation of packed switch - generate cascaded compare/jumps.
5755void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5756 LocationSummary* locations =
5757 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5758 locations->SetInAt(0, Location::RequiresRegister());
5759}
5760
5761void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5762 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005763 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005764 Register value_reg = InputRegisterAt(switch_instr, 0);
5765 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5766
Zheng Xu3927c8b2015-11-18 17:46:25 +08005767 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005768 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005769 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5770 // make sure we don't emit it if the target may run out of range.
5771 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5772 // ranges and emit the tables only as required.
5773 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005774
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005775 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005776 // Current instruction id is an upper bound of the number of HIRs in the graph.
5777 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5778 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005779 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5780 Register temp = temps.AcquireW();
5781 __ Subs(temp, value_reg, Operand(lower_bound));
5782
Zheng Xu3927c8b2015-11-18 17:46:25 +08005783 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005784 // Jump to successors[0] if value == lower_bound.
5785 __ B(eq, codegen_->GetLabelOf(successors[0]));
5786 int32_t last_index = 0;
5787 for (; num_entries - last_index > 2; last_index += 2) {
5788 __ Subs(temp, temp, Operand(2));
5789 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5790 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5791 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5792 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5793 }
5794 if (num_entries - last_index == 2) {
5795 // The last missing case_value.
5796 __ Cmp(temp, Operand(1));
5797 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005798 }
5799
5800 // And the default for any other value.
5801 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5802 __ B(codegen_->GetLabelOf(default_block));
5803 }
5804 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005805 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005806
5807 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5808
5809 // Below instructions should use at most one blocked register. Since there are two blocked
5810 // registers, we are free to block one.
5811 Register temp_w = temps.AcquireW();
5812 Register index;
5813 // Remove the bias.
5814 if (lower_bound != 0) {
5815 index = temp_w;
5816 __ Sub(index, value_reg, Operand(lower_bound));
5817 } else {
5818 index = value_reg;
5819 }
5820
5821 // Jump to default block if index is out of the range.
5822 __ Cmp(index, Operand(num_entries));
5823 __ B(hs, codegen_->GetLabelOf(default_block));
5824
5825 // In current VIXL implementation, it won't require any blocked registers to encode the
5826 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5827 // register pressure.
5828 Register table_base = temps.AcquireX();
5829 // Load jump offset from the table.
5830 __ Adr(table_base, jump_table->GetTableStartLabel());
5831 Register jump_offset = temp_w;
5832 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5833
5834 // Jump to target block by branching to table_base(pc related) + offset.
5835 Register target_address = table_base;
5836 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5837 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005838 }
5839}
5840
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005841void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5842 HInstruction* instruction,
5843 Location out,
5844 uint32_t offset,
5845 Location maybe_temp,
5846 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005847 Primitive::Type type = Primitive::kPrimNot;
5848 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005849 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005850 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005851 if (kUseBakerReadBarrier) {
5852 // Load with fast path based Baker's read barrier.
5853 // /* HeapReference<Object> */ out = *(out + offset)
5854 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5855 out,
5856 out_reg,
5857 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005858 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005859 /* needs_null_check */ false,
5860 /* use_load_acquire */ false);
5861 } else {
5862 // Load with slow path based read barrier.
5863 // Save the value of `out` into `maybe_temp` before overwriting it
5864 // in the following move operation, as we will need it for the
5865 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005866 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005867 __ Mov(temp_reg, out_reg);
5868 // /* HeapReference<Object> */ out = *(out + offset)
5869 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5870 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5871 }
5872 } else {
5873 // Plain load with no read barrier.
5874 // /* HeapReference<Object> */ out = *(out + offset)
5875 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5876 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5877 }
5878}
5879
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005880void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5881 HInstruction* instruction,
5882 Location out,
5883 Location obj,
5884 uint32_t offset,
5885 Location maybe_temp,
5886 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005887 Primitive::Type type = Primitive::kPrimNot;
5888 Register out_reg = RegisterFrom(out, type);
5889 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005890 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005891 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005892 if (kUseBakerReadBarrier) {
5893 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005894 // /* HeapReference<Object> */ out = *(obj + offset)
5895 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5896 out,
5897 obj_reg,
5898 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005899 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005900 /* needs_null_check */ false,
5901 /* use_load_acquire */ false);
5902 } else {
5903 // Load with slow path based read barrier.
5904 // /* HeapReference<Object> */ out = *(obj + offset)
5905 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5906 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5907 }
5908 } else {
5909 // Plain load with no read barrier.
5910 // /* HeapReference<Object> */ out = *(obj + offset)
5911 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5912 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5913 }
5914}
5915
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005916void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5917 HInstruction* instruction,
5918 Location root,
5919 Register obj,
5920 uint32_t offset,
5921 vixl::aarch64::Label* fixup_label,
5922 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005923 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005924 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005925 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005926 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005927 if (kUseBakerReadBarrier) {
5928 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005929 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005930 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5931 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01005932 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
5933 // the Marking Register) to decide whether we need to enter
5934 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005935 //
5936 // We use link-time generated thunks for the slow path. That thunk
5937 // checks the reference and jumps to the entrypoint if needed.
5938 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005939 // lr = &return_address;
5940 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01005941 // if (mr) { // Thread::Current()->GetIsGcMarking()
5942 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005943 // }
5944 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00005945
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005946 UseScratchRegisterScope temps(GetVIXLAssembler());
5947 DCHECK(temps.IsAvailable(ip0));
5948 DCHECK(temps.IsAvailable(ip1));
5949 temps.Exclude(ip0, ip1);
5950 uint32_t custom_data =
5951 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
5952 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00005953
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005954 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5955 vixl::aarch64::Label return_address;
5956 __ adr(lr, &return_address);
5957 if (fixup_label != nullptr) {
5958 __ Bind(fixup_label);
5959 }
5960 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
5961 "GC root LDR must be 2 instruction (8B) before the return address label.");
5962 __ ldr(root_reg, MemOperand(obj.X(), offset));
5963 __ Bind(cbnz_label);
Roland Levillain97c46462017-05-11 14:04:03 +01005964 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005965 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005966 } else {
Roland Levillain97c46462017-05-11 14:04:03 +01005967 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
5968 // the Marking Register) to decide whether we need to enter
5969 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005970 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005971 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01005972 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005973 // // Slow path.
Roland Levillain97c46462017-05-11 14:04:03 +01005974 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5975 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005976 // }
Roland Levillain44015862016-01-22 11:47:17 +00005977
Roland Levillain97c46462017-05-11 14:04:03 +01005978 // Slow path marking the GC root `root`. The entrypoint will
5979 // be loaded by the slow path code.
5980 SlowPathCodeARM64* slow_path =
5981 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005982 codegen_->AddSlowPath(slow_path);
5983
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005984 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5985 if (fixup_label == nullptr) {
5986 __ Ldr(root_reg, MemOperand(obj, offset));
5987 } else {
5988 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
5989 }
5990 static_assert(
5991 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5992 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5993 "have different sizes.");
5994 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5995 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5996 "have different sizes.");
5997
Roland Levillain97c46462017-05-11 14:04:03 +01005998 __ Cbnz(mr, slow_path->GetEntryLabel());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005999 __ Bind(slow_path->GetExitLabel());
6000 }
Roland Levillain44015862016-01-22 11:47:17 +00006001 } else {
6002 // GC root loaded through a slow path for read barriers other
6003 // than Baker's.
6004 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006005 if (fixup_label == nullptr) {
6006 __ Add(root_reg.X(), obj.X(), offset);
6007 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006008 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006009 }
Roland Levillain44015862016-01-22 11:47:17 +00006010 // /* mirror::Object* */ root = root->Read()
6011 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6012 }
6013 } else {
6014 // Plain GC root load with no read barrier.
6015 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006016 if (fixup_label == nullptr) {
6017 __ Ldr(root_reg, MemOperand(obj, offset));
6018 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006019 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006020 }
Roland Levillain44015862016-01-22 11:47:17 +00006021 // Note that GC roots are not affected by heap poisoning, thus we
6022 // do not have to unpoison `root_reg` here.
6023 }
6024}
6025
6026void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6027 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006028 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006029 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006030 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006031 bool needs_null_check,
6032 bool use_load_acquire) {
6033 DCHECK(kEmitCompilerReadBarrier);
6034 DCHECK(kUseBakerReadBarrier);
6035
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006036 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6037 !use_load_acquire &&
6038 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006039 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6040 // Marking Register) to decide whether we need to enter the slow
6041 // path to mark the reference. Then, in the slow path, check the
6042 // gray bit in the lock word of the reference's holder (`obj`) to
6043 // decide whether to mark `ref` or not.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006044 //
6045 // We use link-time generated thunks for the slow path. That thunk checks
6046 // the holder and jumps to the entrypoint if needed. If the holder is not
6047 // gray, it creates a fake dependency and returns to the LDR instruction.
6048 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006049 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006050 // if (mr) { // Thread::Current()->GetIsGcMarking()
6051 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006052 // }
6053 // not_gray_return_address:
6054 // // Original reference load. If the offset is too large to fit
6055 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006056 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006057 // gray_return_address:
6058
6059 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6060 Register base = obj;
6061 if (offset >= kReferenceLoadMinFarOffset) {
6062 DCHECK(maybe_temp.IsRegister());
6063 base = WRegisterFrom(maybe_temp);
6064 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6065 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6066 offset &= (kReferenceLoadMinFarOffset - 1u);
6067 }
6068 UseScratchRegisterScope temps(GetVIXLAssembler());
6069 DCHECK(temps.IsAvailable(ip0));
6070 DCHECK(temps.IsAvailable(ip1));
6071 temps.Exclude(ip0, ip1);
6072 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6073 base.GetCode(),
6074 obj.GetCode());
6075 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6076
Vladimir Markod1ef8732017-04-18 13:55:13 +01006077 EmissionCheckScope guard(GetVIXLAssembler(),
6078 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006079 vixl::aarch64::Label return_address;
6080 __ adr(lr, &return_address);
6081 __ Bind(cbnz_label);
Roland Levillain97c46462017-05-11 14:04:03 +01006082 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markod1ef8732017-04-18 13:55:13 +01006083 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6084 "Field LDR must be 1 instruction (4B) before the return address label; "
6085 " 2 instructions (8B) for heap poisoning.");
6086 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6087 __ ldr(ref_reg, MemOperand(base.X(), offset));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006088 if (needs_null_check) {
6089 MaybeRecordImplicitNullCheck(instruction);
6090 }
Vladimir Markod1ef8732017-04-18 13:55:13 +01006091 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006092 __ Bind(&return_address);
6093 return;
6094 }
6095
Roland Levillain44015862016-01-22 11:47:17 +00006096 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006097 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006098 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006099 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006100 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6101 ref,
6102 obj,
6103 offset,
6104 no_index,
6105 no_scale_factor,
6106 temp,
6107 needs_null_check,
6108 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006109}
6110
6111void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6112 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006113 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006114 uint32_t data_offset,
6115 Location index,
6116 Register temp,
6117 bool needs_null_check) {
6118 DCHECK(kEmitCompilerReadBarrier);
6119 DCHECK(kUseBakerReadBarrier);
6120
Vladimir Marko66d691d2017-04-07 17:53:39 +01006121 static_assert(
6122 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6123 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6124 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
6125
6126 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6127 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006128 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6129 // Marking Register) to decide whether we need to enter the slow
6130 // path to mark the reference. Then, in the slow path, check the
6131 // gray bit in the lock word of the reference's holder (`obj`) to
6132 // decide whether to mark `ref` or not.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006133 //
6134 // We use link-time generated thunks for the slow path. That thunk checks
6135 // the holder and jumps to the entrypoint if needed. If the holder is not
6136 // gray, it creates a fake dependency and returns to the LDR instruction.
6137 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006138 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006139 // if (mr) { // Thread::Current()->GetIsGcMarking()
6140 // goto array_thunk<base_reg>(lr)
Vladimir Marko66d691d2017-04-07 17:53:39 +01006141 // }
6142 // not_gray_return_address:
6143 // // Original reference load. If the offset is too large to fit
6144 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006145 // HeapReference<mirror::Object> reference = data[index];
Vladimir Marko66d691d2017-04-07 17:53:39 +01006146 // gray_return_address:
6147
6148 DCHECK(index.IsValid());
6149 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
6150 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6151
6152 UseScratchRegisterScope temps(GetVIXLAssembler());
6153 DCHECK(temps.IsAvailable(ip0));
6154 DCHECK(temps.IsAvailable(ip1));
6155 temps.Exclude(ip0, ip1);
6156 uint32_t custom_data =
6157 linker::Arm64RelativePatcher::EncodeBakerReadBarrierArrayData(temp.GetCode());
6158 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6159
Vladimir Marko66d691d2017-04-07 17:53:39 +01006160 __ Add(temp.X(), obj.X(), Operand(data_offset));
6161 EmissionCheckScope guard(GetVIXLAssembler(),
6162 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6163 vixl::aarch64::Label return_address;
6164 __ adr(lr, &return_address);
6165 __ Bind(cbnz_label);
Roland Levillain97c46462017-05-11 14:04:03 +01006166 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006167 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6168 "Array LDR must be 1 instruction (4B) before the return address label; "
6169 " 2 instructions (8B) for heap poisoning.");
6170 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6171 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6172 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6173 __ Bind(&return_address);
6174 return;
6175 }
6176
Roland Levillain44015862016-01-22 11:47:17 +00006177 // Array cells are never volatile variables, therefore array loads
6178 // never use Load-Acquire instructions on ARM64.
6179 const bool use_load_acquire = false;
6180
6181 // /* HeapReference<Object> */ ref =
6182 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006183 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6184 ref,
6185 obj,
6186 data_offset,
6187 index,
6188 scale_factor,
6189 temp,
6190 needs_null_check,
6191 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006192}
6193
6194void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6195 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006196 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006197 uint32_t offset,
6198 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006199 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006200 Register temp,
6201 bool needs_null_check,
Roland Levillainff487002017-03-07 16:50:01 +00006202 bool use_load_acquire) {
Roland Levillain44015862016-01-22 11:47:17 +00006203 DCHECK(kEmitCompilerReadBarrier);
6204 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006205 // If we are emitting an array load, we should not be using a
6206 // Load Acquire instruction. In other words:
6207 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6208 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006209
Roland Levillain97c46462017-05-11 14:04:03 +01006210 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6211 // Marking Register) to decide whether we need to enter the slow
6212 // path to mark the reference. Then, in the slow path, check the
6213 // gray bit in the lock word of the reference's holder (`obj`) to
6214 // decide whether to mark `ref` or not.
Roland Levillain44015862016-01-22 11:47:17 +00006215 //
Roland Levillain97c46462017-05-11 14:04:03 +01006216 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00006217 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006218 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6219 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6220 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6221 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6222 // if (is_gray) {
Roland Levillain97c46462017-05-11 14:04:03 +01006223 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6224 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillain54f869e2017-03-06 13:54:11 +00006225 // }
6226 // } else {
6227 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006228 // }
Roland Levillain44015862016-01-22 11:47:17 +00006229
Roland Levillainba650a42017-03-06 13:52:32 +00006230 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain97c46462017-05-11 14:04:03 +01006231 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006232 SlowPathCodeARM64* slow_path =
6233 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6234 instruction,
6235 ref,
6236 obj,
6237 offset,
6238 index,
6239 scale_factor,
6240 needs_null_check,
6241 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006242 temp);
Roland Levillainba650a42017-03-06 13:52:32 +00006243 AddSlowPath(slow_path);
6244
Roland Levillain97c46462017-05-11 14:04:03 +01006245 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006246 // Fast path: the GC is not marking: just load the reference.
Roland Levillain54f869e2017-03-06 13:54:11 +00006247 GenerateRawReferenceLoad(
6248 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006249 __ Bind(slow_path->GetExitLabel());
6250}
6251
Roland Levillainff487002017-03-07 16:50:01 +00006252void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6253 Location ref,
6254 Register obj,
6255 Location field_offset,
6256 Register temp,
6257 bool needs_null_check,
6258 bool use_load_acquire) {
6259 DCHECK(kEmitCompilerReadBarrier);
6260 DCHECK(kUseBakerReadBarrier);
6261 // If we are emitting an array load, we should not be using a
6262 // Load Acquire instruction. In other words:
6263 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6264 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6265
Roland Levillain97c46462017-05-11 14:04:03 +01006266 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6267 // Marking Register) to decide whether we need to enter the slow
6268 // path to update the reference field within `obj`. Then, in the
6269 // slow path, check the gray bit in the lock word of the reference's
6270 // holder (`obj`) to decide whether to mark `ref` and update the
6271 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00006272 //
Roland Levillain97c46462017-05-11 14:04:03 +01006273 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00006274 // // Slow path.
6275 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6276 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6277 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6278 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6279 // if (is_gray) {
6280 // old_ref = ref;
Roland Levillain97c46462017-05-11 14:04:03 +01006281 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6282 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00006283 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6284 // }
6285 // }
6286
6287 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain97c46462017-05-11 14:04:03 +01006288 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006289 SlowPathCodeARM64* slow_path =
6290 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6291 instruction,
6292 ref,
6293 obj,
6294 /* offset */ 0u,
6295 /* index */ field_offset,
6296 /* scale_factor */ 0u /* "times 1" */,
6297 needs_null_check,
6298 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006299 temp);
Roland Levillainff487002017-03-07 16:50:01 +00006300 AddSlowPath(slow_path);
6301
Roland Levillain97c46462017-05-11 14:04:03 +01006302 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006303 // Fast path: the GC is not marking: nothing to do (the field is
6304 // up-to-date, and we don't need to load the reference).
6305 __ Bind(slow_path->GetExitLabel());
6306}
6307
Roland Levillainba650a42017-03-06 13:52:32 +00006308void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6309 Location ref,
6310 Register obj,
6311 uint32_t offset,
6312 Location index,
6313 size_t scale_factor,
6314 bool needs_null_check,
6315 bool use_load_acquire) {
6316 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006317 Primitive::Type type = Primitive::kPrimNot;
6318 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006319
Roland Levillainba650a42017-03-06 13:52:32 +00006320 // If needed, vixl::EmissionCheckScope guards are used to ensure
6321 // that no pools are emitted between the load (macro) instruction
6322 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006323
Roland Levillain44015862016-01-22 11:47:17 +00006324 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006325 // Load types involving an "index": ArrayGet,
6326 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6327 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006328 if (use_load_acquire) {
6329 // UnsafeGetObjectVolatile intrinsic case.
6330 // Register `index` is not an index in an object array, but an
6331 // offset to an object reference field within object `obj`.
6332 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6333 DCHECK(instruction->GetLocations()->Intrinsified());
6334 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6335 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006336 DCHECK_EQ(offset, 0u);
6337 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006338 DCHECK_EQ(needs_null_check, false);
6339 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006340 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6341 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006342 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006343 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6344 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006345 if (index.IsConstant()) {
6346 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006347 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006348 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006349 if (needs_null_check) {
6350 MaybeRecordImplicitNullCheck(instruction);
6351 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006352 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006353 UseScratchRegisterScope temps(GetVIXLAssembler());
6354 Register temp = temps.AcquireW();
6355 __ Add(temp, obj, offset);
6356 {
6357 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6358 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6359 if (needs_null_check) {
6360 MaybeRecordImplicitNullCheck(instruction);
6361 }
6362 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006363 }
Roland Levillain44015862016-01-22 11:47:17 +00006364 }
Roland Levillain44015862016-01-22 11:47:17 +00006365 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006366 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006367 MemOperand field = HeapOperand(obj, offset);
6368 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006369 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6370 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006371 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006372 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006373 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006374 if (needs_null_check) {
6375 MaybeRecordImplicitNullCheck(instruction);
6376 }
Roland Levillain44015862016-01-22 11:47:17 +00006377 }
6378 }
6379
6380 // Object* ref = ref_addr->AsMirrorPtr()
6381 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006382}
6383
6384void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6385 Location out,
6386 Location ref,
6387 Location obj,
6388 uint32_t offset,
6389 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006390 DCHECK(kEmitCompilerReadBarrier);
6391
Roland Levillain44015862016-01-22 11:47:17 +00006392 // Insert a slow path based read barrier *after* the reference load.
6393 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006394 // If heap poisoning is enabled, the unpoisoning of the loaded
6395 // reference will be carried out by the runtime within the slow
6396 // path.
6397 //
6398 // Note that `ref` currently does not get unpoisoned (when heap
6399 // poisoning is enabled), which is alright as the `ref` argument is
6400 // not used by the artReadBarrierSlow entry point.
6401 //
6402 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6403 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6404 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6405 AddSlowPath(slow_path);
6406
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006407 __ B(slow_path->GetEntryLabel());
6408 __ Bind(slow_path->GetExitLabel());
6409}
6410
Roland Levillain44015862016-01-22 11:47:17 +00006411void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6412 Location out,
6413 Location ref,
6414 Location obj,
6415 uint32_t offset,
6416 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006417 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006418 // Baker's read barriers shall be handled by the fast path
6419 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6420 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006421 // If heap poisoning is enabled, unpoisoning will be taken care of
6422 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006423 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006424 } else if (kPoisonHeapReferences) {
6425 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6426 }
6427}
6428
Roland Levillain44015862016-01-22 11:47:17 +00006429void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6430 Location out,
6431 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006432 DCHECK(kEmitCompilerReadBarrier);
6433
Roland Levillain44015862016-01-22 11:47:17 +00006434 // Insert a slow path based read barrier *after* the GC root load.
6435 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006436 // Note that GC roots are not affected by heap poisoning, so we do
6437 // not need to do anything special for this here.
6438 SlowPathCodeARM64* slow_path =
6439 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6440 AddSlowPath(slow_path);
6441
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006442 __ B(slow_path->GetEntryLabel());
6443 __ Bind(slow_path->GetExitLabel());
6444}
6445
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006446void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6447 LocationSummary* locations =
6448 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6449 locations->SetInAt(0, Location::RequiresRegister());
6450 locations->SetOut(Location::RequiresRegister());
6451}
6452
6453void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6454 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006455 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006456 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006457 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006458 __ Ldr(XRegisterFrom(locations->Out()),
6459 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006460 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006461 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006462 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006463 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6464 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006465 __ Ldr(XRegisterFrom(locations->Out()),
6466 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006467 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006468}
6469
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006470static void PatchJitRootUse(uint8_t* code,
6471 const uint8_t* roots_data,
6472 vixl::aarch64::Literal<uint32_t>* literal,
6473 uint64_t index_in_table) {
6474 uint32_t literal_offset = literal->GetOffset();
6475 uintptr_t address =
6476 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6477 uint8_t* data = code + literal_offset;
6478 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6479}
6480
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006481void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6482 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006483 const StringReference& string_reference = entry.first;
6484 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
6485 const auto it = jit_string_roots_.find(string_reference);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006486 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006487 uint64_t index_in_table = it->second;
6488 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006489 }
6490 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006491 const TypeReference& type_reference = entry.first;
6492 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
6493 const auto it = jit_class_roots_.find(type_reference);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006494 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006495 uint64_t index_in_table = it->second;
6496 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006497 }
6498}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006499
Alexandre Rames67555f72014-11-18 10:55:16 +00006500#undef __
6501#undef QUICK_ENTRY_POINT
6502
Alexandre Rames5319def2014-10-23 10:03:10 +01006503} // namespace arm64
6504} // namespace art