blob: d8e709c7a95a234ff989fd72a2d0baabbc620eb7 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Vladimir Markof4f2daa2017-03-20 18:26:59 +000019#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000020#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080022#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010024#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080025#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010026#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080027#include "intrinsics.h"
28#include "intrinsics_arm64.h"
Vladimir Markof4f2daa2017-03-20 18:26:59 +000029#include "linker/arm64/relative_patcher_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010030#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "mirror/class-inl.h"
Andreas Gamped4901292017-05-30 18:41:34 -070032#include "lock_word.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000033#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010034#include "thread.h"
35#include "utils/arm64/assembler_arm64.h"
36#include "utils/assembler.h"
37#include "utils/stack_checks.h"
38
Scott Wakeling97c72b72016-06-24 16:19:36 +010039using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000040using vixl::ExactAssemblyScope;
41using vixl::CodeBufferCheckScope;
42using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010043
44#ifdef __
45#error "ARM64 Codegen VIXL macro-assembler macro already defined."
46#endif
47
Alexandre Rames5319def2014-10-23 10:03:10 +010048namespace art {
49
Roland Levillain22ccc3a2015-11-24 13:10:05 +000050template<class MirrorType>
51class GcRoot;
52
Alexandre Rames5319def2014-10-23 10:03:10 +010053namespace arm64 {
54
Alexandre Ramesbe919d92016-08-23 18:33:36 +010055using helpers::ARM64EncodableConstantOrRegister;
56using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080057using helpers::CPURegisterFrom;
58using helpers::DRegisterFrom;
59using helpers::FPRegisterFrom;
60using helpers::HeapOperand;
61using helpers::HeapOperandFrom;
62using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010063using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080064using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080065using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010066using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080067using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010068using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::LocationFrom;
70using helpers::OperandFromMemOperand;
71using helpers::OutputCPURegister;
72using helpers::OutputFPRegister;
73using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010074using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080075using helpers::RegisterFrom;
76using helpers::StackOperandFrom;
77using helpers::VIXLRegCodeFromART;
78using helpers::WRegisterFrom;
79using helpers::XRegisterFrom;
80
Alexandre Rames5319def2014-10-23 10:03:10 +010081static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000082// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080083// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
84// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000085static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010086
Vladimir Markof4f2daa2017-03-20 18:26:59 +000087// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
88// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
89// For the Baker read barrier implementation using link-generated thunks we need to split
90// the offset explicitly.
91constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
92
93// Flags controlling the use of link-time generated thunks for Baker read barriers.
Vladimir Markod1ef8732017-04-18 13:55:13 +010094constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
Vladimir Marko66d691d2017-04-07 17:53:39 +010095constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
Vladimir Markod1ef8732017-04-18 13:55:13 +010096constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
Vladimir Markof4f2daa2017-03-20 18:26:59 +000097
98// Some instructions have special requirements for a temporary, for example
99// LoadClass/kBssEntry and LoadString/kBssEntry for Baker read barrier require
100// temp that's not an R0 (to avoid an extra move) and Baker read barrier field
101// loads with large offsets need a fixed register to limit the number of link-time
102// thunks we generate. For these and similar cases, we want to reserve a specific
103// register that's neither callee-save nor an argument register. We choose x15.
104inline Location FixedTempLocation() {
105 return Location::RegisterLocation(x15.GetCode());
106}
107
Alexandre Rames5319def2014-10-23 10:03:10 +0100108inline Condition ARM64Condition(IfCondition cond) {
109 switch (cond) {
110 case kCondEQ: return eq;
111 case kCondNE: return ne;
112 case kCondLT: return lt;
113 case kCondLE: return le;
114 case kCondGT: return gt;
115 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700116 case kCondB: return lo;
117 case kCondBE: return ls;
118 case kCondA: return hi;
119 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100120 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000121 LOG(FATAL) << "Unreachable";
122 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100123}
124
Vladimir Markod6e069b2016-01-18 11:11:01 +0000125inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
126 // The ARM64 condition codes can express all the necessary branches, see the
127 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
128 // There is no dex instruction or HIR that would need the missing conditions
129 // "equal or unordered" or "not equal".
130 switch (cond) {
131 case kCondEQ: return eq;
132 case kCondNE: return ne /* unordered */;
133 case kCondLT: return gt_bias ? cc : lt /* unordered */;
134 case kCondLE: return gt_bias ? ls : le /* unordered */;
135 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
136 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
137 default:
138 LOG(FATAL) << "UNREACHABLE";
139 UNREACHABLE();
140 }
141}
142
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000143Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000144 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
145 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
146 // but we use the exact registers for clarity.
147 if (return_type == Primitive::kPrimFloat) {
148 return LocationFrom(s0);
149 } else if (return_type == Primitive::kPrimDouble) {
150 return LocationFrom(d0);
151 } else if (return_type == Primitive::kPrimLong) {
152 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100153 } else if (return_type == Primitive::kPrimVoid) {
154 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000155 } else {
156 return LocationFrom(w0);
157 }
158}
159
Alexandre Rames5319def2014-10-23 10:03:10 +0100160Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000161 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100162}
163
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100164// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
165#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700166#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100167
Zheng Xuda403092015-04-24 17:35:39 +0800168// Calculate memory accessing operand for save/restore live registers.
169static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100170 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800171 int64_t spill_offset,
172 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100173 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
174 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
175 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800176 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100177 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800178 codegen->GetNumberOfFloatingPointRegisters()));
179
Vladimir Marko804b03f2016-09-14 16:26:36 +0100180 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100181 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
182 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800183
184 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
185 UseScratchRegisterScope temps(masm);
186
187 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100188 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
189 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800190 int64_t reg_size = kXRegSizeInBytes;
191 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
192 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100193 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800194 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
195 // If the offset does not fit in the instruction's immediate field, use an alternate register
196 // to compute the base address(float point registers spill base address).
197 Register new_base = temps.AcquireSameSizeAs(base);
198 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
199 base = new_base;
200 spill_offset = -core_spill_size;
201 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
202 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
203 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
204 }
205
206 if (is_save) {
207 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
208 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
209 } else {
210 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
211 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
212 }
213}
214
215void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800216 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100217 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
218 for (uint32_t i : LowToHighBits(core_spills)) {
219 // If the register holds an object, update the stack mask.
220 if (locations->RegisterContainsObject(i)) {
221 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800222 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100223 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
224 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
225 saved_core_stack_offsets_[i] = stack_offset;
226 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800227 }
228
Vladimir Marko804b03f2016-09-14 16:26:36 +0100229 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
230 for (uint32_t i : LowToHighBits(fp_spills)) {
231 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
232 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
233 saved_fpu_stack_offsets_[i] = stack_offset;
234 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800235 }
236
Vladimir Marko804b03f2016-09-14 16:26:36 +0100237 SaveRestoreLiveRegistersHelper(codegen,
238 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800239 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
240}
241
242void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100243 SaveRestoreLiveRegistersHelper(codegen,
244 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800245 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
246}
247
Alexandre Rames5319def2014-10-23 10:03:10 +0100248class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
249 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000250 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100251
Alexandre Rames67555f72014-11-18 10:55:16 +0000252 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100253 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000254 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100255
Alexandre Rames5319def2014-10-23 10:03:10 +0100256 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000257 if (instruction_->CanThrowIntoCatchBlock()) {
258 // Live registers will be restored in the catch block if caught.
259 SaveLiveRegisters(codegen, instruction_->GetLocations());
260 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000261 // We're moving two locations to locations that could overlap, so we need a parallel
262 // move resolver.
263 InvokeRuntimeCallingConvention calling_convention;
264 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100265 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
266 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000267 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
268 ? kQuickThrowStringBounds
269 : kQuickThrowArrayBounds;
270 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100271 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800272 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100273 }
274
Alexandre Rames8158f282015-08-07 10:26:17 +0100275 bool IsFatal() const OVERRIDE { return true; }
276
Alexandre Rames9931f312015-06-19 14:47:01 +0100277 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
278
Alexandre Rames5319def2014-10-23 10:03:10 +0100279 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100280 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
281};
282
Alexandre Rames67555f72014-11-18 10:55:16 +0000283class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
284 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000285 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000286
287 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
288 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
289 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000290 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800291 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000292 }
293
Alexandre Rames8158f282015-08-07 10:26:17 +0100294 bool IsFatal() const OVERRIDE { return true; }
295
Alexandre Rames9931f312015-06-19 14:47:01 +0100296 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
297
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000299 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
300};
301
302class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
303 public:
304 LoadClassSlowPathARM64(HLoadClass* cls,
305 HInstruction* at,
306 uint32_t dex_pc,
Vladimir Markoea4c1262017-02-06 19:59:33 +0000307 bool do_clinit,
308 vixl::aarch64::Register bss_entry_temp = vixl::aarch64::Register(),
309 vixl::aarch64::Label* bss_entry_adrp_label = nullptr)
310 : SlowPathCodeARM64(at),
311 cls_(cls),
312 dex_pc_(dex_pc),
313 do_clinit_(do_clinit),
314 bss_entry_temp_(bss_entry_temp),
315 bss_entry_adrp_label_(bss_entry_adrp_label) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000316 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
317 }
318
319 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000320 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000321 Location out = locations->Out();
322 constexpr bool call_saves_everything_except_r0_ip0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexandre Rames67555f72014-11-18 10:55:16 +0000323 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
324
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000325 InvokeRuntimeCallingConvention calling_convention;
326 // For HLoadClass/kBssEntry/kSaveEverything, the page address of the entry is in a temp
327 // register, make sure it's not clobbered by the call or by saving/restoring registers.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000328 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
329 bool is_load_class_bss_entry =
330 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000331 if (is_load_class_bss_entry) {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000332 DCHECK(bss_entry_temp_.IsValid());
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000333 DCHECK(!bss_entry_temp_.Is(calling_convention.GetRegisterAt(0)));
334 DCHECK(
335 !UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(bss_entry_temp_));
Vladimir Markoea4c1262017-02-06 19:59:33 +0000336 }
337
Alexandre Rames67555f72014-11-18 10:55:16 +0000338 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000339 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000340
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000341 dex::TypeIndex type_index = cls_->GetTypeIndex();
342 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000343 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
344 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000345 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800346 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100347 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800348 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100349 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800350 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000351
352 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000353 if (out.IsValid()) {
354 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000355 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000356 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000357 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000358 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000359 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000360 if (is_load_class_bss_entry) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000361 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000362 const DexFile& dex_file = cls_->GetDexFile();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000363 if (call_saves_everything_except_r0_ip0) {
364 // The class entry page address was preserved in bss_entry_temp_ thanks to kSaveEverything.
365 } else {
366 // For non-Baker read barrier, we need to re-calculate the address of the class entry page.
367 bss_entry_adrp_label_ = arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
368 arm64_codegen->EmitAdrpPlaceholder(bss_entry_adrp_label_, bss_entry_temp_);
369 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000370 vixl::aarch64::Label* strp_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +0000371 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label_);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000372 {
373 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
374 __ Bind(strp_label);
375 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Markoea4c1262017-02-06 19:59:33 +0000376 MemOperand(bss_entry_temp_, /* offset placeholder */ 0));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000377 }
378 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000379 __ B(GetExitLabel());
380 }
381
Alexandre Rames9931f312015-06-19 14:47:01 +0100382 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
383
Alexandre Rames67555f72014-11-18 10:55:16 +0000384 private:
385 // The class this slow path will load.
386 HLoadClass* const cls_;
387
Alexandre Rames67555f72014-11-18 10:55:16 +0000388 // The dex PC of `at_`.
389 const uint32_t dex_pc_;
390
391 // Whether to initialize the class.
392 const bool do_clinit_;
393
Vladimir Markoea4c1262017-02-06 19:59:33 +0000394 // For HLoadClass/kBssEntry, the temp register and the label of the ADRP where it was loaded.
395 vixl::aarch64::Register bss_entry_temp_;
396 vixl::aarch64::Label* bss_entry_adrp_label_;
397
Alexandre Rames67555f72014-11-18 10:55:16 +0000398 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
399};
400
Vladimir Markoaad75c62016-10-03 08:46:48 +0000401class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
402 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100403 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
404 : SlowPathCodeARM64(instruction),
405 temp_(temp),
406 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000407
408 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
409 LocationSummary* locations = instruction_->GetLocations();
410 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
411 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
412
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000413 InvokeRuntimeCallingConvention calling_convention;
414 // Make sure `temp_` is not clobbered by the call or by saving/restoring registers.
415 DCHECK(temp_.IsValid());
416 DCHECK(!temp_.Is(calling_convention.GetRegisterAt(0)));
417 DCHECK(!UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(temp_));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100418
Vladimir Markoaad75c62016-10-03 08:46:48 +0000419 __ Bind(GetEntryLabel());
420 SaveLiveRegisters(codegen, locations);
421
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000422 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
423 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000424 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
425 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
426 Primitive::Type type = instruction_->GetType();
427 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
428
429 RestoreLiveRegisters(codegen, locations);
430
431 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000432 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100433 if (!kUseReadBarrier || kUseBakerReadBarrier) {
434 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
435 } else {
436 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
437 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
438 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
439 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000440 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100441 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000442 {
443 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
444 __ Bind(strp_label);
445 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100446 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000447 }
448
449 __ B(GetExitLabel());
450 }
451
452 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
453
454 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100455 const Register temp_;
456 vixl::aarch64::Label* adrp_label_;
457
Vladimir Markoaad75c62016-10-03 08:46:48 +0000458 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
459};
460
Alexandre Rames5319def2014-10-23 10:03:10 +0100461class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
462 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000463 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100464
Alexandre Rames67555f72014-11-18 10:55:16 +0000465 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
466 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100467 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000468 if (instruction_->CanThrowIntoCatchBlock()) {
469 // Live registers will be restored in the catch block if caught.
470 SaveLiveRegisters(codegen, instruction_->GetLocations());
471 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000472 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
473 instruction_,
474 instruction_->GetDexPc(),
475 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800476 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100477 }
478
Alexandre Rames8158f282015-08-07 10:26:17 +0100479 bool IsFatal() const OVERRIDE { return true; }
480
Alexandre Rames9931f312015-06-19 14:47:01 +0100481 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
482
Alexandre Rames5319def2014-10-23 10:03:10 +0100483 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100484 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
485};
486
487class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
488 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100489 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000490 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100491
Alexandre Rames67555f72014-11-18 10:55:16 +0000492 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100493 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000494 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100495 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100496 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000497 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800498 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100499 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000500 if (successor_ == nullptr) {
501 __ B(GetReturnLabel());
502 } else {
503 __ B(arm64_codegen->GetLabelOf(successor_));
504 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100505 }
506
Scott Wakeling97c72b72016-06-24 16:19:36 +0100507 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100508 DCHECK(successor_ == nullptr);
509 return &return_label_;
510 }
511
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100512 HBasicBlock* GetSuccessor() const {
513 return successor_;
514 }
515
Alexandre Rames9931f312015-06-19 14:47:01 +0100516 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
517
Alexandre Rames5319def2014-10-23 10:03:10 +0100518 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100519 // If not null, the block to branch to after the suspend check.
520 HBasicBlock* const successor_;
521
522 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100523 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100524
525 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
526};
527
Alexandre Rames67555f72014-11-18 10:55:16 +0000528class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
529 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000530 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000531 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000532
533 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000534 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800535
Alexandre Rames3e69f162014-12-10 10:36:50 +0000536 DCHECK(instruction_->IsCheckCast()
537 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
538 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100539 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000540
Alexandre Rames67555f72014-11-18 10:55:16 +0000541 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000542
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000543 if (!is_fatal_) {
544 SaveLiveRegisters(codegen, locations);
545 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000546
547 // We're moving two locations to locations that could overlap, so we need a parallel
548 // move resolver.
549 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800550 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800551 LocationFrom(calling_convention.GetRegisterAt(0)),
552 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800553 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800554 LocationFrom(calling_convention.GetRegisterAt(1)),
555 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000556 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000557 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800558 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000559 Primitive::Type ret_type = instruction_->GetType();
560 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
561 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
562 } else {
563 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800564 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
565 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000566 }
567
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000568 if (!is_fatal_) {
569 RestoreLiveRegisters(codegen, locations);
570 __ B(GetExitLabel());
571 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000572 }
573
Alexandre Rames9931f312015-06-19 14:47:01 +0100574 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100575 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100576
Alexandre Rames67555f72014-11-18 10:55:16 +0000577 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000578 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000579
Alexandre Rames67555f72014-11-18 10:55:16 +0000580 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
581};
582
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700583class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
584 public:
Aart Bik42249c32016-01-07 15:33:50 -0800585 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000586 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700587
588 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800589 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700590 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100591 LocationSummary* locations = instruction_->GetLocations();
592 SaveLiveRegisters(codegen, locations);
593 InvokeRuntimeCallingConvention calling_convention;
594 __ Mov(calling_convention.GetRegisterAt(0),
595 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000596 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100597 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700598 }
599
Alexandre Rames9931f312015-06-19 14:47:01 +0100600 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
601
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700602 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700603 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
604};
605
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100606class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
607 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000608 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100609
610 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
611 LocationSummary* locations = instruction_->GetLocations();
612 __ Bind(GetEntryLabel());
613 SaveLiveRegisters(codegen, locations);
614
615 InvokeRuntimeCallingConvention calling_convention;
616 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
617 parallel_move.AddMove(
618 locations->InAt(0),
619 LocationFrom(calling_convention.GetRegisterAt(0)),
620 Primitive::kPrimNot,
621 nullptr);
622 parallel_move.AddMove(
623 locations->InAt(1),
624 LocationFrom(calling_convention.GetRegisterAt(1)),
625 Primitive::kPrimInt,
626 nullptr);
627 parallel_move.AddMove(
628 locations->InAt(2),
629 LocationFrom(calling_convention.GetRegisterAt(2)),
630 Primitive::kPrimNot,
631 nullptr);
632 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
633
634 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000635 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100636 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
637 RestoreLiveRegisters(codegen, locations);
638 __ B(GetExitLabel());
639 }
640
641 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
642
643 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100644 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
645};
646
Zheng Xu3927c8b2015-11-18 17:46:25 +0800647void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
648 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000649 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800650
651 // We are about to use the assembler to place literals directly. Make sure we have enough
652 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000653 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
654 num_entries * sizeof(int32_t),
655 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800656
657 __ Bind(&table_start_);
658 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
659 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100660 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800661 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100662 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800663 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
664 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
665 Literal<int32_t> literal(jump_offset);
666 __ place(&literal);
667 }
668}
669
Roland Levillain54f869e2017-03-06 13:54:11 +0000670// Abstract base class for read barrier slow paths marking a reference
671// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000672//
Roland Levillain54f869e2017-03-06 13:54:11 +0000673// Argument `entrypoint` must be a register location holding the read
674// barrier marking runtime entry point to be invoked.
675class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
676 protected:
677 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
678 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000679 DCHECK(kEmitCompilerReadBarrier);
680 }
681
Roland Levillain54f869e2017-03-06 13:54:11 +0000682 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000683
Roland Levillain54f869e2017-03-06 13:54:11 +0000684 // Generate assembly code calling the read barrier marking runtime
685 // entry point (ReadBarrierMarkRegX).
686 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000687 // No need to save live registers; it's taken care of by the
688 // entrypoint. Also, there is no need to update the stack mask,
689 // as this runtime call will not trigger a garbage collection.
690 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
691 DCHECK_NE(ref_.reg(), LR);
692 DCHECK_NE(ref_.reg(), WSP);
693 DCHECK_NE(ref_.reg(), WZR);
694 // IP0 is used internally by the ReadBarrierMarkRegX entry point
695 // as a temporary, it cannot be the entry point's input/output.
696 DCHECK_NE(ref_.reg(), IP0);
697 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
698 // "Compact" slow path, saving two moves.
699 //
700 // Instead of using the standard runtime calling convention (input
701 // and output in W0):
702 //
703 // W0 <- ref
704 // W0 <- ReadBarrierMark(W0)
705 // ref <- W0
706 //
707 // we just use rX (the register containing `ref`) as input and output
708 // of a dedicated entrypoint:
709 //
710 // rX <- ReadBarrierMarkRegX(rX)
711 //
712 if (entrypoint_.IsValid()) {
713 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
714 __ Blr(XRegisterFrom(entrypoint_));
715 } else {
716 // Entrypoint is not already loaded, load from the thread.
717 int32_t entry_point_offset =
718 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
719 // This runtime call does not require a stack map.
720 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
721 }
722 }
723
724 // The location (register) of the marked object reference.
725 const Location ref_;
726
727 // The location of the entrypoint if it is already loaded.
728 const Location entrypoint_;
729
Roland Levillain54f869e2017-03-06 13:54:11 +0000730 private:
731 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
732};
733
Alexandre Rames5319def2014-10-23 10:03:10 +0100734// Slow path marking an object reference `ref` during a read
735// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000736// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100737//
738// This means that after the execution of this slow path, `ref` will
739// always be up-to-date, but `obj.field` may not; i.e., after the
740// flip, `ref` will be a to-space reference, but `obj.field` will
741// probably still be a from-space reference (unless it gets updated by
742// another thread, or if another thread installed another object
743// reference (different from `ref`) in `obj.field`).
744//
745// If `entrypoint` is a valid location it is assumed to already be
746// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000747// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000748class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100749 public:
750 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
751 Location ref,
752 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000753 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100754 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100755 }
756
757 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
758
759 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000760 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100761 DCHECK(locations->CanCall());
762 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000763 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000764 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
765 << "Unexpected instruction in read barrier marking slow path: "
766 << instruction_->DebugName();
767
768 __ Bind(GetEntryLabel());
769 GenerateReadBarrierMarkRuntimeCall(codegen);
770 __ B(GetExitLabel());
771 }
772
773 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000774 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
775};
776
Roland Levillain54f869e2017-03-06 13:54:11 +0000777// Slow path loading `obj`'s lock word, loading a reference from
778// object `*(obj + offset + (index << scale_factor))` into `ref`, and
779// marking `ref` if `obj` is gray according to the lock word (Baker
780// read barrier). The field `obj.field` in the object `obj` holding
781// this reference does not get updated by this slow path after marking
782// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
783// below for that).
784//
785// This means that after the execution of this slow path, `ref` will
786// always be up-to-date, but `obj.field` may not; i.e., after the
787// flip, `ref` will be a to-space reference, but `obj.field` will
788// probably still be a from-space reference (unless it gets updated by
789// another thread, or if another thread installed another object
790// reference (different from `ref`) in `obj.field`).
791//
792// Argument `entrypoint` must be a register location holding the read
793// barrier marking runtime entry point to be invoked.
794class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
795 public:
796 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
797 Location ref,
798 Register obj,
799 uint32_t offset,
800 Location index,
801 size_t scale_factor,
802 bool needs_null_check,
803 bool use_load_acquire,
804 Register temp,
805 Location entrypoint)
806 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
807 obj_(obj),
808 offset_(offset),
809 index_(index),
810 scale_factor_(scale_factor),
811 needs_null_check_(needs_null_check),
812 use_load_acquire_(use_load_acquire),
813 temp_(temp) {
814 DCHECK(kEmitCompilerReadBarrier);
815 DCHECK(kUseBakerReadBarrier);
816 }
817
818 const char* GetDescription() const OVERRIDE {
819 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
820 }
821
822 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
823 LocationSummary* locations = instruction_->GetLocations();
824 DCHECK(locations->CanCall());
825 DCHECK(ref_.IsRegister()) << ref_;
826 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
827 DCHECK(obj_.IsW());
828 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100829 DCHECK(instruction_->IsInstanceFieldGet() ||
830 instruction_->IsStaticFieldGet() ||
831 instruction_->IsArrayGet() ||
832 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100833 instruction_->IsInstanceOf() ||
834 instruction_->IsCheckCast() ||
835 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
836 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
837 << "Unexpected instruction in read barrier marking slow path: "
838 << instruction_->DebugName();
839 // The read barrier instrumentation of object ArrayGet
840 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000841 // instruction.
842 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000843 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
844
Roland Levillain54f869e2017-03-06 13:54:11 +0000845 // Temporary register `temp_`, used to store the lock word, must
846 // not be IP0 nor IP1, as we may use them to emit the reference
847 // load (in the call to GenerateRawReferenceLoad below), and we
848 // need the lock word to still be in `temp_` after the reference
849 // load.
850 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
851 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
852
Alexandre Rames5319def2014-10-23 10:03:10 +0100853 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000854
855 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
856 // inserted after the original load. However, in fast path based
857 // Baker's read barriers, we need to perform the load of
858 // mirror::Object::monitor_ *before* the original reference load.
859 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000860 // The slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100861 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000862 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
863 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
864 // HeapReference<mirror::Object> ref = *src; // Original reference load.
865 // bool is_gray = (rb_state == ReadBarrier::GrayState());
866 // if (is_gray) {
867 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
868 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000869 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000870 // Note: the original implementation in ReadBarrier::Barrier is
871 // slightly more complex as it performs additional checks that we do
872 // not do here for performance reasons.
873
874 // /* int32_t */ monitor = obj->monitor_
875 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
876 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
877 if (needs_null_check_) {
878 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100879 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000880 // /* LockWord */ lock_word = LockWord(monitor)
881 static_assert(sizeof(LockWord) == sizeof(int32_t),
882 "art::LockWord and int32_t have different sizes.");
883
884 // Introduce a dependency on the lock_word including rb_state,
885 // to prevent load-load reordering, and without using
886 // a memory barrier (which would be more expensive).
887 // `obj` is unchanged by this operation, but its value now depends
888 // on `temp`.
889 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
890
891 // The actual reference load.
892 // A possible implicit null check has already been handled above.
893 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
894 arm64_codegen->GenerateRawReferenceLoad(instruction_,
895 ref_,
896 obj_,
897 offset_,
898 index_,
899 scale_factor_,
900 /* needs_null_check */ false,
901 use_load_acquire_);
902
903 // Mark the object `ref` when `obj` is gray.
904 //
905 // if (rb_state == ReadBarrier::GrayState())
906 // ref = ReadBarrier::Mark(ref);
907 //
908 // Given the numeric representation, it's enough to check the low bit of the rb_state.
909 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
910 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
911 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
912 GenerateReadBarrierMarkRuntimeCall(codegen);
913
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000914 __ B(GetExitLabel());
915 }
916
917 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000918 // The register containing the object holding the marked object reference field.
919 Register obj_;
920 // The offset, index and scale factor to access the reference in `obj_`.
921 uint32_t offset_;
922 Location index_;
923 size_t scale_factor_;
924 // Is a null check required?
925 bool needs_null_check_;
926 // Should this reference load use Load-Acquire semantics?
927 bool use_load_acquire_;
928 // A temporary register used to hold the lock word of `obj_`.
929 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000930
Roland Levillain54f869e2017-03-06 13:54:11 +0000931 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000932};
933
Roland Levillain54f869e2017-03-06 13:54:11 +0000934// Slow path loading `obj`'s lock word, loading a reference from
935// object `*(obj + offset + (index << scale_factor))` into `ref`, and
936// marking `ref` if `obj` is gray according to the lock word (Baker
937// read barrier). If needed, this slow path also atomically updates
938// the field `obj.field` in the object `obj` holding this reference
939// after marking (contrary to
940// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
941// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100942//
943// This means that after the execution of this slow path, both `ref`
944// and `obj.field` will be up-to-date; i.e., after the flip, both will
945// hold the same to-space reference (unless another thread installed
946// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000947//
Roland Levillain54f869e2017-03-06 13:54:11 +0000948// Argument `entrypoint` must be a register location holding the read
949// barrier marking runtime entry point to be invoked.
950class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
951 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100952 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000953 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(HInstruction* instruction,
954 Location ref,
955 Register obj,
956 uint32_t offset,
957 Location index,
958 size_t scale_factor,
959 bool needs_null_check,
960 bool use_load_acquire,
961 Register temp,
962 Location entrypoint)
963 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100964 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000965 offset_(offset),
966 index_(index),
967 scale_factor_(scale_factor),
968 needs_null_check_(needs_null_check),
969 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000970 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100971 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000972 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100973 }
974
975 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000976 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100977 }
978
979 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
980 LocationSummary* locations = instruction_->GetLocations();
981 Register ref_reg = WRegisterFrom(ref_);
982 DCHECK(locations->CanCall());
983 DCHECK(ref_.IsRegister()) << ref_;
984 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000985 DCHECK(obj_.IsW());
986 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
987
988 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100989 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
990 << "Unexpected instruction in read barrier marking and field updating slow path: "
991 << instruction_->DebugName();
992 DCHECK(instruction_->GetLocations()->Intrinsified());
993 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000994 DCHECK_EQ(offset_, 0u);
995 DCHECK_EQ(scale_factor_, 0u);
996 DCHECK_EQ(use_load_acquire_, false);
997 // The location of the offset of the marked reference field within `obj_`.
998 Location field_offset = index_;
999 DCHECK(field_offset.IsRegister()) << field_offset;
1000
1001 // Temporary register `temp_`, used to store the lock word, must
1002 // not be IP0 nor IP1, as we may use them to emit the reference
1003 // load (in the call to GenerateRawReferenceLoad below), and we
1004 // need the lock word to still be in `temp_` after the reference
1005 // load.
1006 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1007 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001008
1009 __ Bind(GetEntryLabel());
1010
Roland Levillainff487002017-03-07 16:50:01 +00001011 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM64's:
1012 //
1013 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1014 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1015 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1016 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1017 // if (is_gray) {
1018 // old_ref = ref;
1019 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1020 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1021 // }
1022
Roland Levillain54f869e2017-03-06 13:54:11 +00001023 // /* int32_t */ monitor = obj->monitor_
1024 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1025 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
1026 if (needs_null_check_) {
1027 codegen->MaybeRecordImplicitNullCheck(instruction_);
1028 }
1029 // /* LockWord */ lock_word = LockWord(monitor)
1030 static_assert(sizeof(LockWord) == sizeof(int32_t),
1031 "art::LockWord and int32_t have different sizes.");
1032
1033 // Introduce a dependency on the lock_word including rb_state,
1034 // to prevent load-load reordering, and without using
1035 // a memory barrier (which would be more expensive).
1036 // `obj` is unchanged by this operation, but its value now depends
1037 // on `temp`.
1038 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
1039
1040 // The actual reference load.
1041 // A possible implicit null check has already been handled above.
1042 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1043 arm64_codegen->GenerateRawReferenceLoad(instruction_,
1044 ref_,
1045 obj_,
1046 offset_,
1047 index_,
1048 scale_factor_,
1049 /* needs_null_check */ false,
1050 use_load_acquire_);
1051
1052 // Mark the object `ref` when `obj` is gray.
1053 //
1054 // if (rb_state == ReadBarrier::GrayState())
1055 // ref = ReadBarrier::Mark(ref);
1056 //
1057 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1058 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1059 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1060 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1061
1062 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001063 // Note that we cannot use IP to save the old reference, as IP is
1064 // used internally by the ReadBarrierMarkRegX entry point, and we
1065 // need the old reference after the call to that entry point.
1066 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1067 __ Mov(temp_.W(), ref_reg);
1068
Roland Levillain54f869e2017-03-06 13:54:11 +00001069 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001070
1071 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001072 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001073 //
1074 // Note that this field could also hold a different object, if
1075 // another thread had concurrently changed it. In that case, the
1076 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1077 // (CAS) operation below would abort the CAS, leaving the field
1078 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001079 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001080 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001081
1082 // Update the the holder's field atomically. This may fail if
1083 // mutator updates before us, but it's OK. This is achieved
1084 // using a strong compare-and-set (CAS) operation with relaxed
1085 // memory synchronization ordering, where the expected value is
1086 // the old reference and the desired value is the new reference.
1087
1088 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1089 UseScratchRegisterScope temps(masm);
1090
1091 // Convenience aliases.
1092 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001093 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001094 Register expected = temp_.W();
1095 Register value = ref_reg;
1096 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1097 Register tmp_value = temps.AcquireW(); // Value in memory.
1098
1099 __ Add(tmp_ptr, base.X(), Operand(offset));
1100
1101 if (kPoisonHeapReferences) {
1102 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1103 if (value.Is(expected)) {
1104 // Do not poison `value`, as it is the same register as
1105 // `expected`, which has just been poisoned.
1106 } else {
1107 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1108 }
1109 }
1110
1111 // do {
1112 // tmp_value = [tmp_ptr] - expected;
1113 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1114
Roland Levillain24a4d112016-10-26 13:10:46 +01001115 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001116 __ Bind(&loop_head);
1117 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1118 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001119 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001120 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1121 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001122 __ B(&exit_loop);
1123 __ Bind(&comparison_failed);
1124 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001125 __ Bind(&exit_loop);
1126
1127 if (kPoisonHeapReferences) {
1128 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1129 if (value.Is(expected)) {
1130 // Do not unpoison `value`, as it is the same register as
1131 // `expected`, which has just been unpoisoned.
1132 } else {
1133 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1134 }
1135 }
1136
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001137 __ B(GetExitLabel());
1138 }
1139
1140 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001141 // The register containing the object holding the marked object reference field.
1142 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001143 // The offset, index and scale factor to access the reference in `obj_`.
1144 uint32_t offset_;
1145 Location index_;
1146 size_t scale_factor_;
1147 // Is a null check required?
1148 bool needs_null_check_;
1149 // Should this reference load use Load-Acquire semantics?
1150 bool use_load_acquire_;
1151 // A temporary register used to hold the lock word of `obj_`; and
1152 // also to hold the original reference value, when the reference is
1153 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001154 const Register temp_;
1155
Roland Levillain54f869e2017-03-06 13:54:11 +00001156 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001157};
1158
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001159// Slow path generating a read barrier for a heap reference.
1160class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1161 public:
1162 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1163 Location out,
1164 Location ref,
1165 Location obj,
1166 uint32_t offset,
1167 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001168 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001169 out_(out),
1170 ref_(ref),
1171 obj_(obj),
1172 offset_(offset),
1173 index_(index) {
1174 DCHECK(kEmitCompilerReadBarrier);
1175 // If `obj` is equal to `out` or `ref`, it means the initial object
1176 // has been overwritten by (or after) the heap object reference load
1177 // to be instrumented, e.g.:
1178 //
1179 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001180 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001181 //
1182 // In that case, we have lost the information about the original
1183 // object, and the emitted read barrier cannot work properly.
1184 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1185 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1186 }
1187
1188 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1189 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1190 LocationSummary* locations = instruction_->GetLocations();
1191 Primitive::Type type = Primitive::kPrimNot;
1192 DCHECK(locations->CanCall());
1193 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001194 DCHECK(instruction_->IsInstanceFieldGet() ||
1195 instruction_->IsStaticFieldGet() ||
1196 instruction_->IsArrayGet() ||
1197 instruction_->IsInstanceOf() ||
1198 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001199 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001200 << "Unexpected instruction in read barrier for heap reference slow path: "
1201 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001202 // The read barrier instrumentation of object ArrayGet
1203 // instructions does not support the HIntermediateAddress
1204 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001205 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001206 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001207
1208 __ Bind(GetEntryLabel());
1209
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001210 SaveLiveRegisters(codegen, locations);
1211
1212 // We may have to change the index's value, but as `index_` is a
1213 // constant member (like other "inputs" of this slow path),
1214 // introduce a copy of it, `index`.
1215 Location index = index_;
1216 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001217 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001218 if (instruction_->IsArrayGet()) {
1219 // Compute the actual memory offset and store it in `index`.
1220 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
1221 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1222 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1223 // We are about to change the value of `index_reg` (see the
1224 // calls to vixl::MacroAssembler::Lsl and
1225 // vixl::MacroAssembler::Mov below), but it has
1226 // not been saved by the previous call to
1227 // art::SlowPathCode::SaveLiveRegisters, as it is a
1228 // callee-save register --
1229 // art::SlowPathCode::SaveLiveRegisters does not consider
1230 // callee-save registers, as it has been designed with the
1231 // assumption that callee-save registers are supposed to be
1232 // handled by the called function. So, as a callee-save
1233 // register, `index_reg` _would_ eventually be saved onto
1234 // the stack, but it would be too late: we would have
1235 // changed its value earlier. Therefore, we manually save
1236 // it here into another freely available register,
1237 // `free_reg`, chosen of course among the caller-save
1238 // registers (as a callee-save `free_reg` register would
1239 // exhibit the same problem).
1240 //
1241 // Note we could have requested a temporary register from
1242 // the register allocator instead; but we prefer not to, as
1243 // this is a slow path, and we know we can find a
1244 // caller-save register that is available.
1245 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1246 __ Mov(free_reg.W(), index_reg);
1247 index_reg = free_reg;
1248 index = LocationFrom(index_reg);
1249 } else {
1250 // The initial register stored in `index_` has already been
1251 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1252 // (as it is not a callee-save register), so we can freely
1253 // use it.
1254 }
1255 // Shifting the index value contained in `index_reg` by the scale
1256 // factor (2) cannot overflow in practice, as the runtime is
1257 // unable to allocate object arrays with a size larger than
1258 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1259 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1260 static_assert(
1261 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1262 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1263 __ Add(index_reg, index_reg, Operand(offset_));
1264 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001265 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1266 // intrinsics, `index_` is not shifted by a scale factor of 2
1267 // (as in the case of ArrayGet), as it is actually an offset
1268 // to an object field within an object.
1269 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001270 DCHECK(instruction_->GetLocations()->Intrinsified());
1271 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1272 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1273 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001274 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001275 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001276 }
1277 }
1278
1279 // We're moving two or three locations to locations that could
1280 // overlap, so we need a parallel move resolver.
1281 InvokeRuntimeCallingConvention calling_convention;
1282 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1283 parallel_move.AddMove(ref_,
1284 LocationFrom(calling_convention.GetRegisterAt(0)),
1285 type,
1286 nullptr);
1287 parallel_move.AddMove(obj_,
1288 LocationFrom(calling_convention.GetRegisterAt(1)),
1289 type,
1290 nullptr);
1291 if (index.IsValid()) {
1292 parallel_move.AddMove(index,
1293 LocationFrom(calling_convention.GetRegisterAt(2)),
1294 Primitive::kPrimInt,
1295 nullptr);
1296 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1297 } else {
1298 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1299 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1300 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001301 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001302 instruction_,
1303 instruction_->GetDexPc(),
1304 this);
1305 CheckEntrypointTypes<
1306 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1307 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1308
1309 RestoreLiveRegisters(codegen, locations);
1310
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001311 __ B(GetExitLabel());
1312 }
1313
1314 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1315
1316 private:
1317 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001318 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1319 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001320 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1321 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1322 return Register(VIXLRegCodeFromART(i), kXRegSize);
1323 }
1324 }
1325 // We shall never fail to find a free caller-save register, as
1326 // there are more than two core caller-save registers on ARM64
1327 // (meaning it is possible to find one which is different from
1328 // `ref` and `obj`).
1329 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1330 LOG(FATAL) << "Could not find a free register";
1331 UNREACHABLE();
1332 }
1333
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001334 const Location out_;
1335 const Location ref_;
1336 const Location obj_;
1337 const uint32_t offset_;
1338 // An additional location containing an index to an array.
1339 // Only used for HArrayGet and the UnsafeGetObject &
1340 // UnsafeGetObjectVolatile intrinsics.
1341 const Location index_;
1342
1343 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1344};
1345
1346// Slow path generating a read barrier for a GC root.
1347class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1348 public:
1349 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001350 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001351 DCHECK(kEmitCompilerReadBarrier);
1352 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001353
1354 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1355 LocationSummary* locations = instruction_->GetLocations();
1356 Primitive::Type type = Primitive::kPrimNot;
1357 DCHECK(locations->CanCall());
1358 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001359 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1360 << "Unexpected instruction in read barrier for GC root slow path: "
1361 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001362
1363 __ Bind(GetEntryLabel());
1364 SaveLiveRegisters(codegen, locations);
1365
1366 InvokeRuntimeCallingConvention calling_convention;
1367 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1368 // The argument of the ReadBarrierForRootSlow is not a managed
1369 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1370 // thus we need a 64-bit move here, and we cannot use
1371 //
1372 // arm64_codegen->MoveLocation(
1373 // LocationFrom(calling_convention.GetRegisterAt(0)),
1374 // root_,
1375 // type);
1376 //
1377 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1378 // reference type (`Primitive::kPrimNot`).
1379 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001380 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001381 instruction_,
1382 instruction_->GetDexPc(),
1383 this);
1384 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1385 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1386
1387 RestoreLiveRegisters(codegen, locations);
1388 __ B(GetExitLabel());
1389 }
1390
1391 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1392
1393 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001394 const Location out_;
1395 const Location root_;
1396
1397 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1398};
1399
Alexandre Rames5319def2014-10-23 10:03:10 +01001400#undef __
1401
1402Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
1403 Location next_location;
1404 if (type == Primitive::kPrimVoid) {
1405 LOG(FATAL) << "Unreachable type " << type;
1406 }
1407
1408 if (Primitive::IsFloatingPointType(type) &&
1409 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001410 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1411 } else if (!Primitive::IsFloatingPointType(type) &&
1412 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1413 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1414 } else {
1415 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001416 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1417 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001418 }
1419
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001420 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001421 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001422 return next_location;
1423}
1424
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001425Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001426 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001427}
1428
Serban Constantinescu579885a2015-02-22 20:51:33 +00001429CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1430 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001431 const CompilerOptions& compiler_options,
1432 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001433 : CodeGenerator(graph,
1434 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001435 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001436 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001437 callee_saved_core_registers.GetList(),
1438 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001439 compiler_options,
1440 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001441 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001442 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001443 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001444 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001445 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001446 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001447 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001448 uint32_literals_(std::less<uint32_t>(),
1449 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001450 uint64_literals_(std::less<uint64_t>(),
1451 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001452 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001453 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001454 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001455 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001456 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001457 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001458 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001459 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1460 jit_class_patches_(TypeReferenceValueComparator(),
1461 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001462 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001463 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001464}
Alexandre Rames5319def2014-10-23 10:03:10 +01001465
Alexandre Rames67555f72014-11-18 10:55:16 +00001466#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001467
Zheng Xu3927c8b2015-11-18 17:46:25 +08001468void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001469 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001470 jump_table->EmitTable(this);
1471 }
1472}
1473
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001474void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001475 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001476 // Ensure we emit the literal pool.
1477 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001478
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001479 CodeGenerator::Finalize(allocator);
1480}
1481
Zheng Xuad4450e2015-04-17 18:48:56 +08001482void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1483 // Note: There are 6 kinds of moves:
1484 // 1. constant -> GPR/FPR (non-cycle)
1485 // 2. constant -> stack (non-cycle)
1486 // 3. GPR/FPR -> GPR/FPR
1487 // 4. GPR/FPR -> stack
1488 // 5. stack -> GPR/FPR
1489 // 6. stack -> stack (non-cycle)
1490 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1491 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1492 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1493 // dependency.
1494 vixl_temps_.Open(GetVIXLAssembler());
1495}
1496
1497void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1498 vixl_temps_.Close();
1499}
1500
1501Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001502 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1503 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1504 || kind == Location::kSIMDStackSlot);
1505 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1506 ? Location::kFpuRegister
1507 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001508 Location scratch = GetScratchLocation(kind);
1509 if (!scratch.Equals(Location::NoLocation())) {
1510 return scratch;
1511 }
1512 // Allocate from VIXL temp registers.
1513 if (kind == Location::kRegister) {
1514 scratch = LocationFrom(vixl_temps_.AcquireX());
1515 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001516 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001517 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1518 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1519 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001520 }
1521 AddScratchLocation(scratch);
1522 return scratch;
1523}
1524
1525void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1526 if (loc.IsRegister()) {
1527 vixl_temps_.Release(XRegisterFrom(loc));
1528 } else {
1529 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001530 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001531 }
1532 RemoveScratchLocation(loc);
1533}
1534
Alexandre Rames3e69f162014-12-10 10:36:50 +00001535void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001536 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001537 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001538}
1539
Alexandre Rames5319def2014-10-23 10:03:10 +01001540void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001541 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001542 __ Bind(&frame_entry_label_);
1543
Serban Constantinescu02164b32014-11-13 14:05:07 +00001544 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1545 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001546 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001547 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001548 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001549 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001550 {
1551 // Ensure that between load and RecordPcInfo there are no pools emitted.
1552 ExactAssemblyScope eas(GetVIXLAssembler(),
1553 kInstructionSize,
1554 CodeBufferCheckScope::kExactSize);
1555 __ ldr(wzr, MemOperand(temp, 0));
1556 RecordPcInfo(nullptr, 0);
1557 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001558 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001559
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001560 if (!HasEmptyFrame()) {
1561 int frame_size = GetFrameSize();
1562 // Stack layout:
1563 // sp[frame_size - 8] : lr.
1564 // ... : other preserved core registers.
1565 // ... : other preserved fp registers.
1566 // ... : reserved frame space.
1567 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001568
1569 // Save the current method if we need it. Note that we do not
1570 // do this in HCurrentMethod, as the instruction might have been removed
1571 // in the SSA graph.
1572 if (RequiresCurrentMethod()) {
1573 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001574 } else {
1575 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001576 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001577 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001578 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1579 frame_size - GetCoreSpillSize());
1580 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1581 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001582
1583 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1584 // Initialize should_deoptimize flag to 0.
1585 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1586 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1587 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001588 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001589}
1590
1591void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001592 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001593 if (!HasEmptyFrame()) {
1594 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001595 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1596 frame_size - FrameEntrySpillSize());
1597 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1598 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001599 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001600 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001601 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001602 __ Ret();
1603 GetAssembler()->cfi().RestoreState();
1604 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001605}
1606
Scott Wakeling97c72b72016-06-24 16:19:36 +01001607CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001608 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001609 return CPURegList(CPURegister::kRegister, kXRegSize,
1610 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001611}
1612
Scott Wakeling97c72b72016-06-24 16:19:36 +01001613CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001614 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1615 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001616 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1617 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001618}
1619
Alexandre Rames5319def2014-10-23 10:03:10 +01001620void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1621 __ Bind(GetLabelOf(block));
1622}
1623
Calin Juravle175dc732015-08-25 15:42:32 +01001624void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1625 DCHECK(location.IsRegister());
1626 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1627}
1628
Calin Juravlee460d1d2015-09-29 04:52:17 +01001629void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1630 if (location.IsRegister()) {
1631 locations->AddTemp(location);
1632 } else {
1633 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1634 }
1635}
1636
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001637void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001638 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001639 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001640 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001641 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001642 if (value_can_be_null) {
1643 __ Cbz(value, &done);
1644 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001645 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001646 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001647 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001648 if (value_can_be_null) {
1649 __ Bind(&done);
1650 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001651}
1652
David Brazdil58282f42016-01-14 12:45:10 +00001653void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001654 // Blocked core registers:
1655 // lr : Runtime reserved.
1656 // tr : Runtime reserved.
1657 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1658 // ip1 : VIXL core temp.
1659 // ip0 : VIXL core temp.
1660 //
1661 // Blocked fp registers:
1662 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001663 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1664 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001665 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001666 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001667 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001668
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001669 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001670 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001671 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001672 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001673
David Brazdil58282f42016-01-14 12:45:10 +00001674 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001675 // Stubs do not save callee-save floating point registers. If the graph
1676 // is debuggable, we need to deal with these registers differently. For
1677 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001678 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1679 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001680 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001681 }
1682 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001683}
1684
Alexandre Rames3e69f162014-12-10 10:36:50 +00001685size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1686 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1687 __ Str(reg, MemOperand(sp, stack_index));
1688 return kArm64WordSize;
1689}
1690
1691size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1692 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1693 __ Ldr(reg, MemOperand(sp, stack_index));
1694 return kArm64WordSize;
1695}
1696
1697size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1698 FPRegister reg = FPRegister(reg_id, kDRegSize);
1699 __ Str(reg, MemOperand(sp, stack_index));
1700 return kArm64WordSize;
1701}
1702
1703size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1704 FPRegister reg = FPRegister(reg_id, kDRegSize);
1705 __ Ldr(reg, MemOperand(sp, stack_index));
1706 return kArm64WordSize;
1707}
1708
Alexandre Rames5319def2014-10-23 10:03:10 +01001709void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001710 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001711}
1712
1713void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001714 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001715}
1716
Alexandre Rames67555f72014-11-18 10:55:16 +00001717void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001718 if (constant->IsIntConstant()) {
1719 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1720 } else if (constant->IsLongConstant()) {
1721 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1722 } else if (constant->IsNullConstant()) {
1723 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001724 } else if (constant->IsFloatConstant()) {
1725 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1726 } else {
1727 DCHECK(constant->IsDoubleConstant());
1728 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1729 }
1730}
1731
Alexandre Rames3e69f162014-12-10 10:36:50 +00001732
1733static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1734 DCHECK(constant.IsConstant());
1735 HConstant* cst = constant.GetConstant();
1736 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001737 // Null is mapped to a core W register, which we associate with kPrimInt.
1738 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001739 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1740 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1741 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1742}
1743
Roland Levillain952b2352017-05-03 19:49:14 +01001744// Allocate a scratch register from the VIXL pool, querying first
1745// the floating-point register pool, and then the core register
1746// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001747// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1748// using a different allocation strategy.
1749static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1750 vixl::aarch64::UseScratchRegisterScope* temps,
1751 int size_in_bits) {
1752 return masm->GetScratchFPRegisterList()->IsEmpty()
1753 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1754 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1755}
1756
Calin Juravlee460d1d2015-09-29 04:52:17 +01001757void CodeGeneratorARM64::MoveLocation(Location destination,
1758 Location source,
1759 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001760 if (source.Equals(destination)) {
1761 return;
1762 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001763
1764 // A valid move can always be inferred from the destination and source
1765 // locations. When moving from and to a register, the argument type can be
1766 // used to generate 32bit instead of 64bit moves. In debug mode we also
1767 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001768 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001769
1770 if (destination.IsRegister() || destination.IsFpuRegister()) {
1771 if (unspecified_type) {
1772 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1773 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001774 (src_cst != nullptr && (src_cst->IsIntConstant()
1775 || src_cst->IsFloatConstant()
1776 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001777 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001778 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001779 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001780 // If the source is a double stack slot or a 64bit constant, a 64bit
1781 // type is appropriate. Else the source is a register, and since the
1782 // type has not been specified, we chose a 64bit type to force a 64bit
1783 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001784 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001785 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001786 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001787 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1788 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1789 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001790 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1791 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1792 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001793 } else if (source.IsSIMDStackSlot()) {
1794 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001795 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001796 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001797 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001798 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001799 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001800 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001801 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001802 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001803 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1804 ? Primitive::kPrimLong
1805 : Primitive::kPrimInt;
1806 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1807 }
1808 } else {
1809 DCHECK(source.IsFpuRegister());
1810 if (destination.IsRegister()) {
1811 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1812 ? Primitive::kPrimDouble
1813 : Primitive::kPrimFloat;
1814 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1815 } else {
1816 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001817 if (GetGraph()->HasSIMD()) {
1818 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1819 } else {
1820 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1821 }
1822 }
1823 }
1824 } else if (destination.IsSIMDStackSlot()) {
1825 if (source.IsFpuRegister()) {
1826 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1827 } else {
1828 DCHECK(source.IsSIMDStackSlot());
1829 UseScratchRegisterScope temps(GetVIXLAssembler());
1830 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1831 Register temp = temps.AcquireX();
1832 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1833 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1834 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1835 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1836 } else {
1837 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1838 __ Ldr(temp, StackOperandFrom(source));
1839 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001840 }
1841 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001842 } else { // The destination is not a register. It must be a stack slot.
1843 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1844 if (source.IsRegister() || source.IsFpuRegister()) {
1845 if (unspecified_type) {
1846 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001847 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001848 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001849 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001850 }
1851 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001852 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1853 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1854 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001855 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001856 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1857 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001858 UseScratchRegisterScope temps(GetVIXLAssembler());
1859 HConstant* src_cst = source.GetConstant();
1860 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001861 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001862 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1863 ? Register(xzr)
1864 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001865 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001866 if (src_cst->IsIntConstant()) {
1867 temp = temps.AcquireW();
1868 } else if (src_cst->IsLongConstant()) {
1869 temp = temps.AcquireX();
1870 } else if (src_cst->IsFloatConstant()) {
1871 temp = temps.AcquireS();
1872 } else {
1873 DCHECK(src_cst->IsDoubleConstant());
1874 temp = temps.AcquireD();
1875 }
1876 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001877 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001878 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001879 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001880 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001881 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001882 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001883 // Use any scratch register (a core or a floating-point one)
1884 // from VIXL scratch register pools as a temporary.
1885 //
1886 // We used to only use the FP scratch register pool, but in some
1887 // rare cases the only register from this pool (D31) would
1888 // already be used (e.g. within a ParallelMove instruction, when
1889 // a move is blocked by a another move requiring a scratch FP
1890 // register, which would reserve D31). To prevent this issue, we
1891 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001892 //
1893 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001894 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001895 // use AcquireFPOrCoreCPURegisterOfSize instead of
1896 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1897 // allocates core scratch registers first.
1898 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1899 GetVIXLAssembler(),
1900 &temps,
1901 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001902 __ Ldr(temp, StackOperandFrom(source));
1903 __ Str(temp, StackOperandFrom(destination));
1904 }
1905 }
1906}
1907
1908void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001909 CPURegister dst,
1910 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001911 switch (type) {
1912 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001913 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001914 break;
1915 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001916 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001917 break;
1918 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001919 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001920 break;
1921 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001922 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001923 break;
1924 case Primitive::kPrimInt:
1925 case Primitive::kPrimNot:
1926 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001927 case Primitive::kPrimFloat:
1928 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001929 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001930 __ Ldr(dst, src);
1931 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001932 case Primitive::kPrimVoid:
1933 LOG(FATAL) << "Unreachable type " << type;
1934 }
1935}
1936
Calin Juravle77520bc2015-01-12 18:45:46 +00001937void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001938 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001939 const MemOperand& src,
1940 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001941 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001942 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001943 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001944 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001945
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001946 DCHECK(!src.IsPreIndex());
1947 DCHECK(!src.IsPostIndex());
1948
1949 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001950 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001951 {
1952 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1953 MemOperand base = MemOperand(temp_base);
1954 switch (type) {
1955 case Primitive::kPrimBoolean:
1956 {
1957 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1958 __ ldarb(Register(dst), base);
1959 if (needs_null_check) {
1960 MaybeRecordImplicitNullCheck(instruction);
1961 }
1962 }
1963 break;
1964 case Primitive::kPrimByte:
1965 {
1966 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1967 __ ldarb(Register(dst), base);
1968 if (needs_null_check) {
1969 MaybeRecordImplicitNullCheck(instruction);
1970 }
1971 }
1972 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1973 break;
1974 case Primitive::kPrimChar:
1975 {
1976 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1977 __ ldarh(Register(dst), base);
1978 if (needs_null_check) {
1979 MaybeRecordImplicitNullCheck(instruction);
1980 }
1981 }
1982 break;
1983 case Primitive::kPrimShort:
1984 {
1985 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1986 __ ldarh(Register(dst), base);
1987 if (needs_null_check) {
1988 MaybeRecordImplicitNullCheck(instruction);
1989 }
1990 }
1991 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1992 break;
1993 case Primitive::kPrimInt:
1994 case Primitive::kPrimNot:
1995 case Primitive::kPrimLong:
1996 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
1997 {
1998 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1999 __ ldar(Register(dst), base);
2000 if (needs_null_check) {
2001 MaybeRecordImplicitNullCheck(instruction);
2002 }
2003 }
2004 break;
2005 case Primitive::kPrimFloat:
2006 case Primitive::kPrimDouble: {
2007 DCHECK(dst.IsFPRegister());
2008 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002009
Artem Serov914d7a82017-02-07 14:33:49 +00002010 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2011 {
2012 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2013 __ ldar(temp, base);
2014 if (needs_null_check) {
2015 MaybeRecordImplicitNullCheck(instruction);
2016 }
2017 }
2018 __ Fmov(FPRegister(dst), temp);
2019 break;
Roland Levillain44015862016-01-22 11:47:17 +00002020 }
Artem Serov914d7a82017-02-07 14:33:49 +00002021 case Primitive::kPrimVoid:
2022 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002023 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002024 }
2025}
2026
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002027void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002028 CPURegister src,
2029 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002030 switch (type) {
2031 case Primitive::kPrimBoolean:
2032 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002033 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002034 break;
2035 case Primitive::kPrimChar:
2036 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002037 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002038 break;
2039 case Primitive::kPrimInt:
2040 case Primitive::kPrimNot:
2041 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002042 case Primitive::kPrimFloat:
2043 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00002044 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002045 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002046 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002047 case Primitive::kPrimVoid:
2048 LOG(FATAL) << "Unreachable type " << type;
2049 }
2050}
2051
Artem Serov914d7a82017-02-07 14:33:49 +00002052void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
2053 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002054 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002055 const MemOperand& dst,
2056 bool needs_null_check) {
2057 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002058 UseScratchRegisterScope temps(GetVIXLAssembler());
2059 Register temp_base = temps.AcquireX();
2060
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002061 DCHECK(!dst.IsPreIndex());
2062 DCHECK(!dst.IsPostIndex());
2063
2064 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002065 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002066 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002067 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002068 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002069 switch (type) {
2070 case Primitive::kPrimBoolean:
2071 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00002072 {
2073 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2074 __ stlrb(Register(src), base);
2075 if (needs_null_check) {
2076 MaybeRecordImplicitNullCheck(instruction);
2077 }
2078 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002079 break;
2080 case Primitive::kPrimChar:
2081 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00002082 {
2083 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2084 __ stlrh(Register(src), base);
2085 if (needs_null_check) {
2086 MaybeRecordImplicitNullCheck(instruction);
2087 }
2088 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002089 break;
2090 case Primitive::kPrimInt:
2091 case Primitive::kPrimNot:
2092 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002093 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002094 {
2095 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2096 __ stlr(Register(src), base);
2097 if (needs_null_check) {
2098 MaybeRecordImplicitNullCheck(instruction);
2099 }
2100 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002101 break;
2102 case Primitive::kPrimFloat:
2103 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002104 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002105 Register temp_src;
2106 if (src.IsZero()) {
2107 // The zero register is used to avoid synthesizing zero constants.
2108 temp_src = Register(src);
2109 } else {
2110 DCHECK(src.IsFPRegister());
2111 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2112 __ Fmov(temp_src, FPRegister(src));
2113 }
Artem Serov914d7a82017-02-07 14:33:49 +00002114 {
2115 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2116 __ stlr(temp_src, base);
2117 if (needs_null_check) {
2118 MaybeRecordImplicitNullCheck(instruction);
2119 }
2120 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002121 break;
2122 }
2123 case Primitive::kPrimVoid:
2124 LOG(FATAL) << "Unreachable type " << type;
2125 }
2126}
2127
Calin Juravle175dc732015-08-25 15:42:32 +01002128void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2129 HInstruction* instruction,
2130 uint32_t dex_pc,
2131 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002132 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002133
2134 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2135 {
2136 // Ensure the pc position is recorded immediately after the `blr` instruction.
2137 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2138 __ blr(lr);
2139 if (EntrypointRequiresStackMap(entrypoint)) {
2140 RecordPcInfo(instruction, dex_pc, slow_path);
2141 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002142 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002143}
2144
Roland Levillaindec8f632016-07-22 17:10:06 +01002145void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2146 HInstruction* instruction,
2147 SlowPathCode* slow_path) {
2148 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002149 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2150 __ Blr(lr);
2151}
2152
Alexandre Rames67555f72014-11-18 10:55:16 +00002153void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002154 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002155 UseScratchRegisterScope temps(GetVIXLAssembler());
2156 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002157 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2158
Serban Constantinescu02164b32014-11-13 14:05:07 +00002159 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002160 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2161 __ Add(temp, class_reg, status_offset);
2162 __ Ldar(temp, HeapOperand(temp));
2163 __ Cmp(temp, mirror::Class::kStatusInitialized);
2164 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002165 __ Bind(slow_path->GetExitLabel());
2166}
Alexandre Rames5319def2014-10-23 10:03:10 +01002167
Roland Levillain44015862016-01-22 11:47:17 +00002168void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002169 BarrierType type = BarrierAll;
2170
2171 switch (kind) {
2172 case MemBarrierKind::kAnyAny:
2173 case MemBarrierKind::kAnyStore: {
2174 type = BarrierAll;
2175 break;
2176 }
2177 case MemBarrierKind::kLoadAny: {
2178 type = BarrierReads;
2179 break;
2180 }
2181 case MemBarrierKind::kStoreStore: {
2182 type = BarrierWrites;
2183 break;
2184 }
2185 default:
2186 LOG(FATAL) << "Unexpected memory barrier " << kind;
2187 }
2188 __ Dmb(InnerShareable, type);
2189}
2190
Serban Constantinescu02164b32014-11-13 14:05:07 +00002191void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2192 HBasicBlock* successor) {
2193 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002194 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2195 if (slow_path == nullptr) {
2196 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2197 instruction->SetSlowPath(slow_path);
2198 codegen_->AddSlowPath(slow_path);
2199 if (successor != nullptr) {
2200 DCHECK(successor->IsLoopHeader());
2201 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2202 }
2203 } else {
2204 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2205 }
2206
Serban Constantinescu02164b32014-11-13 14:05:07 +00002207 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2208 Register temp = temps.AcquireW();
2209
Andreas Gampe542451c2016-07-26 09:02:02 -07002210 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002211 if (successor == nullptr) {
2212 __ Cbnz(temp, slow_path->GetEntryLabel());
2213 __ Bind(slow_path->GetReturnLabel());
2214 } else {
2215 __ Cbz(temp, codegen_->GetLabelOf(successor));
2216 __ B(slow_path->GetEntryLabel());
2217 // slow_path will return to GetLabelOf(successor).
2218 }
2219}
2220
Alexandre Rames5319def2014-10-23 10:03:10 +01002221InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2222 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002223 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002224 assembler_(codegen->GetAssembler()),
2225 codegen_(codegen) {}
2226
2227#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002228 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002229
2230#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2231
2232enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002233 // Using a base helps identify when we hit such breakpoints.
2234 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002235#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2236 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2237#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2238};
2239
2240#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002241 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002242 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2243 } \
2244 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2245 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2246 locations->SetOut(Location::Any()); \
2247 }
2248 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2249#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2250
2251#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002252#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002253
Alexandre Rames67555f72014-11-18 10:55:16 +00002254void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002255 DCHECK_EQ(instr->InputCount(), 2U);
2256 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2257 Primitive::Type type = instr->GetResultType();
2258 switch (type) {
2259 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002260 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002261 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002262 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002263 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002264 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002265
2266 case Primitive::kPrimFloat:
2267 case Primitive::kPrimDouble:
2268 locations->SetInAt(0, Location::RequiresFpuRegister());
2269 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002270 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002271 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002272
Alexandre Rames5319def2014-10-23 10:03:10 +01002273 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002274 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002275 }
2276}
2277
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002278void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2279 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002280 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2281
2282 bool object_field_get_with_read_barrier =
2283 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002284 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002285 new (GetGraph()->GetArena()) LocationSummary(instruction,
2286 object_field_get_with_read_barrier ?
2287 LocationSummary::kCallOnSlowPath :
2288 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002289 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002290 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002291 // We need a temporary register for the read barrier marking slow
2292 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002293 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2294 !Runtime::Current()->UseJitCompilation() &&
2295 !field_info.IsVolatile()) {
2296 // If link-time thunks for the Baker read barrier are enabled, for AOT
2297 // non-volatile loads we need a temporary only if the offset is too big.
2298 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2299 locations->AddTemp(FixedTempLocation());
2300 }
2301 } else {
2302 locations->AddTemp(Location::RequiresRegister());
2303 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002304 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002305 locations->SetInAt(0, Location::RequiresRegister());
2306 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2307 locations->SetOut(Location::RequiresFpuRegister());
2308 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002309 // The output overlaps for an object field get when read barriers
2310 // are enabled: we do not want the load to overwrite the object's
2311 // location, as we need it to emit the read barrier.
2312 locations->SetOut(
2313 Location::RequiresRegister(),
2314 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002315 }
2316}
2317
2318void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2319 const FieldInfo& field_info) {
2320 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002321 LocationSummary* locations = instruction->GetLocations();
2322 Location base_loc = locations->InAt(0);
2323 Location out = locations->Out();
2324 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002325 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002326 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002327
Roland Levillain44015862016-01-22 11:47:17 +00002328 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2329 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002330 // /* HeapReference<Object> */ out = *(base + offset)
2331 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002332 Location maybe_temp =
2333 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002334 // Note that potential implicit null checks are handled in this
2335 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2336 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2337 instruction,
2338 out,
2339 base,
2340 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002341 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002342 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002343 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002344 } else {
2345 // General case.
2346 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002347 // Note that a potential implicit null check is handled in this
2348 // CodeGeneratorARM64::LoadAcquire call.
2349 // NB: LoadAcquire will record the pc info if needed.
2350 codegen_->LoadAcquire(
2351 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002352 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002353 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2354 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002355 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002356 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002357 }
Roland Levillain44015862016-01-22 11:47:17 +00002358 if (field_type == Primitive::kPrimNot) {
2359 // If read barriers are enabled, emit read barriers other than
2360 // Baker's using a slow path (and also unpoison the loaded
2361 // reference, if heap poisoning is enabled).
2362 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2363 }
Roland Levillain4d027112015-07-01 15:41:14 +01002364 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002365}
2366
2367void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2368 LocationSummary* locations =
2369 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2370 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002371 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2372 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2373 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002374 locations->SetInAt(1, Location::RequiresFpuRegister());
2375 } else {
2376 locations->SetInAt(1, Location::RequiresRegister());
2377 }
2378}
2379
2380void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002381 const FieldInfo& field_info,
2382 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002383 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2384
2385 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002386 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002387 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002388 Offset offset = field_info.GetFieldOffset();
2389 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002390
Roland Levillain4d027112015-07-01 15:41:14 +01002391 {
2392 // We use a block to end the scratch scope before the write barrier, thus
2393 // freeing the temporary registers so they can be used in `MarkGCCard`.
2394 UseScratchRegisterScope temps(GetVIXLAssembler());
2395
2396 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2397 DCHECK(value.IsW());
2398 Register temp = temps.AcquireW();
2399 __ Mov(temp, value.W());
2400 GetAssembler()->PoisonHeapReference(temp.W());
2401 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002402 }
Roland Levillain4d027112015-07-01 15:41:14 +01002403
2404 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002405 codegen_->StoreRelease(
2406 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002407 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002408 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2409 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002410 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2411 codegen_->MaybeRecordImplicitNullCheck(instruction);
2412 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002413 }
2414
2415 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002416 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002417 }
2418}
2419
Alexandre Rames67555f72014-11-18 10:55:16 +00002420void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002421 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002422
2423 switch (type) {
2424 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002425 case Primitive::kPrimLong: {
2426 Register dst = OutputRegister(instr);
2427 Register lhs = InputRegisterAt(instr, 0);
2428 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002429 if (instr->IsAdd()) {
2430 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002431 } else if (instr->IsAnd()) {
2432 __ And(dst, lhs, rhs);
2433 } else if (instr->IsOr()) {
2434 __ Orr(dst, lhs, rhs);
2435 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002436 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002437 } else if (instr->IsRor()) {
2438 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002439 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002440 __ Ror(dst, lhs, shift);
2441 } else {
2442 // Ensure shift distance is in the same size register as the result. If
2443 // we are rotating a long and the shift comes in a w register originally,
2444 // we don't need to sxtw for use as an x since the shift distances are
2445 // all & reg_bits - 1.
2446 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2447 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002448 } else {
2449 DCHECK(instr->IsXor());
2450 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002451 }
2452 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002453 }
2454 case Primitive::kPrimFloat:
2455 case Primitive::kPrimDouble: {
2456 FPRegister dst = OutputFPRegister(instr);
2457 FPRegister lhs = InputFPRegisterAt(instr, 0);
2458 FPRegister rhs = InputFPRegisterAt(instr, 1);
2459 if (instr->IsAdd()) {
2460 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002461 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002462 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002463 } else {
2464 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002465 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002466 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002467 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002468 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002469 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002470 }
2471}
2472
Serban Constantinescu02164b32014-11-13 14:05:07 +00002473void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2474 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2475
2476 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2477 Primitive::Type type = instr->GetResultType();
2478 switch (type) {
2479 case Primitive::kPrimInt:
2480 case Primitive::kPrimLong: {
2481 locations->SetInAt(0, Location::RequiresRegister());
2482 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002483 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002484 break;
2485 }
2486 default:
2487 LOG(FATAL) << "Unexpected shift type " << type;
2488 }
2489}
2490
2491void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2492 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2493
2494 Primitive::Type type = instr->GetType();
2495 switch (type) {
2496 case Primitive::kPrimInt:
2497 case Primitive::kPrimLong: {
2498 Register dst = OutputRegister(instr);
2499 Register lhs = InputRegisterAt(instr, 0);
2500 Operand rhs = InputOperandAt(instr, 1);
2501 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002502 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002503 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002504 if (instr->IsShl()) {
2505 __ Lsl(dst, lhs, shift_value);
2506 } else if (instr->IsShr()) {
2507 __ Asr(dst, lhs, shift_value);
2508 } else {
2509 __ Lsr(dst, lhs, shift_value);
2510 }
2511 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002512 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002513
2514 if (instr->IsShl()) {
2515 __ Lsl(dst, lhs, rhs_reg);
2516 } else if (instr->IsShr()) {
2517 __ Asr(dst, lhs, rhs_reg);
2518 } else {
2519 __ Lsr(dst, lhs, rhs_reg);
2520 }
2521 }
2522 break;
2523 }
2524 default:
2525 LOG(FATAL) << "Unexpected shift operation type " << type;
2526 }
2527}
2528
Alexandre Rames5319def2014-10-23 10:03:10 +01002529void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002530 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002531}
2532
2533void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002534 HandleBinaryOp(instruction);
2535}
2536
2537void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2538 HandleBinaryOp(instruction);
2539}
2540
2541void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2542 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002543}
2544
Artem Serov7fc63502016-02-09 17:15:29 +00002545void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002546 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2547 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2548 locations->SetInAt(0, Location::RequiresRegister());
2549 // There is no immediate variant of negated bitwise instructions in AArch64.
2550 locations->SetInAt(1, Location::RequiresRegister());
2551 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2552}
2553
Artem Serov7fc63502016-02-09 17:15:29 +00002554void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002555 Register dst = OutputRegister(instr);
2556 Register lhs = InputRegisterAt(instr, 0);
2557 Register rhs = InputRegisterAt(instr, 1);
2558
2559 switch (instr->GetOpKind()) {
2560 case HInstruction::kAnd:
2561 __ Bic(dst, lhs, rhs);
2562 break;
2563 case HInstruction::kOr:
2564 __ Orn(dst, lhs, rhs);
2565 break;
2566 case HInstruction::kXor:
2567 __ Eon(dst, lhs, rhs);
2568 break;
2569 default:
2570 LOG(FATAL) << "Unreachable";
2571 }
2572}
2573
Anton Kirilov74234da2017-01-13 14:42:47 +00002574void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2575 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002576 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2577 instruction->GetType() == Primitive::kPrimLong);
2578 LocationSummary* locations =
2579 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2580 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2581 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2582 } else {
2583 locations->SetInAt(0, Location::RequiresRegister());
2584 }
2585 locations->SetInAt(1, Location::RequiresRegister());
2586 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2587}
2588
Anton Kirilov74234da2017-01-13 14:42:47 +00002589void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2590 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002591 Primitive::Type type = instruction->GetType();
2592 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2593 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2594 Register out = OutputRegister(instruction);
2595 Register left;
2596 if (kind != HInstruction::kNeg) {
2597 left = InputRegisterAt(instruction, 0);
2598 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002599 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002600 // shifter operand operation, the IR generating `right_reg` (input to the type
2601 // conversion) can have a different type from the current instruction's type,
2602 // so we manually indicate the type.
2603 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002604 Operand right_operand(0);
2605
Anton Kirilov74234da2017-01-13 14:42:47 +00002606 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2607 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002608 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2609 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002610 right_operand = Operand(right_reg,
2611 helpers::ShiftFromOpKind(op_kind),
2612 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002613 }
2614
2615 // Logical binary operations do not support extension operations in the
2616 // operand. Note that VIXL would still manage if it was passed by generating
2617 // the extension as a separate instruction.
2618 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2619 DCHECK(!right_operand.IsExtendedRegister() ||
2620 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2621 kind != HInstruction::kNeg));
2622 switch (kind) {
2623 case HInstruction::kAdd:
2624 __ Add(out, left, right_operand);
2625 break;
2626 case HInstruction::kAnd:
2627 __ And(out, left, right_operand);
2628 break;
2629 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002630 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002631 __ Neg(out, right_operand);
2632 break;
2633 case HInstruction::kOr:
2634 __ Orr(out, left, right_operand);
2635 break;
2636 case HInstruction::kSub:
2637 __ Sub(out, left, right_operand);
2638 break;
2639 case HInstruction::kXor:
2640 __ Eor(out, left, right_operand);
2641 break;
2642 default:
2643 LOG(FATAL) << "Unexpected operation kind: " << kind;
2644 UNREACHABLE();
2645 }
2646}
2647
Artem Serov328429f2016-07-06 16:23:04 +01002648void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002649 LocationSummary* locations =
2650 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2651 locations->SetInAt(0, Location::RequiresRegister());
2652 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002653 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002654}
2655
Roland Levillain19c54192016-11-04 13:44:09 +00002656void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002657 __ Add(OutputRegister(instruction),
2658 InputRegisterAt(instruction, 0),
2659 Operand(InputOperandAt(instruction, 1)));
2660}
2661
Artem Serove1811ed2017-04-27 16:50:47 +01002662void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2663 LocationSummary* locations =
2664 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2665
2666 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2667
2668 locations->SetInAt(0, Location::RequiresRegister());
2669 // For byte case we don't need to shift the index variable so we can encode the data offset into
2670 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2671 // data offset constant generation out of the loop and reduce the critical path length in the
2672 // loop.
2673 locations->SetInAt(1, shift->GetValue() == 0
2674 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2675 : Location::RequiresRegister());
2676 locations->SetInAt(2, Location::ConstantLocation(shift));
2677 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2678}
2679
2680void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2681 HIntermediateAddressIndex* instruction) {
2682 Register index_reg = InputRegisterAt(instruction, 0);
2683 uint32_t shift = Int64ConstantFrom(instruction->GetLocations()->InAt(2));
2684 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2685
2686 if (shift == 0) {
2687 __ Add(OutputRegister(instruction), index_reg, offset);
2688 } else {
2689 Register offset_reg = InputRegisterAt(instruction, 1);
2690 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2691 }
2692}
2693
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002694void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002695 LocationSummary* locations =
2696 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002697 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2698 if (instr->GetOpKind() == HInstruction::kSub &&
2699 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002700 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002701 // Don't allocate register for Mneg instruction.
2702 } else {
2703 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2704 Location::RequiresRegister());
2705 }
2706 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2707 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002708 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2709}
2710
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002711void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002712 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002713 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2714 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002715
2716 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2717 // This fixup should be carried out for all multiply-accumulate instructions:
2718 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2719 if (instr->GetType() == Primitive::kPrimLong &&
2720 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2721 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002722 vixl::aarch64::Instruction* prev =
2723 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002724 if (prev->IsLoadOrStore()) {
2725 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002726 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002727 __ nop();
2728 }
2729 }
2730
2731 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002732 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002733 __ Madd(res, mul_left, mul_right, accumulator);
2734 } else {
2735 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002736 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002737 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002738 __ Mneg(res, mul_left, mul_right);
2739 } else {
2740 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2741 __ Msub(res, mul_left, mul_right, accumulator);
2742 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002743 }
2744}
2745
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002746void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002747 bool object_array_get_with_read_barrier =
2748 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002749 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002750 new (GetGraph()->GetArena()) LocationSummary(instruction,
2751 object_array_get_with_read_barrier ?
2752 LocationSummary::kCallOnSlowPath :
2753 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002754 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002755 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002756 // We need a temporary register for the read barrier marking slow
2757 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002758 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2759 !Runtime::Current()->UseJitCompilation() &&
2760 instruction->GetIndex()->IsConstant()) {
2761 // Array loads with constant index are treated as field loads.
2762 // If link-time thunks for the Baker read barrier are enabled, for AOT
2763 // constant index loads we need a temporary only if the offset is too big.
2764 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2765 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
2766 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
2767 if (offset >= kReferenceLoadMinFarOffset) {
2768 locations->AddTemp(FixedTempLocation());
2769 }
2770 } else {
2771 locations->AddTemp(Location::RequiresRegister());
2772 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002773 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002774 locations->SetInAt(0, Location::RequiresRegister());
2775 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002776 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2777 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2778 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002779 // The output overlaps in the case of an object array get with
2780 // read barriers enabled: we do not want the move to overwrite the
2781 // array's location, as we need it to emit the read barrier.
2782 locations->SetOut(
2783 Location::RequiresRegister(),
2784 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002785 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002786}
2787
2788void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002789 Primitive::Type type = instruction->GetType();
2790 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002791 LocationSummary* locations = instruction->GetLocations();
2792 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002793 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002794 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002795 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2796 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002797 MacroAssembler* masm = GetVIXLAssembler();
2798 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002799
Roland Levillain19c54192016-11-04 13:44:09 +00002800 // The read barrier instrumentation of object ArrayGet instructions
2801 // does not support the HIntermediateAddress instruction.
2802 DCHECK(!((type == Primitive::kPrimNot) &&
2803 instruction->GetArray()->IsIntermediateAddress() &&
2804 kEmitCompilerReadBarrier));
2805
Roland Levillain44015862016-01-22 11:47:17 +00002806 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2807 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002808 // Note that a potential implicit null check is handled in the
2809 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002810 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002811 if (index.IsConstant()) {
2812 // Array load with a constant index can be treated as a field load.
2813 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2814 Location maybe_temp =
2815 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2816 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2817 out,
2818 obj.W(),
2819 offset,
2820 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002821 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002822 /* use_load_acquire */ false);
2823 } else {
2824 Register temp = WRegisterFrom(locations->GetTemp(0));
2825 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002826 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002827 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002828 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002829 // General case.
2830 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002831 Register length;
2832 if (maybe_compressed_char_at) {
2833 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2834 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002835 {
2836 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2837 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2838
2839 if (instruction->GetArray()->IsIntermediateAddress()) {
2840 DCHECK_LT(count_offset, offset);
2841 int64_t adjusted_offset =
2842 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2843 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2844 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2845 } else {
2846 __ Ldr(length, HeapOperand(obj, count_offset));
2847 }
2848 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002849 }
jessicahandojo05765752016-09-09 19:01:32 -07002850 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002851 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002852 if (maybe_compressed_char_at) {
2853 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002854 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2855 "Expecting 0=compressed, 1=uncompressed");
2856 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002857 __ Ldrb(Register(OutputCPURegister(instruction)),
2858 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2859 __ B(&done);
2860 __ Bind(&uncompressed_load);
2861 __ Ldrh(Register(OutputCPURegister(instruction)),
2862 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2863 __ Bind(&done);
2864 } else {
2865 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2866 source = HeapOperand(obj, offset);
2867 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002868 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002869 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002870 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002871 // We do not need to compute the intermediate address from the array: the
2872 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002873 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002874 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002875 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002876 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2877 }
2878 temp = obj;
2879 } else {
2880 __ Add(temp, obj, offset);
2881 }
jessicahandojo05765752016-09-09 19:01:32 -07002882 if (maybe_compressed_char_at) {
2883 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002884 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2885 "Expecting 0=compressed, 1=uncompressed");
2886 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002887 __ Ldrb(Register(OutputCPURegister(instruction)),
2888 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2889 __ B(&done);
2890 __ Bind(&uncompressed_load);
2891 __ Ldrh(Register(OutputCPURegister(instruction)),
2892 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2893 __ Bind(&done);
2894 } else {
2895 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2896 }
Roland Levillain44015862016-01-22 11:47:17 +00002897 }
jessicahandojo05765752016-09-09 19:01:32 -07002898 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002899 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2900 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002901 codegen_->Load(type, OutputCPURegister(instruction), source);
2902 codegen_->MaybeRecordImplicitNullCheck(instruction);
2903 }
Roland Levillain44015862016-01-22 11:47:17 +00002904
2905 if (type == Primitive::kPrimNot) {
2906 static_assert(
2907 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2908 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2909 Location obj_loc = locations->InAt(0);
2910 if (index.IsConstant()) {
2911 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2912 } else {
2913 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2914 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002915 }
Roland Levillain4d027112015-07-01 15:41:14 +01002916 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002917}
2918
Alexandre Rames5319def2014-10-23 10:03:10 +01002919void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2920 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2921 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002922 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002923}
2924
2925void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002926 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002927 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002928 {
2929 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2930 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2931 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2932 codegen_->MaybeRecordImplicitNullCheck(instruction);
2933 }
jessicahandojo05765752016-09-09 19:01:32 -07002934 // Mask out compression flag from String's array length.
2935 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002936 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002937 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002938}
2939
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002940void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002941 Primitive::Type value_type = instruction->GetComponentType();
2942
2943 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002944 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2945 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002946 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002947 LocationSummary::kCallOnSlowPath :
2948 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002949 locations->SetInAt(0, Location::RequiresRegister());
2950 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002951 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2952 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2953 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002954 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002955 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002956 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002957 }
2958}
2959
2960void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2961 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002962 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002963 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002964 bool needs_write_barrier =
2965 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002966
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002967 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002968 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002969 CPURegister source = value;
2970 Location index = locations->InAt(1);
2971 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2972 MemOperand destination = HeapOperand(array);
2973 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002974
2975 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002976 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002977 if (index.IsConstant()) {
2978 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2979 destination = HeapOperand(array, offset);
2980 } else {
2981 UseScratchRegisterScope temps(masm);
2982 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002983 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002984 // We do not need to compute the intermediate address from the array: the
2985 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002986 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002987 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002988 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002989 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2990 }
2991 temp = array;
2992 } else {
2993 __ Add(temp, array, offset);
2994 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002995 destination = HeapOperand(temp,
2996 XRegisterFrom(index),
2997 LSL,
2998 Primitive::ComponentSizeShift(value_type));
2999 }
Artem Serov914d7a82017-02-07 14:33:49 +00003000 {
3001 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3002 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3003 codegen_->Store(value_type, value, destination);
3004 codegen_->MaybeRecordImplicitNullCheck(instruction);
3005 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003006 } else {
Artem Serov328429f2016-07-06 16:23:04 +01003007 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003008 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003009 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01003010 {
3011 // We use a block to end the scratch scope before the write barrier, thus
3012 // freeing the temporary registers so they can be used in `MarkGCCard`.
3013 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003014 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01003015 if (index.IsConstant()) {
3016 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003017 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01003018 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01003019 destination = HeapOperand(temp,
3020 XRegisterFrom(index),
3021 LSL,
3022 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01003023 }
3024
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003025 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3026 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3027 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3028
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003029 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003030 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
3031 codegen_->AddSlowPath(slow_path);
3032 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003033 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003034 __ Cbnz(Register(value), &non_zero);
3035 if (!index.IsConstant()) {
3036 __ Add(temp, array, offset);
3037 }
Artem Serov914d7a82017-02-07 14:33:49 +00003038 {
3039 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
3040 // emitted.
3041 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3042 __ Str(wzr, destination);
3043 codegen_->MaybeRecordImplicitNullCheck(instruction);
3044 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003045 __ B(&done);
3046 __ Bind(&non_zero);
3047 }
3048
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003049 // Note that when Baker read barriers are enabled, the type
3050 // checks are performed without read barriers. This is fine,
3051 // even in the case where a class object is in the from-space
3052 // after the flip, as a comparison involving such a type would
3053 // not produce a false positive; it may of course produce a
3054 // false negative, in which case we would take the ArraySet
3055 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003056
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003057 Register temp2 = temps.AcquireSameSizeAs(array);
3058 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003059 {
3060 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3061 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3062 __ Ldr(temp, HeapOperand(array, class_offset));
3063 codegen_->MaybeRecordImplicitNullCheck(instruction);
3064 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003065 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003066
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003067 // /* HeapReference<Class> */ temp = temp->component_type_
3068 __ Ldr(temp, HeapOperand(temp, component_offset));
3069 // /* HeapReference<Class> */ temp2 = value->klass_
3070 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3071 // If heap poisoning is enabled, no need to unpoison `temp`
3072 // nor `temp2`, as we are comparing two poisoned references.
3073 __ Cmp(temp, temp2);
3074 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003075
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003076 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3077 vixl::aarch64::Label do_put;
3078 __ B(eq, &do_put);
3079 // If heap poisoning is enabled, the `temp` reference has
3080 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003081 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3082
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003083 // /* HeapReference<Class> */ temp = temp->super_class_
3084 __ Ldr(temp, HeapOperand(temp, super_offset));
3085 // If heap poisoning is enabled, no need to unpoison
3086 // `temp`, as we are comparing against null below.
3087 __ Cbnz(temp, slow_path->GetEntryLabel());
3088 __ Bind(&do_put);
3089 } else {
3090 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003091 }
3092 }
3093
3094 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003095 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003096 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003097 __ Mov(temp2, value.W());
3098 GetAssembler()->PoisonHeapReference(temp2);
3099 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003100 }
3101
3102 if (!index.IsConstant()) {
3103 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003104 } else {
3105 // We no longer need the `temp` here so release it as the store below may
3106 // need a scratch register (if the constant index makes the offset too large)
3107 // and the poisoned `source` could be using the other scratch register.
3108 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003109 }
Artem Serov914d7a82017-02-07 14:33:49 +00003110 {
3111 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3112 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3113 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003114
Artem Serov914d7a82017-02-07 14:33:49 +00003115 if (!may_need_runtime_call_for_type_check) {
3116 codegen_->MaybeRecordImplicitNullCheck(instruction);
3117 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003118 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003119 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003120
3121 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3122
3123 if (done.IsLinked()) {
3124 __ Bind(&done);
3125 }
3126
3127 if (slow_path != nullptr) {
3128 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003129 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003130 }
3131}
3132
Alexandre Rames67555f72014-11-18 10:55:16 +00003133void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003134 RegisterSet caller_saves = RegisterSet::Empty();
3135 InvokeRuntimeCallingConvention calling_convention;
3136 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3137 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3138 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003139 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003140 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003141}
3142
3143void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003144 BoundsCheckSlowPathARM64* slow_path =
3145 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003146 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003147 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3148 __ B(slow_path->GetEntryLabel(), hs);
3149}
3150
Alexandre Rames67555f72014-11-18 10:55:16 +00003151void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3152 LocationSummary* locations =
3153 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3154 locations->SetInAt(0, Location::RequiresRegister());
3155 if (check->HasUses()) {
3156 locations->SetOut(Location::SameAsFirstInput());
3157 }
3158}
3159
3160void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3161 // We assume the class is not null.
3162 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3163 check->GetLoadClass(), check, check->GetDexPc(), true);
3164 codegen_->AddSlowPath(slow_path);
3165 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3166}
3167
Roland Levillain1a653882016-03-18 18:05:57 +00003168static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3169 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3170 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3171}
3172
3173void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3174 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3175 Location rhs_loc = instruction->GetLocations()->InAt(1);
3176 if (rhs_loc.IsConstant()) {
3177 // 0.0 is the only immediate that can be encoded directly in
3178 // an FCMP instruction.
3179 //
3180 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3181 // specify that in a floating-point comparison, positive zero
3182 // and negative zero are considered equal, so we can use the
3183 // literal 0.0 for both cases here.
3184 //
3185 // Note however that some methods (Float.equal, Float.compare,
3186 // Float.compareTo, Double.equal, Double.compare,
3187 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3188 // StrictMath.min) consider 0.0 to be (strictly) greater than
3189 // -0.0. So if we ever translate calls to these methods into a
3190 // HCompare instruction, we must handle the -0.0 case with
3191 // care here.
3192 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3193 __ Fcmp(lhs_reg, 0.0);
3194 } else {
3195 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3196 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003197}
3198
Serban Constantinescu02164b32014-11-13 14:05:07 +00003199void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003200 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003201 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3202 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003203 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003204 case Primitive::kPrimBoolean:
3205 case Primitive::kPrimByte:
3206 case Primitive::kPrimShort:
3207 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003208 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003209 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003210 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003211 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003212 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3213 break;
3214 }
3215 case Primitive::kPrimFloat:
3216 case Primitive::kPrimDouble: {
3217 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003218 locations->SetInAt(1,
3219 IsFloatingPointZeroConstant(compare->InputAt(1))
3220 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3221 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003222 locations->SetOut(Location::RequiresRegister());
3223 break;
3224 }
3225 default:
3226 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3227 }
3228}
3229
3230void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3231 Primitive::Type in_type = compare->InputAt(0)->GetType();
3232
3233 // 0 if: left == right
3234 // 1 if: left > right
3235 // -1 if: left < right
3236 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003237 case Primitive::kPrimBoolean:
3238 case Primitive::kPrimByte:
3239 case Primitive::kPrimShort:
3240 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003241 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003242 case Primitive::kPrimLong: {
3243 Register result = OutputRegister(compare);
3244 Register left = InputRegisterAt(compare, 0);
3245 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003246 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003247 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3248 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003249 break;
3250 }
3251 case Primitive::kPrimFloat:
3252 case Primitive::kPrimDouble: {
3253 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003254 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003255 __ Cset(result, ne);
3256 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003257 break;
3258 }
3259 default:
3260 LOG(FATAL) << "Unimplemented compare type " << in_type;
3261 }
3262}
3263
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003264void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003265 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003266
3267 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3268 locations->SetInAt(0, Location::RequiresFpuRegister());
3269 locations->SetInAt(1,
3270 IsFloatingPointZeroConstant(instruction->InputAt(1))
3271 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3272 : Location::RequiresFpuRegister());
3273 } else {
3274 // Integer cases.
3275 locations->SetInAt(0, Location::RequiresRegister());
3276 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3277 }
3278
David Brazdilb3e773e2016-01-26 11:28:37 +00003279 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003280 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003281 }
3282}
3283
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003284void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003285 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003286 return;
3287 }
3288
3289 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003290 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003291 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003292
Roland Levillain7f63c522015-07-13 15:54:55 +00003293 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003294 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003295 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003296 } else {
3297 // Integer cases.
3298 Register lhs = InputRegisterAt(instruction, 0);
3299 Operand rhs = InputOperandAt(instruction, 1);
3300 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003301 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003302 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003303}
3304
3305#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3306 M(Equal) \
3307 M(NotEqual) \
3308 M(LessThan) \
3309 M(LessThanOrEqual) \
3310 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003311 M(GreaterThanOrEqual) \
3312 M(Below) \
3313 M(BelowOrEqual) \
3314 M(Above) \
3315 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003316#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003317void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3318void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003319FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003320#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003321#undef FOR_EACH_CONDITION_INSTRUCTION
3322
Zheng Xuc6667102015-05-15 16:08:45 +08003323void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3324 DCHECK(instruction->IsDiv() || instruction->IsRem());
3325
3326 LocationSummary* locations = instruction->GetLocations();
3327 Location second = locations->InAt(1);
3328 DCHECK(second.IsConstant());
3329
3330 Register out = OutputRegister(instruction);
3331 Register dividend = InputRegisterAt(instruction, 0);
3332 int64_t imm = Int64FromConstant(second.GetConstant());
3333 DCHECK(imm == 1 || imm == -1);
3334
3335 if (instruction->IsRem()) {
3336 __ Mov(out, 0);
3337 } else {
3338 if (imm == 1) {
3339 __ Mov(out, dividend);
3340 } else {
3341 __ Neg(out, dividend);
3342 }
3343 }
3344}
3345
3346void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3347 DCHECK(instruction->IsDiv() || instruction->IsRem());
3348
3349 LocationSummary* locations = instruction->GetLocations();
3350 Location second = locations->InAt(1);
3351 DCHECK(second.IsConstant());
3352
3353 Register out = OutputRegister(instruction);
3354 Register dividend = InputRegisterAt(instruction, 0);
3355 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003356 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003357 int ctz_imm = CTZ(abs_imm);
3358
3359 UseScratchRegisterScope temps(GetVIXLAssembler());
3360 Register temp = temps.AcquireSameSizeAs(out);
3361
3362 if (instruction->IsDiv()) {
3363 __ Add(temp, dividend, abs_imm - 1);
3364 __ Cmp(dividend, 0);
3365 __ Csel(out, temp, dividend, lt);
3366 if (imm > 0) {
3367 __ Asr(out, out, ctz_imm);
3368 } else {
3369 __ Neg(out, Operand(out, ASR, ctz_imm));
3370 }
3371 } else {
3372 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3373 __ Asr(temp, dividend, bits - 1);
3374 __ Lsr(temp, temp, bits - ctz_imm);
3375 __ Add(out, dividend, temp);
3376 __ And(out, out, abs_imm - 1);
3377 __ Sub(out, out, temp);
3378 }
3379}
3380
3381void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3382 DCHECK(instruction->IsDiv() || instruction->IsRem());
3383
3384 LocationSummary* locations = instruction->GetLocations();
3385 Location second = locations->InAt(1);
3386 DCHECK(second.IsConstant());
3387
3388 Register out = OutputRegister(instruction);
3389 Register dividend = InputRegisterAt(instruction, 0);
3390 int64_t imm = Int64FromConstant(second.GetConstant());
3391
3392 Primitive::Type type = instruction->GetResultType();
3393 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3394
3395 int64_t magic;
3396 int shift;
3397 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3398
3399 UseScratchRegisterScope temps(GetVIXLAssembler());
3400 Register temp = temps.AcquireSameSizeAs(out);
3401
3402 // temp = get_high(dividend * magic)
3403 __ Mov(temp, magic);
3404 if (type == Primitive::kPrimLong) {
3405 __ Smulh(temp, dividend, temp);
3406 } else {
3407 __ Smull(temp.X(), dividend, temp);
3408 __ Lsr(temp.X(), temp.X(), 32);
3409 }
3410
3411 if (imm > 0 && magic < 0) {
3412 __ Add(temp, temp, dividend);
3413 } else if (imm < 0 && magic > 0) {
3414 __ Sub(temp, temp, dividend);
3415 }
3416
3417 if (shift != 0) {
3418 __ Asr(temp, temp, shift);
3419 }
3420
3421 if (instruction->IsDiv()) {
3422 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3423 } else {
3424 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3425 // TODO: Strength reduction for msub.
3426 Register temp_imm = temps.AcquireSameSizeAs(out);
3427 __ Mov(temp_imm, imm);
3428 __ Msub(out, temp, temp_imm, dividend);
3429 }
3430}
3431
3432void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3433 DCHECK(instruction->IsDiv() || instruction->IsRem());
3434 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003435 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003436
3437 LocationSummary* locations = instruction->GetLocations();
3438 Register out = OutputRegister(instruction);
3439 Location second = locations->InAt(1);
3440
3441 if (second.IsConstant()) {
3442 int64_t imm = Int64FromConstant(second.GetConstant());
3443
3444 if (imm == 0) {
3445 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3446 } else if (imm == 1 || imm == -1) {
3447 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003448 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003449 DivRemByPowerOfTwo(instruction);
3450 } else {
3451 DCHECK(imm <= -2 || imm >= 2);
3452 GenerateDivRemWithAnyConstant(instruction);
3453 }
3454 } else {
3455 Register dividend = InputRegisterAt(instruction, 0);
3456 Register divisor = InputRegisterAt(instruction, 1);
3457 if (instruction->IsDiv()) {
3458 __ Sdiv(out, dividend, divisor);
3459 } else {
3460 UseScratchRegisterScope temps(GetVIXLAssembler());
3461 Register temp = temps.AcquireSameSizeAs(out);
3462 __ Sdiv(temp, dividend, divisor);
3463 __ Msub(out, temp, divisor, dividend);
3464 }
3465 }
3466}
3467
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003468void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3469 LocationSummary* locations =
3470 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3471 switch (div->GetResultType()) {
3472 case Primitive::kPrimInt:
3473 case Primitive::kPrimLong:
3474 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003475 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003476 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3477 break;
3478
3479 case Primitive::kPrimFloat:
3480 case Primitive::kPrimDouble:
3481 locations->SetInAt(0, Location::RequiresFpuRegister());
3482 locations->SetInAt(1, Location::RequiresFpuRegister());
3483 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3484 break;
3485
3486 default:
3487 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3488 }
3489}
3490
3491void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3492 Primitive::Type type = div->GetResultType();
3493 switch (type) {
3494 case Primitive::kPrimInt:
3495 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003496 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003497 break;
3498
3499 case Primitive::kPrimFloat:
3500 case Primitive::kPrimDouble:
3501 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3502 break;
3503
3504 default:
3505 LOG(FATAL) << "Unexpected div type " << type;
3506 }
3507}
3508
Alexandre Rames67555f72014-11-18 10:55:16 +00003509void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003510 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003511 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003512}
3513
3514void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3515 SlowPathCodeARM64* slow_path =
3516 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3517 codegen_->AddSlowPath(slow_path);
3518 Location value = instruction->GetLocations()->InAt(0);
3519
Alexandre Rames3e69f162014-12-10 10:36:50 +00003520 Primitive::Type type = instruction->GetType();
3521
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003522 if (!Primitive::IsIntegralType(type)) {
3523 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003524 return;
3525 }
3526
Alexandre Rames67555f72014-11-18 10:55:16 +00003527 if (value.IsConstant()) {
3528 int64_t divisor = Int64ConstantFrom(value);
3529 if (divisor == 0) {
3530 __ B(slow_path->GetEntryLabel());
3531 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003532 // A division by a non-null constant is valid. We don't need to perform
3533 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003534 }
3535 } else {
3536 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3537 }
3538}
3539
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003540void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3541 LocationSummary* locations =
3542 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3543 locations->SetOut(Location::ConstantLocation(constant));
3544}
3545
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003546void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3547 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003548 // Will be generated at use site.
3549}
3550
Alexandre Rames5319def2014-10-23 10:03:10 +01003551void LocationsBuilderARM64::VisitExit(HExit* exit) {
3552 exit->SetLocations(nullptr);
3553}
3554
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003555void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003556}
3557
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003558void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3559 LocationSummary* locations =
3560 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3561 locations->SetOut(Location::ConstantLocation(constant));
3562}
3563
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003564void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003565 // Will be generated at use site.
3566}
3567
David Brazdilfc6a86a2015-06-26 10:33:45 +00003568void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003569 DCHECK(!successor->IsExitBlock());
3570 HBasicBlock* block = got->GetBlock();
3571 HInstruction* previous = got->GetPrevious();
3572 HLoopInformation* info = block->GetLoopInformation();
3573
David Brazdil46e2a392015-03-16 17:31:52 +00003574 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003575 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3576 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3577 return;
3578 }
3579 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3580 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3581 }
3582 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003583 __ B(codegen_->GetLabelOf(successor));
3584 }
3585}
3586
David Brazdilfc6a86a2015-06-26 10:33:45 +00003587void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3588 got->SetLocations(nullptr);
3589}
3590
3591void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3592 HandleGoto(got, got->GetSuccessor());
3593}
3594
3595void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3596 try_boundary->SetLocations(nullptr);
3597}
3598
3599void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3600 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3601 if (!successor->IsExitBlock()) {
3602 HandleGoto(try_boundary, successor);
3603 }
3604}
3605
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003606void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003607 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003608 vixl::aarch64::Label* true_target,
3609 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003610 // FP branching requires both targets to be explicit. If either of the targets
3611 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003612 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003613 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003614
David Brazdil0debae72015-11-12 18:37:00 +00003615 if (true_target == nullptr && false_target == nullptr) {
3616 // Nothing to do. The code always falls through.
3617 return;
3618 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003619 // Constant condition, statically compared against "true" (integer value 1).
3620 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003621 if (true_target != nullptr) {
3622 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003623 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003624 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003625 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003626 if (false_target != nullptr) {
3627 __ B(false_target);
3628 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003629 }
David Brazdil0debae72015-11-12 18:37:00 +00003630 return;
3631 }
3632
3633 // The following code generates these patterns:
3634 // (1) true_target == nullptr && false_target != nullptr
3635 // - opposite condition true => branch to false_target
3636 // (2) true_target != nullptr && false_target == nullptr
3637 // - condition true => branch to true_target
3638 // (3) true_target != nullptr && false_target != nullptr
3639 // - condition true => branch to true_target
3640 // - branch to false_target
3641 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003642 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003643 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003644 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003645 if (true_target == nullptr) {
3646 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3647 } else {
3648 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3649 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003650 } else {
3651 // The condition instruction has not been materialized, use its inputs as
3652 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003653 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003654
David Brazdil0debae72015-11-12 18:37:00 +00003655 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003656 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003657 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003658 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003659 IfCondition opposite_condition = condition->GetOppositeCondition();
3660 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003661 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003662 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003663 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003664 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003665 // Integer cases.
3666 Register lhs = InputRegisterAt(condition, 0);
3667 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003668
3669 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003670 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003671 if (true_target == nullptr) {
3672 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3673 non_fallthrough_target = false_target;
3674 } else {
3675 arm64_cond = ARM64Condition(condition->GetCondition());
3676 non_fallthrough_target = true_target;
3677 }
3678
Aart Bik086d27e2016-01-20 17:02:00 -08003679 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003680 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003681 switch (arm64_cond) {
3682 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003683 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003684 break;
3685 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003686 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003687 break;
3688 case lt:
3689 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003690 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003691 break;
3692 case ge:
3693 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003694 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003695 break;
3696 default:
3697 // Without the `static_cast` the compiler throws an error for
3698 // `-Werror=sign-promo`.
3699 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3700 }
3701 } else {
3702 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003703 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003704 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003705 }
3706 }
David Brazdil0debae72015-11-12 18:37:00 +00003707
3708 // If neither branch falls through (case 3), the conditional branch to `true_target`
3709 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3710 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003711 __ B(false_target);
3712 }
David Brazdil0debae72015-11-12 18:37:00 +00003713
3714 if (fallthrough_target.IsLinked()) {
3715 __ Bind(&fallthrough_target);
3716 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003717}
3718
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003719void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3720 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003721 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003722 locations->SetInAt(0, Location::RequiresRegister());
3723 }
3724}
3725
3726void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003727 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3728 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003729 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3730 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3731 true_target = nullptr;
3732 }
3733 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3734 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3735 false_target = nullptr;
3736 }
David Brazdil0debae72015-11-12 18:37:00 +00003737 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003738}
3739
3740void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3741 LocationSummary* locations = new (GetGraph()->GetArena())
3742 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003743 InvokeRuntimeCallingConvention calling_convention;
3744 RegisterSet caller_saves = RegisterSet::Empty();
3745 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3746 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003747 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003748 locations->SetInAt(0, Location::RequiresRegister());
3749 }
3750}
3751
3752void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003753 SlowPathCodeARM64* slow_path =
3754 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003755 GenerateTestAndBranch(deoptimize,
3756 /* condition_input_index */ 0,
3757 slow_path->GetEntryLabel(),
3758 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003759}
3760
Mingyao Yang063fc772016-08-02 11:02:54 -07003761void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3762 LocationSummary* locations = new (GetGraph()->GetArena())
3763 LocationSummary(flag, LocationSummary::kNoCall);
3764 locations->SetOut(Location::RequiresRegister());
3765}
3766
3767void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3768 __ Ldr(OutputRegister(flag),
3769 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3770}
3771
David Brazdilc0b601b2016-02-08 14:20:45 +00003772static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3773 return condition->IsCondition() &&
3774 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3775}
3776
Alexandre Rames880f1192016-06-13 16:04:50 +01003777static inline Condition GetConditionForSelect(HCondition* condition) {
3778 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003779 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3780 : ARM64Condition(cond);
3781}
3782
David Brazdil74eb1b22015-12-14 11:44:01 +00003783void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3784 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003785 if (Primitive::IsFloatingPointType(select->GetType())) {
3786 locations->SetInAt(0, Location::RequiresFpuRegister());
3787 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003788 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003789 } else {
3790 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3791 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3792 bool is_true_value_constant = cst_true_value != nullptr;
3793 bool is_false_value_constant = cst_false_value != nullptr;
3794 // Ask VIXL whether we should synthesize constants in registers.
3795 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3796 Operand true_op = is_true_value_constant ?
3797 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3798 Operand false_op = is_false_value_constant ?
3799 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3800 bool true_value_in_register = false;
3801 bool false_value_in_register = false;
3802 MacroAssembler::GetCselSynthesisInformation(
3803 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3804 true_value_in_register |= !is_true_value_constant;
3805 false_value_in_register |= !is_false_value_constant;
3806
3807 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3808 : Location::ConstantLocation(cst_true_value));
3809 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3810 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003811 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003812 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003813
David Brazdil74eb1b22015-12-14 11:44:01 +00003814 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3815 locations->SetInAt(2, Location::RequiresRegister());
3816 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003817}
3818
3819void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003820 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003821 Condition csel_cond;
3822
3823 if (IsBooleanValueOrMaterializedCondition(cond)) {
3824 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003825 // Use the condition flags set by the previous instruction.
3826 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003827 } else {
3828 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003829 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003830 }
3831 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003832 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003833 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003834 } else {
3835 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003836 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003837 }
3838
Alexandre Rames880f1192016-06-13 16:04:50 +01003839 if (Primitive::IsFloatingPointType(select->GetType())) {
3840 __ Fcsel(OutputFPRegister(select),
3841 InputFPRegisterAt(select, 1),
3842 InputFPRegisterAt(select, 0),
3843 csel_cond);
3844 } else {
3845 __ Csel(OutputRegister(select),
3846 InputOperandAt(select, 1),
3847 InputOperandAt(select, 0),
3848 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003849 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003850}
3851
David Srbecky0cf44932015-12-09 14:09:59 +00003852void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3853 new (GetGraph()->GetArena()) LocationSummary(info);
3854}
3855
David Srbeckyd28f4a02016-03-14 17:14:24 +00003856void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3857 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003858}
3859
3860void CodeGeneratorARM64::GenerateNop() {
3861 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003862}
3863
Alexandre Rames5319def2014-10-23 10:03:10 +01003864void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003865 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003866}
3867
3868void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003869 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003870}
3871
3872void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003873 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003874}
3875
3876void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003877 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003878}
3879
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003880// Temp is used for read barrier.
3881static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3882 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003883 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003884 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3885 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3886 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3887 return 1;
3888 }
3889 return 0;
3890}
3891
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003892// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003893// interface pointer, one for loading the current interface.
3894// The other checks have one temp for loading the object's class.
3895static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3896 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3897 return 3;
3898 }
3899 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003900}
3901
Alexandre Rames67555f72014-11-18 10:55:16 +00003902void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003903 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003904 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003905 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003906 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003907 case TypeCheckKind::kExactCheck:
3908 case TypeCheckKind::kAbstractClassCheck:
3909 case TypeCheckKind::kClassHierarchyCheck:
3910 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003911 call_kind =
3912 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003913 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003914 break;
3915 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003916 case TypeCheckKind::kUnresolvedCheck:
3917 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003918 call_kind = LocationSummary::kCallOnSlowPath;
3919 break;
3920 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003921
Alexandre Rames67555f72014-11-18 10:55:16 +00003922 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003923 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003924 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003925 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003926 locations->SetInAt(0, Location::RequiresRegister());
3927 locations->SetInAt(1, Location::RequiresRegister());
3928 // The "out" register is used as a temporary, so it overlaps with the inputs.
3929 // Note that TypeCheckSlowPathARM64 uses this register too.
3930 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003931 // Add temps if necessary for read barriers.
3932 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003933}
3934
3935void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003936 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003937 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003938 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003939 Register obj = InputRegisterAt(instruction, 0);
3940 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003941 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003942 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003943 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3944 DCHECK_LE(num_temps, 1u);
3945 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003946 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3947 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3948 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3949 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003950
Scott Wakeling97c72b72016-06-24 16:19:36 +01003951 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003952 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003953
3954 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003955 // Avoid null check if we know `obj` is not null.
3956 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003957 __ Cbz(obj, &zero);
3958 }
3959
Roland Levillain44015862016-01-22 11:47:17 +00003960 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003961 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003962 // /* HeapReference<Class> */ out = obj->klass_
3963 GenerateReferenceLoadTwoRegisters(instruction,
3964 out_loc,
3965 obj_loc,
3966 class_offset,
3967 maybe_temp_loc,
3968 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003969 __ Cmp(out, cls);
3970 __ Cset(out, eq);
3971 if (zero.IsLinked()) {
3972 __ B(&done);
3973 }
3974 break;
3975 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003976
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003977 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003978 // /* HeapReference<Class> */ out = obj->klass_
3979 GenerateReferenceLoadTwoRegisters(instruction,
3980 out_loc,
3981 obj_loc,
3982 class_offset,
3983 maybe_temp_loc,
3984 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003985 // If the class is abstract, we eagerly fetch the super class of the
3986 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003987 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003988 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003989 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003990 GenerateReferenceLoadOneRegister(instruction,
3991 out_loc,
3992 super_offset,
3993 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003994 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003995 // If `out` is null, we use it for the result, and jump to `done`.
3996 __ Cbz(out, &done);
3997 __ Cmp(out, cls);
3998 __ B(ne, &loop);
3999 __ Mov(out, 1);
4000 if (zero.IsLinked()) {
4001 __ B(&done);
4002 }
4003 break;
4004 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004005
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004006 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004007 // /* HeapReference<Class> */ out = obj->klass_
4008 GenerateReferenceLoadTwoRegisters(instruction,
4009 out_loc,
4010 obj_loc,
4011 class_offset,
4012 maybe_temp_loc,
4013 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004014 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004015 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004016 __ Bind(&loop);
4017 __ Cmp(out, cls);
4018 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004019 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004020 GenerateReferenceLoadOneRegister(instruction,
4021 out_loc,
4022 super_offset,
4023 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004024 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004025 __ Cbnz(out, &loop);
4026 // If `out` is null, we use it for the result, and jump to `done`.
4027 __ B(&done);
4028 __ Bind(&success);
4029 __ Mov(out, 1);
4030 if (zero.IsLinked()) {
4031 __ B(&done);
4032 }
4033 break;
4034 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004035
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004036 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004037 // /* HeapReference<Class> */ out = obj->klass_
4038 GenerateReferenceLoadTwoRegisters(instruction,
4039 out_loc,
4040 obj_loc,
4041 class_offset,
4042 maybe_temp_loc,
4043 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004044 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004045 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004046 __ Cmp(out, cls);
4047 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004048 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004049 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004050 GenerateReferenceLoadOneRegister(instruction,
4051 out_loc,
4052 component_offset,
4053 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004054 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004055 // If `out` is null, we use it for the result, and jump to `done`.
4056 __ Cbz(out, &done);
4057 __ Ldrh(out, HeapOperand(out, primitive_offset));
4058 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4059 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004060 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004061 __ Mov(out, 1);
4062 __ B(&done);
4063 break;
4064 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004065
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004066 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004067 // No read barrier since the slow path will retry upon failure.
4068 // /* HeapReference<Class> */ out = obj->klass_
4069 GenerateReferenceLoadTwoRegisters(instruction,
4070 out_loc,
4071 obj_loc,
4072 class_offset,
4073 maybe_temp_loc,
4074 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004075 __ Cmp(out, cls);
4076 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004077 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4078 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004079 codegen_->AddSlowPath(slow_path);
4080 __ B(ne, slow_path->GetEntryLabel());
4081 __ Mov(out, 1);
4082 if (zero.IsLinked()) {
4083 __ B(&done);
4084 }
4085 break;
4086 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004087
Calin Juravle98893e12015-10-02 21:05:03 +01004088 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004089 case TypeCheckKind::kInterfaceCheck: {
4090 // Note that we indeed only call on slow path, but we always go
4091 // into the slow path for the unresolved and interface check
4092 // cases.
4093 //
4094 // We cannot directly call the InstanceofNonTrivial runtime
4095 // entry point without resorting to a type checking slow path
4096 // here (i.e. by calling InvokeRuntime directly), as it would
4097 // require to assign fixed registers for the inputs of this
4098 // HInstanceOf instruction (following the runtime calling
4099 // convention), which might be cluttered by the potential first
4100 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004101 //
4102 // TODO: Introduce a new runtime entry point taking the object
4103 // to test (instead of its class) as argument, and let it deal
4104 // with the read barrier issues. This will let us refactor this
4105 // case of the `switch` code as it was previously (with a direct
4106 // call to the runtime not using a type checking slow path).
4107 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004108 DCHECK(locations->OnlyCallsOnSlowPath());
4109 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4110 /* is_fatal */ false);
4111 codegen_->AddSlowPath(slow_path);
4112 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004113 if (zero.IsLinked()) {
4114 __ B(&done);
4115 }
4116 break;
4117 }
4118 }
4119
4120 if (zero.IsLinked()) {
4121 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004122 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004123 }
4124
4125 if (done.IsLinked()) {
4126 __ Bind(&done);
4127 }
4128
4129 if (slow_path != nullptr) {
4130 __ Bind(slow_path->GetExitLabel());
4131 }
4132}
4133
4134void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4135 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4136 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4137
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004138 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4139 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004140 case TypeCheckKind::kExactCheck:
4141 case TypeCheckKind::kAbstractClassCheck:
4142 case TypeCheckKind::kClassHierarchyCheck:
4143 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004144 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4145 LocationSummary::kCallOnSlowPath :
4146 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004147 break;
4148 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004149 case TypeCheckKind::kUnresolvedCheck:
4150 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004151 call_kind = LocationSummary::kCallOnSlowPath;
4152 break;
4153 }
4154
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004155 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4156 locations->SetInAt(0, Location::RequiresRegister());
4157 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004158 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4159 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004160}
4161
4162void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004163 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004164 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004165 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004166 Register obj = InputRegisterAt(instruction, 0);
4167 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004168 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4169 DCHECK_GE(num_temps, 1u);
4170 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004171 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004172 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4173 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004174 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004175 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4176 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4177 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4178 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4179 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4180 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4181 const uint32_t object_array_data_offset =
4182 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004183
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004184 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004185 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4186 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4187 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004188 if (!kEmitCompilerReadBarrier) {
4189 is_type_check_slow_path_fatal =
4190 (type_check_kind == TypeCheckKind::kExactCheck ||
4191 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4192 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4193 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4194 !instruction->CanThrowIntoCatchBlock();
4195 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004196 SlowPathCodeARM64* type_check_slow_path =
4197 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4198 is_type_check_slow_path_fatal);
4199 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004200
Scott Wakeling97c72b72016-06-24 16:19:36 +01004201 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004202 // Avoid null check if we know obj is not null.
4203 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004204 __ Cbz(obj, &done);
4205 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004206
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004207 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004208 case TypeCheckKind::kExactCheck:
4209 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004210 // /* HeapReference<Class> */ temp = obj->klass_
4211 GenerateReferenceLoadTwoRegisters(instruction,
4212 temp_loc,
4213 obj_loc,
4214 class_offset,
4215 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004216 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004217
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004218 __ Cmp(temp, cls);
4219 // Jump to slow path for throwing the exception or doing a
4220 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004221 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004222 break;
4223 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004224
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004225 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004226 // /* HeapReference<Class> */ temp = obj->klass_
4227 GenerateReferenceLoadTwoRegisters(instruction,
4228 temp_loc,
4229 obj_loc,
4230 class_offset,
4231 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004232 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004233
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004234 // If the class is abstract, we eagerly fetch the super class of the
4235 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004236 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004237 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004238 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004239 GenerateReferenceLoadOneRegister(instruction,
4240 temp_loc,
4241 super_offset,
4242 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004243 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004244
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004245 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4246 // exception.
4247 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4248 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004249 __ Cmp(temp, cls);
4250 __ B(ne, &loop);
4251 break;
4252 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004253
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004254 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004255 // /* HeapReference<Class> */ temp = obj->klass_
4256 GenerateReferenceLoadTwoRegisters(instruction,
4257 temp_loc,
4258 obj_loc,
4259 class_offset,
4260 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004261 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004262
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004263 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004264 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004265 __ Bind(&loop);
4266 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004267 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004268
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004269 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004270 GenerateReferenceLoadOneRegister(instruction,
4271 temp_loc,
4272 super_offset,
4273 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004274 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004275
4276 // If the class reference currently in `temp` is not null, jump
4277 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004278 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004279 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004280 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004281 break;
4282 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004283
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004284 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004285 // /* HeapReference<Class> */ temp = obj->klass_
4286 GenerateReferenceLoadTwoRegisters(instruction,
4287 temp_loc,
4288 obj_loc,
4289 class_offset,
4290 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004291 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004292
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004293 // Do an exact check.
4294 __ Cmp(temp, cls);
4295 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004296
4297 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004298 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004299 GenerateReferenceLoadOneRegister(instruction,
4300 temp_loc,
4301 component_offset,
4302 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004303 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004304
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004305 // If the component type is null, jump to the slow path to throw the exception.
4306 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4307 // Otherwise, the object is indeed an array. Further check that this component type is not a
4308 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004309 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4310 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004311 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004312 break;
4313 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004314
Calin Juravle98893e12015-10-02 21:05:03 +01004315 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004316 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004317 //
4318 // We cannot directly call the CheckCast runtime entry point
4319 // without resorting to a type checking slow path here (i.e. by
4320 // calling InvokeRuntime directly), as it would require to
4321 // assign fixed registers for the inputs of this HInstanceOf
4322 // instruction (following the runtime calling convention), which
4323 // might be cluttered by the potential first read barrier
4324 // emission at the beginning of this method.
4325 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004326 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004327 case TypeCheckKind::kInterfaceCheck: {
4328 // /* HeapReference<Class> */ temp = obj->klass_
4329 GenerateReferenceLoadTwoRegisters(instruction,
4330 temp_loc,
4331 obj_loc,
4332 class_offset,
4333 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004334 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004335
4336 // /* HeapReference<Class> */ temp = temp->iftable_
4337 GenerateReferenceLoadTwoRegisters(instruction,
4338 temp_loc,
4339 temp_loc,
4340 iftable_offset,
4341 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004342 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004343 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004344 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004345 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004346 vixl::aarch64::Label start_loop;
4347 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004348 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004349 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4350 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004351 // Go to next interface.
4352 __ Add(temp, temp, 2 * kHeapReferenceSize);
4353 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004354 // Compare the classes and continue the loop if they do not match.
4355 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4356 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004357 break;
4358 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004359 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004360 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004361
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004362 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004363}
4364
Alexandre Rames5319def2014-10-23 10:03:10 +01004365void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4366 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4367 locations->SetOut(Location::ConstantLocation(constant));
4368}
4369
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004370void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004371 // Will be generated at use site.
4372}
4373
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004374void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4375 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4376 locations->SetOut(Location::ConstantLocation(constant));
4377}
4378
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004379void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004380 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004381}
4382
Calin Juravle175dc732015-08-25 15:42:32 +01004383void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4384 // The trampoline uses the same calling convention as dex calling conventions,
4385 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4386 // the method_idx.
4387 HandleInvoke(invoke);
4388}
4389
4390void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4391 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4392}
4393
Alexandre Rames5319def2014-10-23 10:03:10 +01004394void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004395 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004396 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004397}
4398
Alexandre Rames67555f72014-11-18 10:55:16 +00004399void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4400 HandleInvoke(invoke);
4401}
4402
4403void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4404 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004405 LocationSummary* locations = invoke->GetLocations();
4406 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004407 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004408 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004409 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004410
4411 // The register ip1 is required to be used for the hidden argument in
4412 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004413 MacroAssembler* masm = GetVIXLAssembler();
4414 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004415 scratch_scope.Exclude(ip1);
4416 __ Mov(ip1, invoke->GetDexMethodIndex());
4417
Artem Serov914d7a82017-02-07 14:33:49 +00004418 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004419 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004420 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004421 {
4422 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4423 // /* HeapReference<Class> */ temp = temp->klass_
4424 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4425 codegen_->MaybeRecordImplicitNullCheck(invoke);
4426 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004427 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004428 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004429 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004430 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004431 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004432 }
Artem Serov914d7a82017-02-07 14:33:49 +00004433
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004434 // Instead of simply (possibly) unpoisoning `temp` here, we should
4435 // emit a read barrier for the previous class reference load.
4436 // However this is not required in practice, as this is an
4437 // intermediate/temporary reference and because the current
4438 // concurrent copying collector keeps the from-space memory
4439 // intact/accessible until the end of the marking phase (the
4440 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004441 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004442 __ Ldr(temp,
4443 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4444 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004445 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004446 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004447 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004448 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004449 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004450
4451 {
4452 // Ensure the pc position is recorded immediately after the `blr` instruction.
4453 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4454
4455 // lr();
4456 __ blr(lr);
4457 DCHECK(!codegen_->IsLeafMethod());
4458 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4459 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004460}
4461
4462void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004463 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004464 if (intrinsic.TryDispatch(invoke)) {
4465 return;
4466 }
4467
Alexandre Rames67555f72014-11-18 10:55:16 +00004468 HandleInvoke(invoke);
4469}
4470
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004471void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004472 // Explicit clinit checks triggered by static invokes must have been pruned by
4473 // art::PrepareForRegisterAllocation.
4474 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004475
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004476 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004477 if (intrinsic.TryDispatch(invoke)) {
4478 return;
4479 }
4480
Alexandre Rames67555f72014-11-18 10:55:16 +00004481 HandleInvoke(invoke);
4482}
4483
Andreas Gampe878d58c2015-01-15 23:24:00 -08004484static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4485 if (invoke->GetLocations()->Intrinsified()) {
4486 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4487 intrinsic.Dispatch(invoke);
4488 return true;
4489 }
4490 return false;
4491}
4492
Vladimir Markodc151b22015-10-15 18:02:30 +01004493HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4494 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004495 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004496 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004497 return desired_dispatch_info;
4498}
4499
Vladimir Markod254f5c2017-06-02 15:18:36 +00004500void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004501 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004502 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4503 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004504 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4505 uint32_t offset =
4506 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004507 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004508 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004509 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004510 }
Vladimir Marko58155012015-08-19 12:49:41 +00004511 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004512 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004513 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004514 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4515 DCHECK(GetCompilerOptions().IsBootImage());
4516 // Add ADRP with its PC-relative method patch.
4517 vixl::aarch64::Label* adrp_label = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
4518 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4519 // Add ADD with its PC-relative method patch.
4520 vixl::aarch64::Label* add_label =
4521 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), adrp_label);
4522 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4523 break;
4524 }
Vladimir Marko58155012015-08-19 12:49:41 +00004525 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4526 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004527 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004528 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004529 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4530 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004531 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004532 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004533 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004534 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004535 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004536 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004537 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004538 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004539 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004540 }
Vladimir Marko58155012015-08-19 12:49:41 +00004541 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004542 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004543 Register reg = XRegisterFrom(temp);
4544 Register method_reg;
4545 if (current_method.IsRegister()) {
4546 method_reg = XRegisterFrom(current_method);
4547 } else {
4548 DCHECK(invoke->GetLocations()->Intrinsified());
4549 DCHECK(!current_method.IsValid());
4550 method_reg = reg;
4551 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4552 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004553
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004554 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004555 __ Ldr(reg.X(),
4556 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004557 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004558 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004559 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4560 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004561 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4562 break;
4563 }
4564 }
4565
4566 switch (invoke->GetCodePtrLocation()) {
4567 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4568 __ Bl(&frame_entry_label_);
4569 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004570 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4571 // LR = callee_method->entry_point_from_quick_compiled_code_;
4572 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004573 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004574 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004575 {
4576 // To ensure that the pc position is recorded immediately after the `blr` instruction
4577 // BLR must be the last instruction emitted in this function.
4578 // Recording the pc will occur right after returning from this function.
4579 ExactAssemblyScope eas(GetVIXLAssembler(),
4580 kInstructionSize,
4581 CodeBufferCheckScope::kExactSize);
4582 // lr()
4583 __ blr(lr);
4584 }
Vladimir Marko58155012015-08-19 12:49:41 +00004585 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004586 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004587
Andreas Gampe878d58c2015-01-15 23:24:00 -08004588 DCHECK(!IsLeafMethod());
4589}
4590
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004591void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004592 // Use the calling convention instead of the location of the receiver, as
4593 // intrinsics may have put the receiver in a different register. In the intrinsics
4594 // slow path, the arguments have been moved to the right place, so here we are
4595 // guaranteed that the receiver is the first register of the calling convention.
4596 InvokeDexCallingConvention calling_convention;
4597 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004598 Register temp = XRegisterFrom(temp_in);
4599 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4600 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4601 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004602 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004603
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004604 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004605
4606 {
4607 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4608 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4609 // /* HeapReference<Class> */ temp = receiver->klass_
4610 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4611 MaybeRecordImplicitNullCheck(invoke);
4612 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004613 // Instead of simply (possibly) unpoisoning `temp` here, we should
4614 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004615 // intermediate/temporary reference and because the current
4616 // concurrent copying collector keeps the from-space memory
4617 // intact/accessible until the end of the marking phase (the
4618 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004619 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4620 // temp = temp->GetMethodAt(method_offset);
4621 __ Ldr(temp, MemOperand(temp, method_offset));
4622 // lr = temp->GetEntryPoint();
4623 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004624 {
4625 // To ensure that the pc position is recorded immediately after the `blr` instruction
4626 // BLR should be the last instruction emitted in this function.
4627 // Recording the pc will occur right after returning from this function.
4628 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4629 // lr();
4630 __ blr(lr);
4631 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004632}
4633
Orion Hodsonac141392017-01-13 11:53:47 +00004634void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4635 HandleInvoke(invoke);
4636}
4637
4638void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4639 codegen_->GenerateInvokePolymorphicCall(invoke);
4640}
4641
Vladimir Marko65979462017-05-19 17:25:12 +01004642vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeMethodPatch(
4643 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004644 vixl::aarch64::Label* adrp_label) {
Vladimir Marko65979462017-05-19 17:25:12 +01004645 return NewPcRelativePatch(*target_method.dex_file,
4646 target_method.dex_method_index,
4647 adrp_label,
4648 &pc_relative_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004649}
4650
Scott Wakeling97c72b72016-06-24 16:19:36 +01004651vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4652 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004653 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004654 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004655 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004656}
4657
Vladimir Marko1998cd02017-01-13 13:02:58 +00004658vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4659 const DexFile& dex_file,
4660 dex::TypeIndex type_index,
4661 vixl::aarch64::Label* adrp_label) {
4662 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4663}
4664
Vladimir Marko65979462017-05-19 17:25:12 +01004665vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4666 const DexFile& dex_file,
4667 dex::StringIndex string_index,
4668 vixl::aarch64::Label* adrp_label) {
4669 return
4670 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
4671}
4672
Scott Wakeling97c72b72016-06-24 16:19:36 +01004673vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4674 const DexFile& dex_file,
4675 uint32_t element_offset,
4676 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004677 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4678}
4679
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004680vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4681 baker_read_barrier_patches_.emplace_back(custom_data);
4682 return &baker_read_barrier_patches_.back().label;
4683}
4684
Scott Wakeling97c72b72016-06-24 16:19:36 +01004685vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4686 const DexFile& dex_file,
4687 uint32_t offset_or_index,
4688 vixl::aarch64::Label* adrp_label,
4689 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004690 // Add a patch entry and return the label.
4691 patches->emplace_back(dex_file, offset_or_index);
4692 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004693 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004694 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4695 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4696 return label;
4697}
4698
Scott Wakeling97c72b72016-06-24 16:19:36 +01004699vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4700 uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00004701 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004702}
4703
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004704vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004705 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4706 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4707 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004708 return jit_string_patches_.GetOrCreate(
4709 StringReference(&dex_file, string_index),
4710 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4711}
4712
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004713vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004714 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4715 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4716 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004717 return jit_class_patches_.GetOrCreate(
4718 TypeReference(&dex_file, type_index),
4719 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4720}
4721
Vladimir Markoaad75c62016-10-03 08:46:48 +00004722void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4723 vixl::aarch64::Register reg) {
4724 DCHECK(reg.IsX());
4725 SingleEmissionCheckScope guard(GetVIXLAssembler());
4726 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004727 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004728}
4729
4730void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4731 vixl::aarch64::Register out,
4732 vixl::aarch64::Register base) {
4733 DCHECK(out.IsX());
4734 DCHECK(base.IsX());
4735 SingleEmissionCheckScope guard(GetVIXLAssembler());
4736 __ Bind(fixup_label);
4737 __ add(out, base, Operand(/* offset placeholder */ 0));
4738}
4739
4740void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4741 vixl::aarch64::Register out,
4742 vixl::aarch64::Register base) {
4743 DCHECK(base.IsX());
4744 SingleEmissionCheckScope guard(GetVIXLAssembler());
4745 __ Bind(fixup_label);
4746 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4747}
4748
4749template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4750inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4751 const ArenaDeque<PcRelativePatchInfo>& infos,
4752 ArenaVector<LinkerPatch>* linker_patches) {
4753 for (const PcRelativePatchInfo& info : infos) {
4754 linker_patches->push_back(Factory(info.label.GetLocation(),
4755 &info.target_dex_file,
4756 info.pc_insn_label->GetLocation(),
4757 info.offset_or_index));
4758 }
4759}
4760
Vladimir Marko58155012015-08-19 12:49:41 +00004761void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4762 DCHECK(linker_patches->empty());
4763 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004764 pc_relative_dex_cache_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004765 pc_relative_method_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004766 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004767 type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004768 pc_relative_string_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004769 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004770 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004771 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004772 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004773 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004774 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004775 info.offset_or_index));
4776 }
Vladimir Marko65979462017-05-19 17:25:12 +01004777 if (GetCompilerOptions().IsBootImage()) {
4778 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004779 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004780 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4781 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004782 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4783 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004784 } else {
4785 DCHECK(pc_relative_method_patches_.empty());
4786 DCHECK(pc_relative_type_patches_.empty());
4787 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4788 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004789 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004790 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4791 linker_patches);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004792 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
4793 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
4794 info.custom_data));
4795 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004796 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004797}
4798
Scott Wakeling97c72b72016-06-24 16:19:36 +01004799vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004800 Uint32ToLiteralMap* map) {
4801 return map->GetOrCreate(
4802 value,
4803 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4804}
4805
Scott Wakeling97c72b72016-06-24 16:19:36 +01004806vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004807 return uint64_literals_.GetOrCreate(
4808 value,
4809 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004810}
4811
Andreas Gampe878d58c2015-01-15 23:24:00 -08004812void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004813 // Explicit clinit checks triggered by static invokes must have been pruned by
4814 // art::PrepareForRegisterAllocation.
4815 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004816
Andreas Gampe878d58c2015-01-15 23:24:00 -08004817 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4818 return;
4819 }
4820
Artem Serov914d7a82017-02-07 14:33:49 +00004821 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4822 // are no pools emitted.
4823 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004824 LocationSummary* locations = invoke->GetLocations();
4825 codegen_->GenerateStaticOrDirectCall(
4826 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004827 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004828}
4829
4830void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004831 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4832 return;
4833 }
4834
Artem Serov914d7a82017-02-07 14:33:49 +00004835 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4836 // are no pools emitted.
4837 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004838 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004839 DCHECK(!codegen_->IsLeafMethod());
4840 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4841}
4842
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004843HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4844 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004845 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004846 case HLoadClass::LoadKind::kInvalid:
4847 LOG(FATAL) << "UNREACHABLE";
4848 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004849 case HLoadClass::LoadKind::kReferrersClass:
4850 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004851 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004852 case HLoadClass::LoadKind::kBssEntry:
4853 DCHECK(!Runtime::Current()->UseJitCompilation());
4854 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004855 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004856 DCHECK(Runtime::Current()->UseJitCompilation());
4857 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004858 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004859 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004860 break;
4861 }
4862 return desired_class_load_kind;
4863}
4864
Alexandre Rames67555f72014-11-18 10:55:16 +00004865void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004866 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004867 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004868 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004869 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004870 cls,
4871 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004872 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004873 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004874 return;
4875 }
Vladimir Marko41559982017-01-06 14:04:23 +00004876 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004877
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004878 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4879 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004880 ? LocationSummary::kCallOnSlowPath
4881 : LocationSummary::kNoCall;
4882 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004883 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004884 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004885 }
4886
Vladimir Marko41559982017-01-06 14:04:23 +00004887 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004888 locations->SetInAt(0, Location::RequiresRegister());
4889 }
4890 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004891 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4892 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4893 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004894 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004895 RegisterSet caller_saves = RegisterSet::Empty();
4896 InvokeRuntimeCallingConvention calling_convention;
4897 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4898 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4899 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4900 Primitive::kPrimNot).GetCode());
4901 locations->SetCustomSlowPathCallerSaves(caller_saves);
4902 } else {
4903 // For non-Baker read barrier we have a temp-clobbering call.
4904 }
4905 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004906}
4907
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004908// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4909// move.
4910void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004911 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004912 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00004913 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004914 return;
4915 }
Vladimir Marko41559982017-01-06 14:04:23 +00004916 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004917
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004918 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004919 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004920 Register bss_entry_temp;
4921 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004922
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004923 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4924 ? kWithoutReadBarrier
4925 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004926 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004927 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004928 case HLoadClass::LoadKind::kReferrersClass: {
4929 DCHECK(!cls->CanCallRuntime());
4930 DCHECK(!cls->MustGenerateClinitCheck());
4931 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4932 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004933 GenerateGcRootFieldLoad(cls,
4934 out_loc,
4935 current_method,
4936 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004937 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004938 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004939 break;
4940 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004941 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004942 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004943 // Add ADRP with its PC-relative type patch.
4944 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004945 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004946 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004947 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004948 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004949 vixl::aarch64::Label* add_label =
4950 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004951 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004952 break;
4953 }
4954 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004955 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004956 uint32_t address = dchecked_integral_cast<uint32_t>(
4957 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4958 DCHECK_NE(address, 0u);
4959 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004960 break;
4961 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004962 case HLoadClass::LoadKind::kBssEntry: {
4963 // Add ADRP with its PC-relative Class .bss entry patch.
4964 const DexFile& dex_file = cls->GetDexFile();
4965 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004966 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004967 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4968 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004969 // Add LDR with its PC-relative Class patch.
4970 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004971 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004972 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4973 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004974 out_loc,
4975 bss_entry_temp,
4976 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004977 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004978 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004979 generate_null_check = true;
4980 break;
4981 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004982 case HLoadClass::LoadKind::kJitTableAddress: {
4983 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4984 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004985 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004986 GenerateGcRootFieldLoad(cls,
4987 out_loc,
4988 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004989 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004990 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004991 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004992 break;
4993 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004994 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004995 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004996 LOG(FATAL) << "UNREACHABLE";
4997 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004998 }
4999
Vladimir Markoea4c1262017-02-06 19:59:33 +00005000 bool do_clinit = cls->MustGenerateClinitCheck();
5001 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005002 DCHECK(cls->CanCallRuntime());
5003 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00005004 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005005 codegen_->AddSlowPath(slow_path);
5006 if (generate_null_check) {
5007 __ Cbz(out, slow_path->GetEntryLabel());
5008 }
5009 if (cls->MustGenerateClinitCheck()) {
5010 GenerateClassInitializationCheck(slow_path, out);
5011 } else {
5012 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005013 }
5014 }
5015}
5016
David Brazdilcb1c0552015-08-04 16:22:25 +01005017static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005018 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005019}
5020
Alexandre Rames67555f72014-11-18 10:55:16 +00005021void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5022 LocationSummary* locations =
5023 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5024 locations->SetOut(Location::RequiresRegister());
5025}
5026
5027void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005028 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5029}
5030
5031void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
5032 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5033}
5034
5035void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5036 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005037}
5038
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005039HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5040 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005041 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005042 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005043 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005044 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005045 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005046 case HLoadString::LoadKind::kJitTableAddress:
5047 DCHECK(Runtime::Current()->UseJitCompilation());
5048 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005049 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005050 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005051 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005052 }
5053 return desired_string_load_kind;
5054}
5055
Alexandre Rames67555f72014-11-18 10:55:16 +00005056void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005057 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005058 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005059 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005060 InvokeRuntimeCallingConvention calling_convention;
5061 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5062 } else {
5063 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005064 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5065 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005066 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005067 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005068 RegisterSet caller_saves = RegisterSet::Empty();
5069 InvokeRuntimeCallingConvention calling_convention;
5070 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5071 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
5072 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
5073 Primitive::kPrimNot).GetCode());
5074 locations->SetCustomSlowPathCallerSaves(caller_saves);
5075 } else {
5076 // For non-Baker read barrier we have a temp-clobbering call.
5077 }
5078 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005079 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005080}
5081
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005082// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5083// move.
5084void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005085 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005086 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005087
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005088 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005089 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005090 // Add ADRP with its PC-relative String patch.
5091 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005092 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005093 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01005094 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005095 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005096 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005097 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005098 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005099 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005100 return; // No dex cache slow path.
5101 }
5102 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005103 uint32_t address = dchecked_integral_cast<uint32_t>(
5104 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5105 DCHECK_NE(address, 0u);
5106 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005107 return; // No dex cache slow path.
5108 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005109 case HLoadString::LoadKind::kBssEntry: {
5110 // Add ADRP with its PC-relative String .bss entry patch.
5111 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005112 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005113 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005114 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005115 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005116 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005117 // Add LDR with its PC-relative String patch.
5118 vixl::aarch64::Label* ldr_label =
5119 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005120 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005121 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005122 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005123 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005124 /* offset placeholder */ 0u,
5125 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005126 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005127 SlowPathCodeARM64* slow_path =
5128 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005129 codegen_->AddSlowPath(slow_path);
5130 __ Cbz(out.X(), slow_path->GetEntryLabel());
5131 __ Bind(slow_path->GetExitLabel());
5132 return;
5133 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005134 case HLoadString::LoadKind::kJitTableAddress: {
5135 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005136 load->GetStringIndex(),
5137 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005138 GenerateGcRootFieldLoad(load,
5139 out_loc,
5140 out.X(),
5141 /* offset */ 0,
5142 /* fixup_label */ nullptr,
5143 kCompilerReadBarrierOption);
5144 return;
5145 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005146 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005147 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005148 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005149
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005150 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005151 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005152 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005153 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005154 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5155 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005156}
5157
Alexandre Rames5319def2014-10-23 10:03:10 +01005158void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5159 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5160 locations->SetOut(Location::ConstantLocation(constant));
5161}
5162
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005163void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005164 // Will be generated at use site.
5165}
5166
Alexandre Rames67555f72014-11-18 10:55:16 +00005167void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5168 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005169 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005170 InvokeRuntimeCallingConvention calling_convention;
5171 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5172}
5173
5174void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005175 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005176 instruction,
5177 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005178 if (instruction->IsEnter()) {
5179 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5180 } else {
5181 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5182 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005183}
5184
Alexandre Rames42d641b2014-10-27 14:00:51 +00005185void LocationsBuilderARM64::VisitMul(HMul* mul) {
5186 LocationSummary* locations =
5187 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5188 switch (mul->GetResultType()) {
5189 case Primitive::kPrimInt:
5190 case Primitive::kPrimLong:
5191 locations->SetInAt(0, Location::RequiresRegister());
5192 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005193 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005194 break;
5195
5196 case Primitive::kPrimFloat:
5197 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005198 locations->SetInAt(0, Location::RequiresFpuRegister());
5199 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005200 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005201 break;
5202
5203 default:
5204 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5205 }
5206}
5207
5208void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5209 switch (mul->GetResultType()) {
5210 case Primitive::kPrimInt:
5211 case Primitive::kPrimLong:
5212 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5213 break;
5214
5215 case Primitive::kPrimFloat:
5216 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005217 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005218 break;
5219
5220 default:
5221 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5222 }
5223}
5224
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005225void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5226 LocationSummary* locations =
5227 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5228 switch (neg->GetResultType()) {
5229 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005230 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005231 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005232 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005233 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005234
5235 case Primitive::kPrimFloat:
5236 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005237 locations->SetInAt(0, Location::RequiresFpuRegister());
5238 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005239 break;
5240
5241 default:
5242 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5243 }
5244}
5245
5246void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5247 switch (neg->GetResultType()) {
5248 case Primitive::kPrimInt:
5249 case Primitive::kPrimLong:
5250 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5251 break;
5252
5253 case Primitive::kPrimFloat:
5254 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005255 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005256 break;
5257
5258 default:
5259 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5260 }
5261}
5262
5263void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5264 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005265 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005266 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005267 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005268 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5269 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005270}
5271
5272void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005273 // Note: if heap poisoning is enabled, the entry point takes cares
5274 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005275 QuickEntrypointEnum entrypoint =
5276 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5277 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005278 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005279}
5280
Alexandre Rames5319def2014-10-23 10:03:10 +01005281void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5282 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005283 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005284 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005285 if (instruction->IsStringAlloc()) {
5286 locations->AddTemp(LocationFrom(kArtMethodRegister));
5287 } else {
5288 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005289 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005290 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5291}
5292
5293void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005294 // Note: if heap poisoning is enabled, the entry point takes cares
5295 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005296 if (instruction->IsStringAlloc()) {
5297 // String is allocated through StringFactory. Call NewEmptyString entry point.
5298 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005299 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005300 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5301 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005302
5303 {
5304 // Ensure the pc position is recorded immediately after the `blr` instruction.
5305 ExactAssemblyScope eas(GetVIXLAssembler(),
5306 kInstructionSize,
5307 CodeBufferCheckScope::kExactSize);
5308 __ blr(lr);
5309 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5310 }
David Brazdil6de19382016-01-08 17:37:10 +00005311 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005312 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005313 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005314 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005315}
5316
5317void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5318 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005319 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005320 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005321}
5322
5323void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005324 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005325 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005326 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005327 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005328 break;
5329
5330 default:
5331 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5332 }
5333}
5334
David Brazdil66d126e2015-04-03 16:02:44 +01005335void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5336 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5337 locations->SetInAt(0, Location::RequiresRegister());
5338 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5339}
5340
5341void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005342 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005343}
5344
Alexandre Rames5319def2014-10-23 10:03:10 +01005345void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005346 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5347 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005348}
5349
Calin Juravle2ae48182016-03-16 14:05:09 +00005350void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5351 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005352 return;
5353 }
Artem Serov914d7a82017-02-07 14:33:49 +00005354 {
5355 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5356 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5357 Location obj = instruction->GetLocations()->InAt(0);
5358 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5359 RecordPcInfo(instruction, instruction->GetDexPc());
5360 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005361}
5362
Calin Juravle2ae48182016-03-16 14:05:09 +00005363void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005364 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005365 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005366
5367 LocationSummary* locations = instruction->GetLocations();
5368 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005369
5370 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005371}
5372
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005373void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005374 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005375}
5376
Alexandre Rames67555f72014-11-18 10:55:16 +00005377void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5378 HandleBinaryOp(instruction);
5379}
5380
5381void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5382 HandleBinaryOp(instruction);
5383}
5384
Alexandre Rames3e69f162014-12-10 10:36:50 +00005385void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5386 LOG(FATAL) << "Unreachable";
5387}
5388
5389void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5390 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5391}
5392
Alexandre Rames5319def2014-10-23 10:03:10 +01005393void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5394 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5395 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5396 if (location.IsStackSlot()) {
5397 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5398 } else if (location.IsDoubleStackSlot()) {
5399 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5400 }
5401 locations->SetOut(location);
5402}
5403
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005404void InstructionCodeGeneratorARM64::VisitParameterValue(
5405 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005406 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005407}
5408
5409void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5410 LocationSummary* locations =
5411 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005412 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005413}
5414
5415void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5416 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5417 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005418}
5419
5420void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5421 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005422 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005423 locations->SetInAt(i, Location::Any());
5424 }
5425 locations->SetOut(Location::Any());
5426}
5427
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005428void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005429 LOG(FATAL) << "Unreachable";
5430}
5431
Serban Constantinescu02164b32014-11-13 14:05:07 +00005432void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005433 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005434 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005435 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5436 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005437 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5438
5439 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005440 case Primitive::kPrimInt:
5441 case Primitive::kPrimLong:
5442 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005443 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005444 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5445 break;
5446
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005447 case Primitive::kPrimFloat:
5448 case Primitive::kPrimDouble: {
5449 InvokeRuntimeCallingConvention calling_convention;
5450 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5451 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5452 locations->SetOut(calling_convention.GetReturnLocation(type));
5453
5454 break;
5455 }
5456
Serban Constantinescu02164b32014-11-13 14:05:07 +00005457 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005458 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005459 }
5460}
5461
5462void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5463 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005464
Serban Constantinescu02164b32014-11-13 14:05:07 +00005465 switch (type) {
5466 case Primitive::kPrimInt:
5467 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005468 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005469 break;
5470 }
5471
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005472 case Primitive::kPrimFloat:
5473 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005474 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5475 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005476 if (type == Primitive::kPrimFloat) {
5477 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5478 } else {
5479 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5480 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005481 break;
5482 }
5483
Serban Constantinescu02164b32014-11-13 14:05:07 +00005484 default:
5485 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005486 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005487 }
5488}
5489
Igor Murashkind01745e2017-04-05 16:40:31 -07005490void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5491 constructor_fence->SetLocations(nullptr);
5492}
5493
5494void InstructionCodeGeneratorARM64::VisitConstructorFence(
5495 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5496 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5497}
5498
Calin Juravle27df7582015-04-17 19:12:31 +01005499void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5500 memory_barrier->SetLocations(nullptr);
5501}
5502
5503void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005504 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005505}
5506
Alexandre Rames5319def2014-10-23 10:03:10 +01005507void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5508 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5509 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005510 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005511}
5512
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005513void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005514 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005515}
5516
5517void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5518 instruction->SetLocations(nullptr);
5519}
5520
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005521void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005522 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005523}
5524
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005525void LocationsBuilderARM64::VisitRor(HRor* ror) {
5526 HandleBinaryOp(ror);
5527}
5528
5529void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5530 HandleBinaryOp(ror);
5531}
5532
Serban Constantinescu02164b32014-11-13 14:05:07 +00005533void LocationsBuilderARM64::VisitShl(HShl* shl) {
5534 HandleShift(shl);
5535}
5536
5537void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5538 HandleShift(shl);
5539}
5540
5541void LocationsBuilderARM64::VisitShr(HShr* shr) {
5542 HandleShift(shr);
5543}
5544
5545void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5546 HandleShift(shr);
5547}
5548
Alexandre Rames5319def2014-10-23 10:03:10 +01005549void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005550 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005551}
5552
5553void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005554 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005555}
5556
Alexandre Rames67555f72014-11-18 10:55:16 +00005557void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005558 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005559}
5560
5561void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005562 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005563}
5564
5565void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005566 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005567}
5568
Alexandre Rames67555f72014-11-18 10:55:16 +00005569void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005570 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005571}
5572
Calin Juravlee460d1d2015-09-29 04:52:17 +01005573void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5574 HUnresolvedInstanceFieldGet* instruction) {
5575 FieldAccessCallingConventionARM64 calling_convention;
5576 codegen_->CreateUnresolvedFieldLocationSummary(
5577 instruction, instruction->GetFieldType(), calling_convention);
5578}
5579
5580void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5581 HUnresolvedInstanceFieldGet* instruction) {
5582 FieldAccessCallingConventionARM64 calling_convention;
5583 codegen_->GenerateUnresolvedFieldAccess(instruction,
5584 instruction->GetFieldType(),
5585 instruction->GetFieldIndex(),
5586 instruction->GetDexPc(),
5587 calling_convention);
5588}
5589
5590void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5591 HUnresolvedInstanceFieldSet* instruction) {
5592 FieldAccessCallingConventionARM64 calling_convention;
5593 codegen_->CreateUnresolvedFieldLocationSummary(
5594 instruction, instruction->GetFieldType(), calling_convention);
5595}
5596
5597void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5598 HUnresolvedInstanceFieldSet* instruction) {
5599 FieldAccessCallingConventionARM64 calling_convention;
5600 codegen_->GenerateUnresolvedFieldAccess(instruction,
5601 instruction->GetFieldType(),
5602 instruction->GetFieldIndex(),
5603 instruction->GetDexPc(),
5604 calling_convention);
5605}
5606
5607void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5608 HUnresolvedStaticFieldGet* instruction) {
5609 FieldAccessCallingConventionARM64 calling_convention;
5610 codegen_->CreateUnresolvedFieldLocationSummary(
5611 instruction, instruction->GetFieldType(), calling_convention);
5612}
5613
5614void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5615 HUnresolvedStaticFieldGet* instruction) {
5616 FieldAccessCallingConventionARM64 calling_convention;
5617 codegen_->GenerateUnresolvedFieldAccess(instruction,
5618 instruction->GetFieldType(),
5619 instruction->GetFieldIndex(),
5620 instruction->GetDexPc(),
5621 calling_convention);
5622}
5623
5624void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5625 HUnresolvedStaticFieldSet* instruction) {
5626 FieldAccessCallingConventionARM64 calling_convention;
5627 codegen_->CreateUnresolvedFieldLocationSummary(
5628 instruction, instruction->GetFieldType(), calling_convention);
5629}
5630
5631void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5632 HUnresolvedStaticFieldSet* instruction) {
5633 FieldAccessCallingConventionARM64 calling_convention;
5634 codegen_->GenerateUnresolvedFieldAccess(instruction,
5635 instruction->GetFieldType(),
5636 instruction->GetFieldIndex(),
5637 instruction->GetDexPc(),
5638 calling_convention);
5639}
5640
Alexandre Rames5319def2014-10-23 10:03:10 +01005641void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005642 LocationSummary* locations =
5643 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005644 // In suspend check slow path, usually there are no caller-save registers at all.
5645 // If SIMD instructions are present, however, we force spilling all live SIMD
5646 // registers in full width (since the runtime only saves/restores lower part).
5647 locations->SetCustomSlowPathCallerSaves(
5648 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005649}
5650
5651void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005652 HBasicBlock* block = instruction->GetBlock();
5653 if (block->GetLoopInformation() != nullptr) {
5654 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5655 // The back edge will generate the suspend check.
5656 return;
5657 }
5658 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5659 // The goto will generate the suspend check.
5660 return;
5661 }
5662 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005663}
5664
Alexandre Rames67555f72014-11-18 10:55:16 +00005665void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5666 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005667 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005668 InvokeRuntimeCallingConvention calling_convention;
5669 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5670}
5671
5672void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005673 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005674 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005675}
5676
5677void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5678 LocationSummary* locations =
5679 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5680 Primitive::Type input_type = conversion->GetInputType();
5681 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005682 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005683 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5684 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5685 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5686 }
5687
Alexandre Rames542361f2015-01-29 16:57:31 +00005688 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005689 locations->SetInAt(0, Location::RequiresFpuRegister());
5690 } else {
5691 locations->SetInAt(0, Location::RequiresRegister());
5692 }
5693
Alexandre Rames542361f2015-01-29 16:57:31 +00005694 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005695 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5696 } else {
5697 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5698 }
5699}
5700
5701void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5702 Primitive::Type result_type = conversion->GetResultType();
5703 Primitive::Type input_type = conversion->GetInputType();
5704
5705 DCHECK_NE(input_type, result_type);
5706
Alexandre Rames542361f2015-01-29 16:57:31 +00005707 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005708 int result_size = Primitive::ComponentSize(result_type);
5709 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005710 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005711 Register output = OutputRegister(conversion);
5712 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005713 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005714 // 'int' values are used directly as W registers, discarding the top
5715 // bits, so we don't need to sign-extend and can just perform a move.
5716 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5717 // top 32 bits of the target register. We theoretically could leave those
5718 // bits unchanged, but we would have to make sure that no code uses a
5719 // 32bit input value as a 64bit value assuming that the top 32 bits are
5720 // zero.
5721 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005722 } else if (result_type == Primitive::kPrimChar ||
5723 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5724 __ Ubfx(output,
5725 output.IsX() ? source.X() : source.W(),
5726 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005727 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005728 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005729 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005730 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005731 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005732 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005733 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5734 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005735 } else if (Primitive::IsFloatingPointType(result_type) &&
5736 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005737 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5738 } else {
5739 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5740 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005741 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005742}
Alexandre Rames67555f72014-11-18 10:55:16 +00005743
Serban Constantinescu02164b32014-11-13 14:05:07 +00005744void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5745 HandleShift(ushr);
5746}
5747
5748void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5749 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005750}
5751
5752void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5753 HandleBinaryOp(instruction);
5754}
5755
5756void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5757 HandleBinaryOp(instruction);
5758}
5759
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005760void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005761 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005762 LOG(FATAL) << "Unreachable";
5763}
5764
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005765void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005766 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005767 LOG(FATAL) << "Unreachable";
5768}
5769
Mark Mendellfe57faa2015-09-18 09:26:15 -04005770// Simple implementation of packed switch - generate cascaded compare/jumps.
5771void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5772 LocationSummary* locations =
5773 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5774 locations->SetInAt(0, Location::RequiresRegister());
5775}
5776
5777void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5778 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005779 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005780 Register value_reg = InputRegisterAt(switch_instr, 0);
5781 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5782
Zheng Xu3927c8b2015-11-18 17:46:25 +08005783 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005784 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005785 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5786 // make sure we don't emit it if the target may run out of range.
5787 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5788 // ranges and emit the tables only as required.
5789 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005790
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005791 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005792 // Current instruction id is an upper bound of the number of HIRs in the graph.
5793 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5794 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005795 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5796 Register temp = temps.AcquireW();
5797 __ Subs(temp, value_reg, Operand(lower_bound));
5798
Zheng Xu3927c8b2015-11-18 17:46:25 +08005799 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005800 // Jump to successors[0] if value == lower_bound.
5801 __ B(eq, codegen_->GetLabelOf(successors[0]));
5802 int32_t last_index = 0;
5803 for (; num_entries - last_index > 2; last_index += 2) {
5804 __ Subs(temp, temp, Operand(2));
5805 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5806 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5807 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5808 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5809 }
5810 if (num_entries - last_index == 2) {
5811 // The last missing case_value.
5812 __ Cmp(temp, Operand(1));
5813 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005814 }
5815
5816 // And the default for any other value.
5817 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5818 __ B(codegen_->GetLabelOf(default_block));
5819 }
5820 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005821 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005822
5823 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5824
5825 // Below instructions should use at most one blocked register. Since there are two blocked
5826 // registers, we are free to block one.
5827 Register temp_w = temps.AcquireW();
5828 Register index;
5829 // Remove the bias.
5830 if (lower_bound != 0) {
5831 index = temp_w;
5832 __ Sub(index, value_reg, Operand(lower_bound));
5833 } else {
5834 index = value_reg;
5835 }
5836
5837 // Jump to default block if index is out of the range.
5838 __ Cmp(index, Operand(num_entries));
5839 __ B(hs, codegen_->GetLabelOf(default_block));
5840
5841 // In current VIXL implementation, it won't require any blocked registers to encode the
5842 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5843 // register pressure.
5844 Register table_base = temps.AcquireX();
5845 // Load jump offset from the table.
5846 __ Adr(table_base, jump_table->GetTableStartLabel());
5847 Register jump_offset = temp_w;
5848 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5849
5850 // Jump to target block by branching to table_base(pc related) + offset.
5851 Register target_address = table_base;
5852 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5853 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005854 }
5855}
5856
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005857void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5858 HInstruction* instruction,
5859 Location out,
5860 uint32_t offset,
5861 Location maybe_temp,
5862 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005863 Primitive::Type type = Primitive::kPrimNot;
5864 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005865 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005866 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005867 if (kUseBakerReadBarrier) {
5868 // Load with fast path based Baker's read barrier.
5869 // /* HeapReference<Object> */ out = *(out + offset)
5870 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5871 out,
5872 out_reg,
5873 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005874 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005875 /* needs_null_check */ false,
5876 /* use_load_acquire */ false);
5877 } else {
5878 // Load with slow path based read barrier.
5879 // Save the value of `out` into `maybe_temp` before overwriting it
5880 // in the following move operation, as we will need it for the
5881 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005882 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005883 __ Mov(temp_reg, out_reg);
5884 // /* HeapReference<Object> */ out = *(out + offset)
5885 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5886 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5887 }
5888 } else {
5889 // Plain load with no read barrier.
5890 // /* HeapReference<Object> */ out = *(out + offset)
5891 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5892 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5893 }
5894}
5895
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005896void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5897 HInstruction* instruction,
5898 Location out,
5899 Location obj,
5900 uint32_t offset,
5901 Location maybe_temp,
5902 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005903 Primitive::Type type = Primitive::kPrimNot;
5904 Register out_reg = RegisterFrom(out, type);
5905 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005906 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005907 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005908 if (kUseBakerReadBarrier) {
5909 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005910 // /* HeapReference<Object> */ out = *(obj + offset)
5911 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5912 out,
5913 obj_reg,
5914 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005915 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005916 /* needs_null_check */ false,
5917 /* use_load_acquire */ false);
5918 } else {
5919 // Load with slow path based read barrier.
5920 // /* HeapReference<Object> */ out = *(obj + offset)
5921 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5922 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5923 }
5924 } else {
5925 // Plain load with no read barrier.
5926 // /* HeapReference<Object> */ out = *(obj + offset)
5927 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5928 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5929 }
5930}
5931
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005932void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5933 HInstruction* instruction,
5934 Location root,
5935 Register obj,
5936 uint32_t offset,
5937 vixl::aarch64::Label* fixup_label,
5938 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005939 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005940 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005941 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005942 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005943 if (kUseBakerReadBarrier) {
5944 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005945 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005946 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5947 !Runtime::Current()->UseJitCompilation()) {
5948 // Note that we do not actually check the value of `GetIsGcMarking()`
5949 // to decide whether to mark the loaded GC root or not. Instead, we
Vladimir Marko66d691d2017-04-07 17:53:39 +01005950 // load into `temp` (actually IP1) the read barrier mark introspection
5951 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
5952 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005953 //
5954 // We use link-time generated thunks for the slow path. That thunk
5955 // checks the reference and jumps to the entrypoint if needed.
5956 //
5957 // temp = Thread::Current()->pReadBarrierMarkIntrospection
5958 // lr = &return_address;
5959 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5960 // if (temp != nullptr) {
5961 // goto gc_root_thunk<root_reg>(lr)
5962 // }
5963 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00005964
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005965 UseScratchRegisterScope temps(GetVIXLAssembler());
5966 DCHECK(temps.IsAvailable(ip0));
5967 DCHECK(temps.IsAvailable(ip1));
5968 temps.Exclude(ip0, ip1);
5969 uint32_t custom_data =
5970 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
5971 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00005972
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005973 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
5974 DCHECK_EQ(ip0.GetCode(), 16u);
5975 const int32_t entry_point_offset =
5976 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
5977 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
5978 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5979 vixl::aarch64::Label return_address;
5980 __ adr(lr, &return_address);
5981 if (fixup_label != nullptr) {
5982 __ Bind(fixup_label);
5983 }
5984 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
5985 "GC root LDR must be 2 instruction (8B) before the return address label.");
5986 __ ldr(root_reg, MemOperand(obj.X(), offset));
5987 __ Bind(cbnz_label);
5988 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
5989 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005990 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005991 // Note that we do not actually check the value of
5992 // `GetIsGcMarking()` to decide whether to mark the loaded GC
5993 // root or not. Instead, we load into `temp` the read barrier
5994 // mark entry point corresponding to register `root`. If `temp`
5995 // is null, it means that `GetIsGcMarking()` is false, and vice
5996 // versa.
5997 //
5998 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5999 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6000 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6001 // // Slow path.
6002 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
6003 // }
Roland Levillain44015862016-01-22 11:47:17 +00006004
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006005 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
6006 Register temp = lr;
6007 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
6008 instruction, root, /* entrypoint */ LocationFrom(temp));
6009 codegen_->AddSlowPath(slow_path);
6010
6011 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6012 const int32_t entry_point_offset =
6013 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
6014 // Loading the entrypoint does not require a load acquire since it is only changed when
6015 // threads are suspended or running a checkpoint.
6016 __ Ldr(temp, MemOperand(tr, entry_point_offset));
6017
6018 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6019 if (fixup_label == nullptr) {
6020 __ Ldr(root_reg, MemOperand(obj, offset));
6021 } else {
6022 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
6023 }
6024 static_assert(
6025 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6026 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6027 "have different sizes.");
6028 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6029 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6030 "have different sizes.");
6031
6032 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6033 // checking GetIsGcMarking.
6034 __ Cbnz(temp, slow_path->GetEntryLabel());
6035 __ Bind(slow_path->GetExitLabel());
6036 }
Roland Levillain44015862016-01-22 11:47:17 +00006037 } else {
6038 // GC root loaded through a slow path for read barriers other
6039 // than Baker's.
6040 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006041 if (fixup_label == nullptr) {
6042 __ Add(root_reg.X(), obj.X(), offset);
6043 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006044 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006045 }
Roland Levillain44015862016-01-22 11:47:17 +00006046 // /* mirror::Object* */ root = root->Read()
6047 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6048 }
6049 } else {
6050 // Plain GC root load with no read barrier.
6051 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006052 if (fixup_label == nullptr) {
6053 __ Ldr(root_reg, MemOperand(obj, offset));
6054 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006055 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006056 }
Roland Levillain44015862016-01-22 11:47:17 +00006057 // Note that GC roots are not affected by heap poisoning, thus we
6058 // do not have to unpoison `root_reg` here.
6059 }
6060}
6061
6062void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6063 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006064 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006065 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006066 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006067 bool needs_null_check,
6068 bool use_load_acquire) {
6069 DCHECK(kEmitCompilerReadBarrier);
6070 DCHECK(kUseBakerReadBarrier);
6071
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006072 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6073 !use_load_acquire &&
6074 !Runtime::Current()->UseJitCompilation()) {
6075 // Note that we do not actually check the value of `GetIsGcMarking()`
Vladimir Marko66d691d2017-04-07 17:53:39 +01006076 // to decide whether to mark the loaded reference or not. Instead, we
6077 // load into `temp` (actually IP1) the read barrier mark introspection
6078 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6079 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006080 //
6081 // We use link-time generated thunks for the slow path. That thunk checks
6082 // the holder and jumps to the entrypoint if needed. If the holder is not
6083 // gray, it creates a fake dependency and returns to the LDR instruction.
6084 //
6085 // temp = Thread::Current()->pReadBarrierMarkIntrospection
Vladimir Marko66d691d2017-04-07 17:53:39 +01006086 // lr = &gray_return_address;
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006087 // if (temp != nullptr) {
6088 // goto field_thunk<holder_reg, base_reg>(lr)
6089 // }
6090 // not_gray_return_address:
6091 // // Original reference load. If the offset is too large to fit
6092 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006093 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006094 // gray_return_address:
6095
6096 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6097 Register base = obj;
6098 if (offset >= kReferenceLoadMinFarOffset) {
6099 DCHECK(maybe_temp.IsRegister());
6100 base = WRegisterFrom(maybe_temp);
6101 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6102 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6103 offset &= (kReferenceLoadMinFarOffset - 1u);
6104 }
6105 UseScratchRegisterScope temps(GetVIXLAssembler());
6106 DCHECK(temps.IsAvailable(ip0));
6107 DCHECK(temps.IsAvailable(ip1));
6108 temps.Exclude(ip0, ip1);
6109 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6110 base.GetCode(),
6111 obj.GetCode());
6112 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6113
6114 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6115 DCHECK_EQ(ip0.GetCode(), 16u);
6116 const int32_t entry_point_offset =
6117 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6118 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
Vladimir Markod1ef8732017-04-18 13:55:13 +01006119 EmissionCheckScope guard(GetVIXLAssembler(),
6120 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006121 vixl::aarch64::Label return_address;
6122 __ adr(lr, &return_address);
6123 __ Bind(cbnz_label);
6124 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markod1ef8732017-04-18 13:55:13 +01006125 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6126 "Field LDR must be 1 instruction (4B) before the return address label; "
6127 " 2 instructions (8B) for heap poisoning.");
6128 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6129 __ ldr(ref_reg, MemOperand(base.X(), offset));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006130 if (needs_null_check) {
6131 MaybeRecordImplicitNullCheck(instruction);
6132 }
Vladimir Markod1ef8732017-04-18 13:55:13 +01006133 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006134 __ Bind(&return_address);
6135 return;
6136 }
6137
Roland Levillain44015862016-01-22 11:47:17 +00006138 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006139 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006140 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006141 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006142 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6143 ref,
6144 obj,
6145 offset,
6146 no_index,
6147 no_scale_factor,
6148 temp,
6149 needs_null_check,
6150 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006151}
6152
6153void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6154 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006155 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006156 uint32_t data_offset,
6157 Location index,
6158 Register temp,
6159 bool needs_null_check) {
6160 DCHECK(kEmitCompilerReadBarrier);
6161 DCHECK(kUseBakerReadBarrier);
6162
Vladimir Marko66d691d2017-04-07 17:53:39 +01006163 static_assert(
6164 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6165 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6166 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
6167
6168 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6169 !Runtime::Current()->UseJitCompilation()) {
6170 // Note that we do not actually check the value of `GetIsGcMarking()`
6171 // to decide whether to mark the loaded reference or not. Instead, we
6172 // load into `temp` (actually IP1) the read barrier mark introspection
6173 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6174 // false, and vice versa.
6175 //
6176 // We use link-time generated thunks for the slow path. That thunk checks
6177 // the holder and jumps to the entrypoint if needed. If the holder is not
6178 // gray, it creates a fake dependency and returns to the LDR instruction.
6179 //
6180 // temp = Thread::Current()->pReadBarrierMarkIntrospection
6181 // lr = &gray_return_address;
6182 // if (temp != nullptr) {
6183 // goto field_thunk<holder_reg, base_reg>(lr)
6184 // }
6185 // not_gray_return_address:
6186 // // Original reference load. If the offset is too large to fit
6187 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006188 // HeapReference<mirror::Object> reference = data[index];
Vladimir Marko66d691d2017-04-07 17:53:39 +01006189 // gray_return_address:
6190
6191 DCHECK(index.IsValid());
6192 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
6193 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6194
6195 UseScratchRegisterScope temps(GetVIXLAssembler());
6196 DCHECK(temps.IsAvailable(ip0));
6197 DCHECK(temps.IsAvailable(ip1));
6198 temps.Exclude(ip0, ip1);
6199 uint32_t custom_data =
6200 linker::Arm64RelativePatcher::EncodeBakerReadBarrierArrayData(temp.GetCode());
6201 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6202
6203 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6204 DCHECK_EQ(ip0.GetCode(), 16u);
6205 const int32_t entry_point_offset =
6206 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6207 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
6208 __ Add(temp.X(), obj.X(), Operand(data_offset));
6209 EmissionCheckScope guard(GetVIXLAssembler(),
6210 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6211 vixl::aarch64::Label return_address;
6212 __ adr(lr, &return_address);
6213 __ Bind(cbnz_label);
6214 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6215 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6216 "Array LDR must be 1 instruction (4B) before the return address label; "
6217 " 2 instructions (8B) for heap poisoning.");
6218 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6219 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6220 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6221 __ Bind(&return_address);
6222 return;
6223 }
6224
Roland Levillain44015862016-01-22 11:47:17 +00006225 // Array cells are never volatile variables, therefore array loads
6226 // never use Load-Acquire instructions on ARM64.
6227 const bool use_load_acquire = false;
6228
6229 // /* HeapReference<Object> */ ref =
6230 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006231 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6232 ref,
6233 obj,
6234 data_offset,
6235 index,
6236 scale_factor,
6237 temp,
6238 needs_null_check,
6239 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006240}
6241
6242void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6243 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006244 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006245 uint32_t offset,
6246 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006247 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006248 Register temp,
6249 bool needs_null_check,
Roland Levillainff487002017-03-07 16:50:01 +00006250 bool use_load_acquire) {
Roland Levillain44015862016-01-22 11:47:17 +00006251 DCHECK(kEmitCompilerReadBarrier);
6252 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006253 // If we are emitting an array load, we should not be using a
6254 // Load Acquire instruction. In other words:
6255 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6256 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006257
Roland Levillain54f869e2017-03-06 13:54:11 +00006258 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6259 // whether we need to enter the slow path to mark the reference.
6260 // Then, in the slow path, check the gray bit in the lock word of
6261 // the reference's holder (`obj`) to decide whether to mark `ref` or
6262 // not.
Roland Levillain44015862016-01-22 11:47:17 +00006263 //
Roland Levillainba650a42017-03-06 13:52:32 +00006264 // Note that we do not actually check the value of `GetIsGcMarking()`;
6265 // instead, we load into `temp2` the read barrier mark entry point
6266 // corresponding to register `ref`. If `temp2` is null, it means
6267 // that `GetIsGcMarking()` is false, and vice versa.
6268 //
6269 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00006270 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6271 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006272 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6273 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6274 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6275 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6276 // if (is_gray) {
6277 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6278 // }
6279 // } else {
6280 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006281 // }
Roland Levillain44015862016-01-22 11:47:17 +00006282
Roland Levillainba650a42017-03-06 13:52:32 +00006283 // Slow path marking the object `ref` when the GC is marking. The
6284 // entrypoint will already be loaded in `temp2`.
6285 Register temp2 = lr;
6286 Location temp2_loc = LocationFrom(temp2);
Roland Levillainff487002017-03-07 16:50:01 +00006287 SlowPathCodeARM64* slow_path =
6288 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6289 instruction,
6290 ref,
6291 obj,
6292 offset,
6293 index,
6294 scale_factor,
6295 needs_null_check,
6296 use_load_acquire,
6297 temp,
6298 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006299 AddSlowPath(slow_path);
6300
6301 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6302 const int32_t entry_point_offset =
6303 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6304 // Loading the entrypoint does not require a load acquire since it is only changed when
6305 // threads are suspended or running a checkpoint.
6306 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006307 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6308 // checking GetIsGcMarking.
6309 __ Cbnz(temp2, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006310 // Fast path: the GC is not marking: just load the reference.
Roland Levillain54f869e2017-03-06 13:54:11 +00006311 GenerateRawReferenceLoad(
6312 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006313 __ Bind(slow_path->GetExitLabel());
6314}
6315
Roland Levillainff487002017-03-07 16:50:01 +00006316void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6317 Location ref,
6318 Register obj,
6319 Location field_offset,
6320 Register temp,
6321 bool needs_null_check,
6322 bool use_load_acquire) {
6323 DCHECK(kEmitCompilerReadBarrier);
6324 DCHECK(kUseBakerReadBarrier);
6325 // If we are emitting an array load, we should not be using a
6326 // Load Acquire instruction. In other words:
6327 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6328 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6329
6330 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6331 // whether we need to enter the slow path to update the reference
6332 // field within `obj`. Then, in the slow path, check the gray bit
6333 // in the lock word of the reference's holder (`obj`) to decide
6334 // whether to mark `ref` and update the field or not.
6335 //
6336 // Note that we do not actually check the value of `GetIsGcMarking()`;
6337 // instead, we load into `temp2` the read barrier mark entry point
6338 // corresponding to register `ref`. If `temp2` is null, it means
6339 // that `GetIsGcMarking()` is false, and vice versa.
6340 //
6341 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6342 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6343 // // Slow path.
6344 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6345 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6346 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6347 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6348 // if (is_gray) {
6349 // old_ref = ref;
6350 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6351 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6352 // }
6353 // }
6354
6355 // Slow path updating the object reference at address `obj + field_offset`
6356 // when the GC is marking. The entrypoint will already be loaded in `temp2`.
6357 Register temp2 = lr;
6358 Location temp2_loc = LocationFrom(temp2);
6359 SlowPathCodeARM64* slow_path =
6360 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6361 instruction,
6362 ref,
6363 obj,
6364 /* offset */ 0u,
6365 /* index */ field_offset,
6366 /* scale_factor */ 0u /* "times 1" */,
6367 needs_null_check,
6368 use_load_acquire,
6369 temp,
6370 /* entrypoint */ temp2_loc);
6371 AddSlowPath(slow_path);
6372
6373 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6374 const int32_t entry_point_offset =
6375 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6376 // Loading the entrypoint does not require a load acquire since it is only changed when
6377 // threads are suspended or running a checkpoint.
6378 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
6379 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6380 // checking GetIsGcMarking.
6381 __ Cbnz(temp2, slow_path->GetEntryLabel());
6382 // Fast path: the GC is not marking: nothing to do (the field is
6383 // up-to-date, and we don't need to load the reference).
6384 __ Bind(slow_path->GetExitLabel());
6385}
6386
Roland Levillainba650a42017-03-06 13:52:32 +00006387void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6388 Location ref,
6389 Register obj,
6390 uint32_t offset,
6391 Location index,
6392 size_t scale_factor,
6393 bool needs_null_check,
6394 bool use_load_acquire) {
6395 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006396 Primitive::Type type = Primitive::kPrimNot;
6397 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006398
Roland Levillainba650a42017-03-06 13:52:32 +00006399 // If needed, vixl::EmissionCheckScope guards are used to ensure
6400 // that no pools are emitted between the load (macro) instruction
6401 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006402
Roland Levillain44015862016-01-22 11:47:17 +00006403 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006404 // Load types involving an "index": ArrayGet,
6405 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6406 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006407 if (use_load_acquire) {
6408 // UnsafeGetObjectVolatile intrinsic case.
6409 // Register `index` is not an index in an object array, but an
6410 // offset to an object reference field within object `obj`.
6411 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6412 DCHECK(instruction->GetLocations()->Intrinsified());
6413 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6414 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006415 DCHECK_EQ(offset, 0u);
6416 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006417 DCHECK_EQ(needs_null_check, false);
6418 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006419 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6420 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006421 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006422 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6423 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006424 if (index.IsConstant()) {
6425 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006426 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006427 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006428 if (needs_null_check) {
6429 MaybeRecordImplicitNullCheck(instruction);
6430 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006431 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006432 UseScratchRegisterScope temps(GetVIXLAssembler());
6433 Register temp = temps.AcquireW();
6434 __ Add(temp, obj, offset);
6435 {
6436 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6437 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6438 if (needs_null_check) {
6439 MaybeRecordImplicitNullCheck(instruction);
6440 }
6441 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006442 }
Roland Levillain44015862016-01-22 11:47:17 +00006443 }
Roland Levillain44015862016-01-22 11:47:17 +00006444 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006445 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006446 MemOperand field = HeapOperand(obj, offset);
6447 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006448 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6449 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006450 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006451 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006452 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006453 if (needs_null_check) {
6454 MaybeRecordImplicitNullCheck(instruction);
6455 }
Roland Levillain44015862016-01-22 11:47:17 +00006456 }
6457 }
6458
6459 // Object* ref = ref_addr->AsMirrorPtr()
6460 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006461}
6462
6463void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6464 Location out,
6465 Location ref,
6466 Location obj,
6467 uint32_t offset,
6468 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006469 DCHECK(kEmitCompilerReadBarrier);
6470
Roland Levillain44015862016-01-22 11:47:17 +00006471 // Insert a slow path based read barrier *after* the reference load.
6472 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006473 // If heap poisoning is enabled, the unpoisoning of the loaded
6474 // reference will be carried out by the runtime within the slow
6475 // path.
6476 //
6477 // Note that `ref` currently does not get unpoisoned (when heap
6478 // poisoning is enabled), which is alright as the `ref` argument is
6479 // not used by the artReadBarrierSlow entry point.
6480 //
6481 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6482 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6483 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6484 AddSlowPath(slow_path);
6485
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006486 __ B(slow_path->GetEntryLabel());
6487 __ Bind(slow_path->GetExitLabel());
6488}
6489
Roland Levillain44015862016-01-22 11:47:17 +00006490void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6491 Location out,
6492 Location ref,
6493 Location obj,
6494 uint32_t offset,
6495 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006496 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006497 // Baker's read barriers shall be handled by the fast path
6498 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6499 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006500 // If heap poisoning is enabled, unpoisoning will be taken care of
6501 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006502 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006503 } else if (kPoisonHeapReferences) {
6504 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6505 }
6506}
6507
Roland Levillain44015862016-01-22 11:47:17 +00006508void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6509 Location out,
6510 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006511 DCHECK(kEmitCompilerReadBarrier);
6512
Roland Levillain44015862016-01-22 11:47:17 +00006513 // Insert a slow path based read barrier *after* the GC root load.
6514 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006515 // Note that GC roots are not affected by heap poisoning, so we do
6516 // not need to do anything special for this here.
6517 SlowPathCodeARM64* slow_path =
6518 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6519 AddSlowPath(slow_path);
6520
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006521 __ B(slow_path->GetEntryLabel());
6522 __ Bind(slow_path->GetExitLabel());
6523}
6524
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006525void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6526 LocationSummary* locations =
6527 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6528 locations->SetInAt(0, Location::RequiresRegister());
6529 locations->SetOut(Location::RequiresRegister());
6530}
6531
6532void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6533 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006534 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006535 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006536 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006537 __ Ldr(XRegisterFrom(locations->Out()),
6538 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006539 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006540 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006541 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006542 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6543 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006544 __ Ldr(XRegisterFrom(locations->Out()),
6545 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006546 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006547}
6548
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006549static void PatchJitRootUse(uint8_t* code,
6550 const uint8_t* roots_data,
6551 vixl::aarch64::Literal<uint32_t>* literal,
6552 uint64_t index_in_table) {
6553 uint32_t literal_offset = literal->GetOffset();
6554 uintptr_t address =
6555 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6556 uint8_t* data = code + literal_offset;
6557 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6558}
6559
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006560void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6561 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006562 const StringReference& string_reference = entry.first;
6563 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
6564 const auto it = jit_string_roots_.find(string_reference);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006565 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006566 uint64_t index_in_table = it->second;
6567 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006568 }
6569 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006570 const TypeReference& type_reference = entry.first;
6571 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
6572 const auto it = jit_class_roots_.find(type_reference);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006573 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006574 uint64_t index_in_table = it->second;
6575 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006576 }
6577}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006578
Alexandre Rames67555f72014-11-18 10:55:16 +00006579#undef __
6580#undef QUICK_ENTRY_POINT
6581
Alexandre Rames5319def2014-10-23 10:03:10 +01006582} // namespace arm64
6583} // namespace art