blob: 8814cfc251d85bd39e27ff5f3cd88971e29278eb [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Vladimir Markof4f2daa2017-03-20 18:26:59 +000019#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000020#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070022#include "base/bit_utils.h"
23#include "base/bit_utils_iterator.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010024#include "class_table.h"
Zheng Xuc6667102015-05-15 16:08:45 +080025#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000026#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010027#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080028#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010029#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070030#include "heap_poisoning.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080031#include "intrinsics.h"
32#include "intrinsics_arm64.h"
Vladimir Markof4f2daa2017-03-20 18:26:59 +000033#include "linker/arm64/relative_patcher_arm64.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070034#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010035#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070036#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000037#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010038#include "thread.h"
39#include "utils/arm64/assembler_arm64.h"
40#include "utils/assembler.h"
41#include "utils/stack_checks.h"
42
Scott Wakeling97c72b72016-06-24 16:19:36 +010043using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000044using vixl::ExactAssemblyScope;
45using vixl::CodeBufferCheckScope;
46using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010047
48#ifdef __
49#error "ARM64 Codegen VIXL macro-assembler macro already defined."
50#endif
51
Alexandre Rames5319def2014-10-23 10:03:10 +010052namespace art {
53
Roland Levillain22ccc3a2015-11-24 13:10:05 +000054template<class MirrorType>
55class GcRoot;
56
Alexandre Rames5319def2014-10-23 10:03:10 +010057namespace arm64 {
58
Alexandre Ramesbe919d92016-08-23 18:33:36 +010059using helpers::ARM64EncodableConstantOrRegister;
60using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080061using helpers::CPURegisterFrom;
62using helpers::DRegisterFrom;
63using helpers::FPRegisterFrom;
64using helpers::HeapOperand;
65using helpers::HeapOperandFrom;
66using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010067using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080068using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010070using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080071using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010072using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080073using helpers::LocationFrom;
74using helpers::OperandFromMemOperand;
75using helpers::OutputCPURegister;
76using helpers::OutputFPRegister;
77using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010078using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080079using helpers::RegisterFrom;
80using helpers::StackOperandFrom;
81using helpers::VIXLRegCodeFromART;
82using helpers::WRegisterFrom;
83using helpers::XRegisterFrom;
84
Vladimir Markof3e0ee22015-12-17 15:23:13 +000085// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080086// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
87// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000088static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010089
Vladimir Markof4f2daa2017-03-20 18:26:59 +000090// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
91// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
92// For the Baker read barrier implementation using link-generated thunks we need to split
93// the offset explicitly.
94constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
95
96// Flags controlling the use of link-time generated thunks for Baker read barriers.
Vladimir Markod1ef8732017-04-18 13:55:13 +010097constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
Vladimir Marko66d691d2017-04-07 17:53:39 +010098constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
Vladimir Markod1ef8732017-04-18 13:55:13 +010099constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000100
101// Some instructions have special requirements for a temporary, for example
102// LoadClass/kBssEntry and LoadString/kBssEntry for Baker read barrier require
103// temp that's not an R0 (to avoid an extra move) and Baker read barrier field
104// loads with large offsets need a fixed register to limit the number of link-time
105// thunks we generate. For these and similar cases, we want to reserve a specific
106// register that's neither callee-save nor an argument register. We choose x15.
107inline Location FixedTempLocation() {
108 return Location::RegisterLocation(x15.GetCode());
109}
110
Alexandre Rames5319def2014-10-23 10:03:10 +0100111inline Condition ARM64Condition(IfCondition cond) {
112 switch (cond) {
113 case kCondEQ: return eq;
114 case kCondNE: return ne;
115 case kCondLT: return lt;
116 case kCondLE: return le;
117 case kCondGT: return gt;
118 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700119 case kCondB: return lo;
120 case kCondBE: return ls;
121 case kCondA: return hi;
122 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100123 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000124 LOG(FATAL) << "Unreachable";
125 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100126}
127
Vladimir Markod6e069b2016-01-18 11:11:01 +0000128inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
129 // The ARM64 condition codes can express all the necessary branches, see the
130 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
131 // There is no dex instruction or HIR that would need the missing conditions
132 // "equal or unordered" or "not equal".
133 switch (cond) {
134 case kCondEQ: return eq;
135 case kCondNE: return ne /* unordered */;
136 case kCondLT: return gt_bias ? cc : lt /* unordered */;
137 case kCondLE: return gt_bias ? ls : le /* unordered */;
138 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
139 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
140 default:
141 LOG(FATAL) << "UNREACHABLE";
142 UNREACHABLE();
143 }
144}
145
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000146Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000147 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
148 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
149 // but we use the exact registers for clarity.
150 if (return_type == Primitive::kPrimFloat) {
151 return LocationFrom(s0);
152 } else if (return_type == Primitive::kPrimDouble) {
153 return LocationFrom(d0);
154 } else if (return_type == Primitive::kPrimLong) {
155 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100156 } else if (return_type == Primitive::kPrimVoid) {
157 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000158 } else {
159 return LocationFrom(w0);
160 }
161}
162
Alexandre Rames5319def2014-10-23 10:03:10 +0100163Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000164 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100165}
166
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100167// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
168#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700169#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100170
Zheng Xuda403092015-04-24 17:35:39 +0800171// Calculate memory accessing operand for save/restore live registers.
172static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100173 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800174 int64_t spill_offset,
175 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100176 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
177 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
178 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800179 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100180 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800181 codegen->GetNumberOfFloatingPointRegisters()));
182
Vladimir Marko804b03f2016-09-14 16:26:36 +0100183 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100184 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
185 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800186
187 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
188 UseScratchRegisterScope temps(masm);
189
190 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100191 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
192 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800193 int64_t reg_size = kXRegSizeInBytes;
194 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
195 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100196 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800197 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
198 // If the offset does not fit in the instruction's immediate field, use an alternate register
199 // to compute the base address(float point registers spill base address).
200 Register new_base = temps.AcquireSameSizeAs(base);
201 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
202 base = new_base;
203 spill_offset = -core_spill_size;
204 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
205 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
206 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
207 }
208
209 if (is_save) {
210 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
211 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
212 } else {
213 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
214 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
215 }
216}
217
218void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800219 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100220 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
221 for (uint32_t i : LowToHighBits(core_spills)) {
222 // If the register holds an object, update the stack mask.
223 if (locations->RegisterContainsObject(i)) {
224 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800225 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100226 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
227 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
228 saved_core_stack_offsets_[i] = stack_offset;
229 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800230 }
231
Vladimir Marko804b03f2016-09-14 16:26:36 +0100232 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
233 for (uint32_t i : LowToHighBits(fp_spills)) {
234 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
235 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
236 saved_fpu_stack_offsets_[i] = stack_offset;
237 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800238 }
239
Vladimir Marko804b03f2016-09-14 16:26:36 +0100240 SaveRestoreLiveRegistersHelper(codegen,
241 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800242 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
243}
244
245void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100246 SaveRestoreLiveRegistersHelper(codegen,
247 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800248 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
249}
250
Alexandre Rames5319def2014-10-23 10:03:10 +0100251class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
252 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000253 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100254
Alexandre Rames67555f72014-11-18 10:55:16 +0000255 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100256 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000257 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100258
Alexandre Rames5319def2014-10-23 10:03:10 +0100259 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000260 if (instruction_->CanThrowIntoCatchBlock()) {
261 // Live registers will be restored in the catch block if caught.
262 SaveLiveRegisters(codegen, instruction_->GetLocations());
263 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000264 // We're moving two locations to locations that could overlap, so we need a parallel
265 // move resolver.
266 InvokeRuntimeCallingConvention calling_convention;
267 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100268 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
269 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000270 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
271 ? kQuickThrowStringBounds
272 : kQuickThrowArrayBounds;
273 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100274 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800275 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100276 }
277
Alexandre Rames8158f282015-08-07 10:26:17 +0100278 bool IsFatal() const OVERRIDE { return true; }
279
Alexandre Rames9931f312015-06-19 14:47:01 +0100280 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
281
Alexandre Rames5319def2014-10-23 10:03:10 +0100282 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100283 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
284};
285
Alexandre Rames67555f72014-11-18 10:55:16 +0000286class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
287 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000288 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000289
290 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
291 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
292 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000293 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800294 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000295 }
296
Alexandre Rames8158f282015-08-07 10:26:17 +0100297 bool IsFatal() const OVERRIDE { return true; }
298
Alexandre Rames9931f312015-06-19 14:47:01 +0100299 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
300
Alexandre Rames67555f72014-11-18 10:55:16 +0000301 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000302 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
303};
304
305class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
306 public:
307 LoadClassSlowPathARM64(HLoadClass* cls,
308 HInstruction* at,
309 uint32_t dex_pc,
Vladimir Markoea4c1262017-02-06 19:59:33 +0000310 bool do_clinit,
311 vixl::aarch64::Register bss_entry_temp = vixl::aarch64::Register(),
312 vixl::aarch64::Label* bss_entry_adrp_label = nullptr)
313 : SlowPathCodeARM64(at),
314 cls_(cls),
315 dex_pc_(dex_pc),
316 do_clinit_(do_clinit),
317 bss_entry_temp_(bss_entry_temp),
318 bss_entry_adrp_label_(bss_entry_adrp_label) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000319 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
320 }
321
322 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000323 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000324 Location out = locations->Out();
325 constexpr bool call_saves_everything_except_r0_ip0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexandre Rames67555f72014-11-18 10:55:16 +0000326 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
327
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000328 InvokeRuntimeCallingConvention calling_convention;
329 // For HLoadClass/kBssEntry/kSaveEverything, the page address of the entry is in a temp
330 // register, make sure it's not clobbered by the call or by saving/restoring registers.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000331 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
332 bool is_load_class_bss_entry =
333 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000334 if (is_load_class_bss_entry) {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000335 DCHECK(bss_entry_temp_.IsValid());
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000336 DCHECK(!bss_entry_temp_.Is(calling_convention.GetRegisterAt(0)));
337 DCHECK(
338 !UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(bss_entry_temp_));
Vladimir Markoea4c1262017-02-06 19:59:33 +0000339 }
340
Alexandre Rames67555f72014-11-18 10:55:16 +0000341 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000342 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000343
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000344 dex::TypeIndex type_index = cls_->GetTypeIndex();
345 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000346 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
347 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000348 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800349 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100350 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800351 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100352 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800353 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000354
355 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 if (out.IsValid()) {
357 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000358 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000359 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000360 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000361 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000362 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000363 if (is_load_class_bss_entry) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000364 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000365 const DexFile& dex_file = cls_->GetDexFile();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000366 if (call_saves_everything_except_r0_ip0) {
367 // The class entry page address was preserved in bss_entry_temp_ thanks to kSaveEverything.
368 } else {
369 // For non-Baker read barrier, we need to re-calculate the address of the class entry page.
370 bss_entry_adrp_label_ = arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
371 arm64_codegen->EmitAdrpPlaceholder(bss_entry_adrp_label_, bss_entry_temp_);
372 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000373 vixl::aarch64::Label* strp_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +0000374 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label_);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000375 {
376 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
377 __ Bind(strp_label);
378 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Markoea4c1262017-02-06 19:59:33 +0000379 MemOperand(bss_entry_temp_, /* offset placeholder */ 0));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000380 }
381 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000382 __ B(GetExitLabel());
383 }
384
Alexandre Rames9931f312015-06-19 14:47:01 +0100385 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
386
Alexandre Rames67555f72014-11-18 10:55:16 +0000387 private:
388 // The class this slow path will load.
389 HLoadClass* const cls_;
390
Alexandre Rames67555f72014-11-18 10:55:16 +0000391 // The dex PC of `at_`.
392 const uint32_t dex_pc_;
393
394 // Whether to initialize the class.
395 const bool do_clinit_;
396
Vladimir Markoea4c1262017-02-06 19:59:33 +0000397 // For HLoadClass/kBssEntry, the temp register and the label of the ADRP where it was loaded.
398 vixl::aarch64::Register bss_entry_temp_;
399 vixl::aarch64::Label* bss_entry_adrp_label_;
400
Alexandre Rames67555f72014-11-18 10:55:16 +0000401 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
402};
403
Vladimir Markoaad75c62016-10-03 08:46:48 +0000404class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
405 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100406 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
407 : SlowPathCodeARM64(instruction),
408 temp_(temp),
409 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000410
411 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
412 LocationSummary* locations = instruction_->GetLocations();
413 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
414 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
415
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000416 InvokeRuntimeCallingConvention calling_convention;
417 // Make sure `temp_` is not clobbered by the call or by saving/restoring registers.
418 DCHECK(temp_.IsValid());
419 DCHECK(!temp_.Is(calling_convention.GetRegisterAt(0)));
420 DCHECK(!UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(temp_));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100421
Vladimir Markoaad75c62016-10-03 08:46:48 +0000422 __ Bind(GetEntryLabel());
423 SaveLiveRegisters(codegen, locations);
424
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000425 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
426 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000427 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
428 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
429 Primitive::Type type = instruction_->GetType();
430 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
431
432 RestoreLiveRegisters(codegen, locations);
433
434 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000435 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100436 if (!kUseReadBarrier || kUseBakerReadBarrier) {
437 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
438 } else {
439 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100440 adrp_label_ = arm64_codegen->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100441 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
442 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000443 vixl::aarch64::Label* strp_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100444 arm64_codegen->NewStringBssEntryPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000445 {
446 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
447 __ Bind(strp_label);
448 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100449 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000450 }
451
452 __ B(GetExitLabel());
453 }
454
455 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
456
457 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100458 const Register temp_;
459 vixl::aarch64::Label* adrp_label_;
460
Vladimir Markoaad75c62016-10-03 08:46:48 +0000461 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
462};
463
Alexandre Rames5319def2014-10-23 10:03:10 +0100464class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
465 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000466 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100467
Alexandre Rames67555f72014-11-18 10:55:16 +0000468 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
469 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100470 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000471 if (instruction_->CanThrowIntoCatchBlock()) {
472 // Live registers will be restored in the catch block if caught.
473 SaveLiveRegisters(codegen, instruction_->GetLocations());
474 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000475 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
476 instruction_,
477 instruction_->GetDexPc(),
478 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800479 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100480 }
481
Alexandre Rames8158f282015-08-07 10:26:17 +0100482 bool IsFatal() const OVERRIDE { return true; }
483
Alexandre Rames9931f312015-06-19 14:47:01 +0100484 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
485
Alexandre Rames5319def2014-10-23 10:03:10 +0100486 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100487 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
488};
489
490class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
491 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100492 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000493 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100494
Alexandre Rames67555f72014-11-18 10:55:16 +0000495 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100496 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000497 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100498 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100499 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000500 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800501 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100502 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000503 if (successor_ == nullptr) {
504 __ B(GetReturnLabel());
505 } else {
506 __ B(arm64_codegen->GetLabelOf(successor_));
507 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100508 }
509
Scott Wakeling97c72b72016-06-24 16:19:36 +0100510 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100511 DCHECK(successor_ == nullptr);
512 return &return_label_;
513 }
514
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100515 HBasicBlock* GetSuccessor() const {
516 return successor_;
517 }
518
Alexandre Rames9931f312015-06-19 14:47:01 +0100519 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
520
Alexandre Rames5319def2014-10-23 10:03:10 +0100521 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100522 // If not null, the block to branch to after the suspend check.
523 HBasicBlock* const successor_;
524
525 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100526 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100527
528 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
529};
530
Alexandre Rames67555f72014-11-18 10:55:16 +0000531class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
532 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000533 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000534 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000535
536 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000537 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800538
Alexandre Rames3e69f162014-12-10 10:36:50 +0000539 DCHECK(instruction_->IsCheckCast()
540 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
541 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100542 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000543
Alexandre Rames67555f72014-11-18 10:55:16 +0000544 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000545
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000546 if (!is_fatal_) {
547 SaveLiveRegisters(codegen, locations);
548 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000549
550 // We're moving two locations to locations that could overlap, so we need a parallel
551 // move resolver.
552 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800553 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800554 LocationFrom(calling_convention.GetRegisterAt(0)),
555 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800556 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800557 LocationFrom(calling_convention.GetRegisterAt(1)),
558 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000559 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000560 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800561 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000562 Primitive::Type ret_type = instruction_->GetType();
563 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
564 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
565 } else {
566 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800567 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
568 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000569 }
570
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000571 if (!is_fatal_) {
572 RestoreLiveRegisters(codegen, locations);
573 __ B(GetExitLabel());
574 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000575 }
576
Alexandre Rames9931f312015-06-19 14:47:01 +0100577 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100578 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100579
Alexandre Rames67555f72014-11-18 10:55:16 +0000580 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000581 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000582
Alexandre Rames67555f72014-11-18 10:55:16 +0000583 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
584};
585
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700586class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
587 public:
Aart Bik42249c32016-01-07 15:33:50 -0800588 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000589 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700590
591 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800592 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700593 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100594 LocationSummary* locations = instruction_->GetLocations();
595 SaveLiveRegisters(codegen, locations);
596 InvokeRuntimeCallingConvention calling_convention;
597 __ Mov(calling_convention.GetRegisterAt(0),
598 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000599 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100600 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700601 }
602
Alexandre Rames9931f312015-06-19 14:47:01 +0100603 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
604
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700605 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700606 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
607};
608
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100609class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
610 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000611 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100612
613 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
614 LocationSummary* locations = instruction_->GetLocations();
615 __ Bind(GetEntryLabel());
616 SaveLiveRegisters(codegen, locations);
617
618 InvokeRuntimeCallingConvention calling_convention;
619 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
620 parallel_move.AddMove(
621 locations->InAt(0),
622 LocationFrom(calling_convention.GetRegisterAt(0)),
623 Primitive::kPrimNot,
624 nullptr);
625 parallel_move.AddMove(
626 locations->InAt(1),
627 LocationFrom(calling_convention.GetRegisterAt(1)),
628 Primitive::kPrimInt,
629 nullptr);
630 parallel_move.AddMove(
631 locations->InAt(2),
632 LocationFrom(calling_convention.GetRegisterAt(2)),
633 Primitive::kPrimNot,
634 nullptr);
635 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
636
637 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000638 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100639 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
640 RestoreLiveRegisters(codegen, locations);
641 __ B(GetExitLabel());
642 }
643
644 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
645
646 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100647 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
648};
649
Zheng Xu3927c8b2015-11-18 17:46:25 +0800650void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
651 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000652 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800653
654 // We are about to use the assembler to place literals directly. Make sure we have enough
655 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000656 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
657 num_entries * sizeof(int32_t),
658 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800659
660 __ Bind(&table_start_);
661 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
662 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100663 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800664 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100665 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800666 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
667 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
668 Literal<int32_t> literal(jump_offset);
669 __ place(&literal);
670 }
671}
672
Roland Levillain54f869e2017-03-06 13:54:11 +0000673// Abstract base class for read barrier slow paths marking a reference
674// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000675//
Roland Levillain54f869e2017-03-06 13:54:11 +0000676// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100677// barrier marking runtime entry point to be invoked or an empty
678// location; in the latter case, the read barrier marking runtime
679// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000680class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
681 protected:
682 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
683 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000684 DCHECK(kEmitCompilerReadBarrier);
685 }
686
Roland Levillain54f869e2017-03-06 13:54:11 +0000687 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000688
Roland Levillain54f869e2017-03-06 13:54:11 +0000689 // Generate assembly code calling the read barrier marking runtime
690 // entry point (ReadBarrierMarkRegX).
691 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000692 // No need to save live registers; it's taken care of by the
693 // entrypoint. Also, there is no need to update the stack mask,
694 // as this runtime call will not trigger a garbage collection.
695 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
696 DCHECK_NE(ref_.reg(), LR);
697 DCHECK_NE(ref_.reg(), WSP);
698 DCHECK_NE(ref_.reg(), WZR);
699 // IP0 is used internally by the ReadBarrierMarkRegX entry point
700 // as a temporary, it cannot be the entry point's input/output.
701 DCHECK_NE(ref_.reg(), IP0);
702 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
703 // "Compact" slow path, saving two moves.
704 //
705 // Instead of using the standard runtime calling convention (input
706 // and output in W0):
707 //
708 // W0 <- ref
709 // W0 <- ReadBarrierMark(W0)
710 // ref <- W0
711 //
712 // we just use rX (the register containing `ref`) as input and output
713 // of a dedicated entrypoint:
714 //
715 // rX <- ReadBarrierMarkRegX(rX)
716 //
717 if (entrypoint_.IsValid()) {
718 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
719 __ Blr(XRegisterFrom(entrypoint_));
720 } else {
721 // Entrypoint is not already loaded, load from the thread.
722 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100723 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000724 // This runtime call does not require a stack map.
725 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
726 }
727 }
728
729 // The location (register) of the marked object reference.
730 const Location ref_;
731
732 // The location of the entrypoint if it is already loaded.
733 const Location entrypoint_;
734
Roland Levillain54f869e2017-03-06 13:54:11 +0000735 private:
736 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
737};
738
Alexandre Rames5319def2014-10-23 10:03:10 +0100739// Slow path marking an object reference `ref` during a read
740// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000741// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100742//
743// This means that after the execution of this slow path, `ref` will
744// always be up-to-date, but `obj.field` may not; i.e., after the
745// flip, `ref` will be a to-space reference, but `obj.field` will
746// probably still be a from-space reference (unless it gets updated by
747// another thread, or if another thread installed another object
748// reference (different from `ref`) in `obj.field`).
749//
Roland Levillain97c46462017-05-11 14:04:03 +0100750// Argument `entrypoint` must be a register location holding the read
751// barrier marking runtime entry point to be invoked or an empty
752// location; in the latter case, the read barrier marking runtime
753// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000754class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100755 public:
756 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
757 Location ref,
758 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000759 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100760 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100761 }
762
763 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
764
765 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000766 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100767 DCHECK(locations->CanCall());
768 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000769 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000770 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
771 << "Unexpected instruction in read barrier marking slow path: "
772 << instruction_->DebugName();
773
774 __ Bind(GetEntryLabel());
775 GenerateReadBarrierMarkRuntimeCall(codegen);
776 __ B(GetExitLabel());
777 }
778
779 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000780 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
781};
782
Roland Levillain54f869e2017-03-06 13:54:11 +0000783// Slow path loading `obj`'s lock word, loading a reference from
784// object `*(obj + offset + (index << scale_factor))` into `ref`, and
785// marking `ref` if `obj` is gray according to the lock word (Baker
786// read barrier). The field `obj.field` in the object `obj` holding
787// this reference does not get updated by this slow path after marking
788// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
789// below for that).
790//
791// This means that after the execution of this slow path, `ref` will
792// always be up-to-date, but `obj.field` may not; i.e., after the
793// flip, `ref` will be a to-space reference, but `obj.field` will
794// probably still be a from-space reference (unless it gets updated by
795// another thread, or if another thread installed another object
796// reference (different from `ref`) in `obj.field`).
797//
798// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100799// barrier marking runtime entry point to be invoked or an empty
800// location; in the latter case, the read barrier marking runtime
801// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000802class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
803 public:
804 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
805 Location ref,
806 Register obj,
807 uint32_t offset,
808 Location index,
809 size_t scale_factor,
810 bool needs_null_check,
811 bool use_load_acquire,
812 Register temp,
Roland Levillain97c46462017-05-11 14:04:03 +0100813 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000814 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
815 obj_(obj),
816 offset_(offset),
817 index_(index),
818 scale_factor_(scale_factor),
819 needs_null_check_(needs_null_check),
820 use_load_acquire_(use_load_acquire),
821 temp_(temp) {
822 DCHECK(kEmitCompilerReadBarrier);
823 DCHECK(kUseBakerReadBarrier);
824 }
825
826 const char* GetDescription() const OVERRIDE {
827 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
828 }
829
830 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
831 LocationSummary* locations = instruction_->GetLocations();
832 DCHECK(locations->CanCall());
833 DCHECK(ref_.IsRegister()) << ref_;
834 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
835 DCHECK(obj_.IsW());
836 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100837 DCHECK(instruction_->IsInstanceFieldGet() ||
838 instruction_->IsStaticFieldGet() ||
839 instruction_->IsArrayGet() ||
840 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100841 instruction_->IsInstanceOf() ||
842 instruction_->IsCheckCast() ||
843 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
844 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
845 << "Unexpected instruction in read barrier marking slow path: "
846 << instruction_->DebugName();
847 // The read barrier instrumentation of object ArrayGet
848 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000849 // instruction.
850 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000851 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
852
Roland Levillain54f869e2017-03-06 13:54:11 +0000853 // Temporary register `temp_`, used to store the lock word, must
854 // not be IP0 nor IP1, as we may use them to emit the reference
855 // load (in the call to GenerateRawReferenceLoad below), and we
856 // need the lock word to still be in `temp_` after the reference
857 // load.
858 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
859 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
860
Alexandre Rames5319def2014-10-23 10:03:10 +0100861 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000862
863 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
864 // inserted after the original load. However, in fast path based
865 // Baker's read barriers, we need to perform the load of
866 // mirror::Object::monitor_ *before* the original reference load.
867 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000868 // The slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100869 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000870 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
871 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
872 // HeapReference<mirror::Object> ref = *src; // Original reference load.
873 // bool is_gray = (rb_state == ReadBarrier::GrayState());
874 // if (is_gray) {
875 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
876 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000877 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000878 // Note: the original implementation in ReadBarrier::Barrier is
879 // slightly more complex as it performs additional checks that we do
880 // not do here for performance reasons.
881
882 // /* int32_t */ monitor = obj->monitor_
883 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
884 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
885 if (needs_null_check_) {
886 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100887 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000888 // /* LockWord */ lock_word = LockWord(monitor)
889 static_assert(sizeof(LockWord) == sizeof(int32_t),
890 "art::LockWord and int32_t have different sizes.");
891
892 // Introduce a dependency on the lock_word including rb_state,
893 // to prevent load-load reordering, and without using
894 // a memory barrier (which would be more expensive).
895 // `obj` is unchanged by this operation, but its value now depends
896 // on `temp`.
897 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
898
899 // The actual reference load.
900 // A possible implicit null check has already been handled above.
901 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
902 arm64_codegen->GenerateRawReferenceLoad(instruction_,
903 ref_,
904 obj_,
905 offset_,
906 index_,
907 scale_factor_,
908 /* needs_null_check */ false,
909 use_load_acquire_);
910
911 // Mark the object `ref` when `obj` is gray.
912 //
913 // if (rb_state == ReadBarrier::GrayState())
914 // ref = ReadBarrier::Mark(ref);
915 //
916 // Given the numeric representation, it's enough to check the low bit of the rb_state.
917 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
918 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
919 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
920 GenerateReadBarrierMarkRuntimeCall(codegen);
921
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000922 __ B(GetExitLabel());
923 }
924
925 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000926 // The register containing the object holding the marked object reference field.
927 Register obj_;
928 // The offset, index and scale factor to access the reference in `obj_`.
929 uint32_t offset_;
930 Location index_;
931 size_t scale_factor_;
932 // Is a null check required?
933 bool needs_null_check_;
934 // Should this reference load use Load-Acquire semantics?
935 bool use_load_acquire_;
936 // A temporary register used to hold the lock word of `obj_`.
937 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000938
Roland Levillain54f869e2017-03-06 13:54:11 +0000939 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000940};
941
Roland Levillain54f869e2017-03-06 13:54:11 +0000942// Slow path loading `obj`'s lock word, loading a reference from
943// object `*(obj + offset + (index << scale_factor))` into `ref`, and
944// marking `ref` if `obj` is gray according to the lock word (Baker
945// read barrier). If needed, this slow path also atomically updates
946// the field `obj.field` in the object `obj` holding this reference
947// after marking (contrary to
948// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
949// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100950//
951// This means that after the execution of this slow path, both `ref`
952// and `obj.field` will be up-to-date; i.e., after the flip, both will
953// hold the same to-space reference (unless another thread installed
954// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000955//
Roland Levillain54f869e2017-03-06 13:54:11 +0000956// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100957// barrier marking runtime entry point to be invoked or an empty
958// location; in the latter case, the read barrier marking runtime
959// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000960class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
961 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100962 public:
Roland Levillain97c46462017-05-11 14:04:03 +0100963 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
964 HInstruction* instruction,
965 Location ref,
966 Register obj,
967 uint32_t offset,
968 Location index,
969 size_t scale_factor,
970 bool needs_null_check,
971 bool use_load_acquire,
972 Register temp,
973 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000974 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100975 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000976 offset_(offset),
977 index_(index),
978 scale_factor_(scale_factor),
979 needs_null_check_(needs_null_check),
980 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000981 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100982 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000983 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100984 }
985
986 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000987 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100988 }
989
990 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
991 LocationSummary* locations = instruction_->GetLocations();
992 Register ref_reg = WRegisterFrom(ref_);
993 DCHECK(locations->CanCall());
994 DCHECK(ref_.IsRegister()) << ref_;
995 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000996 DCHECK(obj_.IsW());
997 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
998
999 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001000 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
1001 << "Unexpected instruction in read barrier marking and field updating slow path: "
1002 << instruction_->DebugName();
1003 DCHECK(instruction_->GetLocations()->Intrinsified());
1004 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +00001005 DCHECK_EQ(offset_, 0u);
1006 DCHECK_EQ(scale_factor_, 0u);
1007 DCHECK_EQ(use_load_acquire_, false);
1008 // The location of the offset of the marked reference field within `obj_`.
1009 Location field_offset = index_;
1010 DCHECK(field_offset.IsRegister()) << field_offset;
1011
1012 // Temporary register `temp_`, used to store the lock word, must
1013 // not be IP0 nor IP1, as we may use them to emit the reference
1014 // load (in the call to GenerateRawReferenceLoad below), and we
1015 // need the lock word to still be in `temp_` after the reference
1016 // load.
1017 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1018 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001019
1020 __ Bind(GetEntryLabel());
1021
Roland Levillainff487002017-03-07 16:50:01 +00001022 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM64's:
1023 //
1024 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1025 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1026 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1027 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1028 // if (is_gray) {
1029 // old_ref = ref;
1030 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1031 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1032 // }
1033
Roland Levillain54f869e2017-03-06 13:54:11 +00001034 // /* int32_t */ monitor = obj->monitor_
1035 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1036 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
1037 if (needs_null_check_) {
1038 codegen->MaybeRecordImplicitNullCheck(instruction_);
1039 }
1040 // /* LockWord */ lock_word = LockWord(monitor)
1041 static_assert(sizeof(LockWord) == sizeof(int32_t),
1042 "art::LockWord and int32_t have different sizes.");
1043
1044 // Introduce a dependency on the lock_word including rb_state,
1045 // to prevent load-load reordering, and without using
1046 // a memory barrier (which would be more expensive).
1047 // `obj` is unchanged by this operation, but its value now depends
1048 // on `temp`.
1049 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
1050
1051 // The actual reference load.
1052 // A possible implicit null check has already been handled above.
1053 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1054 arm64_codegen->GenerateRawReferenceLoad(instruction_,
1055 ref_,
1056 obj_,
1057 offset_,
1058 index_,
1059 scale_factor_,
1060 /* needs_null_check */ false,
1061 use_load_acquire_);
1062
1063 // Mark the object `ref` when `obj` is gray.
1064 //
1065 // if (rb_state == ReadBarrier::GrayState())
1066 // ref = ReadBarrier::Mark(ref);
1067 //
1068 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1069 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1070 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1071 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1072
1073 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001074 // Note that we cannot use IP to save the old reference, as IP is
1075 // used internally by the ReadBarrierMarkRegX entry point, and we
1076 // need the old reference after the call to that entry point.
1077 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1078 __ Mov(temp_.W(), ref_reg);
1079
Roland Levillain54f869e2017-03-06 13:54:11 +00001080 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001081
1082 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001083 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001084 //
1085 // Note that this field could also hold a different object, if
1086 // another thread had concurrently changed it. In that case, the
1087 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1088 // (CAS) operation below would abort the CAS, leaving the field
1089 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001090 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001091 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001092
1093 // Update the the holder's field atomically. This may fail if
1094 // mutator updates before us, but it's OK. This is achieved
1095 // using a strong compare-and-set (CAS) operation with relaxed
1096 // memory synchronization ordering, where the expected value is
1097 // the old reference and the desired value is the new reference.
1098
1099 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1100 UseScratchRegisterScope temps(masm);
1101
1102 // Convenience aliases.
1103 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001104 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001105 Register expected = temp_.W();
1106 Register value = ref_reg;
1107 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1108 Register tmp_value = temps.AcquireW(); // Value in memory.
1109
1110 __ Add(tmp_ptr, base.X(), Operand(offset));
1111
1112 if (kPoisonHeapReferences) {
1113 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1114 if (value.Is(expected)) {
1115 // Do not poison `value`, as it is the same register as
1116 // `expected`, which has just been poisoned.
1117 } else {
1118 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1119 }
1120 }
1121
1122 // do {
1123 // tmp_value = [tmp_ptr] - expected;
1124 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1125
Roland Levillain24a4d112016-10-26 13:10:46 +01001126 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001127 __ Bind(&loop_head);
1128 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1129 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001130 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001131 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1132 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001133 __ B(&exit_loop);
1134 __ Bind(&comparison_failed);
1135 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001136 __ Bind(&exit_loop);
1137
1138 if (kPoisonHeapReferences) {
1139 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1140 if (value.Is(expected)) {
1141 // Do not unpoison `value`, as it is the same register as
1142 // `expected`, which has just been unpoisoned.
1143 } else {
1144 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1145 }
1146 }
1147
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001148 __ B(GetExitLabel());
1149 }
1150
1151 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001152 // The register containing the object holding the marked object reference field.
1153 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001154 // The offset, index and scale factor to access the reference in `obj_`.
1155 uint32_t offset_;
1156 Location index_;
1157 size_t scale_factor_;
1158 // Is a null check required?
1159 bool needs_null_check_;
1160 // Should this reference load use Load-Acquire semantics?
1161 bool use_load_acquire_;
1162 // A temporary register used to hold the lock word of `obj_`; and
1163 // also to hold the original reference value, when the reference is
1164 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001165 const Register temp_;
1166
Roland Levillain54f869e2017-03-06 13:54:11 +00001167 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001168};
1169
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001170// Slow path generating a read barrier for a heap reference.
1171class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1172 public:
1173 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1174 Location out,
1175 Location ref,
1176 Location obj,
1177 uint32_t offset,
1178 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001179 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001180 out_(out),
1181 ref_(ref),
1182 obj_(obj),
1183 offset_(offset),
1184 index_(index) {
1185 DCHECK(kEmitCompilerReadBarrier);
1186 // If `obj` is equal to `out` or `ref`, it means the initial object
1187 // has been overwritten by (or after) the heap object reference load
1188 // to be instrumented, e.g.:
1189 //
1190 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001191 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001192 //
1193 // In that case, we have lost the information about the original
1194 // object, and the emitted read barrier cannot work properly.
1195 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1196 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1197 }
1198
1199 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1200 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1201 LocationSummary* locations = instruction_->GetLocations();
1202 Primitive::Type type = Primitive::kPrimNot;
1203 DCHECK(locations->CanCall());
1204 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001205 DCHECK(instruction_->IsInstanceFieldGet() ||
1206 instruction_->IsStaticFieldGet() ||
1207 instruction_->IsArrayGet() ||
1208 instruction_->IsInstanceOf() ||
1209 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001210 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001211 << "Unexpected instruction in read barrier for heap reference slow path: "
1212 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001213 // The read barrier instrumentation of object ArrayGet
1214 // instructions does not support the HIntermediateAddress
1215 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001216 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001217 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001218
1219 __ Bind(GetEntryLabel());
1220
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001221 SaveLiveRegisters(codegen, locations);
1222
1223 // We may have to change the index's value, but as `index_` is a
1224 // constant member (like other "inputs" of this slow path),
1225 // introduce a copy of it, `index`.
1226 Location index = index_;
1227 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001228 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001229 if (instruction_->IsArrayGet()) {
1230 // Compute the actual memory offset and store it in `index`.
1231 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
1232 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1233 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1234 // We are about to change the value of `index_reg` (see the
1235 // calls to vixl::MacroAssembler::Lsl and
1236 // vixl::MacroAssembler::Mov below), but it has
1237 // not been saved by the previous call to
1238 // art::SlowPathCode::SaveLiveRegisters, as it is a
1239 // callee-save register --
1240 // art::SlowPathCode::SaveLiveRegisters does not consider
1241 // callee-save registers, as it has been designed with the
1242 // assumption that callee-save registers are supposed to be
1243 // handled by the called function. So, as a callee-save
1244 // register, `index_reg` _would_ eventually be saved onto
1245 // the stack, but it would be too late: we would have
1246 // changed its value earlier. Therefore, we manually save
1247 // it here into another freely available register,
1248 // `free_reg`, chosen of course among the caller-save
1249 // registers (as a callee-save `free_reg` register would
1250 // exhibit the same problem).
1251 //
1252 // Note we could have requested a temporary register from
1253 // the register allocator instead; but we prefer not to, as
1254 // this is a slow path, and we know we can find a
1255 // caller-save register that is available.
1256 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1257 __ Mov(free_reg.W(), index_reg);
1258 index_reg = free_reg;
1259 index = LocationFrom(index_reg);
1260 } else {
1261 // The initial register stored in `index_` has already been
1262 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1263 // (as it is not a callee-save register), so we can freely
1264 // use it.
1265 }
1266 // Shifting the index value contained in `index_reg` by the scale
1267 // factor (2) cannot overflow in practice, as the runtime is
1268 // unable to allocate object arrays with a size larger than
1269 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1270 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1271 static_assert(
1272 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1273 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1274 __ Add(index_reg, index_reg, Operand(offset_));
1275 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001276 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1277 // intrinsics, `index_` is not shifted by a scale factor of 2
1278 // (as in the case of ArrayGet), as it is actually an offset
1279 // to an object field within an object.
1280 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001281 DCHECK(instruction_->GetLocations()->Intrinsified());
1282 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1283 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1284 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001285 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001286 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001287 }
1288 }
1289
1290 // We're moving two or three locations to locations that could
1291 // overlap, so we need a parallel move resolver.
1292 InvokeRuntimeCallingConvention calling_convention;
1293 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1294 parallel_move.AddMove(ref_,
1295 LocationFrom(calling_convention.GetRegisterAt(0)),
1296 type,
1297 nullptr);
1298 parallel_move.AddMove(obj_,
1299 LocationFrom(calling_convention.GetRegisterAt(1)),
1300 type,
1301 nullptr);
1302 if (index.IsValid()) {
1303 parallel_move.AddMove(index,
1304 LocationFrom(calling_convention.GetRegisterAt(2)),
1305 Primitive::kPrimInt,
1306 nullptr);
1307 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1308 } else {
1309 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1310 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1311 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001312 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001313 instruction_,
1314 instruction_->GetDexPc(),
1315 this);
1316 CheckEntrypointTypes<
1317 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1318 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1319
1320 RestoreLiveRegisters(codegen, locations);
1321
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001322 __ B(GetExitLabel());
1323 }
1324
1325 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1326
1327 private:
1328 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001329 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1330 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001331 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1332 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1333 return Register(VIXLRegCodeFromART(i), kXRegSize);
1334 }
1335 }
1336 // We shall never fail to find a free caller-save register, as
1337 // there are more than two core caller-save registers on ARM64
1338 // (meaning it is possible to find one which is different from
1339 // `ref` and `obj`).
1340 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1341 LOG(FATAL) << "Could not find a free register";
1342 UNREACHABLE();
1343 }
1344
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001345 const Location out_;
1346 const Location ref_;
1347 const Location obj_;
1348 const uint32_t offset_;
1349 // An additional location containing an index to an array.
1350 // Only used for HArrayGet and the UnsafeGetObject &
1351 // UnsafeGetObjectVolatile intrinsics.
1352 const Location index_;
1353
1354 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1355};
1356
1357// Slow path generating a read barrier for a GC root.
1358class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1359 public:
1360 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001361 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001362 DCHECK(kEmitCompilerReadBarrier);
1363 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001364
1365 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1366 LocationSummary* locations = instruction_->GetLocations();
1367 Primitive::Type type = Primitive::kPrimNot;
1368 DCHECK(locations->CanCall());
1369 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001370 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1371 << "Unexpected instruction in read barrier for GC root slow path: "
1372 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001373
1374 __ Bind(GetEntryLabel());
1375 SaveLiveRegisters(codegen, locations);
1376
1377 InvokeRuntimeCallingConvention calling_convention;
1378 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1379 // The argument of the ReadBarrierForRootSlow is not a managed
1380 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1381 // thus we need a 64-bit move here, and we cannot use
1382 //
1383 // arm64_codegen->MoveLocation(
1384 // LocationFrom(calling_convention.GetRegisterAt(0)),
1385 // root_,
1386 // type);
1387 //
1388 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1389 // reference type (`Primitive::kPrimNot`).
1390 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001391 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001392 instruction_,
1393 instruction_->GetDexPc(),
1394 this);
1395 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1396 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1397
1398 RestoreLiveRegisters(codegen, locations);
1399 __ B(GetExitLabel());
1400 }
1401
1402 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1403
1404 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001405 const Location out_;
1406 const Location root_;
1407
1408 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1409};
1410
Alexandre Rames5319def2014-10-23 10:03:10 +01001411#undef __
1412
1413Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
1414 Location next_location;
1415 if (type == Primitive::kPrimVoid) {
1416 LOG(FATAL) << "Unreachable type " << type;
1417 }
1418
1419 if (Primitive::IsFloatingPointType(type) &&
1420 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001421 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1422 } else if (!Primitive::IsFloatingPointType(type) &&
1423 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1424 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1425 } else {
1426 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001427 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1428 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001429 }
1430
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001431 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001432 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001433 return next_location;
1434}
1435
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001436Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001437 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001438}
1439
Serban Constantinescu579885a2015-02-22 20:51:33 +00001440CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1441 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001442 const CompilerOptions& compiler_options,
1443 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001444 : CodeGenerator(graph,
1445 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001446 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001447 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001448 callee_saved_core_registers.GetList(),
1449 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001450 compiler_options,
1451 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001452 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001453 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001454 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001455 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001456 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001457 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001458 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001459 uint32_literals_(std::less<uint32_t>(),
1460 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001461 uint64_literals_(std::less<uint64_t>(),
1462 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001463 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001464 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001465 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001466 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001467 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001468 string_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001469 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001470 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001471 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1472 jit_class_patches_(TypeReferenceValueComparator(),
1473 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001474 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001475 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001476}
Alexandre Rames5319def2014-10-23 10:03:10 +01001477
Alexandre Rames67555f72014-11-18 10:55:16 +00001478#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001479
Zheng Xu3927c8b2015-11-18 17:46:25 +08001480void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001481 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001482 jump_table->EmitTable(this);
1483 }
1484}
1485
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001486void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001487 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001488 // Ensure we emit the literal pool.
1489 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001490
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001491 CodeGenerator::Finalize(allocator);
1492}
1493
Zheng Xuad4450e2015-04-17 18:48:56 +08001494void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1495 // Note: There are 6 kinds of moves:
1496 // 1. constant -> GPR/FPR (non-cycle)
1497 // 2. constant -> stack (non-cycle)
1498 // 3. GPR/FPR -> GPR/FPR
1499 // 4. GPR/FPR -> stack
1500 // 5. stack -> GPR/FPR
1501 // 6. stack -> stack (non-cycle)
1502 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1503 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1504 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1505 // dependency.
1506 vixl_temps_.Open(GetVIXLAssembler());
1507}
1508
1509void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1510 vixl_temps_.Close();
1511}
1512
1513Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001514 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1515 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1516 || kind == Location::kSIMDStackSlot);
1517 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1518 ? Location::kFpuRegister
1519 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001520 Location scratch = GetScratchLocation(kind);
1521 if (!scratch.Equals(Location::NoLocation())) {
1522 return scratch;
1523 }
1524 // Allocate from VIXL temp registers.
1525 if (kind == Location::kRegister) {
1526 scratch = LocationFrom(vixl_temps_.AcquireX());
1527 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001528 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001529 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1530 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1531 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001532 }
1533 AddScratchLocation(scratch);
1534 return scratch;
1535}
1536
1537void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1538 if (loc.IsRegister()) {
1539 vixl_temps_.Release(XRegisterFrom(loc));
1540 } else {
1541 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001542 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001543 }
1544 RemoveScratchLocation(loc);
1545}
1546
Alexandre Rames3e69f162014-12-10 10:36:50 +00001547void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001548 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001549 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001550}
1551
Alexandre Rames5319def2014-10-23 10:03:10 +01001552void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001553 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001554 __ Bind(&frame_entry_label_);
1555
Serban Constantinescu02164b32014-11-13 14:05:07 +00001556 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1557 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001558 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001559 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001560 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001561 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001562 {
1563 // Ensure that between load and RecordPcInfo there are no pools emitted.
1564 ExactAssemblyScope eas(GetVIXLAssembler(),
1565 kInstructionSize,
1566 CodeBufferCheckScope::kExactSize);
1567 __ ldr(wzr, MemOperand(temp, 0));
1568 RecordPcInfo(nullptr, 0);
1569 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001570 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001571
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001572 if (!HasEmptyFrame()) {
1573 int frame_size = GetFrameSize();
1574 // Stack layout:
1575 // sp[frame_size - 8] : lr.
1576 // ... : other preserved core registers.
1577 // ... : other preserved fp registers.
1578 // ... : reserved frame space.
1579 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001580
1581 // Save the current method if we need it. Note that we do not
1582 // do this in HCurrentMethod, as the instruction might have been removed
1583 // in the SSA graph.
1584 if (RequiresCurrentMethod()) {
1585 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001586 } else {
1587 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001588 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001589 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001590 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1591 frame_size - GetCoreSpillSize());
1592 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1593 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001594
1595 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1596 // Initialize should_deoptimize flag to 0.
1597 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1598 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1599 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001600 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01001601
1602 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001603}
1604
1605void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001606 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001607 if (!HasEmptyFrame()) {
1608 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001609 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1610 frame_size - FrameEntrySpillSize());
1611 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1612 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001613 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001614 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001615 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001616 __ Ret();
1617 GetAssembler()->cfi().RestoreState();
1618 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001619}
1620
Scott Wakeling97c72b72016-06-24 16:19:36 +01001621CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001622 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001623 return CPURegList(CPURegister::kRegister, kXRegSize,
1624 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001625}
1626
Scott Wakeling97c72b72016-06-24 16:19:36 +01001627CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001628 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1629 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001630 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1631 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001632}
1633
Alexandre Rames5319def2014-10-23 10:03:10 +01001634void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1635 __ Bind(GetLabelOf(block));
1636}
1637
Calin Juravle175dc732015-08-25 15:42:32 +01001638void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1639 DCHECK(location.IsRegister());
1640 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1641}
1642
Calin Juravlee460d1d2015-09-29 04:52:17 +01001643void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1644 if (location.IsRegister()) {
1645 locations->AddTemp(location);
1646 } else {
1647 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1648 }
1649}
1650
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001651void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001652 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001653 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001654 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001655 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001656 if (value_can_be_null) {
1657 __ Cbz(value, &done);
1658 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001659 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001660 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001661 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001662 if (value_can_be_null) {
1663 __ Bind(&done);
1664 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001665}
1666
David Brazdil58282f42016-01-14 12:45:10 +00001667void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001668 // Blocked core registers:
1669 // lr : Runtime reserved.
1670 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001671 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001672 // ip1 : VIXL core temp.
1673 // ip0 : VIXL core temp.
1674 //
1675 // Blocked fp registers:
1676 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001677 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1678 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001679 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001680 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001681 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001682
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001683 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001684 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001685 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001686 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001687
David Brazdil58282f42016-01-14 12:45:10 +00001688 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001689 // Stubs do not save callee-save floating point registers. If the graph
1690 // is debuggable, we need to deal with these registers differently. For
1691 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001692 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1693 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001694 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001695 }
1696 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001697}
1698
Alexandre Rames3e69f162014-12-10 10:36:50 +00001699size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1700 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1701 __ Str(reg, MemOperand(sp, stack_index));
1702 return kArm64WordSize;
1703}
1704
1705size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1706 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1707 __ Ldr(reg, MemOperand(sp, stack_index));
1708 return kArm64WordSize;
1709}
1710
1711size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1712 FPRegister reg = FPRegister(reg_id, kDRegSize);
1713 __ Str(reg, MemOperand(sp, stack_index));
1714 return kArm64WordSize;
1715}
1716
1717size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1718 FPRegister reg = FPRegister(reg_id, kDRegSize);
1719 __ Ldr(reg, MemOperand(sp, stack_index));
1720 return kArm64WordSize;
1721}
1722
Alexandre Rames5319def2014-10-23 10:03:10 +01001723void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001724 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001725}
1726
1727void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001728 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001729}
1730
Alexandre Rames67555f72014-11-18 10:55:16 +00001731void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001732 if (constant->IsIntConstant()) {
1733 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1734 } else if (constant->IsLongConstant()) {
1735 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1736 } else if (constant->IsNullConstant()) {
1737 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001738 } else if (constant->IsFloatConstant()) {
1739 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1740 } else {
1741 DCHECK(constant->IsDoubleConstant());
1742 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1743 }
1744}
1745
Alexandre Rames3e69f162014-12-10 10:36:50 +00001746
1747static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1748 DCHECK(constant.IsConstant());
1749 HConstant* cst = constant.GetConstant();
1750 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001751 // Null is mapped to a core W register, which we associate with kPrimInt.
1752 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001753 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1754 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1755 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1756}
1757
Roland Levillain952b2352017-05-03 19:49:14 +01001758// Allocate a scratch register from the VIXL pool, querying first
1759// the floating-point register pool, and then the core register
1760// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001761// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1762// using a different allocation strategy.
1763static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1764 vixl::aarch64::UseScratchRegisterScope* temps,
1765 int size_in_bits) {
1766 return masm->GetScratchFPRegisterList()->IsEmpty()
1767 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1768 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1769}
1770
Calin Juravlee460d1d2015-09-29 04:52:17 +01001771void CodeGeneratorARM64::MoveLocation(Location destination,
1772 Location source,
1773 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001774 if (source.Equals(destination)) {
1775 return;
1776 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001777
1778 // A valid move can always be inferred from the destination and source
1779 // locations. When moving from and to a register, the argument type can be
1780 // used to generate 32bit instead of 64bit moves. In debug mode we also
1781 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001782 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001783
1784 if (destination.IsRegister() || destination.IsFpuRegister()) {
1785 if (unspecified_type) {
1786 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1787 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001788 (src_cst != nullptr && (src_cst->IsIntConstant()
1789 || src_cst->IsFloatConstant()
1790 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001791 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001792 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001793 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001794 // If the source is a double stack slot or a 64bit constant, a 64bit
1795 // type is appropriate. Else the source is a register, and since the
1796 // type has not been specified, we chose a 64bit type to force a 64bit
1797 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001798 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001799 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001800 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001801 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1802 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1803 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001804 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1805 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1806 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001807 } else if (source.IsSIMDStackSlot()) {
1808 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001809 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001810 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001811 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001812 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001813 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001814 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001815 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001816 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001817 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1818 ? Primitive::kPrimLong
1819 : Primitive::kPrimInt;
1820 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1821 }
1822 } else {
1823 DCHECK(source.IsFpuRegister());
1824 if (destination.IsRegister()) {
1825 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1826 ? Primitive::kPrimDouble
1827 : Primitive::kPrimFloat;
1828 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1829 } else {
1830 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001831 if (GetGraph()->HasSIMD()) {
1832 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1833 } else {
1834 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1835 }
1836 }
1837 }
1838 } else if (destination.IsSIMDStackSlot()) {
1839 if (source.IsFpuRegister()) {
1840 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1841 } else {
1842 DCHECK(source.IsSIMDStackSlot());
1843 UseScratchRegisterScope temps(GetVIXLAssembler());
1844 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1845 Register temp = temps.AcquireX();
1846 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1847 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1848 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1849 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1850 } else {
1851 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1852 __ Ldr(temp, StackOperandFrom(source));
1853 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001854 }
1855 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001856 } else { // The destination is not a register. It must be a stack slot.
1857 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1858 if (source.IsRegister() || source.IsFpuRegister()) {
1859 if (unspecified_type) {
1860 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001861 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001862 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001863 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001864 }
1865 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001866 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1867 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1868 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001869 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001870 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1871 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001872 UseScratchRegisterScope temps(GetVIXLAssembler());
1873 HConstant* src_cst = source.GetConstant();
1874 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001875 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001876 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1877 ? Register(xzr)
1878 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001879 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001880 if (src_cst->IsIntConstant()) {
1881 temp = temps.AcquireW();
1882 } else if (src_cst->IsLongConstant()) {
1883 temp = temps.AcquireX();
1884 } else if (src_cst->IsFloatConstant()) {
1885 temp = temps.AcquireS();
1886 } else {
1887 DCHECK(src_cst->IsDoubleConstant());
1888 temp = temps.AcquireD();
1889 }
1890 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001891 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001892 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001893 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001894 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001895 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001896 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001897 // Use any scratch register (a core or a floating-point one)
1898 // from VIXL scratch register pools as a temporary.
1899 //
1900 // We used to only use the FP scratch register pool, but in some
1901 // rare cases the only register from this pool (D31) would
1902 // already be used (e.g. within a ParallelMove instruction, when
1903 // a move is blocked by a another move requiring a scratch FP
1904 // register, which would reserve D31). To prevent this issue, we
1905 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001906 //
1907 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001908 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001909 // use AcquireFPOrCoreCPURegisterOfSize instead of
1910 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1911 // allocates core scratch registers first.
1912 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1913 GetVIXLAssembler(),
1914 &temps,
1915 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001916 __ Ldr(temp, StackOperandFrom(source));
1917 __ Str(temp, StackOperandFrom(destination));
1918 }
1919 }
1920}
1921
1922void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001923 CPURegister dst,
1924 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001925 switch (type) {
1926 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001927 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001928 break;
1929 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001930 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001931 break;
1932 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001933 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001934 break;
1935 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001936 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001937 break;
1938 case Primitive::kPrimInt:
1939 case Primitive::kPrimNot:
1940 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001941 case Primitive::kPrimFloat:
1942 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001943 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001944 __ Ldr(dst, src);
1945 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001946 case Primitive::kPrimVoid:
1947 LOG(FATAL) << "Unreachable type " << type;
1948 }
1949}
1950
Calin Juravle77520bc2015-01-12 18:45:46 +00001951void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001952 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001953 const MemOperand& src,
1954 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001955 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001956 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001957 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001958 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001959
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001960 DCHECK(!src.IsPreIndex());
1961 DCHECK(!src.IsPostIndex());
1962
1963 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001964 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001965 {
1966 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1967 MemOperand base = MemOperand(temp_base);
1968 switch (type) {
1969 case Primitive::kPrimBoolean:
1970 {
1971 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1972 __ ldarb(Register(dst), base);
1973 if (needs_null_check) {
1974 MaybeRecordImplicitNullCheck(instruction);
1975 }
1976 }
1977 break;
1978 case Primitive::kPrimByte:
1979 {
1980 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1981 __ ldarb(Register(dst), base);
1982 if (needs_null_check) {
1983 MaybeRecordImplicitNullCheck(instruction);
1984 }
1985 }
1986 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1987 break;
1988 case Primitive::kPrimChar:
1989 {
1990 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1991 __ ldarh(Register(dst), base);
1992 if (needs_null_check) {
1993 MaybeRecordImplicitNullCheck(instruction);
1994 }
1995 }
1996 break;
1997 case Primitive::kPrimShort:
1998 {
1999 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2000 __ ldarh(Register(dst), base);
2001 if (needs_null_check) {
2002 MaybeRecordImplicitNullCheck(instruction);
2003 }
2004 }
2005 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
2006 break;
2007 case Primitive::kPrimInt:
2008 case Primitive::kPrimNot:
2009 case Primitive::kPrimLong:
2010 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
2011 {
2012 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2013 __ ldar(Register(dst), base);
2014 if (needs_null_check) {
2015 MaybeRecordImplicitNullCheck(instruction);
2016 }
2017 }
2018 break;
2019 case Primitive::kPrimFloat:
2020 case Primitive::kPrimDouble: {
2021 DCHECK(dst.IsFPRegister());
2022 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002023
Artem Serov914d7a82017-02-07 14:33:49 +00002024 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2025 {
2026 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2027 __ ldar(temp, base);
2028 if (needs_null_check) {
2029 MaybeRecordImplicitNullCheck(instruction);
2030 }
2031 }
2032 __ Fmov(FPRegister(dst), temp);
2033 break;
Roland Levillain44015862016-01-22 11:47:17 +00002034 }
Artem Serov914d7a82017-02-07 14:33:49 +00002035 case Primitive::kPrimVoid:
2036 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002037 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002038 }
2039}
2040
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002041void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002042 CPURegister src,
2043 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002044 switch (type) {
2045 case Primitive::kPrimBoolean:
2046 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002047 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002048 break;
2049 case Primitive::kPrimChar:
2050 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002051 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002052 break;
2053 case Primitive::kPrimInt:
2054 case Primitive::kPrimNot:
2055 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002056 case Primitive::kPrimFloat:
2057 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00002058 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002059 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002060 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002061 case Primitive::kPrimVoid:
2062 LOG(FATAL) << "Unreachable type " << type;
2063 }
2064}
2065
Artem Serov914d7a82017-02-07 14:33:49 +00002066void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
2067 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002068 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002069 const MemOperand& dst,
2070 bool needs_null_check) {
2071 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002072 UseScratchRegisterScope temps(GetVIXLAssembler());
2073 Register temp_base = temps.AcquireX();
2074
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002075 DCHECK(!dst.IsPreIndex());
2076 DCHECK(!dst.IsPostIndex());
2077
2078 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002079 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002080 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002081 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002082 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002083 switch (type) {
2084 case Primitive::kPrimBoolean:
2085 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00002086 {
2087 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2088 __ stlrb(Register(src), base);
2089 if (needs_null_check) {
2090 MaybeRecordImplicitNullCheck(instruction);
2091 }
2092 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002093 break;
2094 case Primitive::kPrimChar:
2095 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00002096 {
2097 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2098 __ stlrh(Register(src), base);
2099 if (needs_null_check) {
2100 MaybeRecordImplicitNullCheck(instruction);
2101 }
2102 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002103 break;
2104 case Primitive::kPrimInt:
2105 case Primitive::kPrimNot:
2106 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002107 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002108 {
2109 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2110 __ stlr(Register(src), base);
2111 if (needs_null_check) {
2112 MaybeRecordImplicitNullCheck(instruction);
2113 }
2114 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002115 break;
2116 case Primitive::kPrimFloat:
2117 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002118 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002119 Register temp_src;
2120 if (src.IsZero()) {
2121 // The zero register is used to avoid synthesizing zero constants.
2122 temp_src = Register(src);
2123 } else {
2124 DCHECK(src.IsFPRegister());
2125 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2126 __ Fmov(temp_src, FPRegister(src));
2127 }
Artem Serov914d7a82017-02-07 14:33:49 +00002128 {
2129 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2130 __ stlr(temp_src, base);
2131 if (needs_null_check) {
2132 MaybeRecordImplicitNullCheck(instruction);
2133 }
2134 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002135 break;
2136 }
2137 case Primitive::kPrimVoid:
2138 LOG(FATAL) << "Unreachable type " << type;
2139 }
2140}
2141
Calin Juravle175dc732015-08-25 15:42:32 +01002142void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2143 HInstruction* instruction,
2144 uint32_t dex_pc,
2145 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002146 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002147
2148 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2149 {
2150 // Ensure the pc position is recorded immediately after the `blr` instruction.
2151 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2152 __ blr(lr);
2153 if (EntrypointRequiresStackMap(entrypoint)) {
2154 RecordPcInfo(instruction, dex_pc, slow_path);
2155 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002156 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002157}
2158
Roland Levillaindec8f632016-07-22 17:10:06 +01002159void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2160 HInstruction* instruction,
2161 SlowPathCode* slow_path) {
2162 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002163 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2164 __ Blr(lr);
2165}
2166
Alexandre Rames67555f72014-11-18 10:55:16 +00002167void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002168 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002169 UseScratchRegisterScope temps(GetVIXLAssembler());
2170 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002171 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2172
Serban Constantinescu02164b32014-11-13 14:05:07 +00002173 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002174 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2175 __ Add(temp, class_reg, status_offset);
2176 __ Ldar(temp, HeapOperand(temp));
2177 __ Cmp(temp, mirror::Class::kStatusInitialized);
2178 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002179 __ Bind(slow_path->GetExitLabel());
2180}
Alexandre Rames5319def2014-10-23 10:03:10 +01002181
Roland Levillain44015862016-01-22 11:47:17 +00002182void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002183 BarrierType type = BarrierAll;
2184
2185 switch (kind) {
2186 case MemBarrierKind::kAnyAny:
2187 case MemBarrierKind::kAnyStore: {
2188 type = BarrierAll;
2189 break;
2190 }
2191 case MemBarrierKind::kLoadAny: {
2192 type = BarrierReads;
2193 break;
2194 }
2195 case MemBarrierKind::kStoreStore: {
2196 type = BarrierWrites;
2197 break;
2198 }
2199 default:
2200 LOG(FATAL) << "Unexpected memory barrier " << kind;
2201 }
2202 __ Dmb(InnerShareable, type);
2203}
2204
Serban Constantinescu02164b32014-11-13 14:05:07 +00002205void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2206 HBasicBlock* successor) {
2207 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002208 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2209 if (slow_path == nullptr) {
2210 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2211 instruction->SetSlowPath(slow_path);
2212 codegen_->AddSlowPath(slow_path);
2213 if (successor != nullptr) {
2214 DCHECK(successor->IsLoopHeader());
2215 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2216 }
2217 } else {
2218 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2219 }
2220
Serban Constantinescu02164b32014-11-13 14:05:07 +00002221 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2222 Register temp = temps.AcquireW();
2223
Andreas Gampe542451c2016-07-26 09:02:02 -07002224 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002225 if (successor == nullptr) {
2226 __ Cbnz(temp, slow_path->GetEntryLabel());
2227 __ Bind(slow_path->GetReturnLabel());
2228 } else {
2229 __ Cbz(temp, codegen_->GetLabelOf(successor));
2230 __ B(slow_path->GetEntryLabel());
2231 // slow_path will return to GetLabelOf(successor).
2232 }
2233}
2234
Alexandre Rames5319def2014-10-23 10:03:10 +01002235InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2236 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002237 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002238 assembler_(codegen->GetAssembler()),
2239 codegen_(codegen) {}
2240
2241#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002242 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002243
2244#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2245
2246enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002247 // Using a base helps identify when we hit such breakpoints.
2248 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002249#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2250 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2251#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2252};
2253
2254#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002255 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002256 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2257 } \
2258 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2259 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2260 locations->SetOut(Location::Any()); \
2261 }
2262 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2263#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2264
2265#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002266#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002267
Alexandre Rames67555f72014-11-18 10:55:16 +00002268void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002269 DCHECK_EQ(instr->InputCount(), 2U);
2270 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2271 Primitive::Type type = instr->GetResultType();
2272 switch (type) {
2273 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002274 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002275 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002276 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002277 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002278 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002279
2280 case Primitive::kPrimFloat:
2281 case Primitive::kPrimDouble:
2282 locations->SetInAt(0, Location::RequiresFpuRegister());
2283 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002284 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002285 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002286
Alexandre Rames5319def2014-10-23 10:03:10 +01002287 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002288 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002289 }
2290}
2291
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002292void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2293 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002294 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2295
2296 bool object_field_get_with_read_barrier =
2297 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002298 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002299 new (GetGraph()->GetArena()) LocationSummary(instruction,
2300 object_field_get_with_read_barrier ?
2301 LocationSummary::kCallOnSlowPath :
2302 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002303 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002304 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002305 // We need a temporary register for the read barrier marking slow
2306 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002307 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2308 !Runtime::Current()->UseJitCompilation() &&
2309 !field_info.IsVolatile()) {
2310 // If link-time thunks for the Baker read barrier are enabled, for AOT
2311 // non-volatile loads we need a temporary only if the offset is too big.
2312 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2313 locations->AddTemp(FixedTempLocation());
2314 }
2315 } else {
2316 locations->AddTemp(Location::RequiresRegister());
2317 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002318 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002319 locations->SetInAt(0, Location::RequiresRegister());
2320 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2321 locations->SetOut(Location::RequiresFpuRegister());
2322 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002323 // The output overlaps for an object field get when read barriers
2324 // are enabled: we do not want the load to overwrite the object's
2325 // location, as we need it to emit the read barrier.
2326 locations->SetOut(
2327 Location::RequiresRegister(),
2328 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002329 }
2330}
2331
2332void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2333 const FieldInfo& field_info) {
2334 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002335 LocationSummary* locations = instruction->GetLocations();
2336 Location base_loc = locations->InAt(0);
2337 Location out = locations->Out();
2338 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002339 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002340 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002341
Roland Levillain44015862016-01-22 11:47:17 +00002342 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2343 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002344 // /* HeapReference<Object> */ out = *(base + offset)
2345 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002346 Location maybe_temp =
2347 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002348 // Note that potential implicit null checks are handled in this
2349 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2350 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2351 instruction,
2352 out,
2353 base,
2354 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002355 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002356 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002357 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002358 } else {
2359 // General case.
2360 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002361 // Note that a potential implicit null check is handled in this
2362 // CodeGeneratorARM64::LoadAcquire call.
2363 // NB: LoadAcquire will record the pc info if needed.
2364 codegen_->LoadAcquire(
2365 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002366 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002367 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2368 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002369 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002370 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002371 }
Roland Levillain44015862016-01-22 11:47:17 +00002372 if (field_type == Primitive::kPrimNot) {
2373 // If read barriers are enabled, emit read barriers other than
2374 // Baker's using a slow path (and also unpoison the loaded
2375 // reference, if heap poisoning is enabled).
2376 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2377 }
Roland Levillain4d027112015-07-01 15:41:14 +01002378 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002379}
2380
2381void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2382 LocationSummary* locations =
2383 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2384 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002385 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2386 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2387 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002388 locations->SetInAt(1, Location::RequiresFpuRegister());
2389 } else {
2390 locations->SetInAt(1, Location::RequiresRegister());
2391 }
2392}
2393
2394void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002395 const FieldInfo& field_info,
2396 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002397 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2398
2399 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002400 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002401 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002402 Offset offset = field_info.GetFieldOffset();
2403 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002404
Roland Levillain4d027112015-07-01 15:41:14 +01002405 {
2406 // We use a block to end the scratch scope before the write barrier, thus
2407 // freeing the temporary registers so they can be used in `MarkGCCard`.
2408 UseScratchRegisterScope temps(GetVIXLAssembler());
2409
2410 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2411 DCHECK(value.IsW());
2412 Register temp = temps.AcquireW();
2413 __ Mov(temp, value.W());
2414 GetAssembler()->PoisonHeapReference(temp.W());
2415 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002416 }
Roland Levillain4d027112015-07-01 15:41:14 +01002417
2418 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002419 codegen_->StoreRelease(
2420 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002421 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002422 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2423 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002424 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2425 codegen_->MaybeRecordImplicitNullCheck(instruction);
2426 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002427 }
2428
2429 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002430 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002431 }
2432}
2433
Alexandre Rames67555f72014-11-18 10:55:16 +00002434void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002435 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002436
2437 switch (type) {
2438 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002439 case Primitive::kPrimLong: {
2440 Register dst = OutputRegister(instr);
2441 Register lhs = InputRegisterAt(instr, 0);
2442 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002443 if (instr->IsAdd()) {
2444 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002445 } else if (instr->IsAnd()) {
2446 __ And(dst, lhs, rhs);
2447 } else if (instr->IsOr()) {
2448 __ Orr(dst, lhs, rhs);
2449 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002450 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002451 } else if (instr->IsRor()) {
2452 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002453 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002454 __ Ror(dst, lhs, shift);
2455 } else {
2456 // Ensure shift distance is in the same size register as the result. If
2457 // we are rotating a long and the shift comes in a w register originally,
2458 // we don't need to sxtw for use as an x since the shift distances are
2459 // all & reg_bits - 1.
2460 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2461 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002462 } else {
2463 DCHECK(instr->IsXor());
2464 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002465 }
2466 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002467 }
2468 case Primitive::kPrimFloat:
2469 case Primitive::kPrimDouble: {
2470 FPRegister dst = OutputFPRegister(instr);
2471 FPRegister lhs = InputFPRegisterAt(instr, 0);
2472 FPRegister rhs = InputFPRegisterAt(instr, 1);
2473 if (instr->IsAdd()) {
2474 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002475 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002476 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002477 } else {
2478 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002479 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002480 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002481 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002482 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002483 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002484 }
2485}
2486
Serban Constantinescu02164b32014-11-13 14:05:07 +00002487void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2488 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2489
2490 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2491 Primitive::Type type = instr->GetResultType();
2492 switch (type) {
2493 case Primitive::kPrimInt:
2494 case Primitive::kPrimLong: {
2495 locations->SetInAt(0, Location::RequiresRegister());
2496 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002497 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002498 break;
2499 }
2500 default:
2501 LOG(FATAL) << "Unexpected shift type " << type;
2502 }
2503}
2504
2505void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2506 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2507
2508 Primitive::Type type = instr->GetType();
2509 switch (type) {
2510 case Primitive::kPrimInt:
2511 case Primitive::kPrimLong: {
2512 Register dst = OutputRegister(instr);
2513 Register lhs = InputRegisterAt(instr, 0);
2514 Operand rhs = InputOperandAt(instr, 1);
2515 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002516 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002517 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002518 if (instr->IsShl()) {
2519 __ Lsl(dst, lhs, shift_value);
2520 } else if (instr->IsShr()) {
2521 __ Asr(dst, lhs, shift_value);
2522 } else {
2523 __ Lsr(dst, lhs, shift_value);
2524 }
2525 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002526 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002527
2528 if (instr->IsShl()) {
2529 __ Lsl(dst, lhs, rhs_reg);
2530 } else if (instr->IsShr()) {
2531 __ Asr(dst, lhs, rhs_reg);
2532 } else {
2533 __ Lsr(dst, lhs, rhs_reg);
2534 }
2535 }
2536 break;
2537 }
2538 default:
2539 LOG(FATAL) << "Unexpected shift operation type " << type;
2540 }
2541}
2542
Alexandre Rames5319def2014-10-23 10:03:10 +01002543void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002544 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002545}
2546
2547void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002548 HandleBinaryOp(instruction);
2549}
2550
2551void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2552 HandleBinaryOp(instruction);
2553}
2554
2555void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2556 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002557}
2558
Artem Serov7fc63502016-02-09 17:15:29 +00002559void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002560 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2561 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2562 locations->SetInAt(0, Location::RequiresRegister());
2563 // There is no immediate variant of negated bitwise instructions in AArch64.
2564 locations->SetInAt(1, Location::RequiresRegister());
2565 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2566}
2567
Artem Serov7fc63502016-02-09 17:15:29 +00002568void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002569 Register dst = OutputRegister(instr);
2570 Register lhs = InputRegisterAt(instr, 0);
2571 Register rhs = InputRegisterAt(instr, 1);
2572
2573 switch (instr->GetOpKind()) {
2574 case HInstruction::kAnd:
2575 __ Bic(dst, lhs, rhs);
2576 break;
2577 case HInstruction::kOr:
2578 __ Orn(dst, lhs, rhs);
2579 break;
2580 case HInstruction::kXor:
2581 __ Eon(dst, lhs, rhs);
2582 break;
2583 default:
2584 LOG(FATAL) << "Unreachable";
2585 }
2586}
2587
Anton Kirilov74234da2017-01-13 14:42:47 +00002588void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2589 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002590 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2591 instruction->GetType() == Primitive::kPrimLong);
2592 LocationSummary* locations =
2593 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2594 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2595 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2596 } else {
2597 locations->SetInAt(0, Location::RequiresRegister());
2598 }
2599 locations->SetInAt(1, Location::RequiresRegister());
2600 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2601}
2602
Anton Kirilov74234da2017-01-13 14:42:47 +00002603void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2604 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002605 Primitive::Type type = instruction->GetType();
2606 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2607 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2608 Register out = OutputRegister(instruction);
2609 Register left;
2610 if (kind != HInstruction::kNeg) {
2611 left = InputRegisterAt(instruction, 0);
2612 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002613 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002614 // shifter operand operation, the IR generating `right_reg` (input to the type
2615 // conversion) can have a different type from the current instruction's type,
2616 // so we manually indicate the type.
2617 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002618 Operand right_operand(0);
2619
Anton Kirilov74234da2017-01-13 14:42:47 +00002620 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2621 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002622 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2623 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002624 right_operand = Operand(right_reg,
2625 helpers::ShiftFromOpKind(op_kind),
2626 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002627 }
2628
2629 // Logical binary operations do not support extension operations in the
2630 // operand. Note that VIXL would still manage if it was passed by generating
2631 // the extension as a separate instruction.
2632 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2633 DCHECK(!right_operand.IsExtendedRegister() ||
2634 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2635 kind != HInstruction::kNeg));
2636 switch (kind) {
2637 case HInstruction::kAdd:
2638 __ Add(out, left, right_operand);
2639 break;
2640 case HInstruction::kAnd:
2641 __ And(out, left, right_operand);
2642 break;
2643 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002644 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002645 __ Neg(out, right_operand);
2646 break;
2647 case HInstruction::kOr:
2648 __ Orr(out, left, right_operand);
2649 break;
2650 case HInstruction::kSub:
2651 __ Sub(out, left, right_operand);
2652 break;
2653 case HInstruction::kXor:
2654 __ Eor(out, left, right_operand);
2655 break;
2656 default:
2657 LOG(FATAL) << "Unexpected operation kind: " << kind;
2658 UNREACHABLE();
2659 }
2660}
2661
Artem Serov328429f2016-07-06 16:23:04 +01002662void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002663 LocationSummary* locations =
2664 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2665 locations->SetInAt(0, Location::RequiresRegister());
2666 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002667 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002668}
2669
Roland Levillain19c54192016-11-04 13:44:09 +00002670void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002671 __ Add(OutputRegister(instruction),
2672 InputRegisterAt(instruction, 0),
2673 Operand(InputOperandAt(instruction, 1)));
2674}
2675
Artem Serove1811ed2017-04-27 16:50:47 +01002676void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2677 LocationSummary* locations =
2678 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2679
2680 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2681
2682 locations->SetInAt(0, Location::RequiresRegister());
2683 // For byte case we don't need to shift the index variable so we can encode the data offset into
2684 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2685 // data offset constant generation out of the loop and reduce the critical path length in the
2686 // loop.
2687 locations->SetInAt(1, shift->GetValue() == 0
2688 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2689 : Location::RequiresRegister());
2690 locations->SetInAt(2, Location::ConstantLocation(shift));
2691 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2692}
2693
2694void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2695 HIntermediateAddressIndex* instruction) {
2696 Register index_reg = InputRegisterAt(instruction, 0);
2697 uint32_t shift = Int64ConstantFrom(instruction->GetLocations()->InAt(2));
2698 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2699
2700 if (shift == 0) {
2701 __ Add(OutputRegister(instruction), index_reg, offset);
2702 } else {
2703 Register offset_reg = InputRegisterAt(instruction, 1);
2704 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2705 }
2706}
2707
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002708void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002709 LocationSummary* locations =
2710 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002711 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2712 if (instr->GetOpKind() == HInstruction::kSub &&
2713 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002714 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002715 // Don't allocate register for Mneg instruction.
2716 } else {
2717 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2718 Location::RequiresRegister());
2719 }
2720 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2721 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002722 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2723}
2724
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002725void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002726 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002727 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2728 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002729
2730 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2731 // This fixup should be carried out for all multiply-accumulate instructions:
2732 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2733 if (instr->GetType() == Primitive::kPrimLong &&
2734 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2735 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002736 vixl::aarch64::Instruction* prev =
2737 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002738 if (prev->IsLoadOrStore()) {
2739 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002740 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002741 __ nop();
2742 }
2743 }
2744
2745 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002746 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002747 __ Madd(res, mul_left, mul_right, accumulator);
2748 } else {
2749 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002750 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002751 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002752 __ Mneg(res, mul_left, mul_right);
2753 } else {
2754 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2755 __ Msub(res, mul_left, mul_right, accumulator);
2756 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002757 }
2758}
2759
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002760void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002761 bool object_array_get_with_read_barrier =
2762 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002763 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002764 new (GetGraph()->GetArena()) LocationSummary(instruction,
2765 object_array_get_with_read_barrier ?
2766 LocationSummary::kCallOnSlowPath :
2767 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002768 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002769 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002770 // We need a temporary register for the read barrier marking slow
2771 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002772 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2773 !Runtime::Current()->UseJitCompilation() &&
2774 instruction->GetIndex()->IsConstant()) {
2775 // Array loads with constant index are treated as field loads.
2776 // If link-time thunks for the Baker read barrier are enabled, for AOT
2777 // constant index loads we need a temporary only if the offset is too big.
2778 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2779 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
2780 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
2781 if (offset >= kReferenceLoadMinFarOffset) {
2782 locations->AddTemp(FixedTempLocation());
2783 }
2784 } else {
2785 locations->AddTemp(Location::RequiresRegister());
2786 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002787 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002788 locations->SetInAt(0, Location::RequiresRegister());
2789 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002790 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2791 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2792 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002793 // The output overlaps in the case of an object array get with
2794 // read barriers enabled: we do not want the move to overwrite the
2795 // array's location, as we need it to emit the read barrier.
2796 locations->SetOut(
2797 Location::RequiresRegister(),
2798 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002799 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002800}
2801
2802void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002803 Primitive::Type type = instruction->GetType();
2804 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002805 LocationSummary* locations = instruction->GetLocations();
2806 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002807 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002808 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002809 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2810 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002811 MacroAssembler* masm = GetVIXLAssembler();
2812 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002813
Roland Levillain19c54192016-11-04 13:44:09 +00002814 // The read barrier instrumentation of object ArrayGet instructions
2815 // does not support the HIntermediateAddress instruction.
2816 DCHECK(!((type == Primitive::kPrimNot) &&
2817 instruction->GetArray()->IsIntermediateAddress() &&
2818 kEmitCompilerReadBarrier));
2819
Roland Levillain44015862016-01-22 11:47:17 +00002820 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2821 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002822 // Note that a potential implicit null check is handled in the
2823 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002824 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002825 if (index.IsConstant()) {
2826 // Array load with a constant index can be treated as a field load.
2827 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2828 Location maybe_temp =
2829 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2830 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2831 out,
2832 obj.W(),
2833 offset,
2834 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002835 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002836 /* use_load_acquire */ false);
2837 } else {
2838 Register temp = WRegisterFrom(locations->GetTemp(0));
2839 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002840 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002841 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002842 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002843 // General case.
2844 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002845 Register length;
2846 if (maybe_compressed_char_at) {
2847 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2848 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002849 {
2850 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2851 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2852
2853 if (instruction->GetArray()->IsIntermediateAddress()) {
2854 DCHECK_LT(count_offset, offset);
2855 int64_t adjusted_offset =
2856 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2857 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2858 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2859 } else {
2860 __ Ldr(length, HeapOperand(obj, count_offset));
2861 }
2862 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002863 }
jessicahandojo05765752016-09-09 19:01:32 -07002864 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002865 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002866 if (maybe_compressed_char_at) {
2867 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002868 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2869 "Expecting 0=compressed, 1=uncompressed");
2870 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002871 __ Ldrb(Register(OutputCPURegister(instruction)),
2872 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2873 __ B(&done);
2874 __ Bind(&uncompressed_load);
2875 __ Ldrh(Register(OutputCPURegister(instruction)),
2876 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2877 __ Bind(&done);
2878 } else {
2879 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2880 source = HeapOperand(obj, offset);
2881 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002882 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002883 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002884 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002885 // We do not need to compute the intermediate address from the array: the
2886 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002887 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002888 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002889 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002890 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2891 }
2892 temp = obj;
2893 } else {
2894 __ Add(temp, obj, offset);
2895 }
jessicahandojo05765752016-09-09 19:01:32 -07002896 if (maybe_compressed_char_at) {
2897 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002898 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2899 "Expecting 0=compressed, 1=uncompressed");
2900 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002901 __ Ldrb(Register(OutputCPURegister(instruction)),
2902 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2903 __ B(&done);
2904 __ Bind(&uncompressed_load);
2905 __ Ldrh(Register(OutputCPURegister(instruction)),
2906 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2907 __ Bind(&done);
2908 } else {
2909 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2910 }
Roland Levillain44015862016-01-22 11:47:17 +00002911 }
jessicahandojo05765752016-09-09 19:01:32 -07002912 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002913 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2914 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002915 codegen_->Load(type, OutputCPURegister(instruction), source);
2916 codegen_->MaybeRecordImplicitNullCheck(instruction);
2917 }
Roland Levillain44015862016-01-22 11:47:17 +00002918
2919 if (type == Primitive::kPrimNot) {
2920 static_assert(
2921 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2922 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2923 Location obj_loc = locations->InAt(0);
2924 if (index.IsConstant()) {
2925 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2926 } else {
2927 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2928 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002929 }
Roland Levillain4d027112015-07-01 15:41:14 +01002930 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002931}
2932
Alexandre Rames5319def2014-10-23 10:03:10 +01002933void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2934 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2935 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002936 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002937}
2938
2939void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002940 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002941 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002942 {
2943 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2944 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2945 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2946 codegen_->MaybeRecordImplicitNullCheck(instruction);
2947 }
jessicahandojo05765752016-09-09 19:01:32 -07002948 // Mask out compression flag from String's array length.
2949 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002950 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002951 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002952}
2953
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002954void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002955 Primitive::Type value_type = instruction->GetComponentType();
2956
2957 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002958 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2959 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002960 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002961 LocationSummary::kCallOnSlowPath :
2962 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002963 locations->SetInAt(0, Location::RequiresRegister());
2964 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002965 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2966 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2967 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002968 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002969 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002970 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002971 }
2972}
2973
2974void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2975 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002976 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002977 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002978 bool needs_write_barrier =
2979 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002980
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002981 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002982 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002983 CPURegister source = value;
2984 Location index = locations->InAt(1);
2985 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2986 MemOperand destination = HeapOperand(array);
2987 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002988
2989 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002990 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002991 if (index.IsConstant()) {
2992 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2993 destination = HeapOperand(array, offset);
2994 } else {
2995 UseScratchRegisterScope temps(masm);
2996 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002997 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002998 // We do not need to compute the intermediate address from the array: the
2999 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01003000 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01003001 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01003002 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01003003 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
3004 }
3005 temp = array;
3006 } else {
3007 __ Add(temp, array, offset);
3008 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003009 destination = HeapOperand(temp,
3010 XRegisterFrom(index),
3011 LSL,
3012 Primitive::ComponentSizeShift(value_type));
3013 }
Artem Serov914d7a82017-02-07 14:33:49 +00003014 {
3015 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3016 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3017 codegen_->Store(value_type, value, destination);
3018 codegen_->MaybeRecordImplicitNullCheck(instruction);
3019 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003020 } else {
Artem Serov328429f2016-07-06 16:23:04 +01003021 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003022 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003023 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01003024 {
3025 // We use a block to end the scratch scope before the write barrier, thus
3026 // freeing the temporary registers so they can be used in `MarkGCCard`.
3027 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003028 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01003029 if (index.IsConstant()) {
3030 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003031 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01003032 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01003033 destination = HeapOperand(temp,
3034 XRegisterFrom(index),
3035 LSL,
3036 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01003037 }
3038
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003039 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3040 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3041 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3042
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003043 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003044 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
3045 codegen_->AddSlowPath(slow_path);
3046 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003047 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003048 __ Cbnz(Register(value), &non_zero);
3049 if (!index.IsConstant()) {
3050 __ Add(temp, array, offset);
3051 }
Artem Serov914d7a82017-02-07 14:33:49 +00003052 {
3053 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
3054 // emitted.
3055 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3056 __ Str(wzr, destination);
3057 codegen_->MaybeRecordImplicitNullCheck(instruction);
3058 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003059 __ B(&done);
3060 __ Bind(&non_zero);
3061 }
3062
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003063 // Note that when Baker read barriers are enabled, the type
3064 // checks are performed without read barriers. This is fine,
3065 // even in the case where a class object is in the from-space
3066 // after the flip, as a comparison involving such a type would
3067 // not produce a false positive; it may of course produce a
3068 // false negative, in which case we would take the ArraySet
3069 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003070
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003071 Register temp2 = temps.AcquireSameSizeAs(array);
3072 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003073 {
3074 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3075 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3076 __ Ldr(temp, HeapOperand(array, class_offset));
3077 codegen_->MaybeRecordImplicitNullCheck(instruction);
3078 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003079 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003080
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003081 // /* HeapReference<Class> */ temp = temp->component_type_
3082 __ Ldr(temp, HeapOperand(temp, component_offset));
3083 // /* HeapReference<Class> */ temp2 = value->klass_
3084 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3085 // If heap poisoning is enabled, no need to unpoison `temp`
3086 // nor `temp2`, as we are comparing two poisoned references.
3087 __ Cmp(temp, temp2);
3088 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003089
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003090 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3091 vixl::aarch64::Label do_put;
3092 __ B(eq, &do_put);
3093 // If heap poisoning is enabled, the `temp` reference has
3094 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003095 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3096
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003097 // /* HeapReference<Class> */ temp = temp->super_class_
3098 __ Ldr(temp, HeapOperand(temp, super_offset));
3099 // If heap poisoning is enabled, no need to unpoison
3100 // `temp`, as we are comparing against null below.
3101 __ Cbnz(temp, slow_path->GetEntryLabel());
3102 __ Bind(&do_put);
3103 } else {
3104 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003105 }
3106 }
3107
3108 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003109 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003110 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003111 __ Mov(temp2, value.W());
3112 GetAssembler()->PoisonHeapReference(temp2);
3113 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003114 }
3115
3116 if (!index.IsConstant()) {
3117 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003118 } else {
3119 // We no longer need the `temp` here so release it as the store below may
3120 // need a scratch register (if the constant index makes the offset too large)
3121 // and the poisoned `source` could be using the other scratch register.
3122 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003123 }
Artem Serov914d7a82017-02-07 14:33:49 +00003124 {
3125 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3126 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3127 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003128
Artem Serov914d7a82017-02-07 14:33:49 +00003129 if (!may_need_runtime_call_for_type_check) {
3130 codegen_->MaybeRecordImplicitNullCheck(instruction);
3131 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003132 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003133 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003134
3135 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3136
3137 if (done.IsLinked()) {
3138 __ Bind(&done);
3139 }
3140
3141 if (slow_path != nullptr) {
3142 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003143 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003144 }
3145}
3146
Alexandre Rames67555f72014-11-18 10:55:16 +00003147void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003148 RegisterSet caller_saves = RegisterSet::Empty();
3149 InvokeRuntimeCallingConvention calling_convention;
3150 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3151 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3152 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003153 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003154 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003155}
3156
3157void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003158 BoundsCheckSlowPathARM64* slow_path =
3159 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003160 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003161 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3162 __ B(slow_path->GetEntryLabel(), hs);
3163}
3164
Alexandre Rames67555f72014-11-18 10:55:16 +00003165void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3166 LocationSummary* locations =
3167 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3168 locations->SetInAt(0, Location::RequiresRegister());
3169 if (check->HasUses()) {
3170 locations->SetOut(Location::SameAsFirstInput());
3171 }
3172}
3173
3174void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3175 // We assume the class is not null.
3176 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3177 check->GetLoadClass(), check, check->GetDexPc(), true);
3178 codegen_->AddSlowPath(slow_path);
3179 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3180}
3181
Roland Levillain1a653882016-03-18 18:05:57 +00003182static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3183 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3184 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3185}
3186
3187void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3188 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3189 Location rhs_loc = instruction->GetLocations()->InAt(1);
3190 if (rhs_loc.IsConstant()) {
3191 // 0.0 is the only immediate that can be encoded directly in
3192 // an FCMP instruction.
3193 //
3194 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3195 // specify that in a floating-point comparison, positive zero
3196 // and negative zero are considered equal, so we can use the
3197 // literal 0.0 for both cases here.
3198 //
3199 // Note however that some methods (Float.equal, Float.compare,
3200 // Float.compareTo, Double.equal, Double.compare,
3201 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3202 // StrictMath.min) consider 0.0 to be (strictly) greater than
3203 // -0.0. So if we ever translate calls to these methods into a
3204 // HCompare instruction, we must handle the -0.0 case with
3205 // care here.
3206 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3207 __ Fcmp(lhs_reg, 0.0);
3208 } else {
3209 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3210 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003211}
3212
Serban Constantinescu02164b32014-11-13 14:05:07 +00003213void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003214 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003215 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3216 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003217 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003218 case Primitive::kPrimBoolean:
3219 case Primitive::kPrimByte:
3220 case Primitive::kPrimShort:
3221 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003222 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003223 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003224 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003225 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003226 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3227 break;
3228 }
3229 case Primitive::kPrimFloat:
3230 case Primitive::kPrimDouble: {
3231 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003232 locations->SetInAt(1,
3233 IsFloatingPointZeroConstant(compare->InputAt(1))
3234 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3235 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003236 locations->SetOut(Location::RequiresRegister());
3237 break;
3238 }
3239 default:
3240 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3241 }
3242}
3243
3244void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3245 Primitive::Type in_type = compare->InputAt(0)->GetType();
3246
3247 // 0 if: left == right
3248 // 1 if: left > right
3249 // -1 if: left < right
3250 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003251 case Primitive::kPrimBoolean:
3252 case Primitive::kPrimByte:
3253 case Primitive::kPrimShort:
3254 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003255 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003256 case Primitive::kPrimLong: {
3257 Register result = OutputRegister(compare);
3258 Register left = InputRegisterAt(compare, 0);
3259 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003260 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003261 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3262 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003263 break;
3264 }
3265 case Primitive::kPrimFloat:
3266 case Primitive::kPrimDouble: {
3267 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003268 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003269 __ Cset(result, ne);
3270 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003271 break;
3272 }
3273 default:
3274 LOG(FATAL) << "Unimplemented compare type " << in_type;
3275 }
3276}
3277
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003278void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003279 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003280
3281 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3282 locations->SetInAt(0, Location::RequiresFpuRegister());
3283 locations->SetInAt(1,
3284 IsFloatingPointZeroConstant(instruction->InputAt(1))
3285 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3286 : Location::RequiresFpuRegister());
3287 } else {
3288 // Integer cases.
3289 locations->SetInAt(0, Location::RequiresRegister());
3290 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3291 }
3292
David Brazdilb3e773e2016-01-26 11:28:37 +00003293 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003294 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003295 }
3296}
3297
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003298void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003299 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003300 return;
3301 }
3302
3303 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003304 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003305 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003306
Roland Levillain7f63c522015-07-13 15:54:55 +00003307 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003308 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003309 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003310 } else {
3311 // Integer cases.
3312 Register lhs = InputRegisterAt(instruction, 0);
3313 Operand rhs = InputOperandAt(instruction, 1);
3314 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003315 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003316 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003317}
3318
3319#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3320 M(Equal) \
3321 M(NotEqual) \
3322 M(LessThan) \
3323 M(LessThanOrEqual) \
3324 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003325 M(GreaterThanOrEqual) \
3326 M(Below) \
3327 M(BelowOrEqual) \
3328 M(Above) \
3329 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003330#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003331void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3332void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003333FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003334#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003335#undef FOR_EACH_CONDITION_INSTRUCTION
3336
Zheng Xuc6667102015-05-15 16:08:45 +08003337void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3338 DCHECK(instruction->IsDiv() || instruction->IsRem());
3339
3340 LocationSummary* locations = instruction->GetLocations();
3341 Location second = locations->InAt(1);
3342 DCHECK(second.IsConstant());
3343
3344 Register out = OutputRegister(instruction);
3345 Register dividend = InputRegisterAt(instruction, 0);
3346 int64_t imm = Int64FromConstant(second.GetConstant());
3347 DCHECK(imm == 1 || imm == -1);
3348
3349 if (instruction->IsRem()) {
3350 __ Mov(out, 0);
3351 } else {
3352 if (imm == 1) {
3353 __ Mov(out, dividend);
3354 } else {
3355 __ Neg(out, dividend);
3356 }
3357 }
3358}
3359
3360void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3361 DCHECK(instruction->IsDiv() || instruction->IsRem());
3362
3363 LocationSummary* locations = instruction->GetLocations();
3364 Location second = locations->InAt(1);
3365 DCHECK(second.IsConstant());
3366
3367 Register out = OutputRegister(instruction);
3368 Register dividend = InputRegisterAt(instruction, 0);
3369 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003370 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003371 int ctz_imm = CTZ(abs_imm);
3372
3373 UseScratchRegisterScope temps(GetVIXLAssembler());
3374 Register temp = temps.AcquireSameSizeAs(out);
3375
3376 if (instruction->IsDiv()) {
3377 __ Add(temp, dividend, abs_imm - 1);
3378 __ Cmp(dividend, 0);
3379 __ Csel(out, temp, dividend, lt);
3380 if (imm > 0) {
3381 __ Asr(out, out, ctz_imm);
3382 } else {
3383 __ Neg(out, Operand(out, ASR, ctz_imm));
3384 }
3385 } else {
3386 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3387 __ Asr(temp, dividend, bits - 1);
3388 __ Lsr(temp, temp, bits - ctz_imm);
3389 __ Add(out, dividend, temp);
3390 __ And(out, out, abs_imm - 1);
3391 __ Sub(out, out, temp);
3392 }
3393}
3394
3395void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3396 DCHECK(instruction->IsDiv() || instruction->IsRem());
3397
3398 LocationSummary* locations = instruction->GetLocations();
3399 Location second = locations->InAt(1);
3400 DCHECK(second.IsConstant());
3401
3402 Register out = OutputRegister(instruction);
3403 Register dividend = InputRegisterAt(instruction, 0);
3404 int64_t imm = Int64FromConstant(second.GetConstant());
3405
3406 Primitive::Type type = instruction->GetResultType();
3407 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3408
3409 int64_t magic;
3410 int shift;
3411 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3412
3413 UseScratchRegisterScope temps(GetVIXLAssembler());
3414 Register temp = temps.AcquireSameSizeAs(out);
3415
3416 // temp = get_high(dividend * magic)
3417 __ Mov(temp, magic);
3418 if (type == Primitive::kPrimLong) {
3419 __ Smulh(temp, dividend, temp);
3420 } else {
3421 __ Smull(temp.X(), dividend, temp);
3422 __ Lsr(temp.X(), temp.X(), 32);
3423 }
3424
3425 if (imm > 0 && magic < 0) {
3426 __ Add(temp, temp, dividend);
3427 } else if (imm < 0 && magic > 0) {
3428 __ Sub(temp, temp, dividend);
3429 }
3430
3431 if (shift != 0) {
3432 __ Asr(temp, temp, shift);
3433 }
3434
3435 if (instruction->IsDiv()) {
3436 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3437 } else {
3438 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3439 // TODO: Strength reduction for msub.
3440 Register temp_imm = temps.AcquireSameSizeAs(out);
3441 __ Mov(temp_imm, imm);
3442 __ Msub(out, temp, temp_imm, dividend);
3443 }
3444}
3445
3446void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3447 DCHECK(instruction->IsDiv() || instruction->IsRem());
3448 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003449 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003450
3451 LocationSummary* locations = instruction->GetLocations();
3452 Register out = OutputRegister(instruction);
3453 Location second = locations->InAt(1);
3454
3455 if (second.IsConstant()) {
3456 int64_t imm = Int64FromConstant(second.GetConstant());
3457
3458 if (imm == 0) {
3459 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3460 } else if (imm == 1 || imm == -1) {
3461 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003462 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003463 DivRemByPowerOfTwo(instruction);
3464 } else {
3465 DCHECK(imm <= -2 || imm >= 2);
3466 GenerateDivRemWithAnyConstant(instruction);
3467 }
3468 } else {
3469 Register dividend = InputRegisterAt(instruction, 0);
3470 Register divisor = InputRegisterAt(instruction, 1);
3471 if (instruction->IsDiv()) {
3472 __ Sdiv(out, dividend, divisor);
3473 } else {
3474 UseScratchRegisterScope temps(GetVIXLAssembler());
3475 Register temp = temps.AcquireSameSizeAs(out);
3476 __ Sdiv(temp, dividend, divisor);
3477 __ Msub(out, temp, divisor, dividend);
3478 }
3479 }
3480}
3481
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003482void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3483 LocationSummary* locations =
3484 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3485 switch (div->GetResultType()) {
3486 case Primitive::kPrimInt:
3487 case Primitive::kPrimLong:
3488 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003489 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003490 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3491 break;
3492
3493 case Primitive::kPrimFloat:
3494 case Primitive::kPrimDouble:
3495 locations->SetInAt(0, Location::RequiresFpuRegister());
3496 locations->SetInAt(1, Location::RequiresFpuRegister());
3497 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3498 break;
3499
3500 default:
3501 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3502 }
3503}
3504
3505void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3506 Primitive::Type type = div->GetResultType();
3507 switch (type) {
3508 case Primitive::kPrimInt:
3509 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003510 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003511 break;
3512
3513 case Primitive::kPrimFloat:
3514 case Primitive::kPrimDouble:
3515 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3516 break;
3517
3518 default:
3519 LOG(FATAL) << "Unexpected div type " << type;
3520 }
3521}
3522
Alexandre Rames67555f72014-11-18 10:55:16 +00003523void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003524 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003525 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003526}
3527
3528void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3529 SlowPathCodeARM64* slow_path =
3530 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3531 codegen_->AddSlowPath(slow_path);
3532 Location value = instruction->GetLocations()->InAt(0);
3533
Alexandre Rames3e69f162014-12-10 10:36:50 +00003534 Primitive::Type type = instruction->GetType();
3535
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003536 if (!Primitive::IsIntegralType(type)) {
3537 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003538 return;
3539 }
3540
Alexandre Rames67555f72014-11-18 10:55:16 +00003541 if (value.IsConstant()) {
3542 int64_t divisor = Int64ConstantFrom(value);
3543 if (divisor == 0) {
3544 __ B(slow_path->GetEntryLabel());
3545 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003546 // A division by a non-null constant is valid. We don't need to perform
3547 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003548 }
3549 } else {
3550 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3551 }
3552}
3553
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003554void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3555 LocationSummary* locations =
3556 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3557 locations->SetOut(Location::ConstantLocation(constant));
3558}
3559
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003560void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3561 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003562 // Will be generated at use site.
3563}
3564
Alexandre Rames5319def2014-10-23 10:03:10 +01003565void LocationsBuilderARM64::VisitExit(HExit* exit) {
3566 exit->SetLocations(nullptr);
3567}
3568
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003569void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003570}
3571
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003572void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3573 LocationSummary* locations =
3574 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3575 locations->SetOut(Location::ConstantLocation(constant));
3576}
3577
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003578void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003579 // Will be generated at use site.
3580}
3581
David Brazdilfc6a86a2015-06-26 10:33:45 +00003582void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003583 DCHECK(!successor->IsExitBlock());
3584 HBasicBlock* block = got->GetBlock();
3585 HInstruction* previous = got->GetPrevious();
3586 HLoopInformation* info = block->GetLoopInformation();
3587
David Brazdil46e2a392015-03-16 17:31:52 +00003588 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003589 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3590 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3591 return;
3592 }
3593 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3594 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01003595 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003596 }
3597 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003598 __ B(codegen_->GetLabelOf(successor));
3599 }
3600}
3601
David Brazdilfc6a86a2015-06-26 10:33:45 +00003602void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3603 got->SetLocations(nullptr);
3604}
3605
3606void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3607 HandleGoto(got, got->GetSuccessor());
3608}
3609
3610void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3611 try_boundary->SetLocations(nullptr);
3612}
3613
3614void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3615 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3616 if (!successor->IsExitBlock()) {
3617 HandleGoto(try_boundary, successor);
3618 }
3619}
3620
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003621void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003622 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003623 vixl::aarch64::Label* true_target,
3624 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003625 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003626
David Brazdil0debae72015-11-12 18:37:00 +00003627 if (true_target == nullptr && false_target == nullptr) {
3628 // Nothing to do. The code always falls through.
3629 return;
3630 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003631 // Constant condition, statically compared against "true" (integer value 1).
3632 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003633 if (true_target != nullptr) {
3634 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003635 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003636 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003637 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003638 if (false_target != nullptr) {
3639 __ B(false_target);
3640 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003641 }
David Brazdil0debae72015-11-12 18:37:00 +00003642 return;
3643 }
3644
3645 // The following code generates these patterns:
3646 // (1) true_target == nullptr && false_target != nullptr
3647 // - opposite condition true => branch to false_target
3648 // (2) true_target != nullptr && false_target == nullptr
3649 // - condition true => branch to true_target
3650 // (3) true_target != nullptr && false_target != nullptr
3651 // - condition true => branch to true_target
3652 // - branch to false_target
3653 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003654 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003655 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003656 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003657 if (true_target == nullptr) {
3658 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3659 } else {
3660 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3661 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003662 } else {
3663 // The condition instruction has not been materialized, use its inputs as
3664 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003665 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003666
David Brazdil0debae72015-11-12 18:37:00 +00003667 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003668 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003669 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003670 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003671 IfCondition opposite_condition = condition->GetOppositeCondition();
3672 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003673 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003674 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003675 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003676 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003677 // Integer cases.
3678 Register lhs = InputRegisterAt(condition, 0);
3679 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003680
3681 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003682 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003683 if (true_target == nullptr) {
3684 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3685 non_fallthrough_target = false_target;
3686 } else {
3687 arm64_cond = ARM64Condition(condition->GetCondition());
3688 non_fallthrough_target = true_target;
3689 }
3690
Aart Bik086d27e2016-01-20 17:02:00 -08003691 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003692 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003693 switch (arm64_cond) {
3694 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003695 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003696 break;
3697 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003698 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003699 break;
3700 case lt:
3701 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003702 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003703 break;
3704 case ge:
3705 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003706 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003707 break;
3708 default:
3709 // Without the `static_cast` the compiler throws an error for
3710 // `-Werror=sign-promo`.
3711 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3712 }
3713 } else {
3714 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003715 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003716 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003717 }
3718 }
David Brazdil0debae72015-11-12 18:37:00 +00003719
3720 // If neither branch falls through (case 3), the conditional branch to `true_target`
3721 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3722 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003723 __ B(false_target);
3724 }
3725}
3726
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003727void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3728 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003729 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003730 locations->SetInAt(0, Location::RequiresRegister());
3731 }
3732}
3733
3734void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003735 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3736 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003737 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3738 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3739 true_target = nullptr;
3740 }
3741 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3742 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3743 false_target = nullptr;
3744 }
David Brazdil0debae72015-11-12 18:37:00 +00003745 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003746}
3747
3748void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3749 LocationSummary* locations = new (GetGraph()->GetArena())
3750 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003751 InvokeRuntimeCallingConvention calling_convention;
3752 RegisterSet caller_saves = RegisterSet::Empty();
3753 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3754 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003755 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003756 locations->SetInAt(0, Location::RequiresRegister());
3757 }
3758}
3759
3760void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003761 SlowPathCodeARM64* slow_path =
3762 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003763 GenerateTestAndBranch(deoptimize,
3764 /* condition_input_index */ 0,
3765 slow_path->GetEntryLabel(),
3766 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003767}
3768
Mingyao Yang063fc772016-08-02 11:02:54 -07003769void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3770 LocationSummary* locations = new (GetGraph()->GetArena())
3771 LocationSummary(flag, LocationSummary::kNoCall);
3772 locations->SetOut(Location::RequiresRegister());
3773}
3774
3775void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3776 __ Ldr(OutputRegister(flag),
3777 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3778}
3779
David Brazdilc0b601b2016-02-08 14:20:45 +00003780static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3781 return condition->IsCondition() &&
3782 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3783}
3784
Alexandre Rames880f1192016-06-13 16:04:50 +01003785static inline Condition GetConditionForSelect(HCondition* condition) {
3786 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003787 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3788 : ARM64Condition(cond);
3789}
3790
David Brazdil74eb1b22015-12-14 11:44:01 +00003791void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3792 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003793 if (Primitive::IsFloatingPointType(select->GetType())) {
3794 locations->SetInAt(0, Location::RequiresFpuRegister());
3795 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003796 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003797 } else {
3798 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3799 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3800 bool is_true_value_constant = cst_true_value != nullptr;
3801 bool is_false_value_constant = cst_false_value != nullptr;
3802 // Ask VIXL whether we should synthesize constants in registers.
3803 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3804 Operand true_op = is_true_value_constant ?
3805 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3806 Operand false_op = is_false_value_constant ?
3807 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3808 bool true_value_in_register = false;
3809 bool false_value_in_register = false;
3810 MacroAssembler::GetCselSynthesisInformation(
3811 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3812 true_value_in_register |= !is_true_value_constant;
3813 false_value_in_register |= !is_false_value_constant;
3814
3815 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3816 : Location::ConstantLocation(cst_true_value));
3817 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3818 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003819 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003820 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003821
David Brazdil74eb1b22015-12-14 11:44:01 +00003822 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3823 locations->SetInAt(2, Location::RequiresRegister());
3824 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003825}
3826
3827void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003828 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003829 Condition csel_cond;
3830
3831 if (IsBooleanValueOrMaterializedCondition(cond)) {
3832 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003833 // Use the condition flags set by the previous instruction.
3834 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003835 } else {
3836 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003837 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003838 }
3839 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003840 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003841 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003842 } else {
3843 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003844 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003845 }
3846
Alexandre Rames880f1192016-06-13 16:04:50 +01003847 if (Primitive::IsFloatingPointType(select->GetType())) {
3848 __ Fcsel(OutputFPRegister(select),
3849 InputFPRegisterAt(select, 1),
3850 InputFPRegisterAt(select, 0),
3851 csel_cond);
3852 } else {
3853 __ Csel(OutputRegister(select),
3854 InputOperandAt(select, 1),
3855 InputOperandAt(select, 0),
3856 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003857 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003858}
3859
David Srbecky0cf44932015-12-09 14:09:59 +00003860void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3861 new (GetGraph()->GetArena()) LocationSummary(info);
3862}
3863
David Srbeckyd28f4a02016-03-14 17:14:24 +00003864void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3865 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003866}
3867
3868void CodeGeneratorARM64::GenerateNop() {
3869 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003870}
3871
Alexandre Rames5319def2014-10-23 10:03:10 +01003872void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003873 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003874}
3875
3876void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003877 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003878}
3879
3880void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003881 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003882}
3883
3884void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003885 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003886}
3887
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003888// Temp is used for read barrier.
3889static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3890 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003891 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003892 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3893 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3894 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3895 return 1;
3896 }
3897 return 0;
3898}
3899
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003900// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003901// interface pointer, one for loading the current interface.
3902// The other checks have one temp for loading the object's class.
3903static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3904 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3905 return 3;
3906 }
3907 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003908}
3909
Alexandre Rames67555f72014-11-18 10:55:16 +00003910void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003911 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003912 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003913 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003914 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003915 case TypeCheckKind::kExactCheck:
3916 case TypeCheckKind::kAbstractClassCheck:
3917 case TypeCheckKind::kClassHierarchyCheck:
3918 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003919 call_kind =
3920 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003921 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003922 break;
3923 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003924 case TypeCheckKind::kUnresolvedCheck:
3925 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003926 call_kind = LocationSummary::kCallOnSlowPath;
3927 break;
3928 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003929
Alexandre Rames67555f72014-11-18 10:55:16 +00003930 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003931 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003932 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003933 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003934 locations->SetInAt(0, Location::RequiresRegister());
3935 locations->SetInAt(1, Location::RequiresRegister());
3936 // The "out" register is used as a temporary, so it overlaps with the inputs.
3937 // Note that TypeCheckSlowPathARM64 uses this register too.
3938 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003939 // Add temps if necessary for read barriers.
3940 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003941}
3942
3943void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003944 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003945 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003946 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003947 Register obj = InputRegisterAt(instruction, 0);
3948 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003949 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003950 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003951 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3952 DCHECK_LE(num_temps, 1u);
3953 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003954 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3955 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3956 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3957 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003958
Scott Wakeling97c72b72016-06-24 16:19:36 +01003959 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003960 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003961
3962 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003963 // Avoid null check if we know `obj` is not null.
3964 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003965 __ Cbz(obj, &zero);
3966 }
3967
Roland Levillain44015862016-01-22 11:47:17 +00003968 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003969 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003970 // /* HeapReference<Class> */ out = obj->klass_
3971 GenerateReferenceLoadTwoRegisters(instruction,
3972 out_loc,
3973 obj_loc,
3974 class_offset,
3975 maybe_temp_loc,
3976 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003977 __ Cmp(out, cls);
3978 __ Cset(out, eq);
3979 if (zero.IsLinked()) {
3980 __ B(&done);
3981 }
3982 break;
3983 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003984
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003985 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003986 // /* HeapReference<Class> */ out = obj->klass_
3987 GenerateReferenceLoadTwoRegisters(instruction,
3988 out_loc,
3989 obj_loc,
3990 class_offset,
3991 maybe_temp_loc,
3992 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003993 // If the class is abstract, we eagerly fetch the super class of the
3994 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003995 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003996 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003997 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003998 GenerateReferenceLoadOneRegister(instruction,
3999 out_loc,
4000 super_offset,
4001 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004002 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004003 // If `out` is null, we use it for the result, and jump to `done`.
4004 __ Cbz(out, &done);
4005 __ Cmp(out, cls);
4006 __ B(ne, &loop);
4007 __ Mov(out, 1);
4008 if (zero.IsLinked()) {
4009 __ B(&done);
4010 }
4011 break;
4012 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004013
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004014 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004015 // /* HeapReference<Class> */ out = obj->klass_
4016 GenerateReferenceLoadTwoRegisters(instruction,
4017 out_loc,
4018 obj_loc,
4019 class_offset,
4020 maybe_temp_loc,
4021 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004022 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004023 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004024 __ Bind(&loop);
4025 __ Cmp(out, cls);
4026 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004027 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004028 GenerateReferenceLoadOneRegister(instruction,
4029 out_loc,
4030 super_offset,
4031 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004032 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004033 __ Cbnz(out, &loop);
4034 // If `out` is null, we use it for the result, and jump to `done`.
4035 __ B(&done);
4036 __ Bind(&success);
4037 __ Mov(out, 1);
4038 if (zero.IsLinked()) {
4039 __ B(&done);
4040 }
4041 break;
4042 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004043
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004044 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004045 // /* HeapReference<Class> */ out = obj->klass_
4046 GenerateReferenceLoadTwoRegisters(instruction,
4047 out_loc,
4048 obj_loc,
4049 class_offset,
4050 maybe_temp_loc,
4051 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004052 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004053 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004054 __ Cmp(out, cls);
4055 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004056 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004057 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004058 GenerateReferenceLoadOneRegister(instruction,
4059 out_loc,
4060 component_offset,
4061 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004062 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004063 // If `out` is null, we use it for the result, and jump to `done`.
4064 __ Cbz(out, &done);
4065 __ Ldrh(out, HeapOperand(out, primitive_offset));
4066 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4067 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004068 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004069 __ Mov(out, 1);
4070 __ B(&done);
4071 break;
4072 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004073
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004074 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004075 // No read barrier since the slow path will retry upon failure.
4076 // /* HeapReference<Class> */ out = obj->klass_
4077 GenerateReferenceLoadTwoRegisters(instruction,
4078 out_loc,
4079 obj_loc,
4080 class_offset,
4081 maybe_temp_loc,
4082 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004083 __ Cmp(out, cls);
4084 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004085 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4086 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004087 codegen_->AddSlowPath(slow_path);
4088 __ B(ne, slow_path->GetEntryLabel());
4089 __ Mov(out, 1);
4090 if (zero.IsLinked()) {
4091 __ B(&done);
4092 }
4093 break;
4094 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004095
Calin Juravle98893e12015-10-02 21:05:03 +01004096 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004097 case TypeCheckKind::kInterfaceCheck: {
4098 // Note that we indeed only call on slow path, but we always go
4099 // into the slow path for the unresolved and interface check
4100 // cases.
4101 //
4102 // We cannot directly call the InstanceofNonTrivial runtime
4103 // entry point without resorting to a type checking slow path
4104 // here (i.e. by calling InvokeRuntime directly), as it would
4105 // require to assign fixed registers for the inputs of this
4106 // HInstanceOf instruction (following the runtime calling
4107 // convention), which might be cluttered by the potential first
4108 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004109 //
4110 // TODO: Introduce a new runtime entry point taking the object
4111 // to test (instead of its class) as argument, and let it deal
4112 // with the read barrier issues. This will let us refactor this
4113 // case of the `switch` code as it was previously (with a direct
4114 // call to the runtime not using a type checking slow path).
4115 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004116 DCHECK(locations->OnlyCallsOnSlowPath());
4117 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4118 /* is_fatal */ false);
4119 codegen_->AddSlowPath(slow_path);
4120 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004121 if (zero.IsLinked()) {
4122 __ B(&done);
4123 }
4124 break;
4125 }
4126 }
4127
4128 if (zero.IsLinked()) {
4129 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004130 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004131 }
4132
4133 if (done.IsLinked()) {
4134 __ Bind(&done);
4135 }
4136
4137 if (slow_path != nullptr) {
4138 __ Bind(slow_path->GetExitLabel());
4139 }
4140}
4141
4142void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4143 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4144 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4145
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004146 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4147 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004148 case TypeCheckKind::kExactCheck:
4149 case TypeCheckKind::kAbstractClassCheck:
4150 case TypeCheckKind::kClassHierarchyCheck:
4151 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004152 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4153 LocationSummary::kCallOnSlowPath :
4154 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004155 break;
4156 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004157 case TypeCheckKind::kUnresolvedCheck:
4158 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004159 call_kind = LocationSummary::kCallOnSlowPath;
4160 break;
4161 }
4162
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004163 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4164 locations->SetInAt(0, Location::RequiresRegister());
4165 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004166 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4167 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004168}
4169
4170void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004171 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004172 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004173 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004174 Register obj = InputRegisterAt(instruction, 0);
4175 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004176 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4177 DCHECK_GE(num_temps, 1u);
4178 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004179 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004180 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4181 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004182 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004183 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4184 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4185 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4186 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4187 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4188 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4189 const uint32_t object_array_data_offset =
4190 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004191
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004192 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004193 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4194 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4195 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004196 if (!kEmitCompilerReadBarrier) {
4197 is_type_check_slow_path_fatal =
4198 (type_check_kind == TypeCheckKind::kExactCheck ||
4199 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4200 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4201 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4202 !instruction->CanThrowIntoCatchBlock();
4203 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004204 SlowPathCodeARM64* type_check_slow_path =
4205 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4206 is_type_check_slow_path_fatal);
4207 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004208
Scott Wakeling97c72b72016-06-24 16:19:36 +01004209 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004210 // Avoid null check if we know obj is not null.
4211 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004212 __ Cbz(obj, &done);
4213 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004214
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004215 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004216 case TypeCheckKind::kExactCheck:
4217 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004218 // /* HeapReference<Class> */ temp = obj->klass_
4219 GenerateReferenceLoadTwoRegisters(instruction,
4220 temp_loc,
4221 obj_loc,
4222 class_offset,
4223 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004224 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004225
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004226 __ Cmp(temp, cls);
4227 // Jump to slow path for throwing the exception or doing a
4228 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004229 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004230 break;
4231 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004232
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004233 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004234 // /* HeapReference<Class> */ temp = obj->klass_
4235 GenerateReferenceLoadTwoRegisters(instruction,
4236 temp_loc,
4237 obj_loc,
4238 class_offset,
4239 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004240 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004241
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004242 // If the class is abstract, we eagerly fetch the super class of the
4243 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004244 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004245 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004246 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004247 GenerateReferenceLoadOneRegister(instruction,
4248 temp_loc,
4249 super_offset,
4250 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004251 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004252
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004253 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4254 // exception.
4255 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4256 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004257 __ Cmp(temp, cls);
4258 __ B(ne, &loop);
4259 break;
4260 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004261
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004262 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004263 // /* HeapReference<Class> */ temp = obj->klass_
4264 GenerateReferenceLoadTwoRegisters(instruction,
4265 temp_loc,
4266 obj_loc,
4267 class_offset,
4268 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004269 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004270
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004271 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004272 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004273 __ Bind(&loop);
4274 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004275 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004276
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004277 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004278 GenerateReferenceLoadOneRegister(instruction,
4279 temp_loc,
4280 super_offset,
4281 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004282 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004283
4284 // If the class reference currently in `temp` is not null, jump
4285 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004286 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004287 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004288 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004289 break;
4290 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004291
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004292 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004293 // /* HeapReference<Class> */ temp = obj->klass_
4294 GenerateReferenceLoadTwoRegisters(instruction,
4295 temp_loc,
4296 obj_loc,
4297 class_offset,
4298 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004299 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004300
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004301 // Do an exact check.
4302 __ Cmp(temp, cls);
4303 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004304
4305 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004306 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004307 GenerateReferenceLoadOneRegister(instruction,
4308 temp_loc,
4309 component_offset,
4310 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004311 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004312
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004313 // If the component type is null, jump to the slow path to throw the exception.
4314 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4315 // Otherwise, the object is indeed an array. Further check that this component type is not a
4316 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004317 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4318 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004319 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004320 break;
4321 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004322
Calin Juravle98893e12015-10-02 21:05:03 +01004323 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004324 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004325 //
4326 // We cannot directly call the CheckCast runtime entry point
4327 // without resorting to a type checking slow path here (i.e. by
4328 // calling InvokeRuntime directly), as it would require to
4329 // assign fixed registers for the inputs of this HInstanceOf
4330 // instruction (following the runtime calling convention), which
4331 // might be cluttered by the potential first read barrier
4332 // emission at the beginning of this method.
4333 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004334 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004335 case TypeCheckKind::kInterfaceCheck: {
4336 // /* HeapReference<Class> */ temp = obj->klass_
4337 GenerateReferenceLoadTwoRegisters(instruction,
4338 temp_loc,
4339 obj_loc,
4340 class_offset,
4341 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004342 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004343
4344 // /* HeapReference<Class> */ temp = temp->iftable_
4345 GenerateReferenceLoadTwoRegisters(instruction,
4346 temp_loc,
4347 temp_loc,
4348 iftable_offset,
4349 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004350 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004351 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004352 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004353 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004354 vixl::aarch64::Label start_loop;
4355 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004356 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004357 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4358 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004359 // Go to next interface.
4360 __ Add(temp, temp, 2 * kHeapReferenceSize);
4361 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004362 // Compare the classes and continue the loop if they do not match.
4363 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4364 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004365 break;
4366 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004367 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004368 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004369
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004370 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004371}
4372
Alexandre Rames5319def2014-10-23 10:03:10 +01004373void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4374 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4375 locations->SetOut(Location::ConstantLocation(constant));
4376}
4377
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004378void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004379 // Will be generated at use site.
4380}
4381
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004382void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4383 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4384 locations->SetOut(Location::ConstantLocation(constant));
4385}
4386
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004387void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004388 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004389}
4390
Calin Juravle175dc732015-08-25 15:42:32 +01004391void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4392 // The trampoline uses the same calling convention as dex calling conventions,
4393 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4394 // the method_idx.
4395 HandleInvoke(invoke);
4396}
4397
4398void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4399 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004400 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004401}
4402
Alexandre Rames5319def2014-10-23 10:03:10 +01004403void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004404 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004405 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004406}
4407
Alexandre Rames67555f72014-11-18 10:55:16 +00004408void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4409 HandleInvoke(invoke);
4410}
4411
4412void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4413 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004414 LocationSummary* locations = invoke->GetLocations();
4415 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004416 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004417 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004418 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004419
4420 // The register ip1 is required to be used for the hidden argument in
4421 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004422 MacroAssembler* masm = GetVIXLAssembler();
4423 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004424 scratch_scope.Exclude(ip1);
4425 __ Mov(ip1, invoke->GetDexMethodIndex());
4426
Artem Serov914d7a82017-02-07 14:33:49 +00004427 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004428 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004429 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004430 {
4431 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4432 // /* HeapReference<Class> */ temp = temp->klass_
4433 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4434 codegen_->MaybeRecordImplicitNullCheck(invoke);
4435 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004436 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004437 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004438 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004439 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004440 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004441 }
Artem Serov914d7a82017-02-07 14:33:49 +00004442
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004443 // Instead of simply (possibly) unpoisoning `temp` here, we should
4444 // emit a read barrier for the previous class reference load.
4445 // However this is not required in practice, as this is an
4446 // intermediate/temporary reference and because the current
4447 // concurrent copying collector keeps the from-space memory
4448 // intact/accessible until the end of the marking phase (the
4449 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004450 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004451 __ Ldr(temp,
4452 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4453 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004454 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004455 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004456 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004457 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004458 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004459
4460 {
4461 // Ensure the pc position is recorded immediately after the `blr` instruction.
4462 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4463
4464 // lr();
4465 __ blr(lr);
4466 DCHECK(!codegen_->IsLeafMethod());
4467 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4468 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004469
4470 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004471}
4472
4473void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004474 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004475 if (intrinsic.TryDispatch(invoke)) {
4476 return;
4477 }
4478
Alexandre Rames67555f72014-11-18 10:55:16 +00004479 HandleInvoke(invoke);
4480}
4481
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004482void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004483 // Explicit clinit checks triggered by static invokes must have been pruned by
4484 // art::PrepareForRegisterAllocation.
4485 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004486
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004487 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004488 if (intrinsic.TryDispatch(invoke)) {
4489 return;
4490 }
4491
Alexandre Rames67555f72014-11-18 10:55:16 +00004492 HandleInvoke(invoke);
4493}
4494
Andreas Gampe878d58c2015-01-15 23:24:00 -08004495static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4496 if (invoke->GetLocations()->Intrinsified()) {
4497 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4498 intrinsic.Dispatch(invoke);
4499 return true;
4500 }
4501 return false;
4502}
4503
Vladimir Markodc151b22015-10-15 18:02:30 +01004504HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4505 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004506 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004507 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004508 return desired_dispatch_info;
4509}
4510
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004511void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4512 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004513 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004514 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4515 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004516 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4517 uint32_t offset =
4518 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004519 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004520 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004521 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004522 }
Vladimir Marko58155012015-08-19 12:49:41 +00004523 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004524 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004525 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004526 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4527 DCHECK(GetCompilerOptions().IsBootImage());
4528 // Add ADRP with its PC-relative method patch.
4529 vixl::aarch64::Label* adrp_label = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
4530 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4531 // Add ADD with its PC-relative method patch.
4532 vixl::aarch64::Label* add_label =
4533 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), adrp_label);
4534 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4535 break;
4536 }
Vladimir Marko58155012015-08-19 12:49:41 +00004537 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4538 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004539 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004540 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004541 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko58155012015-08-19 12:49:41 +00004542 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004543 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4544 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004545 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004546 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004547 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004548 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004549 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004550 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004551 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004552 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4553 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4554 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004555 }
4556 }
4557
4558 switch (invoke->GetCodePtrLocation()) {
4559 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004560 {
4561 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4562 ExactAssemblyScope eas(GetVIXLAssembler(),
4563 kInstructionSize,
4564 CodeBufferCheckScope::kExactSize);
4565 __ bl(&frame_entry_label_);
4566 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4567 }
Vladimir Marko58155012015-08-19 12:49:41 +00004568 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004569 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4570 // LR = callee_method->entry_point_from_quick_compiled_code_;
4571 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004572 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004573 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004574 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004575 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004576 ExactAssemblyScope eas(GetVIXLAssembler(),
4577 kInstructionSize,
4578 CodeBufferCheckScope::kExactSize);
4579 // lr()
4580 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004581 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004582 }
Vladimir Marko58155012015-08-19 12:49:41 +00004583 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004584 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004585
Andreas Gampe878d58c2015-01-15 23:24:00 -08004586 DCHECK(!IsLeafMethod());
4587}
4588
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004589void CodeGeneratorARM64::GenerateVirtualCall(
4590 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004591 // Use the calling convention instead of the location of the receiver, as
4592 // intrinsics may have put the receiver in a different register. In the intrinsics
4593 // slow path, the arguments have been moved to the right place, so here we are
4594 // guaranteed that the receiver is the first register of the calling convention.
4595 InvokeDexCallingConvention calling_convention;
4596 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004597 Register temp = XRegisterFrom(temp_in);
4598 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4599 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4600 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004601 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004602
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004603 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004604
4605 {
4606 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4607 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4608 // /* HeapReference<Class> */ temp = receiver->klass_
4609 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4610 MaybeRecordImplicitNullCheck(invoke);
4611 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004612 // Instead of simply (possibly) unpoisoning `temp` here, we should
4613 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004614 // intermediate/temporary reference and because the current
4615 // concurrent copying collector keeps the from-space memory
4616 // intact/accessible until the end of the marking phase (the
4617 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004618 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4619 // temp = temp->GetMethodAt(method_offset);
4620 __ Ldr(temp, MemOperand(temp, method_offset));
4621 // lr = temp->GetEntryPoint();
4622 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004623 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004624 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004625 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4626 // lr();
4627 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004628 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004629 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004630}
4631
Orion Hodsonac141392017-01-13 11:53:47 +00004632void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4633 HandleInvoke(invoke);
4634}
4635
4636void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4637 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004638 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004639}
4640
Vladimir Marko65979462017-05-19 17:25:12 +01004641vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeMethodPatch(
4642 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004643 vixl::aarch64::Label* adrp_label) {
Vladimir Marko65979462017-05-19 17:25:12 +01004644 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004645 target_method.index,
Vladimir Marko65979462017-05-19 17:25:12 +01004646 adrp_label,
4647 &pc_relative_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004648}
4649
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004650vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4651 MethodReference target_method,
4652 vixl::aarch64::Label* adrp_label) {
4653 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004654 target_method.index,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004655 adrp_label,
4656 &method_bss_entry_patches_);
4657}
4658
Scott Wakeling97c72b72016-06-24 16:19:36 +01004659vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4660 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004661 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004662 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004663 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004664}
4665
Vladimir Marko1998cd02017-01-13 13:02:58 +00004666vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4667 const DexFile& dex_file,
4668 dex::TypeIndex type_index,
4669 vixl::aarch64::Label* adrp_label) {
4670 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4671}
4672
Vladimir Marko65979462017-05-19 17:25:12 +01004673vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4674 const DexFile& dex_file,
4675 dex::StringIndex string_index,
4676 vixl::aarch64::Label* adrp_label) {
4677 return
4678 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
4679}
4680
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004681vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4682 const DexFile& dex_file,
4683 dex::StringIndex string_index,
4684 vixl::aarch64::Label* adrp_label) {
4685 return NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
4686}
4687
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004688vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4689 baker_read_barrier_patches_.emplace_back(custom_data);
4690 return &baker_read_barrier_patches_.back().label;
4691}
4692
Scott Wakeling97c72b72016-06-24 16:19:36 +01004693vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4694 const DexFile& dex_file,
4695 uint32_t offset_or_index,
4696 vixl::aarch64::Label* adrp_label,
4697 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004698 // Add a patch entry and return the label.
4699 patches->emplace_back(dex_file, offset_or_index);
4700 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004701 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004702 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4703 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4704 return label;
4705}
4706
Scott Wakeling97c72b72016-06-24 16:19:36 +01004707vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4708 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004709 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004710}
4711
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004712vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004713 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4714 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4715 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004716 return jit_string_patches_.GetOrCreate(
4717 StringReference(&dex_file, string_index),
4718 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4719}
4720
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004721vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004722 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4723 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4724 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004725 return jit_class_patches_.GetOrCreate(
4726 TypeReference(&dex_file, type_index),
4727 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4728}
4729
Vladimir Markoaad75c62016-10-03 08:46:48 +00004730void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4731 vixl::aarch64::Register reg) {
4732 DCHECK(reg.IsX());
4733 SingleEmissionCheckScope guard(GetVIXLAssembler());
4734 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004735 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004736}
4737
4738void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4739 vixl::aarch64::Register out,
4740 vixl::aarch64::Register base) {
4741 DCHECK(out.IsX());
4742 DCHECK(base.IsX());
4743 SingleEmissionCheckScope guard(GetVIXLAssembler());
4744 __ Bind(fixup_label);
4745 __ add(out, base, Operand(/* offset placeholder */ 0));
4746}
4747
4748void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4749 vixl::aarch64::Register out,
4750 vixl::aarch64::Register base) {
4751 DCHECK(base.IsX());
4752 SingleEmissionCheckScope guard(GetVIXLAssembler());
4753 __ Bind(fixup_label);
4754 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4755}
4756
4757template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4758inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4759 const ArenaDeque<PcRelativePatchInfo>& infos,
4760 ArenaVector<LinkerPatch>* linker_patches) {
4761 for (const PcRelativePatchInfo& info : infos) {
4762 linker_patches->push_back(Factory(info.label.GetLocation(),
4763 &info.target_dex_file,
4764 info.pc_insn_label->GetLocation(),
4765 info.offset_or_index));
4766 }
4767}
4768
Vladimir Marko58155012015-08-19 12:49:41 +00004769void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4770 DCHECK(linker_patches->empty());
4771 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004772 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004773 method_bss_entry_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004774 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004775 type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004776 pc_relative_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004777 string_bss_entry_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004778 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004779 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01004780 if (GetCompilerOptions().IsBootImage()) {
4781 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004782 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004783 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4784 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004785 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4786 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004787 } else {
4788 DCHECK(pc_relative_method_patches_.empty());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004789 EmitPcRelativeLinkerPatches<LinkerPatch::TypeClassTablePatch>(pc_relative_type_patches_,
4790 linker_patches);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004791 EmitPcRelativeLinkerPatches<LinkerPatch::StringInternTablePatch>(pc_relative_string_patches_,
4792 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004793 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004794 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
4795 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004796 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4797 linker_patches);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004798 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_bss_entry_patches_,
4799 linker_patches);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004800 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
4801 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
4802 info.custom_data));
4803 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004804 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004805}
4806
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004807vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4808 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004809 value,
4810 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4811}
4812
Scott Wakeling97c72b72016-06-24 16:19:36 +01004813vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004814 return uint64_literals_.GetOrCreate(
4815 value,
4816 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004817}
4818
Andreas Gampe878d58c2015-01-15 23:24:00 -08004819void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004820 // Explicit clinit checks triggered by static invokes must have been pruned by
4821 // art::PrepareForRegisterAllocation.
4822 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004823
Andreas Gampe878d58c2015-01-15 23:24:00 -08004824 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004825 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004826 return;
4827 }
4828
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004829 {
4830 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4831 // are no pools emitted.
4832 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4833 LocationSummary* locations = invoke->GetLocations();
4834 codegen_->GenerateStaticOrDirectCall(
4835 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
4836 }
4837
4838 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004839}
4840
4841void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004842 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004843 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004844 return;
4845 }
4846
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004847 {
4848 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4849 // are no pools emitted.
4850 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4851 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
4852 DCHECK(!codegen_->IsLeafMethod());
4853 }
4854
4855 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004856}
4857
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004858HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4859 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004860 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004861 case HLoadClass::LoadKind::kInvalid:
4862 LOG(FATAL) << "UNREACHABLE";
4863 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004864 case HLoadClass::LoadKind::kReferrersClass:
4865 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004866 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004867 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004868 case HLoadClass::LoadKind::kBssEntry:
4869 DCHECK(!Runtime::Current()->UseJitCompilation());
4870 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004871 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004872 DCHECK(Runtime::Current()->UseJitCompilation());
4873 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004874 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004875 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004876 break;
4877 }
4878 return desired_class_load_kind;
4879}
4880
Alexandre Rames67555f72014-11-18 10:55:16 +00004881void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004882 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004883 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004884 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004885 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004886 cls,
4887 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004888 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004889 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004890 return;
4891 }
Vladimir Marko41559982017-01-06 14:04:23 +00004892 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004893
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004894 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4895 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004896 ? LocationSummary::kCallOnSlowPath
4897 : LocationSummary::kNoCall;
4898 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004899 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004900 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004901 }
4902
Vladimir Marko41559982017-01-06 14:04:23 +00004903 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004904 locations->SetInAt(0, Location::RequiresRegister());
4905 }
4906 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004907 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4908 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4909 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004910 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004911 RegisterSet caller_saves = RegisterSet::Empty();
4912 InvokeRuntimeCallingConvention calling_convention;
4913 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4914 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4915 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4916 Primitive::kPrimNot).GetCode());
4917 locations->SetCustomSlowPathCallerSaves(caller_saves);
4918 } else {
4919 // For non-Baker read barrier we have a temp-clobbering call.
4920 }
4921 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004922}
4923
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004924// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4925// move.
4926void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004927 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004928 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00004929 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004930 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01004931 return;
4932 }
Vladimir Marko41559982017-01-06 14:04:23 +00004933 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004934
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004935 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004936 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004937 Register bss_entry_temp;
4938 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004939
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004940 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4941 ? kWithoutReadBarrier
4942 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004943 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004944 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004945 case HLoadClass::LoadKind::kReferrersClass: {
4946 DCHECK(!cls->CanCallRuntime());
4947 DCHECK(!cls->MustGenerateClinitCheck());
4948 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4949 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004950 GenerateGcRootFieldLoad(cls,
4951 out_loc,
4952 current_method,
4953 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004954 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004955 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004956 break;
4957 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004958 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004959 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004960 // Add ADRP with its PC-relative type patch.
4961 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004962 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004963 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004964 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004965 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004966 vixl::aarch64::Label* add_label =
4967 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004968 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004969 break;
4970 }
4971 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004972 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004973 uint32_t address = dchecked_integral_cast<uint32_t>(
4974 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4975 DCHECK_NE(address, 0u);
4976 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004977 break;
4978 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004979 case HLoadClass::LoadKind::kBootImageClassTable: {
4980 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
4981 // Add ADRP with its PC-relative type patch.
4982 const DexFile& dex_file = cls->GetDexFile();
4983 dex::TypeIndex type_index = cls->GetTypeIndex();
4984 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
4985 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
4986 // Add LDR with its PC-relative type patch.
4987 vixl::aarch64::Label* ldr_label =
4988 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
4989 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
4990 // Extract the reference from the slot data, i.e. clear the hash bits.
4991 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
4992 ComputeModifiedUtf8Hash(dex_file.StringByTypeIdx(type_index)));
4993 if (masked_hash != 0) {
4994 __ Sub(out.W(), out.W(), Operand(masked_hash));
4995 }
4996 break;
4997 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004998 case HLoadClass::LoadKind::kBssEntry: {
4999 // Add ADRP with its PC-relative Class .bss entry patch.
5000 const DexFile& dex_file = cls->GetDexFile();
5001 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005002 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005003 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
5004 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005005 // Add LDR with its PC-relative Class patch.
5006 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00005007 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005008 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
5009 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005010 out_loc,
5011 bss_entry_temp,
5012 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005013 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005014 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005015 generate_null_check = true;
5016 break;
5017 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005018 case HLoadClass::LoadKind::kJitTableAddress: {
5019 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5020 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005021 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005022 GenerateGcRootFieldLoad(cls,
5023 out_loc,
5024 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005025 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01005026 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005027 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005028 break;
5029 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005030 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005031 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005032 LOG(FATAL) << "UNREACHABLE";
5033 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005034 }
5035
Vladimir Markoea4c1262017-02-06 19:59:33 +00005036 bool do_clinit = cls->MustGenerateClinitCheck();
5037 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005038 DCHECK(cls->CanCallRuntime());
5039 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00005040 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005041 codegen_->AddSlowPath(slow_path);
5042 if (generate_null_check) {
5043 __ Cbz(out, slow_path->GetEntryLabel());
5044 }
5045 if (cls->MustGenerateClinitCheck()) {
5046 GenerateClassInitializationCheck(slow_path, out);
5047 } else {
5048 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005049 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005050 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005051 }
5052}
5053
David Brazdilcb1c0552015-08-04 16:22:25 +01005054static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005055 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005056}
5057
Alexandre Rames67555f72014-11-18 10:55:16 +00005058void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5059 LocationSummary* locations =
5060 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5061 locations->SetOut(Location::RequiresRegister());
5062}
5063
5064void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005065 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5066}
5067
5068void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
5069 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5070}
5071
5072void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5073 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005074}
5075
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005076HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5077 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005078 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005079 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005080 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005081 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005082 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005083 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005084 case HLoadString::LoadKind::kJitTableAddress:
5085 DCHECK(Runtime::Current()->UseJitCompilation());
5086 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005087 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005088 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005089 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005090 }
5091 return desired_string_load_kind;
5092}
5093
Alexandre Rames67555f72014-11-18 10:55:16 +00005094void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005095 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005096 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005097 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005098 InvokeRuntimeCallingConvention calling_convention;
5099 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5100 } else {
5101 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005102 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5103 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005104 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005105 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005106 RegisterSet caller_saves = RegisterSet::Empty();
5107 InvokeRuntimeCallingConvention calling_convention;
5108 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5109 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
5110 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
5111 Primitive::kPrimNot).GetCode());
5112 locations->SetCustomSlowPathCallerSaves(caller_saves);
5113 } else {
5114 // For non-Baker read barrier we have a temp-clobbering call.
5115 }
5116 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005117 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005118}
5119
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005120// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5121// move.
5122void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005123 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005124 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005125
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005126 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005127 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005128 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005129 // Add ADRP with its PC-relative String patch.
5130 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005131 const dex::StringIndex string_index = load->GetStringIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01005132 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005133 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005134 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005135 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005136 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005137 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005138 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005139 }
5140 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005141 uint32_t address = dchecked_integral_cast<uint32_t>(
5142 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5143 DCHECK_NE(address, 0u);
5144 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005145 return;
5146 }
5147 case HLoadString::LoadKind::kBootImageInternTable: {
5148 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5149 // Add ADRP with its PC-relative String patch.
5150 const DexFile& dex_file = load->GetDexFile();
5151 const dex::StringIndex string_index = load->GetStringIndex();
5152 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
5153 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
5154 // Add LDR with its PC-relative String patch.
5155 vixl::aarch64::Label* ldr_label =
5156 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
5157 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5158 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005159 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005160 case HLoadString::LoadKind::kBssEntry: {
5161 // Add ADRP with its PC-relative String .bss entry patch.
5162 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005163 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005164 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005165 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005166 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005167 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005168 // Add LDR with its .bss entry String patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005169 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005170 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005171 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005172 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005173 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005174 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005175 /* offset placeholder */ 0u,
5176 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005177 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005178 SlowPathCodeARM64* slow_path =
5179 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005180 codegen_->AddSlowPath(slow_path);
5181 __ Cbz(out.X(), slow_path->GetEntryLabel());
5182 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005183 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005184 return;
5185 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005186 case HLoadString::LoadKind::kJitTableAddress: {
5187 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005188 load->GetStringIndex(),
5189 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005190 GenerateGcRootFieldLoad(load,
5191 out_loc,
5192 out.X(),
5193 /* offset */ 0,
5194 /* fixup_label */ nullptr,
5195 kCompilerReadBarrierOption);
5196 return;
5197 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005198 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005199 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005200 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005201
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005202 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005203 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005204 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005205 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005206 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5207 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005208 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005209}
5210
Alexandre Rames5319def2014-10-23 10:03:10 +01005211void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5212 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5213 locations->SetOut(Location::ConstantLocation(constant));
5214}
5215
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005216void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005217 // Will be generated at use site.
5218}
5219
Alexandre Rames67555f72014-11-18 10:55:16 +00005220void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5221 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005222 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005223 InvokeRuntimeCallingConvention calling_convention;
5224 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5225}
5226
5227void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005228 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005229 instruction,
5230 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005231 if (instruction->IsEnter()) {
5232 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5233 } else {
5234 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5235 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005236 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005237}
5238
Alexandre Rames42d641b2014-10-27 14:00:51 +00005239void LocationsBuilderARM64::VisitMul(HMul* mul) {
5240 LocationSummary* locations =
5241 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5242 switch (mul->GetResultType()) {
5243 case Primitive::kPrimInt:
5244 case Primitive::kPrimLong:
5245 locations->SetInAt(0, Location::RequiresRegister());
5246 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005247 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005248 break;
5249
5250 case Primitive::kPrimFloat:
5251 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005252 locations->SetInAt(0, Location::RequiresFpuRegister());
5253 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005254 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005255 break;
5256
5257 default:
5258 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5259 }
5260}
5261
5262void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5263 switch (mul->GetResultType()) {
5264 case Primitive::kPrimInt:
5265 case Primitive::kPrimLong:
5266 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5267 break;
5268
5269 case Primitive::kPrimFloat:
5270 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005271 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005272 break;
5273
5274 default:
5275 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5276 }
5277}
5278
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005279void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5280 LocationSummary* locations =
5281 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5282 switch (neg->GetResultType()) {
5283 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005284 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005285 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005286 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005287 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005288
5289 case Primitive::kPrimFloat:
5290 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005291 locations->SetInAt(0, Location::RequiresFpuRegister());
5292 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005293 break;
5294
5295 default:
5296 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5297 }
5298}
5299
5300void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5301 switch (neg->GetResultType()) {
5302 case Primitive::kPrimInt:
5303 case Primitive::kPrimLong:
5304 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5305 break;
5306
5307 case Primitive::kPrimFloat:
5308 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005309 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005310 break;
5311
5312 default:
5313 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5314 }
5315}
5316
5317void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5318 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005319 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005320 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005321 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005322 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5323 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005324}
5325
5326void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005327 // Note: if heap poisoning is enabled, the entry point takes cares
5328 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005329 QuickEntrypointEnum entrypoint =
5330 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5331 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005332 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005333 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005334}
5335
Alexandre Rames5319def2014-10-23 10:03:10 +01005336void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5337 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005338 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005339 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005340 if (instruction->IsStringAlloc()) {
5341 locations->AddTemp(LocationFrom(kArtMethodRegister));
5342 } else {
5343 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005344 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005345 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5346}
5347
5348void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005349 // Note: if heap poisoning is enabled, the entry point takes cares
5350 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005351 if (instruction->IsStringAlloc()) {
5352 // String is allocated through StringFactory. Call NewEmptyString entry point.
5353 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005354 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005355 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5356 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005357
5358 {
5359 // Ensure the pc position is recorded immediately after the `blr` instruction.
5360 ExactAssemblyScope eas(GetVIXLAssembler(),
5361 kInstructionSize,
5362 CodeBufferCheckScope::kExactSize);
5363 __ blr(lr);
5364 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5365 }
David Brazdil6de19382016-01-08 17:37:10 +00005366 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005367 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005368 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005369 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005370 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005371}
5372
5373void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5374 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005375 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005376 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005377}
5378
5379void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005380 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005381 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005382 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005383 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005384 break;
5385
5386 default:
5387 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5388 }
5389}
5390
David Brazdil66d126e2015-04-03 16:02:44 +01005391void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5392 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5393 locations->SetInAt(0, Location::RequiresRegister());
5394 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5395}
5396
5397void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005398 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005399}
5400
Alexandre Rames5319def2014-10-23 10:03:10 +01005401void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005402 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5403 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005404}
5405
Calin Juravle2ae48182016-03-16 14:05:09 +00005406void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5407 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005408 return;
5409 }
Artem Serov914d7a82017-02-07 14:33:49 +00005410 {
5411 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5412 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5413 Location obj = instruction->GetLocations()->InAt(0);
5414 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5415 RecordPcInfo(instruction, instruction->GetDexPc());
5416 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005417}
5418
Calin Juravle2ae48182016-03-16 14:05:09 +00005419void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005420 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005421 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005422
5423 LocationSummary* locations = instruction->GetLocations();
5424 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005425
5426 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005427}
5428
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005429void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005430 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005431}
5432
Alexandre Rames67555f72014-11-18 10:55:16 +00005433void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5434 HandleBinaryOp(instruction);
5435}
5436
5437void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5438 HandleBinaryOp(instruction);
5439}
5440
Alexandre Rames3e69f162014-12-10 10:36:50 +00005441void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5442 LOG(FATAL) << "Unreachable";
5443}
5444
5445void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5446 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5447}
5448
Alexandre Rames5319def2014-10-23 10:03:10 +01005449void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5450 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5451 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5452 if (location.IsStackSlot()) {
5453 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5454 } else if (location.IsDoubleStackSlot()) {
5455 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5456 }
5457 locations->SetOut(location);
5458}
5459
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005460void InstructionCodeGeneratorARM64::VisitParameterValue(
5461 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005462 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005463}
5464
5465void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5466 LocationSummary* locations =
5467 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005468 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005469}
5470
5471void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5472 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5473 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005474}
5475
5476void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5477 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005478 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005479 locations->SetInAt(i, Location::Any());
5480 }
5481 locations->SetOut(Location::Any());
5482}
5483
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005484void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005485 LOG(FATAL) << "Unreachable";
5486}
5487
Serban Constantinescu02164b32014-11-13 14:05:07 +00005488void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005489 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005490 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005491 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5492 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005493 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5494
5495 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005496 case Primitive::kPrimInt:
5497 case Primitive::kPrimLong:
5498 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005499 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005500 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5501 break;
5502
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005503 case Primitive::kPrimFloat:
5504 case Primitive::kPrimDouble: {
5505 InvokeRuntimeCallingConvention calling_convention;
5506 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5507 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5508 locations->SetOut(calling_convention.GetReturnLocation(type));
5509
5510 break;
5511 }
5512
Serban Constantinescu02164b32014-11-13 14:05:07 +00005513 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005514 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005515 }
5516}
5517
5518void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5519 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005520
Serban Constantinescu02164b32014-11-13 14:05:07 +00005521 switch (type) {
5522 case Primitive::kPrimInt:
5523 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005524 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005525 break;
5526 }
5527
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005528 case Primitive::kPrimFloat:
5529 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005530 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5531 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005532 if (type == Primitive::kPrimFloat) {
5533 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5534 } else {
5535 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5536 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005537 break;
5538 }
5539
Serban Constantinescu02164b32014-11-13 14:05:07 +00005540 default:
5541 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005542 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005543 }
5544}
5545
Igor Murashkind01745e2017-04-05 16:40:31 -07005546void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5547 constructor_fence->SetLocations(nullptr);
5548}
5549
5550void InstructionCodeGeneratorARM64::VisitConstructorFence(
5551 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5552 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5553}
5554
Calin Juravle27df7582015-04-17 19:12:31 +01005555void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5556 memory_barrier->SetLocations(nullptr);
5557}
5558
5559void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005560 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005561}
5562
Alexandre Rames5319def2014-10-23 10:03:10 +01005563void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5564 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5565 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005566 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005567}
5568
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005569void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005570 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005571}
5572
5573void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5574 instruction->SetLocations(nullptr);
5575}
5576
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005577void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005578 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005579}
5580
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005581void LocationsBuilderARM64::VisitRor(HRor* ror) {
5582 HandleBinaryOp(ror);
5583}
5584
5585void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5586 HandleBinaryOp(ror);
5587}
5588
Serban Constantinescu02164b32014-11-13 14:05:07 +00005589void LocationsBuilderARM64::VisitShl(HShl* shl) {
5590 HandleShift(shl);
5591}
5592
5593void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5594 HandleShift(shl);
5595}
5596
5597void LocationsBuilderARM64::VisitShr(HShr* shr) {
5598 HandleShift(shr);
5599}
5600
5601void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5602 HandleShift(shr);
5603}
5604
Alexandre Rames5319def2014-10-23 10:03:10 +01005605void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005606 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005607}
5608
5609void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005610 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005611}
5612
Alexandre Rames67555f72014-11-18 10:55:16 +00005613void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005614 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005615}
5616
5617void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005618 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005619}
5620
5621void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005622 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005623}
5624
Alexandre Rames67555f72014-11-18 10:55:16 +00005625void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005626 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005627}
5628
Calin Juravlee460d1d2015-09-29 04:52:17 +01005629void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5630 HUnresolvedInstanceFieldGet* instruction) {
5631 FieldAccessCallingConventionARM64 calling_convention;
5632 codegen_->CreateUnresolvedFieldLocationSummary(
5633 instruction, instruction->GetFieldType(), calling_convention);
5634}
5635
5636void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5637 HUnresolvedInstanceFieldGet* instruction) {
5638 FieldAccessCallingConventionARM64 calling_convention;
5639 codegen_->GenerateUnresolvedFieldAccess(instruction,
5640 instruction->GetFieldType(),
5641 instruction->GetFieldIndex(),
5642 instruction->GetDexPc(),
5643 calling_convention);
5644}
5645
5646void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5647 HUnresolvedInstanceFieldSet* instruction) {
5648 FieldAccessCallingConventionARM64 calling_convention;
5649 codegen_->CreateUnresolvedFieldLocationSummary(
5650 instruction, instruction->GetFieldType(), calling_convention);
5651}
5652
5653void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5654 HUnresolvedInstanceFieldSet* instruction) {
5655 FieldAccessCallingConventionARM64 calling_convention;
5656 codegen_->GenerateUnresolvedFieldAccess(instruction,
5657 instruction->GetFieldType(),
5658 instruction->GetFieldIndex(),
5659 instruction->GetDexPc(),
5660 calling_convention);
5661}
5662
5663void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5664 HUnresolvedStaticFieldGet* instruction) {
5665 FieldAccessCallingConventionARM64 calling_convention;
5666 codegen_->CreateUnresolvedFieldLocationSummary(
5667 instruction, instruction->GetFieldType(), calling_convention);
5668}
5669
5670void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5671 HUnresolvedStaticFieldGet* instruction) {
5672 FieldAccessCallingConventionARM64 calling_convention;
5673 codegen_->GenerateUnresolvedFieldAccess(instruction,
5674 instruction->GetFieldType(),
5675 instruction->GetFieldIndex(),
5676 instruction->GetDexPc(),
5677 calling_convention);
5678}
5679
5680void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5681 HUnresolvedStaticFieldSet* instruction) {
5682 FieldAccessCallingConventionARM64 calling_convention;
5683 codegen_->CreateUnresolvedFieldLocationSummary(
5684 instruction, instruction->GetFieldType(), calling_convention);
5685}
5686
5687void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5688 HUnresolvedStaticFieldSet* instruction) {
5689 FieldAccessCallingConventionARM64 calling_convention;
5690 codegen_->GenerateUnresolvedFieldAccess(instruction,
5691 instruction->GetFieldType(),
5692 instruction->GetFieldIndex(),
5693 instruction->GetDexPc(),
5694 calling_convention);
5695}
5696
Alexandre Rames5319def2014-10-23 10:03:10 +01005697void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005698 LocationSummary* locations =
5699 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005700 // In suspend check slow path, usually there are no caller-save registers at all.
5701 // If SIMD instructions are present, however, we force spilling all live SIMD
5702 // registers in full width (since the runtime only saves/restores lower part).
5703 locations->SetCustomSlowPathCallerSaves(
5704 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005705}
5706
5707void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005708 HBasicBlock* block = instruction->GetBlock();
5709 if (block->GetLoopInformation() != nullptr) {
5710 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5711 // The back edge will generate the suspend check.
5712 return;
5713 }
5714 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5715 // The goto will generate the suspend check.
5716 return;
5717 }
5718 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005719 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005720}
5721
Alexandre Rames67555f72014-11-18 10:55:16 +00005722void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5723 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005724 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005725 InvokeRuntimeCallingConvention calling_convention;
5726 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5727}
5728
5729void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005730 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005731 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005732}
5733
5734void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5735 LocationSummary* locations =
5736 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5737 Primitive::Type input_type = conversion->GetInputType();
5738 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005739 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005740 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5741 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5742 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5743 }
5744
Alexandre Rames542361f2015-01-29 16:57:31 +00005745 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005746 locations->SetInAt(0, Location::RequiresFpuRegister());
5747 } else {
5748 locations->SetInAt(0, Location::RequiresRegister());
5749 }
5750
Alexandre Rames542361f2015-01-29 16:57:31 +00005751 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005752 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5753 } else {
5754 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5755 }
5756}
5757
5758void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5759 Primitive::Type result_type = conversion->GetResultType();
5760 Primitive::Type input_type = conversion->GetInputType();
5761
5762 DCHECK_NE(input_type, result_type);
5763
Alexandre Rames542361f2015-01-29 16:57:31 +00005764 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005765 int result_size = Primitive::ComponentSize(result_type);
5766 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005767 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005768 Register output = OutputRegister(conversion);
5769 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005770 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005771 // 'int' values are used directly as W registers, discarding the top
5772 // bits, so we don't need to sign-extend and can just perform a move.
5773 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5774 // top 32 bits of the target register. We theoretically could leave those
5775 // bits unchanged, but we would have to make sure that no code uses a
5776 // 32bit input value as a 64bit value assuming that the top 32 bits are
5777 // zero.
5778 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005779 } else if (result_type == Primitive::kPrimChar ||
5780 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5781 __ Ubfx(output,
5782 output.IsX() ? source.X() : source.W(),
5783 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005784 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005785 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005786 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005787 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005788 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005789 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005790 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5791 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005792 } else if (Primitive::IsFloatingPointType(result_type) &&
5793 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005794 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5795 } else {
5796 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5797 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005798 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005799}
Alexandre Rames67555f72014-11-18 10:55:16 +00005800
Serban Constantinescu02164b32014-11-13 14:05:07 +00005801void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5802 HandleShift(ushr);
5803}
5804
5805void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5806 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005807}
5808
5809void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5810 HandleBinaryOp(instruction);
5811}
5812
5813void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5814 HandleBinaryOp(instruction);
5815}
5816
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005817void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005818 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005819 LOG(FATAL) << "Unreachable";
5820}
5821
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005822void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005823 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005824 LOG(FATAL) << "Unreachable";
5825}
5826
Mark Mendellfe57faa2015-09-18 09:26:15 -04005827// Simple implementation of packed switch - generate cascaded compare/jumps.
5828void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5829 LocationSummary* locations =
5830 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5831 locations->SetInAt(0, Location::RequiresRegister());
5832}
5833
5834void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5835 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005836 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005837 Register value_reg = InputRegisterAt(switch_instr, 0);
5838 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5839
Zheng Xu3927c8b2015-11-18 17:46:25 +08005840 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005841 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005842 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5843 // make sure we don't emit it if the target may run out of range.
5844 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5845 // ranges and emit the tables only as required.
5846 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005847
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005848 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005849 // Current instruction id is an upper bound of the number of HIRs in the graph.
5850 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5851 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005852 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5853 Register temp = temps.AcquireW();
5854 __ Subs(temp, value_reg, Operand(lower_bound));
5855
Zheng Xu3927c8b2015-11-18 17:46:25 +08005856 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005857 // Jump to successors[0] if value == lower_bound.
5858 __ B(eq, codegen_->GetLabelOf(successors[0]));
5859 int32_t last_index = 0;
5860 for (; num_entries - last_index > 2; last_index += 2) {
5861 __ Subs(temp, temp, Operand(2));
5862 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5863 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5864 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5865 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5866 }
5867 if (num_entries - last_index == 2) {
5868 // The last missing case_value.
5869 __ Cmp(temp, Operand(1));
5870 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005871 }
5872
5873 // And the default for any other value.
5874 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5875 __ B(codegen_->GetLabelOf(default_block));
5876 }
5877 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005878 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005879
5880 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5881
5882 // Below instructions should use at most one blocked register. Since there are two blocked
5883 // registers, we are free to block one.
5884 Register temp_w = temps.AcquireW();
5885 Register index;
5886 // Remove the bias.
5887 if (lower_bound != 0) {
5888 index = temp_w;
5889 __ Sub(index, value_reg, Operand(lower_bound));
5890 } else {
5891 index = value_reg;
5892 }
5893
5894 // Jump to default block if index is out of the range.
5895 __ Cmp(index, Operand(num_entries));
5896 __ B(hs, codegen_->GetLabelOf(default_block));
5897
5898 // In current VIXL implementation, it won't require any blocked registers to encode the
5899 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5900 // register pressure.
5901 Register table_base = temps.AcquireX();
5902 // Load jump offset from the table.
5903 __ Adr(table_base, jump_table->GetTableStartLabel());
5904 Register jump_offset = temp_w;
5905 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5906
5907 // Jump to target block by branching to table_base(pc related) + offset.
5908 Register target_address = table_base;
5909 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5910 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005911 }
5912}
5913
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005914void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5915 HInstruction* instruction,
5916 Location out,
5917 uint32_t offset,
5918 Location maybe_temp,
5919 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005920 Primitive::Type type = Primitive::kPrimNot;
5921 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005922 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005923 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005924 if (kUseBakerReadBarrier) {
5925 // Load with fast path based Baker's read barrier.
5926 // /* HeapReference<Object> */ out = *(out + offset)
5927 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5928 out,
5929 out_reg,
5930 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005931 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005932 /* needs_null_check */ false,
5933 /* use_load_acquire */ false);
5934 } else {
5935 // Load with slow path based read barrier.
5936 // Save the value of `out` into `maybe_temp` before overwriting it
5937 // in the following move operation, as we will need it for the
5938 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005939 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005940 __ Mov(temp_reg, out_reg);
5941 // /* HeapReference<Object> */ out = *(out + offset)
5942 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5943 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5944 }
5945 } else {
5946 // Plain load with no read barrier.
5947 // /* HeapReference<Object> */ out = *(out + offset)
5948 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5949 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5950 }
5951}
5952
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005953void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5954 HInstruction* instruction,
5955 Location out,
5956 Location obj,
5957 uint32_t offset,
5958 Location maybe_temp,
5959 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005960 Primitive::Type type = Primitive::kPrimNot;
5961 Register out_reg = RegisterFrom(out, type);
5962 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005963 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005964 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005965 if (kUseBakerReadBarrier) {
5966 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005967 // /* HeapReference<Object> */ out = *(obj + offset)
5968 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5969 out,
5970 obj_reg,
5971 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005972 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005973 /* needs_null_check */ false,
5974 /* use_load_acquire */ false);
5975 } else {
5976 // Load with slow path based read barrier.
5977 // /* HeapReference<Object> */ out = *(obj + offset)
5978 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5979 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5980 }
5981 } else {
5982 // Plain load with no read barrier.
5983 // /* HeapReference<Object> */ out = *(obj + offset)
5984 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5985 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5986 }
5987}
5988
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005989void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5990 HInstruction* instruction,
5991 Location root,
5992 Register obj,
5993 uint32_t offset,
5994 vixl::aarch64::Label* fixup_label,
5995 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005996 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005997 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005998 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005999 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006000 if (kUseBakerReadBarrier) {
6001 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006002 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006003 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
6004 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006005 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6006 // the Marking Register) to decide whether we need to enter
6007 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006008 //
6009 // We use link-time generated thunks for the slow path. That thunk
6010 // checks the reference and jumps to the entrypoint if needed.
6011 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006012 // lr = &return_address;
6013 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01006014 // if (mr) { // Thread::Current()->GetIsGcMarking()
6015 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006016 // }
6017 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00006018
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006019 UseScratchRegisterScope temps(GetVIXLAssembler());
6020 DCHECK(temps.IsAvailable(ip0));
6021 DCHECK(temps.IsAvailable(ip1));
6022 temps.Exclude(ip0, ip1);
6023 uint32_t custom_data =
6024 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
6025 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00006026
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006027 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6028 vixl::aarch64::Label return_address;
6029 __ adr(lr, &return_address);
6030 if (fixup_label != nullptr) {
6031 __ Bind(fixup_label);
6032 }
6033 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6034 "GC root LDR must be 2 instruction (8B) before the return address label.");
6035 __ ldr(root_reg, MemOperand(obj.X(), offset));
6036 __ Bind(cbnz_label);
Roland Levillain97c46462017-05-11 14:04:03 +01006037 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006038 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006039 } else {
Roland Levillain97c46462017-05-11 14:04:03 +01006040 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6041 // the Marking Register) to decide whether we need to enter
6042 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006043 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006044 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01006045 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006046 // // Slow path.
Roland Levillain97c46462017-05-11 14:04:03 +01006047 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6048 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006049 // }
Roland Levillain44015862016-01-22 11:47:17 +00006050
Roland Levillain97c46462017-05-11 14:04:03 +01006051 // Slow path marking the GC root `root`. The entrypoint will
6052 // be loaded by the slow path code.
6053 SlowPathCodeARM64* slow_path =
6054 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006055 codegen_->AddSlowPath(slow_path);
6056
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006057 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6058 if (fixup_label == nullptr) {
6059 __ Ldr(root_reg, MemOperand(obj, offset));
6060 } else {
6061 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
6062 }
6063 static_assert(
6064 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6065 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6066 "have different sizes.");
6067 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6068 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6069 "have different sizes.");
6070
Roland Levillain97c46462017-05-11 14:04:03 +01006071 __ Cbnz(mr, slow_path->GetEntryLabel());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006072 __ Bind(slow_path->GetExitLabel());
6073 }
Roland Levillain44015862016-01-22 11:47:17 +00006074 } else {
6075 // GC root loaded through a slow path for read barriers other
6076 // than Baker's.
6077 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006078 if (fixup_label == nullptr) {
6079 __ Add(root_reg.X(), obj.X(), offset);
6080 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006081 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006082 }
Roland Levillain44015862016-01-22 11:47:17 +00006083 // /* mirror::Object* */ root = root->Read()
6084 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6085 }
6086 } else {
6087 // Plain GC root load with no read barrier.
6088 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006089 if (fixup_label == nullptr) {
6090 __ Ldr(root_reg, MemOperand(obj, offset));
6091 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006092 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006093 }
Roland Levillain44015862016-01-22 11:47:17 +00006094 // Note that GC roots are not affected by heap poisoning, thus we
6095 // do not have to unpoison `root_reg` here.
6096 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006097 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006098}
6099
6100void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6101 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006102 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006103 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006104 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006105 bool needs_null_check,
6106 bool use_load_acquire) {
6107 DCHECK(kEmitCompilerReadBarrier);
6108 DCHECK(kUseBakerReadBarrier);
6109
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006110 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6111 !use_load_acquire &&
6112 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006113 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6114 // Marking Register) to decide whether we need to enter the slow
6115 // path to mark the reference. Then, in the slow path, check the
6116 // gray bit in the lock word of the reference's holder (`obj`) to
6117 // decide whether to mark `ref` or not.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006118 //
6119 // We use link-time generated thunks for the slow path. That thunk checks
6120 // the holder and jumps to the entrypoint if needed. If the holder is not
6121 // gray, it creates a fake dependency and returns to the LDR instruction.
6122 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006123 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006124 // if (mr) { // Thread::Current()->GetIsGcMarking()
6125 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006126 // }
6127 // not_gray_return_address:
6128 // // Original reference load. If the offset is too large to fit
6129 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006130 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006131 // gray_return_address:
6132
6133 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6134 Register base = obj;
6135 if (offset >= kReferenceLoadMinFarOffset) {
6136 DCHECK(maybe_temp.IsRegister());
6137 base = WRegisterFrom(maybe_temp);
6138 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6139 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6140 offset &= (kReferenceLoadMinFarOffset - 1u);
6141 }
6142 UseScratchRegisterScope temps(GetVIXLAssembler());
6143 DCHECK(temps.IsAvailable(ip0));
6144 DCHECK(temps.IsAvailable(ip1));
6145 temps.Exclude(ip0, ip1);
6146 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6147 base.GetCode(),
6148 obj.GetCode());
6149 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6150
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006151 {
6152 EmissionCheckScope guard(GetVIXLAssembler(),
6153 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6154 vixl::aarch64::Label return_address;
6155 __ adr(lr, &return_address);
6156 __ Bind(cbnz_label);
6157 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6158 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6159 "Field LDR must be 1 instruction (4B) before the return address label; "
6160 " 2 instructions (8B) for heap poisoning.");
6161 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6162 __ ldr(ref_reg, MemOperand(base.X(), offset));
6163 if (needs_null_check) {
6164 MaybeRecordImplicitNullCheck(instruction);
6165 }
6166 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6167 __ Bind(&return_address);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006168 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006169 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006170 return;
6171 }
6172
Roland Levillain44015862016-01-22 11:47:17 +00006173 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006174 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006175 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006176 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006177 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6178 ref,
6179 obj,
6180 offset,
6181 no_index,
6182 no_scale_factor,
6183 temp,
6184 needs_null_check,
6185 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006186}
6187
6188void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6189 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006190 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006191 uint32_t data_offset,
6192 Location index,
6193 Register temp,
6194 bool needs_null_check) {
6195 DCHECK(kEmitCompilerReadBarrier);
6196 DCHECK(kUseBakerReadBarrier);
6197
Vladimir Marko66d691d2017-04-07 17:53:39 +01006198 static_assert(
6199 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6200 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6201 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
6202
6203 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6204 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006205 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6206 // Marking Register) to decide whether we need to enter the slow
6207 // path to mark the reference. Then, in the slow path, check the
6208 // gray bit in the lock word of the reference's holder (`obj`) to
6209 // decide whether to mark `ref` or not.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006210 //
6211 // We use link-time generated thunks for the slow path. That thunk checks
6212 // the holder and jumps to the entrypoint if needed. If the holder is not
6213 // gray, it creates a fake dependency and returns to the LDR instruction.
6214 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006215 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006216 // if (mr) { // Thread::Current()->GetIsGcMarking()
6217 // goto array_thunk<base_reg>(lr)
Vladimir Marko66d691d2017-04-07 17:53:39 +01006218 // }
6219 // not_gray_return_address:
6220 // // Original reference load. If the offset is too large to fit
6221 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006222 // HeapReference<mirror::Object> reference = data[index];
Vladimir Marko66d691d2017-04-07 17:53:39 +01006223 // gray_return_address:
6224
6225 DCHECK(index.IsValid());
6226 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
6227 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6228
6229 UseScratchRegisterScope temps(GetVIXLAssembler());
6230 DCHECK(temps.IsAvailable(ip0));
6231 DCHECK(temps.IsAvailable(ip1));
6232 temps.Exclude(ip0, ip1);
6233 uint32_t custom_data =
6234 linker::Arm64RelativePatcher::EncodeBakerReadBarrierArrayData(temp.GetCode());
6235 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6236
Vladimir Marko66d691d2017-04-07 17:53:39 +01006237 __ Add(temp.X(), obj.X(), Operand(data_offset));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006238 {
6239 EmissionCheckScope guard(GetVIXLAssembler(),
6240 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6241 vixl::aarch64::Label return_address;
6242 __ adr(lr, &return_address);
6243 __ Bind(cbnz_label);
6244 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6245 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6246 "Array LDR must be 1 instruction (4B) before the return address label; "
6247 " 2 instructions (8B) for heap poisoning.");
6248 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6249 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6250 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6251 __ Bind(&return_address);
6252 }
6253 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Marko66d691d2017-04-07 17:53:39 +01006254 return;
6255 }
6256
Roland Levillain44015862016-01-22 11:47:17 +00006257 // Array cells are never volatile variables, therefore array loads
6258 // never use Load-Acquire instructions on ARM64.
6259 const bool use_load_acquire = false;
6260
6261 // /* HeapReference<Object> */ ref =
6262 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006263 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6264 ref,
6265 obj,
6266 data_offset,
6267 index,
6268 scale_factor,
6269 temp,
6270 needs_null_check,
6271 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006272}
6273
6274void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6275 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006276 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006277 uint32_t offset,
6278 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006279 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006280 Register temp,
6281 bool needs_null_check,
Roland Levillainff487002017-03-07 16:50:01 +00006282 bool use_load_acquire) {
Roland Levillain44015862016-01-22 11:47:17 +00006283 DCHECK(kEmitCompilerReadBarrier);
6284 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006285 // If we are emitting an array load, we should not be using a
6286 // Load Acquire instruction. In other words:
6287 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6288 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006289
Roland Levillain97c46462017-05-11 14:04:03 +01006290 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6291 // Marking Register) to decide whether we need to enter the slow
6292 // path to mark the reference. Then, in the slow path, check the
6293 // gray bit in the lock word of the reference's holder (`obj`) to
6294 // decide whether to mark `ref` or not.
Roland Levillain44015862016-01-22 11:47:17 +00006295 //
Roland Levillain97c46462017-05-11 14:04:03 +01006296 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00006297 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006298 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6299 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6300 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6301 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6302 // if (is_gray) {
Roland Levillain97c46462017-05-11 14:04:03 +01006303 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6304 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillain54f869e2017-03-06 13:54:11 +00006305 // }
6306 // } else {
6307 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006308 // }
Roland Levillain44015862016-01-22 11:47:17 +00006309
Roland Levillainba650a42017-03-06 13:52:32 +00006310 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain97c46462017-05-11 14:04:03 +01006311 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006312 SlowPathCodeARM64* slow_path =
6313 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6314 instruction,
6315 ref,
6316 obj,
6317 offset,
6318 index,
6319 scale_factor,
6320 needs_null_check,
6321 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006322 temp);
Roland Levillainba650a42017-03-06 13:52:32 +00006323 AddSlowPath(slow_path);
6324
Roland Levillain97c46462017-05-11 14:04:03 +01006325 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006326 // Fast path: the GC is not marking: just load the reference.
Roland Levillain54f869e2017-03-06 13:54:11 +00006327 GenerateRawReferenceLoad(
6328 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006329 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006330 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainba650a42017-03-06 13:52:32 +00006331}
6332
Roland Levillainff487002017-03-07 16:50:01 +00006333void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6334 Location ref,
6335 Register obj,
6336 Location field_offset,
6337 Register temp,
6338 bool needs_null_check,
6339 bool use_load_acquire) {
6340 DCHECK(kEmitCompilerReadBarrier);
6341 DCHECK(kUseBakerReadBarrier);
6342 // If we are emitting an array load, we should not be using a
6343 // Load Acquire instruction. In other words:
6344 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6345 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6346
Roland Levillain97c46462017-05-11 14:04:03 +01006347 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6348 // Marking Register) to decide whether we need to enter the slow
6349 // path to update the reference field within `obj`. Then, in the
6350 // slow path, check the gray bit in the lock word of the reference's
6351 // holder (`obj`) to decide whether to mark `ref` and update the
6352 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00006353 //
Roland Levillain97c46462017-05-11 14:04:03 +01006354 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00006355 // // Slow path.
6356 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6357 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6358 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6359 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6360 // if (is_gray) {
6361 // old_ref = ref;
Roland Levillain97c46462017-05-11 14:04:03 +01006362 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6363 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00006364 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6365 // }
6366 // }
6367
6368 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain97c46462017-05-11 14:04:03 +01006369 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006370 SlowPathCodeARM64* slow_path =
6371 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6372 instruction,
6373 ref,
6374 obj,
6375 /* offset */ 0u,
6376 /* index */ field_offset,
6377 /* scale_factor */ 0u /* "times 1" */,
6378 needs_null_check,
6379 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006380 temp);
Roland Levillainff487002017-03-07 16:50:01 +00006381 AddSlowPath(slow_path);
6382
Roland Levillain97c46462017-05-11 14:04:03 +01006383 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006384 // Fast path: the GC is not marking: nothing to do (the field is
6385 // up-to-date, and we don't need to load the reference).
6386 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006387 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainff487002017-03-07 16:50:01 +00006388}
6389
Roland Levillainba650a42017-03-06 13:52:32 +00006390void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6391 Location ref,
6392 Register obj,
6393 uint32_t offset,
6394 Location index,
6395 size_t scale_factor,
6396 bool needs_null_check,
6397 bool use_load_acquire) {
6398 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006399 Primitive::Type type = Primitive::kPrimNot;
6400 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006401
Roland Levillainba650a42017-03-06 13:52:32 +00006402 // If needed, vixl::EmissionCheckScope guards are used to ensure
6403 // that no pools are emitted between the load (macro) instruction
6404 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006405
Roland Levillain44015862016-01-22 11:47:17 +00006406 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006407 // Load types involving an "index": ArrayGet,
6408 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6409 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006410 if (use_load_acquire) {
6411 // UnsafeGetObjectVolatile intrinsic case.
6412 // Register `index` is not an index in an object array, but an
6413 // offset to an object reference field within object `obj`.
6414 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6415 DCHECK(instruction->GetLocations()->Intrinsified());
6416 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6417 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006418 DCHECK_EQ(offset, 0u);
6419 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006420 DCHECK_EQ(needs_null_check, false);
6421 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006422 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6423 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006424 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006425 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6426 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006427 if (index.IsConstant()) {
6428 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006429 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006430 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006431 if (needs_null_check) {
6432 MaybeRecordImplicitNullCheck(instruction);
6433 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006434 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006435 UseScratchRegisterScope temps(GetVIXLAssembler());
6436 Register temp = temps.AcquireW();
6437 __ Add(temp, obj, offset);
6438 {
6439 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6440 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6441 if (needs_null_check) {
6442 MaybeRecordImplicitNullCheck(instruction);
6443 }
6444 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006445 }
Roland Levillain44015862016-01-22 11:47:17 +00006446 }
Roland Levillain44015862016-01-22 11:47:17 +00006447 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006448 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006449 MemOperand field = HeapOperand(obj, offset);
6450 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006451 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6452 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006453 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006454 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006455 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006456 if (needs_null_check) {
6457 MaybeRecordImplicitNullCheck(instruction);
6458 }
Roland Levillain44015862016-01-22 11:47:17 +00006459 }
6460 }
6461
6462 // Object* ref = ref_addr->AsMirrorPtr()
6463 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006464}
6465
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006466void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6467 // The following condition is a compile-time one, so it does not have a run-time cost.
6468 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6469 // The following condition is a run-time one; it is executed after the
6470 // previous compile-time test, to avoid penalizing non-debug builds.
6471 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6472 UseScratchRegisterScope temps(GetVIXLAssembler());
6473 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6474 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6475 }
6476 }
6477}
6478
Roland Levillain44015862016-01-22 11:47:17 +00006479void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6480 Location out,
6481 Location ref,
6482 Location obj,
6483 uint32_t offset,
6484 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006485 DCHECK(kEmitCompilerReadBarrier);
6486
Roland Levillain44015862016-01-22 11:47:17 +00006487 // Insert a slow path based read barrier *after* the reference load.
6488 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006489 // If heap poisoning is enabled, the unpoisoning of the loaded
6490 // reference will be carried out by the runtime within the slow
6491 // path.
6492 //
6493 // Note that `ref` currently does not get unpoisoned (when heap
6494 // poisoning is enabled), which is alright as the `ref` argument is
6495 // not used by the artReadBarrierSlow entry point.
6496 //
6497 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6498 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6499 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6500 AddSlowPath(slow_path);
6501
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006502 __ B(slow_path->GetEntryLabel());
6503 __ Bind(slow_path->GetExitLabel());
6504}
6505
Roland Levillain44015862016-01-22 11:47:17 +00006506void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6507 Location out,
6508 Location ref,
6509 Location obj,
6510 uint32_t offset,
6511 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006512 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006513 // Baker's read barriers shall be handled by the fast path
6514 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6515 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006516 // If heap poisoning is enabled, unpoisoning will be taken care of
6517 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006518 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006519 } else if (kPoisonHeapReferences) {
6520 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6521 }
6522}
6523
Roland Levillain44015862016-01-22 11:47:17 +00006524void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6525 Location out,
6526 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006527 DCHECK(kEmitCompilerReadBarrier);
6528
Roland Levillain44015862016-01-22 11:47:17 +00006529 // Insert a slow path based read barrier *after* the GC root load.
6530 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006531 // Note that GC roots are not affected by heap poisoning, so we do
6532 // not need to do anything special for this here.
6533 SlowPathCodeARM64* slow_path =
6534 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6535 AddSlowPath(slow_path);
6536
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006537 __ B(slow_path->GetEntryLabel());
6538 __ Bind(slow_path->GetExitLabel());
6539}
6540
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006541void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6542 LocationSummary* locations =
6543 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6544 locations->SetInAt(0, Location::RequiresRegister());
6545 locations->SetOut(Location::RequiresRegister());
6546}
6547
6548void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6549 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006550 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006551 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006552 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006553 __ Ldr(XRegisterFrom(locations->Out()),
6554 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006555 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006556 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006557 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006558 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6559 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006560 __ Ldr(XRegisterFrom(locations->Out()),
6561 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006562 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006563}
6564
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006565static void PatchJitRootUse(uint8_t* code,
6566 const uint8_t* roots_data,
6567 vixl::aarch64::Literal<uint32_t>* literal,
6568 uint64_t index_in_table) {
6569 uint32_t literal_offset = literal->GetOffset();
6570 uintptr_t address =
6571 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6572 uint8_t* data = code + literal_offset;
6573 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6574}
6575
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006576void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6577 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006578 const StringReference& string_reference = entry.first;
6579 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
6580 const auto it = jit_string_roots_.find(string_reference);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006581 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006582 uint64_t index_in_table = it->second;
6583 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006584 }
6585 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006586 const TypeReference& type_reference = entry.first;
6587 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
6588 const auto it = jit_class_roots_.find(type_reference);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006589 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006590 uint64_t index_in_table = it->second;
6591 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006592 }
6593}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006594
Alexandre Rames67555f72014-11-18 10:55:16 +00006595#undef __
6596#undef QUICK_ENTRY_POINT
6597
Alexandre Rames5319def2014-10-23 10:03:10 +01006598} // namespace arm64
6599} // namespace art