blob: 2561ed076241e26e929baf83c00b53784fed50ce [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Vladimir Markof4f2daa2017-03-20 18:26:59 +000019#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000020#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070022#include "base/bit_utils.h"
23#include "base/bit_utils_iterator.h"
Zheng Xuc6667102015-05-15 16:08:45 +080024#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000025#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010026#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080027#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080029#include "intrinsics.h"
30#include "intrinsics_arm64.h"
Vladimir Markof4f2daa2017-03-20 18:26:59 +000031#include "linker/arm64/relative_patcher_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010032#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070033#include "mirror/class-inl.h"
Andreas Gamped4901292017-05-30 18:41:34 -070034#include "lock_word.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000035#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010036#include "thread.h"
37#include "utils/arm64/assembler_arm64.h"
38#include "utils/assembler.h"
39#include "utils/stack_checks.h"
40
Scott Wakeling97c72b72016-06-24 16:19:36 +010041using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000042using vixl::ExactAssemblyScope;
43using vixl::CodeBufferCheckScope;
44using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010045
46#ifdef __
47#error "ARM64 Codegen VIXL macro-assembler macro already defined."
48#endif
49
Alexandre Rames5319def2014-10-23 10:03:10 +010050namespace art {
51
Roland Levillain22ccc3a2015-11-24 13:10:05 +000052template<class MirrorType>
53class GcRoot;
54
Alexandre Rames5319def2014-10-23 10:03:10 +010055namespace arm64 {
56
Alexandre Ramesbe919d92016-08-23 18:33:36 +010057using helpers::ARM64EncodableConstantOrRegister;
58using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080059using helpers::CPURegisterFrom;
60using helpers::DRegisterFrom;
61using helpers::FPRegisterFrom;
62using helpers::HeapOperand;
63using helpers::HeapOperandFrom;
64using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010065using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080066using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080067using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010068using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010070using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080071using helpers::LocationFrom;
72using helpers::OperandFromMemOperand;
73using helpers::OutputCPURegister;
74using helpers::OutputFPRegister;
75using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010076using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080077using helpers::RegisterFrom;
78using helpers::StackOperandFrom;
79using helpers::VIXLRegCodeFromART;
80using helpers::WRegisterFrom;
81using helpers::XRegisterFrom;
82
Vladimir Markof3e0ee22015-12-17 15:23:13 +000083// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080084// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
85// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000086static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010087
Vladimir Markof4f2daa2017-03-20 18:26:59 +000088// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
89// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
90// For the Baker read barrier implementation using link-generated thunks we need to split
91// the offset explicitly.
92constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
93
94// Flags controlling the use of link-time generated thunks for Baker read barriers.
Vladimir Markod1ef8732017-04-18 13:55:13 +010095constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
Vladimir Marko66d691d2017-04-07 17:53:39 +010096constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
Vladimir Markod1ef8732017-04-18 13:55:13 +010097constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
Vladimir Markof4f2daa2017-03-20 18:26:59 +000098
99// Some instructions have special requirements for a temporary, for example
100// LoadClass/kBssEntry and LoadString/kBssEntry for Baker read barrier require
101// temp that's not an R0 (to avoid an extra move) and Baker read barrier field
102// loads with large offsets need a fixed register to limit the number of link-time
103// thunks we generate. For these and similar cases, we want to reserve a specific
104// register that's neither callee-save nor an argument register. We choose x15.
105inline Location FixedTempLocation() {
106 return Location::RegisterLocation(x15.GetCode());
107}
108
Alexandre Rames5319def2014-10-23 10:03:10 +0100109inline Condition ARM64Condition(IfCondition cond) {
110 switch (cond) {
111 case kCondEQ: return eq;
112 case kCondNE: return ne;
113 case kCondLT: return lt;
114 case kCondLE: return le;
115 case kCondGT: return gt;
116 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700117 case kCondB: return lo;
118 case kCondBE: return ls;
119 case kCondA: return hi;
120 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100121 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000122 LOG(FATAL) << "Unreachable";
123 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100124}
125
Vladimir Markod6e069b2016-01-18 11:11:01 +0000126inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
127 // The ARM64 condition codes can express all the necessary branches, see the
128 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
129 // There is no dex instruction or HIR that would need the missing conditions
130 // "equal or unordered" or "not equal".
131 switch (cond) {
132 case kCondEQ: return eq;
133 case kCondNE: return ne /* unordered */;
134 case kCondLT: return gt_bias ? cc : lt /* unordered */;
135 case kCondLE: return gt_bias ? ls : le /* unordered */;
136 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
137 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
138 default:
139 LOG(FATAL) << "UNREACHABLE";
140 UNREACHABLE();
141 }
142}
143
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000144Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000145 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
146 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
147 // but we use the exact registers for clarity.
148 if (return_type == Primitive::kPrimFloat) {
149 return LocationFrom(s0);
150 } else if (return_type == Primitive::kPrimDouble) {
151 return LocationFrom(d0);
152 } else if (return_type == Primitive::kPrimLong) {
153 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100154 } else if (return_type == Primitive::kPrimVoid) {
155 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000156 } else {
157 return LocationFrom(w0);
158 }
159}
160
Alexandre Rames5319def2014-10-23 10:03:10 +0100161Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000162 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100163}
164
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100165// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
166#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700167#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100168
Zheng Xuda403092015-04-24 17:35:39 +0800169// Calculate memory accessing operand for save/restore live registers.
170static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100171 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800172 int64_t spill_offset,
173 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100174 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
175 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
176 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800177 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100178 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800179 codegen->GetNumberOfFloatingPointRegisters()));
180
Vladimir Marko804b03f2016-09-14 16:26:36 +0100181 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100182 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
183 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800184
185 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
186 UseScratchRegisterScope temps(masm);
187
188 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100189 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
190 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800191 int64_t reg_size = kXRegSizeInBytes;
192 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
193 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100194 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800195 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
196 // If the offset does not fit in the instruction's immediate field, use an alternate register
197 // to compute the base address(float point registers spill base address).
198 Register new_base = temps.AcquireSameSizeAs(base);
199 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
200 base = new_base;
201 spill_offset = -core_spill_size;
202 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
203 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
204 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
205 }
206
207 if (is_save) {
208 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
209 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
210 } else {
211 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
212 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
213 }
214}
215
216void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800217 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100218 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
219 for (uint32_t i : LowToHighBits(core_spills)) {
220 // If the register holds an object, update the stack mask.
221 if (locations->RegisterContainsObject(i)) {
222 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800223 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100224 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
225 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
226 saved_core_stack_offsets_[i] = stack_offset;
227 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800228 }
229
Vladimir Marko804b03f2016-09-14 16:26:36 +0100230 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
231 for (uint32_t i : LowToHighBits(fp_spills)) {
232 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
233 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
234 saved_fpu_stack_offsets_[i] = stack_offset;
235 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800236 }
237
Vladimir Marko804b03f2016-09-14 16:26:36 +0100238 SaveRestoreLiveRegistersHelper(codegen,
239 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800240 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
241}
242
243void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100244 SaveRestoreLiveRegistersHelper(codegen,
245 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800246 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
247}
248
Alexandre Rames5319def2014-10-23 10:03:10 +0100249class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
250 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000251 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100252
Alexandre Rames67555f72014-11-18 10:55:16 +0000253 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100254 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000255 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100256
Alexandre Rames5319def2014-10-23 10:03:10 +0100257 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000258 if (instruction_->CanThrowIntoCatchBlock()) {
259 // Live registers will be restored in the catch block if caught.
260 SaveLiveRegisters(codegen, instruction_->GetLocations());
261 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000262 // We're moving two locations to locations that could overlap, so we need a parallel
263 // move resolver.
264 InvokeRuntimeCallingConvention calling_convention;
265 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100266 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
267 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000268 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
269 ? kQuickThrowStringBounds
270 : kQuickThrowArrayBounds;
271 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100272 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800273 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100274 }
275
Alexandre Rames8158f282015-08-07 10:26:17 +0100276 bool IsFatal() const OVERRIDE { return true; }
277
Alexandre Rames9931f312015-06-19 14:47:01 +0100278 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
279
Alexandre Rames5319def2014-10-23 10:03:10 +0100280 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100281 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
282};
283
Alexandre Rames67555f72014-11-18 10:55:16 +0000284class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
285 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000286 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000287
288 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
289 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
290 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000291 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800292 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000293 }
294
Alexandre Rames8158f282015-08-07 10:26:17 +0100295 bool IsFatal() const OVERRIDE { return true; }
296
Alexandre Rames9931f312015-06-19 14:47:01 +0100297 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
298
Alexandre Rames67555f72014-11-18 10:55:16 +0000299 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000300 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
301};
302
303class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
304 public:
305 LoadClassSlowPathARM64(HLoadClass* cls,
306 HInstruction* at,
307 uint32_t dex_pc,
Vladimir Markoea4c1262017-02-06 19:59:33 +0000308 bool do_clinit,
309 vixl::aarch64::Register bss_entry_temp = vixl::aarch64::Register(),
310 vixl::aarch64::Label* bss_entry_adrp_label = nullptr)
311 : SlowPathCodeARM64(at),
312 cls_(cls),
313 dex_pc_(dex_pc),
314 do_clinit_(do_clinit),
315 bss_entry_temp_(bss_entry_temp),
316 bss_entry_adrp_label_(bss_entry_adrp_label) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000317 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
318 }
319
320 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000321 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000322 Location out = locations->Out();
323 constexpr bool call_saves_everything_except_r0_ip0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexandre Rames67555f72014-11-18 10:55:16 +0000324 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
325
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000326 InvokeRuntimeCallingConvention calling_convention;
327 // For HLoadClass/kBssEntry/kSaveEverything, the page address of the entry is in a temp
328 // register, make sure it's not clobbered by the call or by saving/restoring registers.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000329 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
330 bool is_load_class_bss_entry =
331 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000332 if (is_load_class_bss_entry) {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000333 DCHECK(bss_entry_temp_.IsValid());
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000334 DCHECK(!bss_entry_temp_.Is(calling_convention.GetRegisterAt(0)));
335 DCHECK(
336 !UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(bss_entry_temp_));
Vladimir Markoea4c1262017-02-06 19:59:33 +0000337 }
338
Alexandre Rames67555f72014-11-18 10:55:16 +0000339 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000340 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000341
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000342 dex::TypeIndex type_index = cls_->GetTypeIndex();
343 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000344 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
345 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000346 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800347 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100348 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800349 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100350 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800351 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000352
353 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000354 if (out.IsValid()) {
355 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000356 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000357 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000358 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000359 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000360 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000361 if (is_load_class_bss_entry) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000362 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000363 const DexFile& dex_file = cls_->GetDexFile();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000364 if (call_saves_everything_except_r0_ip0) {
365 // The class entry page address was preserved in bss_entry_temp_ thanks to kSaveEverything.
366 } else {
367 // For non-Baker read barrier, we need to re-calculate the address of the class entry page.
368 bss_entry_adrp_label_ = arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
369 arm64_codegen->EmitAdrpPlaceholder(bss_entry_adrp_label_, bss_entry_temp_);
370 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000371 vixl::aarch64::Label* strp_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +0000372 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label_);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000373 {
374 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
375 __ Bind(strp_label);
376 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Markoea4c1262017-02-06 19:59:33 +0000377 MemOperand(bss_entry_temp_, /* offset placeholder */ 0));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000378 }
379 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000380 __ B(GetExitLabel());
381 }
382
Alexandre Rames9931f312015-06-19 14:47:01 +0100383 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
384
Alexandre Rames67555f72014-11-18 10:55:16 +0000385 private:
386 // The class this slow path will load.
387 HLoadClass* const cls_;
388
Alexandre Rames67555f72014-11-18 10:55:16 +0000389 // The dex PC of `at_`.
390 const uint32_t dex_pc_;
391
392 // Whether to initialize the class.
393 const bool do_clinit_;
394
Vladimir Markoea4c1262017-02-06 19:59:33 +0000395 // For HLoadClass/kBssEntry, the temp register and the label of the ADRP where it was loaded.
396 vixl::aarch64::Register bss_entry_temp_;
397 vixl::aarch64::Label* bss_entry_adrp_label_;
398
Alexandre Rames67555f72014-11-18 10:55:16 +0000399 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
400};
401
Vladimir Markoaad75c62016-10-03 08:46:48 +0000402class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
403 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100404 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
405 : SlowPathCodeARM64(instruction),
406 temp_(temp),
407 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000408
409 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
410 LocationSummary* locations = instruction_->GetLocations();
411 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
412 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
413
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000414 InvokeRuntimeCallingConvention calling_convention;
415 // Make sure `temp_` is not clobbered by the call or by saving/restoring registers.
416 DCHECK(temp_.IsValid());
417 DCHECK(!temp_.Is(calling_convention.GetRegisterAt(0)));
418 DCHECK(!UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(temp_));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100419
Vladimir Markoaad75c62016-10-03 08:46:48 +0000420 __ Bind(GetEntryLabel());
421 SaveLiveRegisters(codegen, locations);
422
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000423 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
424 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000425 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
426 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
427 Primitive::Type type = instruction_->GetType();
428 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
429
430 RestoreLiveRegisters(codegen, locations);
431
432 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000433 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100434 if (!kUseReadBarrier || kUseBakerReadBarrier) {
435 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
436 } else {
437 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
438 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
439 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
440 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000441 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100442 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000443 {
444 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
445 __ Bind(strp_label);
446 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100447 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000448 }
449
450 __ B(GetExitLabel());
451 }
452
453 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
454
455 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100456 const Register temp_;
457 vixl::aarch64::Label* adrp_label_;
458
Vladimir Markoaad75c62016-10-03 08:46:48 +0000459 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
460};
461
Alexandre Rames5319def2014-10-23 10:03:10 +0100462class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
463 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000464 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100465
Alexandre Rames67555f72014-11-18 10:55:16 +0000466 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
467 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100468 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000469 if (instruction_->CanThrowIntoCatchBlock()) {
470 // Live registers will be restored in the catch block if caught.
471 SaveLiveRegisters(codegen, instruction_->GetLocations());
472 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000473 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
474 instruction_,
475 instruction_->GetDexPc(),
476 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800477 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100478 }
479
Alexandre Rames8158f282015-08-07 10:26:17 +0100480 bool IsFatal() const OVERRIDE { return true; }
481
Alexandre Rames9931f312015-06-19 14:47:01 +0100482 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
483
Alexandre Rames5319def2014-10-23 10:03:10 +0100484 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100485 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
486};
487
488class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
489 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100490 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000491 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100492
Alexandre Rames67555f72014-11-18 10:55:16 +0000493 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100494 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000495 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100496 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100497 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000498 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800499 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100500 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000501 if (successor_ == nullptr) {
502 __ B(GetReturnLabel());
503 } else {
504 __ B(arm64_codegen->GetLabelOf(successor_));
505 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100506 }
507
Scott Wakeling97c72b72016-06-24 16:19:36 +0100508 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100509 DCHECK(successor_ == nullptr);
510 return &return_label_;
511 }
512
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100513 HBasicBlock* GetSuccessor() const {
514 return successor_;
515 }
516
Alexandre Rames9931f312015-06-19 14:47:01 +0100517 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
518
Alexandre Rames5319def2014-10-23 10:03:10 +0100519 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100520 // If not null, the block to branch to after the suspend check.
521 HBasicBlock* const successor_;
522
523 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100524 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100525
526 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
527};
528
Alexandre Rames67555f72014-11-18 10:55:16 +0000529class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
530 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000531 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000532 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000533
534 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000535 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800536
Alexandre Rames3e69f162014-12-10 10:36:50 +0000537 DCHECK(instruction_->IsCheckCast()
538 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
539 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100540 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000541
Alexandre Rames67555f72014-11-18 10:55:16 +0000542 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000543
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000544 if (!is_fatal_) {
545 SaveLiveRegisters(codegen, locations);
546 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000547
548 // We're moving two locations to locations that could overlap, so we need a parallel
549 // move resolver.
550 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800551 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800552 LocationFrom(calling_convention.GetRegisterAt(0)),
553 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800554 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800555 LocationFrom(calling_convention.GetRegisterAt(1)),
556 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000557 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000558 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800559 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000560 Primitive::Type ret_type = instruction_->GetType();
561 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
562 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
563 } else {
564 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800565 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
566 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000567 }
568
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000569 if (!is_fatal_) {
570 RestoreLiveRegisters(codegen, locations);
571 __ B(GetExitLabel());
572 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000573 }
574
Alexandre Rames9931f312015-06-19 14:47:01 +0100575 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100576 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100577
Alexandre Rames67555f72014-11-18 10:55:16 +0000578 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000579 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000580
Alexandre Rames67555f72014-11-18 10:55:16 +0000581 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
582};
583
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700584class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
585 public:
Aart Bik42249c32016-01-07 15:33:50 -0800586 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000587 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700588
589 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800590 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700591 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100592 LocationSummary* locations = instruction_->GetLocations();
593 SaveLiveRegisters(codegen, locations);
594 InvokeRuntimeCallingConvention calling_convention;
595 __ Mov(calling_convention.GetRegisterAt(0),
596 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000597 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100598 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700599 }
600
Alexandre Rames9931f312015-06-19 14:47:01 +0100601 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
602
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700603 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700604 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
605};
606
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100607class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
608 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000609 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100610
611 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
612 LocationSummary* locations = instruction_->GetLocations();
613 __ Bind(GetEntryLabel());
614 SaveLiveRegisters(codegen, locations);
615
616 InvokeRuntimeCallingConvention calling_convention;
617 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
618 parallel_move.AddMove(
619 locations->InAt(0),
620 LocationFrom(calling_convention.GetRegisterAt(0)),
621 Primitive::kPrimNot,
622 nullptr);
623 parallel_move.AddMove(
624 locations->InAt(1),
625 LocationFrom(calling_convention.GetRegisterAt(1)),
626 Primitive::kPrimInt,
627 nullptr);
628 parallel_move.AddMove(
629 locations->InAt(2),
630 LocationFrom(calling_convention.GetRegisterAt(2)),
631 Primitive::kPrimNot,
632 nullptr);
633 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
634
635 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000636 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100637 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
638 RestoreLiveRegisters(codegen, locations);
639 __ B(GetExitLabel());
640 }
641
642 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
643
644 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100645 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
646};
647
Zheng Xu3927c8b2015-11-18 17:46:25 +0800648void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
649 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000650 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800651
652 // We are about to use the assembler to place literals directly. Make sure we have enough
653 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000654 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
655 num_entries * sizeof(int32_t),
656 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800657
658 __ Bind(&table_start_);
659 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
660 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100661 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800662 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100663 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800664 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
665 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
666 Literal<int32_t> literal(jump_offset);
667 __ place(&literal);
668 }
669}
670
Roland Levillain54f869e2017-03-06 13:54:11 +0000671// Abstract base class for read barrier slow paths marking a reference
672// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000673//
Roland Levillain54f869e2017-03-06 13:54:11 +0000674// Argument `entrypoint` must be a register location holding the read
675// barrier marking runtime entry point to be invoked.
676class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
677 protected:
678 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
679 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000680 DCHECK(kEmitCompilerReadBarrier);
681 }
682
Roland Levillain54f869e2017-03-06 13:54:11 +0000683 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000684
Roland Levillain54f869e2017-03-06 13:54:11 +0000685 // Generate assembly code calling the read barrier marking runtime
686 // entry point (ReadBarrierMarkRegX).
687 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000688 // No need to save live registers; it's taken care of by the
689 // entrypoint. Also, there is no need to update the stack mask,
690 // as this runtime call will not trigger a garbage collection.
691 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
692 DCHECK_NE(ref_.reg(), LR);
693 DCHECK_NE(ref_.reg(), WSP);
694 DCHECK_NE(ref_.reg(), WZR);
695 // IP0 is used internally by the ReadBarrierMarkRegX entry point
696 // as a temporary, it cannot be the entry point's input/output.
697 DCHECK_NE(ref_.reg(), IP0);
698 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
699 // "Compact" slow path, saving two moves.
700 //
701 // Instead of using the standard runtime calling convention (input
702 // and output in W0):
703 //
704 // W0 <- ref
705 // W0 <- ReadBarrierMark(W0)
706 // ref <- W0
707 //
708 // we just use rX (the register containing `ref`) as input and output
709 // of a dedicated entrypoint:
710 //
711 // rX <- ReadBarrierMarkRegX(rX)
712 //
713 if (entrypoint_.IsValid()) {
714 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
715 __ Blr(XRegisterFrom(entrypoint_));
716 } else {
717 // Entrypoint is not already loaded, load from the thread.
718 int32_t entry_point_offset =
719 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
720 // This runtime call does not require a stack map.
721 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
722 }
723 }
724
725 // The location (register) of the marked object reference.
726 const Location ref_;
727
728 // The location of the entrypoint if it is already loaded.
729 const Location entrypoint_;
730
Roland Levillain54f869e2017-03-06 13:54:11 +0000731 private:
732 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
733};
734
Alexandre Rames5319def2014-10-23 10:03:10 +0100735// Slow path marking an object reference `ref` during a read
736// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000737// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100738//
739// This means that after the execution of this slow path, `ref` will
740// always be up-to-date, but `obj.field` may not; i.e., after the
741// flip, `ref` will be a to-space reference, but `obj.field` will
742// probably still be a from-space reference (unless it gets updated by
743// another thread, or if another thread installed another object
744// reference (different from `ref`) in `obj.field`).
745//
746// If `entrypoint` is a valid location it is assumed to already be
747// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000748// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000749class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100750 public:
751 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
752 Location ref,
753 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000754 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100755 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100756 }
757
758 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
759
760 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000761 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100762 DCHECK(locations->CanCall());
763 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000764 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000765 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
766 << "Unexpected instruction in read barrier marking slow path: "
767 << instruction_->DebugName();
768
769 __ Bind(GetEntryLabel());
770 GenerateReadBarrierMarkRuntimeCall(codegen);
771 __ B(GetExitLabel());
772 }
773
774 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000775 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
776};
777
Roland Levillain54f869e2017-03-06 13:54:11 +0000778// Slow path loading `obj`'s lock word, loading a reference from
779// object `*(obj + offset + (index << scale_factor))` into `ref`, and
780// marking `ref` if `obj` is gray according to the lock word (Baker
781// read barrier). The field `obj.field` in the object `obj` holding
782// this reference does not get updated by this slow path after marking
783// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
784// below for that).
785//
786// This means that after the execution of this slow path, `ref` will
787// always be up-to-date, but `obj.field` may not; i.e., after the
788// flip, `ref` will be a to-space reference, but `obj.field` will
789// probably still be a from-space reference (unless it gets updated by
790// another thread, or if another thread installed another object
791// reference (different from `ref`) in `obj.field`).
792//
793// Argument `entrypoint` must be a register location holding the read
794// barrier marking runtime entry point to be invoked.
795class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
796 public:
797 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
798 Location ref,
799 Register obj,
800 uint32_t offset,
801 Location index,
802 size_t scale_factor,
803 bool needs_null_check,
804 bool use_load_acquire,
805 Register temp,
806 Location entrypoint)
807 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
808 obj_(obj),
809 offset_(offset),
810 index_(index),
811 scale_factor_(scale_factor),
812 needs_null_check_(needs_null_check),
813 use_load_acquire_(use_load_acquire),
814 temp_(temp) {
815 DCHECK(kEmitCompilerReadBarrier);
816 DCHECK(kUseBakerReadBarrier);
817 }
818
819 const char* GetDescription() const OVERRIDE {
820 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
821 }
822
823 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
824 LocationSummary* locations = instruction_->GetLocations();
825 DCHECK(locations->CanCall());
826 DCHECK(ref_.IsRegister()) << ref_;
827 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
828 DCHECK(obj_.IsW());
829 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100830 DCHECK(instruction_->IsInstanceFieldGet() ||
831 instruction_->IsStaticFieldGet() ||
832 instruction_->IsArrayGet() ||
833 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100834 instruction_->IsInstanceOf() ||
835 instruction_->IsCheckCast() ||
836 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
837 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
838 << "Unexpected instruction in read barrier marking slow path: "
839 << instruction_->DebugName();
840 // The read barrier instrumentation of object ArrayGet
841 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000842 // instruction.
843 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000844 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
845
Roland Levillain54f869e2017-03-06 13:54:11 +0000846 // Temporary register `temp_`, used to store the lock word, must
847 // not be IP0 nor IP1, as we may use them to emit the reference
848 // load (in the call to GenerateRawReferenceLoad below), and we
849 // need the lock word to still be in `temp_` after the reference
850 // load.
851 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
852 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
853
Alexandre Rames5319def2014-10-23 10:03:10 +0100854 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000855
856 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
857 // inserted after the original load. However, in fast path based
858 // Baker's read barriers, we need to perform the load of
859 // mirror::Object::monitor_ *before* the original reference load.
860 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000861 // The slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100862 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000863 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
864 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
865 // HeapReference<mirror::Object> ref = *src; // Original reference load.
866 // bool is_gray = (rb_state == ReadBarrier::GrayState());
867 // if (is_gray) {
868 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
869 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000870 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000871 // Note: the original implementation in ReadBarrier::Barrier is
872 // slightly more complex as it performs additional checks that we do
873 // not do here for performance reasons.
874
875 // /* int32_t */ monitor = obj->monitor_
876 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
877 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
878 if (needs_null_check_) {
879 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100880 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000881 // /* LockWord */ lock_word = LockWord(monitor)
882 static_assert(sizeof(LockWord) == sizeof(int32_t),
883 "art::LockWord and int32_t have different sizes.");
884
885 // Introduce a dependency on the lock_word including rb_state,
886 // to prevent load-load reordering, and without using
887 // a memory barrier (which would be more expensive).
888 // `obj` is unchanged by this operation, but its value now depends
889 // on `temp`.
890 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
891
892 // The actual reference load.
893 // A possible implicit null check has already been handled above.
894 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
895 arm64_codegen->GenerateRawReferenceLoad(instruction_,
896 ref_,
897 obj_,
898 offset_,
899 index_,
900 scale_factor_,
901 /* needs_null_check */ false,
902 use_load_acquire_);
903
904 // Mark the object `ref` when `obj` is gray.
905 //
906 // if (rb_state == ReadBarrier::GrayState())
907 // ref = ReadBarrier::Mark(ref);
908 //
909 // Given the numeric representation, it's enough to check the low bit of the rb_state.
910 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
911 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
912 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
913 GenerateReadBarrierMarkRuntimeCall(codegen);
914
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000915 __ B(GetExitLabel());
916 }
917
918 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000919 // The register containing the object holding the marked object reference field.
920 Register obj_;
921 // The offset, index and scale factor to access the reference in `obj_`.
922 uint32_t offset_;
923 Location index_;
924 size_t scale_factor_;
925 // Is a null check required?
926 bool needs_null_check_;
927 // Should this reference load use Load-Acquire semantics?
928 bool use_load_acquire_;
929 // A temporary register used to hold the lock word of `obj_`.
930 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000931
Roland Levillain54f869e2017-03-06 13:54:11 +0000932 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000933};
934
Roland Levillain54f869e2017-03-06 13:54:11 +0000935// Slow path loading `obj`'s lock word, loading a reference from
936// object `*(obj + offset + (index << scale_factor))` into `ref`, and
937// marking `ref` if `obj` is gray according to the lock word (Baker
938// read barrier). If needed, this slow path also atomically updates
939// the field `obj.field` in the object `obj` holding this reference
940// after marking (contrary to
941// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
942// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100943//
944// This means that after the execution of this slow path, both `ref`
945// and `obj.field` will be up-to-date; i.e., after the flip, both will
946// hold the same to-space reference (unless another thread installed
947// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000948//
Roland Levillain54f869e2017-03-06 13:54:11 +0000949// Argument `entrypoint` must be a register location holding the read
950// barrier marking runtime entry point to be invoked.
951class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
952 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100953 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000954 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(HInstruction* instruction,
955 Location ref,
956 Register obj,
957 uint32_t offset,
958 Location index,
959 size_t scale_factor,
960 bool needs_null_check,
961 bool use_load_acquire,
962 Register temp,
963 Location entrypoint)
964 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100965 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000966 offset_(offset),
967 index_(index),
968 scale_factor_(scale_factor),
969 needs_null_check_(needs_null_check),
970 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000971 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100972 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000973 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100974 }
975
976 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000977 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100978 }
979
980 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
981 LocationSummary* locations = instruction_->GetLocations();
982 Register ref_reg = WRegisterFrom(ref_);
983 DCHECK(locations->CanCall());
984 DCHECK(ref_.IsRegister()) << ref_;
985 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000986 DCHECK(obj_.IsW());
987 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
988
989 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100990 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
991 << "Unexpected instruction in read barrier marking and field updating slow path: "
992 << instruction_->DebugName();
993 DCHECK(instruction_->GetLocations()->Intrinsified());
994 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000995 DCHECK_EQ(offset_, 0u);
996 DCHECK_EQ(scale_factor_, 0u);
997 DCHECK_EQ(use_load_acquire_, false);
998 // The location of the offset of the marked reference field within `obj_`.
999 Location field_offset = index_;
1000 DCHECK(field_offset.IsRegister()) << field_offset;
1001
1002 // Temporary register `temp_`, used to store the lock word, must
1003 // not be IP0 nor IP1, as we may use them to emit the reference
1004 // load (in the call to GenerateRawReferenceLoad below), and we
1005 // need the lock word to still be in `temp_` after the reference
1006 // load.
1007 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1008 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001009
1010 __ Bind(GetEntryLabel());
1011
Roland Levillainff487002017-03-07 16:50:01 +00001012 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM64's:
1013 //
1014 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1015 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1016 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1017 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1018 // if (is_gray) {
1019 // old_ref = ref;
1020 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1021 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1022 // }
1023
Roland Levillain54f869e2017-03-06 13:54:11 +00001024 // /* int32_t */ monitor = obj->monitor_
1025 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1026 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
1027 if (needs_null_check_) {
1028 codegen->MaybeRecordImplicitNullCheck(instruction_);
1029 }
1030 // /* LockWord */ lock_word = LockWord(monitor)
1031 static_assert(sizeof(LockWord) == sizeof(int32_t),
1032 "art::LockWord and int32_t have different sizes.");
1033
1034 // Introduce a dependency on the lock_word including rb_state,
1035 // to prevent load-load reordering, and without using
1036 // a memory barrier (which would be more expensive).
1037 // `obj` is unchanged by this operation, but its value now depends
1038 // on `temp`.
1039 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
1040
1041 // The actual reference load.
1042 // A possible implicit null check has already been handled above.
1043 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1044 arm64_codegen->GenerateRawReferenceLoad(instruction_,
1045 ref_,
1046 obj_,
1047 offset_,
1048 index_,
1049 scale_factor_,
1050 /* needs_null_check */ false,
1051 use_load_acquire_);
1052
1053 // Mark the object `ref` when `obj` is gray.
1054 //
1055 // if (rb_state == ReadBarrier::GrayState())
1056 // ref = ReadBarrier::Mark(ref);
1057 //
1058 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1059 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1060 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1061 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1062
1063 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001064 // Note that we cannot use IP to save the old reference, as IP is
1065 // used internally by the ReadBarrierMarkRegX entry point, and we
1066 // need the old reference after the call to that entry point.
1067 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1068 __ Mov(temp_.W(), ref_reg);
1069
Roland Levillain54f869e2017-03-06 13:54:11 +00001070 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001071
1072 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001073 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001074 //
1075 // Note that this field could also hold a different object, if
1076 // another thread had concurrently changed it. In that case, the
1077 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1078 // (CAS) operation below would abort the CAS, leaving the field
1079 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001080 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001081 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001082
1083 // Update the the holder's field atomically. This may fail if
1084 // mutator updates before us, but it's OK. This is achieved
1085 // using a strong compare-and-set (CAS) operation with relaxed
1086 // memory synchronization ordering, where the expected value is
1087 // the old reference and the desired value is the new reference.
1088
1089 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1090 UseScratchRegisterScope temps(masm);
1091
1092 // Convenience aliases.
1093 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001094 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001095 Register expected = temp_.W();
1096 Register value = ref_reg;
1097 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1098 Register tmp_value = temps.AcquireW(); // Value in memory.
1099
1100 __ Add(tmp_ptr, base.X(), Operand(offset));
1101
1102 if (kPoisonHeapReferences) {
1103 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1104 if (value.Is(expected)) {
1105 // Do not poison `value`, as it is the same register as
1106 // `expected`, which has just been poisoned.
1107 } else {
1108 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1109 }
1110 }
1111
1112 // do {
1113 // tmp_value = [tmp_ptr] - expected;
1114 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1115
Roland Levillain24a4d112016-10-26 13:10:46 +01001116 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001117 __ Bind(&loop_head);
1118 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1119 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001120 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001121 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1122 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001123 __ B(&exit_loop);
1124 __ Bind(&comparison_failed);
1125 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001126 __ Bind(&exit_loop);
1127
1128 if (kPoisonHeapReferences) {
1129 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1130 if (value.Is(expected)) {
1131 // Do not unpoison `value`, as it is the same register as
1132 // `expected`, which has just been unpoisoned.
1133 } else {
1134 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1135 }
1136 }
1137
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001138 __ B(GetExitLabel());
1139 }
1140
1141 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001142 // The register containing the object holding the marked object reference field.
1143 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001144 // The offset, index and scale factor to access the reference in `obj_`.
1145 uint32_t offset_;
1146 Location index_;
1147 size_t scale_factor_;
1148 // Is a null check required?
1149 bool needs_null_check_;
1150 // Should this reference load use Load-Acquire semantics?
1151 bool use_load_acquire_;
1152 // A temporary register used to hold the lock word of `obj_`; and
1153 // also to hold the original reference value, when the reference is
1154 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001155 const Register temp_;
1156
Roland Levillain54f869e2017-03-06 13:54:11 +00001157 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001158};
1159
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001160// Slow path generating a read barrier for a heap reference.
1161class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1162 public:
1163 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1164 Location out,
1165 Location ref,
1166 Location obj,
1167 uint32_t offset,
1168 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001169 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001170 out_(out),
1171 ref_(ref),
1172 obj_(obj),
1173 offset_(offset),
1174 index_(index) {
1175 DCHECK(kEmitCompilerReadBarrier);
1176 // If `obj` is equal to `out` or `ref`, it means the initial object
1177 // has been overwritten by (or after) the heap object reference load
1178 // to be instrumented, e.g.:
1179 //
1180 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001181 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001182 //
1183 // In that case, we have lost the information about the original
1184 // object, and the emitted read barrier cannot work properly.
1185 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1186 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1187 }
1188
1189 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1190 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1191 LocationSummary* locations = instruction_->GetLocations();
1192 Primitive::Type type = Primitive::kPrimNot;
1193 DCHECK(locations->CanCall());
1194 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001195 DCHECK(instruction_->IsInstanceFieldGet() ||
1196 instruction_->IsStaticFieldGet() ||
1197 instruction_->IsArrayGet() ||
1198 instruction_->IsInstanceOf() ||
1199 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001200 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001201 << "Unexpected instruction in read barrier for heap reference slow path: "
1202 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001203 // The read barrier instrumentation of object ArrayGet
1204 // instructions does not support the HIntermediateAddress
1205 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001206 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001207 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001208
1209 __ Bind(GetEntryLabel());
1210
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001211 SaveLiveRegisters(codegen, locations);
1212
1213 // We may have to change the index's value, but as `index_` is a
1214 // constant member (like other "inputs" of this slow path),
1215 // introduce a copy of it, `index`.
1216 Location index = index_;
1217 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001218 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001219 if (instruction_->IsArrayGet()) {
1220 // Compute the actual memory offset and store it in `index`.
1221 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
1222 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1223 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1224 // We are about to change the value of `index_reg` (see the
1225 // calls to vixl::MacroAssembler::Lsl and
1226 // vixl::MacroAssembler::Mov below), but it has
1227 // not been saved by the previous call to
1228 // art::SlowPathCode::SaveLiveRegisters, as it is a
1229 // callee-save register --
1230 // art::SlowPathCode::SaveLiveRegisters does not consider
1231 // callee-save registers, as it has been designed with the
1232 // assumption that callee-save registers are supposed to be
1233 // handled by the called function. So, as a callee-save
1234 // register, `index_reg` _would_ eventually be saved onto
1235 // the stack, but it would be too late: we would have
1236 // changed its value earlier. Therefore, we manually save
1237 // it here into another freely available register,
1238 // `free_reg`, chosen of course among the caller-save
1239 // registers (as a callee-save `free_reg` register would
1240 // exhibit the same problem).
1241 //
1242 // Note we could have requested a temporary register from
1243 // the register allocator instead; but we prefer not to, as
1244 // this is a slow path, and we know we can find a
1245 // caller-save register that is available.
1246 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1247 __ Mov(free_reg.W(), index_reg);
1248 index_reg = free_reg;
1249 index = LocationFrom(index_reg);
1250 } else {
1251 // The initial register stored in `index_` has already been
1252 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1253 // (as it is not a callee-save register), so we can freely
1254 // use it.
1255 }
1256 // Shifting the index value contained in `index_reg` by the scale
1257 // factor (2) cannot overflow in practice, as the runtime is
1258 // unable to allocate object arrays with a size larger than
1259 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1260 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1261 static_assert(
1262 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1263 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1264 __ Add(index_reg, index_reg, Operand(offset_));
1265 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001266 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1267 // intrinsics, `index_` is not shifted by a scale factor of 2
1268 // (as in the case of ArrayGet), as it is actually an offset
1269 // to an object field within an object.
1270 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001271 DCHECK(instruction_->GetLocations()->Intrinsified());
1272 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1273 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1274 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001275 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001276 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001277 }
1278 }
1279
1280 // We're moving two or three locations to locations that could
1281 // overlap, so we need a parallel move resolver.
1282 InvokeRuntimeCallingConvention calling_convention;
1283 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1284 parallel_move.AddMove(ref_,
1285 LocationFrom(calling_convention.GetRegisterAt(0)),
1286 type,
1287 nullptr);
1288 parallel_move.AddMove(obj_,
1289 LocationFrom(calling_convention.GetRegisterAt(1)),
1290 type,
1291 nullptr);
1292 if (index.IsValid()) {
1293 parallel_move.AddMove(index,
1294 LocationFrom(calling_convention.GetRegisterAt(2)),
1295 Primitive::kPrimInt,
1296 nullptr);
1297 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1298 } else {
1299 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1300 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1301 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001302 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001303 instruction_,
1304 instruction_->GetDexPc(),
1305 this);
1306 CheckEntrypointTypes<
1307 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1308 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1309
1310 RestoreLiveRegisters(codegen, locations);
1311
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001312 __ B(GetExitLabel());
1313 }
1314
1315 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1316
1317 private:
1318 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001319 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1320 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001321 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1322 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1323 return Register(VIXLRegCodeFromART(i), kXRegSize);
1324 }
1325 }
1326 // We shall never fail to find a free caller-save register, as
1327 // there are more than two core caller-save registers on ARM64
1328 // (meaning it is possible to find one which is different from
1329 // `ref` and `obj`).
1330 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1331 LOG(FATAL) << "Could not find a free register";
1332 UNREACHABLE();
1333 }
1334
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001335 const Location out_;
1336 const Location ref_;
1337 const Location obj_;
1338 const uint32_t offset_;
1339 // An additional location containing an index to an array.
1340 // Only used for HArrayGet and the UnsafeGetObject &
1341 // UnsafeGetObjectVolatile intrinsics.
1342 const Location index_;
1343
1344 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1345};
1346
1347// Slow path generating a read barrier for a GC root.
1348class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1349 public:
1350 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001351 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001352 DCHECK(kEmitCompilerReadBarrier);
1353 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001354
1355 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1356 LocationSummary* locations = instruction_->GetLocations();
1357 Primitive::Type type = Primitive::kPrimNot;
1358 DCHECK(locations->CanCall());
1359 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001360 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1361 << "Unexpected instruction in read barrier for GC root slow path: "
1362 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001363
1364 __ Bind(GetEntryLabel());
1365 SaveLiveRegisters(codegen, locations);
1366
1367 InvokeRuntimeCallingConvention calling_convention;
1368 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1369 // The argument of the ReadBarrierForRootSlow is not a managed
1370 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1371 // thus we need a 64-bit move here, and we cannot use
1372 //
1373 // arm64_codegen->MoveLocation(
1374 // LocationFrom(calling_convention.GetRegisterAt(0)),
1375 // root_,
1376 // type);
1377 //
1378 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1379 // reference type (`Primitive::kPrimNot`).
1380 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001381 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001382 instruction_,
1383 instruction_->GetDexPc(),
1384 this);
1385 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1386 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1387
1388 RestoreLiveRegisters(codegen, locations);
1389 __ B(GetExitLabel());
1390 }
1391
1392 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1393
1394 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001395 const Location out_;
1396 const Location root_;
1397
1398 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1399};
1400
Alexandre Rames5319def2014-10-23 10:03:10 +01001401#undef __
1402
1403Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
1404 Location next_location;
1405 if (type == Primitive::kPrimVoid) {
1406 LOG(FATAL) << "Unreachable type " << type;
1407 }
1408
1409 if (Primitive::IsFloatingPointType(type) &&
1410 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001411 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1412 } else if (!Primitive::IsFloatingPointType(type) &&
1413 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1414 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1415 } else {
1416 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001417 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1418 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001419 }
1420
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001421 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001422 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001423 return next_location;
1424}
1425
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001426Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001427 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001428}
1429
Serban Constantinescu579885a2015-02-22 20:51:33 +00001430CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1431 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001432 const CompilerOptions& compiler_options,
1433 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001434 : CodeGenerator(graph,
1435 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001436 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001437 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001438 callee_saved_core_registers.GetList(),
1439 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001440 compiler_options,
1441 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001442 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001443 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001444 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001445 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001446 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001447 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001448 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001449 uint32_literals_(std::less<uint32_t>(),
1450 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001451 uint64_literals_(std::less<uint64_t>(),
1452 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001453 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001454 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001455 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001456 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001457 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001458 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001459 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001460 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1461 jit_class_patches_(TypeReferenceValueComparator(),
1462 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001463 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001464 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001465}
Alexandre Rames5319def2014-10-23 10:03:10 +01001466
Alexandre Rames67555f72014-11-18 10:55:16 +00001467#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001468
Zheng Xu3927c8b2015-11-18 17:46:25 +08001469void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001470 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001471 jump_table->EmitTable(this);
1472 }
1473}
1474
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001475void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001476 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001477 // Ensure we emit the literal pool.
1478 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001479
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001480 CodeGenerator::Finalize(allocator);
1481}
1482
Zheng Xuad4450e2015-04-17 18:48:56 +08001483void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1484 // Note: There are 6 kinds of moves:
1485 // 1. constant -> GPR/FPR (non-cycle)
1486 // 2. constant -> stack (non-cycle)
1487 // 3. GPR/FPR -> GPR/FPR
1488 // 4. GPR/FPR -> stack
1489 // 5. stack -> GPR/FPR
1490 // 6. stack -> stack (non-cycle)
1491 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1492 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1493 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1494 // dependency.
1495 vixl_temps_.Open(GetVIXLAssembler());
1496}
1497
1498void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1499 vixl_temps_.Close();
1500}
1501
1502Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001503 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1504 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1505 || kind == Location::kSIMDStackSlot);
1506 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1507 ? Location::kFpuRegister
1508 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001509 Location scratch = GetScratchLocation(kind);
1510 if (!scratch.Equals(Location::NoLocation())) {
1511 return scratch;
1512 }
1513 // Allocate from VIXL temp registers.
1514 if (kind == Location::kRegister) {
1515 scratch = LocationFrom(vixl_temps_.AcquireX());
1516 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001517 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001518 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1519 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1520 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001521 }
1522 AddScratchLocation(scratch);
1523 return scratch;
1524}
1525
1526void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1527 if (loc.IsRegister()) {
1528 vixl_temps_.Release(XRegisterFrom(loc));
1529 } else {
1530 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001531 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001532 }
1533 RemoveScratchLocation(loc);
1534}
1535
Alexandre Rames3e69f162014-12-10 10:36:50 +00001536void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001537 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001538 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001539}
1540
Alexandre Rames5319def2014-10-23 10:03:10 +01001541void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001542 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001543 __ Bind(&frame_entry_label_);
1544
Serban Constantinescu02164b32014-11-13 14:05:07 +00001545 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1546 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001547 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001548 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001549 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001550 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001551 {
1552 // Ensure that between load and RecordPcInfo there are no pools emitted.
1553 ExactAssemblyScope eas(GetVIXLAssembler(),
1554 kInstructionSize,
1555 CodeBufferCheckScope::kExactSize);
1556 __ ldr(wzr, MemOperand(temp, 0));
1557 RecordPcInfo(nullptr, 0);
1558 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001559 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001560
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001561 if (!HasEmptyFrame()) {
1562 int frame_size = GetFrameSize();
1563 // Stack layout:
1564 // sp[frame_size - 8] : lr.
1565 // ... : other preserved core registers.
1566 // ... : other preserved fp registers.
1567 // ... : reserved frame space.
1568 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001569
1570 // Save the current method if we need it. Note that we do not
1571 // do this in HCurrentMethod, as the instruction might have been removed
1572 // in the SSA graph.
1573 if (RequiresCurrentMethod()) {
1574 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001575 } else {
1576 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001577 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001578 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001579 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1580 frame_size - GetCoreSpillSize());
1581 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1582 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001583
1584 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1585 // Initialize should_deoptimize flag to 0.
1586 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1587 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1588 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001589 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001590}
1591
1592void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001593 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001594 if (!HasEmptyFrame()) {
1595 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001596 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1597 frame_size - FrameEntrySpillSize());
1598 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1599 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001600 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001601 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001602 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001603 __ Ret();
1604 GetAssembler()->cfi().RestoreState();
1605 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001606}
1607
Scott Wakeling97c72b72016-06-24 16:19:36 +01001608CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001609 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001610 return CPURegList(CPURegister::kRegister, kXRegSize,
1611 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001612}
1613
Scott Wakeling97c72b72016-06-24 16:19:36 +01001614CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001615 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1616 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001617 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1618 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001619}
1620
Alexandre Rames5319def2014-10-23 10:03:10 +01001621void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1622 __ Bind(GetLabelOf(block));
1623}
1624
Calin Juravle175dc732015-08-25 15:42:32 +01001625void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1626 DCHECK(location.IsRegister());
1627 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1628}
1629
Calin Juravlee460d1d2015-09-29 04:52:17 +01001630void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1631 if (location.IsRegister()) {
1632 locations->AddTemp(location);
1633 } else {
1634 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1635 }
1636}
1637
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001638void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001639 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001640 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001641 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001642 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001643 if (value_can_be_null) {
1644 __ Cbz(value, &done);
1645 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001646 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001647 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001648 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001649 if (value_can_be_null) {
1650 __ Bind(&done);
1651 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001652}
1653
David Brazdil58282f42016-01-14 12:45:10 +00001654void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001655 // Blocked core registers:
1656 // lr : Runtime reserved.
1657 // tr : Runtime reserved.
1658 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1659 // ip1 : VIXL core temp.
1660 // ip0 : VIXL core temp.
1661 //
1662 // Blocked fp registers:
1663 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001664 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1665 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001666 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001667 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001668 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001669
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001670 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001671 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001672 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001673 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001674
David Brazdil58282f42016-01-14 12:45:10 +00001675 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001676 // Stubs do not save callee-save floating point registers. If the graph
1677 // is debuggable, we need to deal with these registers differently. For
1678 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001679 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1680 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001681 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001682 }
1683 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001684}
1685
Alexandre Rames3e69f162014-12-10 10:36:50 +00001686size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1687 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1688 __ Str(reg, MemOperand(sp, stack_index));
1689 return kArm64WordSize;
1690}
1691
1692size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1693 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1694 __ Ldr(reg, MemOperand(sp, stack_index));
1695 return kArm64WordSize;
1696}
1697
1698size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1699 FPRegister reg = FPRegister(reg_id, kDRegSize);
1700 __ Str(reg, MemOperand(sp, stack_index));
1701 return kArm64WordSize;
1702}
1703
1704size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1705 FPRegister reg = FPRegister(reg_id, kDRegSize);
1706 __ Ldr(reg, MemOperand(sp, stack_index));
1707 return kArm64WordSize;
1708}
1709
Alexandre Rames5319def2014-10-23 10:03:10 +01001710void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001711 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001712}
1713
1714void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001715 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001716}
1717
Alexandre Rames67555f72014-11-18 10:55:16 +00001718void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001719 if (constant->IsIntConstant()) {
1720 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1721 } else if (constant->IsLongConstant()) {
1722 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1723 } else if (constant->IsNullConstant()) {
1724 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001725 } else if (constant->IsFloatConstant()) {
1726 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1727 } else {
1728 DCHECK(constant->IsDoubleConstant());
1729 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1730 }
1731}
1732
Alexandre Rames3e69f162014-12-10 10:36:50 +00001733
1734static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1735 DCHECK(constant.IsConstant());
1736 HConstant* cst = constant.GetConstant();
1737 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001738 // Null is mapped to a core W register, which we associate with kPrimInt.
1739 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001740 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1741 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1742 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1743}
1744
Roland Levillain952b2352017-05-03 19:49:14 +01001745// Allocate a scratch register from the VIXL pool, querying first
1746// the floating-point register pool, and then the core register
1747// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001748// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1749// using a different allocation strategy.
1750static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1751 vixl::aarch64::UseScratchRegisterScope* temps,
1752 int size_in_bits) {
1753 return masm->GetScratchFPRegisterList()->IsEmpty()
1754 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1755 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1756}
1757
Calin Juravlee460d1d2015-09-29 04:52:17 +01001758void CodeGeneratorARM64::MoveLocation(Location destination,
1759 Location source,
1760 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001761 if (source.Equals(destination)) {
1762 return;
1763 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001764
1765 // A valid move can always be inferred from the destination and source
1766 // locations. When moving from and to a register, the argument type can be
1767 // used to generate 32bit instead of 64bit moves. In debug mode we also
1768 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001769 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001770
1771 if (destination.IsRegister() || destination.IsFpuRegister()) {
1772 if (unspecified_type) {
1773 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1774 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001775 (src_cst != nullptr && (src_cst->IsIntConstant()
1776 || src_cst->IsFloatConstant()
1777 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001778 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001779 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001780 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001781 // If the source is a double stack slot or a 64bit constant, a 64bit
1782 // type is appropriate. Else the source is a register, and since the
1783 // type has not been specified, we chose a 64bit type to force a 64bit
1784 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001785 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001786 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001787 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001788 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1789 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1790 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001791 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1792 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1793 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001794 } else if (source.IsSIMDStackSlot()) {
1795 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001796 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001797 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001798 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001799 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001800 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001801 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001802 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001803 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001804 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1805 ? Primitive::kPrimLong
1806 : Primitive::kPrimInt;
1807 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1808 }
1809 } else {
1810 DCHECK(source.IsFpuRegister());
1811 if (destination.IsRegister()) {
1812 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1813 ? Primitive::kPrimDouble
1814 : Primitive::kPrimFloat;
1815 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1816 } else {
1817 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001818 if (GetGraph()->HasSIMD()) {
1819 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1820 } else {
1821 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1822 }
1823 }
1824 }
1825 } else if (destination.IsSIMDStackSlot()) {
1826 if (source.IsFpuRegister()) {
1827 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1828 } else {
1829 DCHECK(source.IsSIMDStackSlot());
1830 UseScratchRegisterScope temps(GetVIXLAssembler());
1831 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1832 Register temp = temps.AcquireX();
1833 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1834 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1835 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1836 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1837 } else {
1838 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1839 __ Ldr(temp, StackOperandFrom(source));
1840 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001841 }
1842 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001843 } else { // The destination is not a register. It must be a stack slot.
1844 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1845 if (source.IsRegister() || source.IsFpuRegister()) {
1846 if (unspecified_type) {
1847 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001848 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001849 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001850 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001851 }
1852 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001853 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1854 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1855 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001856 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001857 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1858 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001859 UseScratchRegisterScope temps(GetVIXLAssembler());
1860 HConstant* src_cst = source.GetConstant();
1861 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001862 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001863 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1864 ? Register(xzr)
1865 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001866 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001867 if (src_cst->IsIntConstant()) {
1868 temp = temps.AcquireW();
1869 } else if (src_cst->IsLongConstant()) {
1870 temp = temps.AcquireX();
1871 } else if (src_cst->IsFloatConstant()) {
1872 temp = temps.AcquireS();
1873 } else {
1874 DCHECK(src_cst->IsDoubleConstant());
1875 temp = temps.AcquireD();
1876 }
1877 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001878 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001879 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001880 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001881 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001882 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001883 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001884 // Use any scratch register (a core or a floating-point one)
1885 // from VIXL scratch register pools as a temporary.
1886 //
1887 // We used to only use the FP scratch register pool, but in some
1888 // rare cases the only register from this pool (D31) would
1889 // already be used (e.g. within a ParallelMove instruction, when
1890 // a move is blocked by a another move requiring a scratch FP
1891 // register, which would reserve D31). To prevent this issue, we
1892 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001893 //
1894 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001895 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001896 // use AcquireFPOrCoreCPURegisterOfSize instead of
1897 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1898 // allocates core scratch registers first.
1899 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1900 GetVIXLAssembler(),
1901 &temps,
1902 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001903 __ Ldr(temp, StackOperandFrom(source));
1904 __ Str(temp, StackOperandFrom(destination));
1905 }
1906 }
1907}
1908
1909void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001910 CPURegister dst,
1911 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001912 switch (type) {
1913 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001914 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001915 break;
1916 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001917 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001918 break;
1919 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001920 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001921 break;
1922 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001923 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001924 break;
1925 case Primitive::kPrimInt:
1926 case Primitive::kPrimNot:
1927 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001928 case Primitive::kPrimFloat:
1929 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001930 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001931 __ Ldr(dst, src);
1932 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001933 case Primitive::kPrimVoid:
1934 LOG(FATAL) << "Unreachable type " << type;
1935 }
1936}
1937
Calin Juravle77520bc2015-01-12 18:45:46 +00001938void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001939 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001940 const MemOperand& src,
1941 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001942 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001943 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001944 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001945 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001946
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001947 DCHECK(!src.IsPreIndex());
1948 DCHECK(!src.IsPostIndex());
1949
1950 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001951 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001952 {
1953 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1954 MemOperand base = MemOperand(temp_base);
1955 switch (type) {
1956 case Primitive::kPrimBoolean:
1957 {
1958 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1959 __ ldarb(Register(dst), base);
1960 if (needs_null_check) {
1961 MaybeRecordImplicitNullCheck(instruction);
1962 }
1963 }
1964 break;
1965 case Primitive::kPrimByte:
1966 {
1967 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1968 __ ldarb(Register(dst), base);
1969 if (needs_null_check) {
1970 MaybeRecordImplicitNullCheck(instruction);
1971 }
1972 }
1973 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1974 break;
1975 case Primitive::kPrimChar:
1976 {
1977 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1978 __ ldarh(Register(dst), base);
1979 if (needs_null_check) {
1980 MaybeRecordImplicitNullCheck(instruction);
1981 }
1982 }
1983 break;
1984 case Primitive::kPrimShort:
1985 {
1986 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1987 __ ldarh(Register(dst), base);
1988 if (needs_null_check) {
1989 MaybeRecordImplicitNullCheck(instruction);
1990 }
1991 }
1992 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1993 break;
1994 case Primitive::kPrimInt:
1995 case Primitive::kPrimNot:
1996 case Primitive::kPrimLong:
1997 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
1998 {
1999 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2000 __ ldar(Register(dst), base);
2001 if (needs_null_check) {
2002 MaybeRecordImplicitNullCheck(instruction);
2003 }
2004 }
2005 break;
2006 case Primitive::kPrimFloat:
2007 case Primitive::kPrimDouble: {
2008 DCHECK(dst.IsFPRegister());
2009 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002010
Artem Serov914d7a82017-02-07 14:33:49 +00002011 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2012 {
2013 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2014 __ ldar(temp, base);
2015 if (needs_null_check) {
2016 MaybeRecordImplicitNullCheck(instruction);
2017 }
2018 }
2019 __ Fmov(FPRegister(dst), temp);
2020 break;
Roland Levillain44015862016-01-22 11:47:17 +00002021 }
Artem Serov914d7a82017-02-07 14:33:49 +00002022 case Primitive::kPrimVoid:
2023 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002024 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002025 }
2026}
2027
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002028void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002029 CPURegister src,
2030 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002031 switch (type) {
2032 case Primitive::kPrimBoolean:
2033 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002034 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002035 break;
2036 case Primitive::kPrimChar:
2037 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002038 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002039 break;
2040 case Primitive::kPrimInt:
2041 case Primitive::kPrimNot:
2042 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002043 case Primitive::kPrimFloat:
2044 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00002045 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002046 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002047 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002048 case Primitive::kPrimVoid:
2049 LOG(FATAL) << "Unreachable type " << type;
2050 }
2051}
2052
Artem Serov914d7a82017-02-07 14:33:49 +00002053void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
2054 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002055 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002056 const MemOperand& dst,
2057 bool needs_null_check) {
2058 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002059 UseScratchRegisterScope temps(GetVIXLAssembler());
2060 Register temp_base = temps.AcquireX();
2061
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002062 DCHECK(!dst.IsPreIndex());
2063 DCHECK(!dst.IsPostIndex());
2064
2065 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002066 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002067 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002068 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002069 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002070 switch (type) {
2071 case Primitive::kPrimBoolean:
2072 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00002073 {
2074 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2075 __ stlrb(Register(src), base);
2076 if (needs_null_check) {
2077 MaybeRecordImplicitNullCheck(instruction);
2078 }
2079 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002080 break;
2081 case Primitive::kPrimChar:
2082 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00002083 {
2084 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2085 __ stlrh(Register(src), base);
2086 if (needs_null_check) {
2087 MaybeRecordImplicitNullCheck(instruction);
2088 }
2089 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002090 break;
2091 case Primitive::kPrimInt:
2092 case Primitive::kPrimNot:
2093 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002094 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002095 {
2096 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2097 __ stlr(Register(src), base);
2098 if (needs_null_check) {
2099 MaybeRecordImplicitNullCheck(instruction);
2100 }
2101 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002102 break;
2103 case Primitive::kPrimFloat:
2104 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002105 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002106 Register temp_src;
2107 if (src.IsZero()) {
2108 // The zero register is used to avoid synthesizing zero constants.
2109 temp_src = Register(src);
2110 } else {
2111 DCHECK(src.IsFPRegister());
2112 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2113 __ Fmov(temp_src, FPRegister(src));
2114 }
Artem Serov914d7a82017-02-07 14:33:49 +00002115 {
2116 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2117 __ stlr(temp_src, base);
2118 if (needs_null_check) {
2119 MaybeRecordImplicitNullCheck(instruction);
2120 }
2121 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002122 break;
2123 }
2124 case Primitive::kPrimVoid:
2125 LOG(FATAL) << "Unreachable type " << type;
2126 }
2127}
2128
Calin Juravle175dc732015-08-25 15:42:32 +01002129void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2130 HInstruction* instruction,
2131 uint32_t dex_pc,
2132 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002133 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002134
2135 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2136 {
2137 // Ensure the pc position is recorded immediately after the `blr` instruction.
2138 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2139 __ blr(lr);
2140 if (EntrypointRequiresStackMap(entrypoint)) {
2141 RecordPcInfo(instruction, dex_pc, slow_path);
2142 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002143 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002144}
2145
Roland Levillaindec8f632016-07-22 17:10:06 +01002146void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2147 HInstruction* instruction,
2148 SlowPathCode* slow_path) {
2149 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002150 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2151 __ Blr(lr);
2152}
2153
Alexandre Rames67555f72014-11-18 10:55:16 +00002154void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002155 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002156 UseScratchRegisterScope temps(GetVIXLAssembler());
2157 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002158 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2159
Serban Constantinescu02164b32014-11-13 14:05:07 +00002160 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002161 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2162 __ Add(temp, class_reg, status_offset);
2163 __ Ldar(temp, HeapOperand(temp));
2164 __ Cmp(temp, mirror::Class::kStatusInitialized);
2165 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002166 __ Bind(slow_path->GetExitLabel());
2167}
Alexandre Rames5319def2014-10-23 10:03:10 +01002168
Roland Levillain44015862016-01-22 11:47:17 +00002169void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002170 BarrierType type = BarrierAll;
2171
2172 switch (kind) {
2173 case MemBarrierKind::kAnyAny:
2174 case MemBarrierKind::kAnyStore: {
2175 type = BarrierAll;
2176 break;
2177 }
2178 case MemBarrierKind::kLoadAny: {
2179 type = BarrierReads;
2180 break;
2181 }
2182 case MemBarrierKind::kStoreStore: {
2183 type = BarrierWrites;
2184 break;
2185 }
2186 default:
2187 LOG(FATAL) << "Unexpected memory barrier " << kind;
2188 }
2189 __ Dmb(InnerShareable, type);
2190}
2191
Serban Constantinescu02164b32014-11-13 14:05:07 +00002192void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2193 HBasicBlock* successor) {
2194 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002195 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2196 if (slow_path == nullptr) {
2197 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2198 instruction->SetSlowPath(slow_path);
2199 codegen_->AddSlowPath(slow_path);
2200 if (successor != nullptr) {
2201 DCHECK(successor->IsLoopHeader());
2202 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2203 }
2204 } else {
2205 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2206 }
2207
Serban Constantinescu02164b32014-11-13 14:05:07 +00002208 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2209 Register temp = temps.AcquireW();
2210
Andreas Gampe542451c2016-07-26 09:02:02 -07002211 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002212 if (successor == nullptr) {
2213 __ Cbnz(temp, slow_path->GetEntryLabel());
2214 __ Bind(slow_path->GetReturnLabel());
2215 } else {
2216 __ Cbz(temp, codegen_->GetLabelOf(successor));
2217 __ B(slow_path->GetEntryLabel());
2218 // slow_path will return to GetLabelOf(successor).
2219 }
2220}
2221
Alexandre Rames5319def2014-10-23 10:03:10 +01002222InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2223 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002224 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002225 assembler_(codegen->GetAssembler()),
2226 codegen_(codegen) {}
2227
2228#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002229 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002230
2231#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2232
2233enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002234 // Using a base helps identify when we hit such breakpoints.
2235 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002236#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2237 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2238#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2239};
2240
2241#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002242 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002243 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2244 } \
2245 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2246 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2247 locations->SetOut(Location::Any()); \
2248 }
2249 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2250#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2251
2252#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002253#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002254
Alexandre Rames67555f72014-11-18 10:55:16 +00002255void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002256 DCHECK_EQ(instr->InputCount(), 2U);
2257 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2258 Primitive::Type type = instr->GetResultType();
2259 switch (type) {
2260 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002261 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002262 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002263 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002264 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002265 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002266
2267 case Primitive::kPrimFloat:
2268 case Primitive::kPrimDouble:
2269 locations->SetInAt(0, Location::RequiresFpuRegister());
2270 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002271 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002272 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002273
Alexandre Rames5319def2014-10-23 10:03:10 +01002274 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002275 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002276 }
2277}
2278
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002279void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2280 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002281 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2282
2283 bool object_field_get_with_read_barrier =
2284 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002285 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002286 new (GetGraph()->GetArena()) LocationSummary(instruction,
2287 object_field_get_with_read_barrier ?
2288 LocationSummary::kCallOnSlowPath :
2289 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002290 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002291 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002292 // We need a temporary register for the read barrier marking slow
2293 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002294 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2295 !Runtime::Current()->UseJitCompilation() &&
2296 !field_info.IsVolatile()) {
2297 // If link-time thunks for the Baker read barrier are enabled, for AOT
2298 // non-volatile loads we need a temporary only if the offset is too big.
2299 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2300 locations->AddTemp(FixedTempLocation());
2301 }
2302 } else {
2303 locations->AddTemp(Location::RequiresRegister());
2304 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002305 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002306 locations->SetInAt(0, Location::RequiresRegister());
2307 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2308 locations->SetOut(Location::RequiresFpuRegister());
2309 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002310 // The output overlaps for an object field get when read barriers
2311 // are enabled: we do not want the load to overwrite the object's
2312 // location, as we need it to emit the read barrier.
2313 locations->SetOut(
2314 Location::RequiresRegister(),
2315 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002316 }
2317}
2318
2319void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2320 const FieldInfo& field_info) {
2321 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002322 LocationSummary* locations = instruction->GetLocations();
2323 Location base_loc = locations->InAt(0);
2324 Location out = locations->Out();
2325 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002326 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002327 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002328
Roland Levillain44015862016-01-22 11:47:17 +00002329 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2330 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002331 // /* HeapReference<Object> */ out = *(base + offset)
2332 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002333 Location maybe_temp =
2334 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002335 // Note that potential implicit null checks are handled in this
2336 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2337 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2338 instruction,
2339 out,
2340 base,
2341 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002342 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002343 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002344 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002345 } else {
2346 // General case.
2347 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002348 // Note that a potential implicit null check is handled in this
2349 // CodeGeneratorARM64::LoadAcquire call.
2350 // NB: LoadAcquire will record the pc info if needed.
2351 codegen_->LoadAcquire(
2352 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002353 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002354 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2355 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002356 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002357 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002358 }
Roland Levillain44015862016-01-22 11:47:17 +00002359 if (field_type == Primitive::kPrimNot) {
2360 // If read barriers are enabled, emit read barriers other than
2361 // Baker's using a slow path (and also unpoison the loaded
2362 // reference, if heap poisoning is enabled).
2363 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2364 }
Roland Levillain4d027112015-07-01 15:41:14 +01002365 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002366}
2367
2368void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2369 LocationSummary* locations =
2370 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2371 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002372 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2373 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2374 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002375 locations->SetInAt(1, Location::RequiresFpuRegister());
2376 } else {
2377 locations->SetInAt(1, Location::RequiresRegister());
2378 }
2379}
2380
2381void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002382 const FieldInfo& field_info,
2383 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002384 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2385
2386 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002387 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002388 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002389 Offset offset = field_info.GetFieldOffset();
2390 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002391
Roland Levillain4d027112015-07-01 15:41:14 +01002392 {
2393 // We use a block to end the scratch scope before the write barrier, thus
2394 // freeing the temporary registers so they can be used in `MarkGCCard`.
2395 UseScratchRegisterScope temps(GetVIXLAssembler());
2396
2397 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2398 DCHECK(value.IsW());
2399 Register temp = temps.AcquireW();
2400 __ Mov(temp, value.W());
2401 GetAssembler()->PoisonHeapReference(temp.W());
2402 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002403 }
Roland Levillain4d027112015-07-01 15:41:14 +01002404
2405 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002406 codegen_->StoreRelease(
2407 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002408 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002409 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2410 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002411 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2412 codegen_->MaybeRecordImplicitNullCheck(instruction);
2413 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002414 }
2415
2416 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002417 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002418 }
2419}
2420
Alexandre Rames67555f72014-11-18 10:55:16 +00002421void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002422 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002423
2424 switch (type) {
2425 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002426 case Primitive::kPrimLong: {
2427 Register dst = OutputRegister(instr);
2428 Register lhs = InputRegisterAt(instr, 0);
2429 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002430 if (instr->IsAdd()) {
2431 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002432 } else if (instr->IsAnd()) {
2433 __ And(dst, lhs, rhs);
2434 } else if (instr->IsOr()) {
2435 __ Orr(dst, lhs, rhs);
2436 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002437 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002438 } else if (instr->IsRor()) {
2439 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002440 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002441 __ Ror(dst, lhs, shift);
2442 } else {
2443 // Ensure shift distance is in the same size register as the result. If
2444 // we are rotating a long and the shift comes in a w register originally,
2445 // we don't need to sxtw for use as an x since the shift distances are
2446 // all & reg_bits - 1.
2447 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2448 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002449 } else {
2450 DCHECK(instr->IsXor());
2451 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002452 }
2453 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002454 }
2455 case Primitive::kPrimFloat:
2456 case Primitive::kPrimDouble: {
2457 FPRegister dst = OutputFPRegister(instr);
2458 FPRegister lhs = InputFPRegisterAt(instr, 0);
2459 FPRegister rhs = InputFPRegisterAt(instr, 1);
2460 if (instr->IsAdd()) {
2461 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002462 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002463 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002464 } else {
2465 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002466 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002467 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002468 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002469 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002470 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002471 }
2472}
2473
Serban Constantinescu02164b32014-11-13 14:05:07 +00002474void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2475 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2476
2477 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2478 Primitive::Type type = instr->GetResultType();
2479 switch (type) {
2480 case Primitive::kPrimInt:
2481 case Primitive::kPrimLong: {
2482 locations->SetInAt(0, Location::RequiresRegister());
2483 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002484 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002485 break;
2486 }
2487 default:
2488 LOG(FATAL) << "Unexpected shift type " << type;
2489 }
2490}
2491
2492void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2493 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2494
2495 Primitive::Type type = instr->GetType();
2496 switch (type) {
2497 case Primitive::kPrimInt:
2498 case Primitive::kPrimLong: {
2499 Register dst = OutputRegister(instr);
2500 Register lhs = InputRegisterAt(instr, 0);
2501 Operand rhs = InputOperandAt(instr, 1);
2502 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002503 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002504 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002505 if (instr->IsShl()) {
2506 __ Lsl(dst, lhs, shift_value);
2507 } else if (instr->IsShr()) {
2508 __ Asr(dst, lhs, shift_value);
2509 } else {
2510 __ Lsr(dst, lhs, shift_value);
2511 }
2512 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002513 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002514
2515 if (instr->IsShl()) {
2516 __ Lsl(dst, lhs, rhs_reg);
2517 } else if (instr->IsShr()) {
2518 __ Asr(dst, lhs, rhs_reg);
2519 } else {
2520 __ Lsr(dst, lhs, rhs_reg);
2521 }
2522 }
2523 break;
2524 }
2525 default:
2526 LOG(FATAL) << "Unexpected shift operation type " << type;
2527 }
2528}
2529
Alexandre Rames5319def2014-10-23 10:03:10 +01002530void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002531 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002532}
2533
2534void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002535 HandleBinaryOp(instruction);
2536}
2537
2538void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2539 HandleBinaryOp(instruction);
2540}
2541
2542void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2543 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002544}
2545
Artem Serov7fc63502016-02-09 17:15:29 +00002546void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002547 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2548 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2549 locations->SetInAt(0, Location::RequiresRegister());
2550 // There is no immediate variant of negated bitwise instructions in AArch64.
2551 locations->SetInAt(1, Location::RequiresRegister());
2552 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2553}
2554
Artem Serov7fc63502016-02-09 17:15:29 +00002555void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002556 Register dst = OutputRegister(instr);
2557 Register lhs = InputRegisterAt(instr, 0);
2558 Register rhs = InputRegisterAt(instr, 1);
2559
2560 switch (instr->GetOpKind()) {
2561 case HInstruction::kAnd:
2562 __ Bic(dst, lhs, rhs);
2563 break;
2564 case HInstruction::kOr:
2565 __ Orn(dst, lhs, rhs);
2566 break;
2567 case HInstruction::kXor:
2568 __ Eon(dst, lhs, rhs);
2569 break;
2570 default:
2571 LOG(FATAL) << "Unreachable";
2572 }
2573}
2574
Anton Kirilov74234da2017-01-13 14:42:47 +00002575void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2576 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002577 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2578 instruction->GetType() == Primitive::kPrimLong);
2579 LocationSummary* locations =
2580 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2581 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2582 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2583 } else {
2584 locations->SetInAt(0, Location::RequiresRegister());
2585 }
2586 locations->SetInAt(1, Location::RequiresRegister());
2587 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2588}
2589
Anton Kirilov74234da2017-01-13 14:42:47 +00002590void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2591 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002592 Primitive::Type type = instruction->GetType();
2593 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2594 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2595 Register out = OutputRegister(instruction);
2596 Register left;
2597 if (kind != HInstruction::kNeg) {
2598 left = InputRegisterAt(instruction, 0);
2599 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002600 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002601 // shifter operand operation, the IR generating `right_reg` (input to the type
2602 // conversion) can have a different type from the current instruction's type,
2603 // so we manually indicate the type.
2604 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002605 Operand right_operand(0);
2606
Anton Kirilov74234da2017-01-13 14:42:47 +00002607 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2608 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002609 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2610 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002611 right_operand = Operand(right_reg,
2612 helpers::ShiftFromOpKind(op_kind),
2613 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002614 }
2615
2616 // Logical binary operations do not support extension operations in the
2617 // operand. Note that VIXL would still manage if it was passed by generating
2618 // the extension as a separate instruction.
2619 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2620 DCHECK(!right_operand.IsExtendedRegister() ||
2621 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2622 kind != HInstruction::kNeg));
2623 switch (kind) {
2624 case HInstruction::kAdd:
2625 __ Add(out, left, right_operand);
2626 break;
2627 case HInstruction::kAnd:
2628 __ And(out, left, right_operand);
2629 break;
2630 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002631 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002632 __ Neg(out, right_operand);
2633 break;
2634 case HInstruction::kOr:
2635 __ Orr(out, left, right_operand);
2636 break;
2637 case HInstruction::kSub:
2638 __ Sub(out, left, right_operand);
2639 break;
2640 case HInstruction::kXor:
2641 __ Eor(out, left, right_operand);
2642 break;
2643 default:
2644 LOG(FATAL) << "Unexpected operation kind: " << kind;
2645 UNREACHABLE();
2646 }
2647}
2648
Artem Serov328429f2016-07-06 16:23:04 +01002649void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002650 LocationSummary* locations =
2651 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2652 locations->SetInAt(0, Location::RequiresRegister());
2653 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002654 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002655}
2656
Roland Levillain19c54192016-11-04 13:44:09 +00002657void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002658 __ Add(OutputRegister(instruction),
2659 InputRegisterAt(instruction, 0),
2660 Operand(InputOperandAt(instruction, 1)));
2661}
2662
Artem Serove1811ed2017-04-27 16:50:47 +01002663void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2664 LocationSummary* locations =
2665 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2666
2667 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2668
2669 locations->SetInAt(0, Location::RequiresRegister());
2670 // For byte case we don't need to shift the index variable so we can encode the data offset into
2671 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2672 // data offset constant generation out of the loop and reduce the critical path length in the
2673 // loop.
2674 locations->SetInAt(1, shift->GetValue() == 0
2675 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2676 : Location::RequiresRegister());
2677 locations->SetInAt(2, Location::ConstantLocation(shift));
2678 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2679}
2680
2681void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2682 HIntermediateAddressIndex* instruction) {
2683 Register index_reg = InputRegisterAt(instruction, 0);
2684 uint32_t shift = Int64ConstantFrom(instruction->GetLocations()->InAt(2));
2685 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2686
2687 if (shift == 0) {
2688 __ Add(OutputRegister(instruction), index_reg, offset);
2689 } else {
2690 Register offset_reg = InputRegisterAt(instruction, 1);
2691 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2692 }
2693}
2694
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002695void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002696 LocationSummary* locations =
2697 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002698 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2699 if (instr->GetOpKind() == HInstruction::kSub &&
2700 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002701 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002702 // Don't allocate register for Mneg instruction.
2703 } else {
2704 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2705 Location::RequiresRegister());
2706 }
2707 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2708 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002709 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2710}
2711
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002712void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002713 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002714 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2715 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002716
2717 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2718 // This fixup should be carried out for all multiply-accumulate instructions:
2719 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2720 if (instr->GetType() == Primitive::kPrimLong &&
2721 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2722 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002723 vixl::aarch64::Instruction* prev =
2724 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002725 if (prev->IsLoadOrStore()) {
2726 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002727 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002728 __ nop();
2729 }
2730 }
2731
2732 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002733 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002734 __ Madd(res, mul_left, mul_right, accumulator);
2735 } else {
2736 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002737 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002738 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002739 __ Mneg(res, mul_left, mul_right);
2740 } else {
2741 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2742 __ Msub(res, mul_left, mul_right, accumulator);
2743 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002744 }
2745}
2746
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002747void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002748 bool object_array_get_with_read_barrier =
2749 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002750 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002751 new (GetGraph()->GetArena()) LocationSummary(instruction,
2752 object_array_get_with_read_barrier ?
2753 LocationSummary::kCallOnSlowPath :
2754 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002755 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002756 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002757 // We need a temporary register for the read barrier marking slow
2758 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002759 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2760 !Runtime::Current()->UseJitCompilation() &&
2761 instruction->GetIndex()->IsConstant()) {
2762 // Array loads with constant index are treated as field loads.
2763 // If link-time thunks for the Baker read barrier are enabled, for AOT
2764 // constant index loads we need a temporary only if the offset is too big.
2765 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2766 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
2767 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
2768 if (offset >= kReferenceLoadMinFarOffset) {
2769 locations->AddTemp(FixedTempLocation());
2770 }
2771 } else {
2772 locations->AddTemp(Location::RequiresRegister());
2773 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002774 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002775 locations->SetInAt(0, Location::RequiresRegister());
2776 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002777 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2778 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2779 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002780 // The output overlaps in the case of an object array get with
2781 // read barriers enabled: we do not want the move to overwrite the
2782 // array's location, as we need it to emit the read barrier.
2783 locations->SetOut(
2784 Location::RequiresRegister(),
2785 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002786 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002787}
2788
2789void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002790 Primitive::Type type = instruction->GetType();
2791 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002792 LocationSummary* locations = instruction->GetLocations();
2793 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002794 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002795 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002796 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2797 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002798 MacroAssembler* masm = GetVIXLAssembler();
2799 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002800
Roland Levillain19c54192016-11-04 13:44:09 +00002801 // The read barrier instrumentation of object ArrayGet instructions
2802 // does not support the HIntermediateAddress instruction.
2803 DCHECK(!((type == Primitive::kPrimNot) &&
2804 instruction->GetArray()->IsIntermediateAddress() &&
2805 kEmitCompilerReadBarrier));
2806
Roland Levillain44015862016-01-22 11:47:17 +00002807 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2808 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002809 // Note that a potential implicit null check is handled in the
2810 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002811 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002812 if (index.IsConstant()) {
2813 // Array load with a constant index can be treated as a field load.
2814 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2815 Location maybe_temp =
2816 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2817 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2818 out,
2819 obj.W(),
2820 offset,
2821 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002822 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002823 /* use_load_acquire */ false);
2824 } else {
2825 Register temp = WRegisterFrom(locations->GetTemp(0));
2826 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002827 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002828 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002829 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002830 // General case.
2831 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002832 Register length;
2833 if (maybe_compressed_char_at) {
2834 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2835 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002836 {
2837 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2838 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2839
2840 if (instruction->GetArray()->IsIntermediateAddress()) {
2841 DCHECK_LT(count_offset, offset);
2842 int64_t adjusted_offset =
2843 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2844 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2845 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2846 } else {
2847 __ Ldr(length, HeapOperand(obj, count_offset));
2848 }
2849 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002850 }
jessicahandojo05765752016-09-09 19:01:32 -07002851 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002852 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002853 if (maybe_compressed_char_at) {
2854 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002855 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2856 "Expecting 0=compressed, 1=uncompressed");
2857 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002858 __ Ldrb(Register(OutputCPURegister(instruction)),
2859 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2860 __ B(&done);
2861 __ Bind(&uncompressed_load);
2862 __ Ldrh(Register(OutputCPURegister(instruction)),
2863 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2864 __ Bind(&done);
2865 } else {
2866 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2867 source = HeapOperand(obj, offset);
2868 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002869 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002870 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002871 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002872 // We do not need to compute the intermediate address from the array: the
2873 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002874 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002875 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002876 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002877 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2878 }
2879 temp = obj;
2880 } else {
2881 __ Add(temp, obj, offset);
2882 }
jessicahandojo05765752016-09-09 19:01:32 -07002883 if (maybe_compressed_char_at) {
2884 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002885 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2886 "Expecting 0=compressed, 1=uncompressed");
2887 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002888 __ Ldrb(Register(OutputCPURegister(instruction)),
2889 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2890 __ B(&done);
2891 __ Bind(&uncompressed_load);
2892 __ Ldrh(Register(OutputCPURegister(instruction)),
2893 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2894 __ Bind(&done);
2895 } else {
2896 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2897 }
Roland Levillain44015862016-01-22 11:47:17 +00002898 }
jessicahandojo05765752016-09-09 19:01:32 -07002899 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002900 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2901 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002902 codegen_->Load(type, OutputCPURegister(instruction), source);
2903 codegen_->MaybeRecordImplicitNullCheck(instruction);
2904 }
Roland Levillain44015862016-01-22 11:47:17 +00002905
2906 if (type == Primitive::kPrimNot) {
2907 static_assert(
2908 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2909 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2910 Location obj_loc = locations->InAt(0);
2911 if (index.IsConstant()) {
2912 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2913 } else {
2914 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2915 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002916 }
Roland Levillain4d027112015-07-01 15:41:14 +01002917 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002918}
2919
Alexandre Rames5319def2014-10-23 10:03:10 +01002920void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2921 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2922 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002923 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002924}
2925
2926void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002927 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002928 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002929 {
2930 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2931 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2932 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2933 codegen_->MaybeRecordImplicitNullCheck(instruction);
2934 }
jessicahandojo05765752016-09-09 19:01:32 -07002935 // Mask out compression flag from String's array length.
2936 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002937 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002938 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002939}
2940
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002941void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002942 Primitive::Type value_type = instruction->GetComponentType();
2943
2944 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002945 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2946 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002947 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002948 LocationSummary::kCallOnSlowPath :
2949 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002950 locations->SetInAt(0, Location::RequiresRegister());
2951 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002952 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2953 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2954 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002955 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002956 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002957 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002958 }
2959}
2960
2961void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2962 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002963 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002964 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002965 bool needs_write_barrier =
2966 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002967
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002968 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002969 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002970 CPURegister source = value;
2971 Location index = locations->InAt(1);
2972 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2973 MemOperand destination = HeapOperand(array);
2974 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002975
2976 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002977 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002978 if (index.IsConstant()) {
2979 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2980 destination = HeapOperand(array, offset);
2981 } else {
2982 UseScratchRegisterScope temps(masm);
2983 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002984 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002985 // We do not need to compute the intermediate address from the array: the
2986 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002987 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002988 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002989 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002990 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2991 }
2992 temp = array;
2993 } else {
2994 __ Add(temp, array, offset);
2995 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002996 destination = HeapOperand(temp,
2997 XRegisterFrom(index),
2998 LSL,
2999 Primitive::ComponentSizeShift(value_type));
3000 }
Artem Serov914d7a82017-02-07 14:33:49 +00003001 {
3002 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3003 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3004 codegen_->Store(value_type, value, destination);
3005 codegen_->MaybeRecordImplicitNullCheck(instruction);
3006 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003007 } else {
Artem Serov328429f2016-07-06 16:23:04 +01003008 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003009 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003010 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01003011 {
3012 // We use a block to end the scratch scope before the write barrier, thus
3013 // freeing the temporary registers so they can be used in `MarkGCCard`.
3014 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003015 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01003016 if (index.IsConstant()) {
3017 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003018 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01003019 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01003020 destination = HeapOperand(temp,
3021 XRegisterFrom(index),
3022 LSL,
3023 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01003024 }
3025
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003026 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3027 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3028 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3029
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003030 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003031 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
3032 codegen_->AddSlowPath(slow_path);
3033 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003034 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003035 __ Cbnz(Register(value), &non_zero);
3036 if (!index.IsConstant()) {
3037 __ Add(temp, array, offset);
3038 }
Artem Serov914d7a82017-02-07 14:33:49 +00003039 {
3040 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
3041 // emitted.
3042 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3043 __ Str(wzr, destination);
3044 codegen_->MaybeRecordImplicitNullCheck(instruction);
3045 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003046 __ B(&done);
3047 __ Bind(&non_zero);
3048 }
3049
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003050 // Note that when Baker read barriers are enabled, the type
3051 // checks are performed without read barriers. This is fine,
3052 // even in the case where a class object is in the from-space
3053 // after the flip, as a comparison involving such a type would
3054 // not produce a false positive; it may of course produce a
3055 // false negative, in which case we would take the ArraySet
3056 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003057
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003058 Register temp2 = temps.AcquireSameSizeAs(array);
3059 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003060 {
3061 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3062 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3063 __ Ldr(temp, HeapOperand(array, class_offset));
3064 codegen_->MaybeRecordImplicitNullCheck(instruction);
3065 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003066 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003067
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003068 // /* HeapReference<Class> */ temp = temp->component_type_
3069 __ Ldr(temp, HeapOperand(temp, component_offset));
3070 // /* HeapReference<Class> */ temp2 = value->klass_
3071 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3072 // If heap poisoning is enabled, no need to unpoison `temp`
3073 // nor `temp2`, as we are comparing two poisoned references.
3074 __ Cmp(temp, temp2);
3075 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003076
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003077 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3078 vixl::aarch64::Label do_put;
3079 __ B(eq, &do_put);
3080 // If heap poisoning is enabled, the `temp` reference has
3081 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003082 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3083
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003084 // /* HeapReference<Class> */ temp = temp->super_class_
3085 __ Ldr(temp, HeapOperand(temp, super_offset));
3086 // If heap poisoning is enabled, no need to unpoison
3087 // `temp`, as we are comparing against null below.
3088 __ Cbnz(temp, slow_path->GetEntryLabel());
3089 __ Bind(&do_put);
3090 } else {
3091 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003092 }
3093 }
3094
3095 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003096 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003097 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003098 __ Mov(temp2, value.W());
3099 GetAssembler()->PoisonHeapReference(temp2);
3100 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003101 }
3102
3103 if (!index.IsConstant()) {
3104 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003105 } else {
3106 // We no longer need the `temp` here so release it as the store below may
3107 // need a scratch register (if the constant index makes the offset too large)
3108 // and the poisoned `source` could be using the other scratch register.
3109 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003110 }
Artem Serov914d7a82017-02-07 14:33:49 +00003111 {
3112 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3113 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3114 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003115
Artem Serov914d7a82017-02-07 14:33:49 +00003116 if (!may_need_runtime_call_for_type_check) {
3117 codegen_->MaybeRecordImplicitNullCheck(instruction);
3118 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003119 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003120 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003121
3122 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3123
3124 if (done.IsLinked()) {
3125 __ Bind(&done);
3126 }
3127
3128 if (slow_path != nullptr) {
3129 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003130 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003131 }
3132}
3133
Alexandre Rames67555f72014-11-18 10:55:16 +00003134void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003135 RegisterSet caller_saves = RegisterSet::Empty();
3136 InvokeRuntimeCallingConvention calling_convention;
3137 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3138 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3139 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003140 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003141 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003142}
3143
3144void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003145 BoundsCheckSlowPathARM64* slow_path =
3146 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003147 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003148 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3149 __ B(slow_path->GetEntryLabel(), hs);
3150}
3151
Alexandre Rames67555f72014-11-18 10:55:16 +00003152void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3153 LocationSummary* locations =
3154 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3155 locations->SetInAt(0, Location::RequiresRegister());
3156 if (check->HasUses()) {
3157 locations->SetOut(Location::SameAsFirstInput());
3158 }
3159}
3160
3161void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3162 // We assume the class is not null.
3163 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3164 check->GetLoadClass(), check, check->GetDexPc(), true);
3165 codegen_->AddSlowPath(slow_path);
3166 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3167}
3168
Roland Levillain1a653882016-03-18 18:05:57 +00003169static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3170 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3171 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3172}
3173
3174void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3175 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3176 Location rhs_loc = instruction->GetLocations()->InAt(1);
3177 if (rhs_loc.IsConstant()) {
3178 // 0.0 is the only immediate that can be encoded directly in
3179 // an FCMP instruction.
3180 //
3181 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3182 // specify that in a floating-point comparison, positive zero
3183 // and negative zero are considered equal, so we can use the
3184 // literal 0.0 for both cases here.
3185 //
3186 // Note however that some methods (Float.equal, Float.compare,
3187 // Float.compareTo, Double.equal, Double.compare,
3188 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3189 // StrictMath.min) consider 0.0 to be (strictly) greater than
3190 // -0.0. So if we ever translate calls to these methods into a
3191 // HCompare instruction, we must handle the -0.0 case with
3192 // care here.
3193 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3194 __ Fcmp(lhs_reg, 0.0);
3195 } else {
3196 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3197 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003198}
3199
Serban Constantinescu02164b32014-11-13 14:05:07 +00003200void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003201 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003202 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3203 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003204 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003205 case Primitive::kPrimBoolean:
3206 case Primitive::kPrimByte:
3207 case Primitive::kPrimShort:
3208 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003209 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003210 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003211 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003212 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003213 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3214 break;
3215 }
3216 case Primitive::kPrimFloat:
3217 case Primitive::kPrimDouble: {
3218 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003219 locations->SetInAt(1,
3220 IsFloatingPointZeroConstant(compare->InputAt(1))
3221 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3222 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003223 locations->SetOut(Location::RequiresRegister());
3224 break;
3225 }
3226 default:
3227 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3228 }
3229}
3230
3231void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3232 Primitive::Type in_type = compare->InputAt(0)->GetType();
3233
3234 // 0 if: left == right
3235 // 1 if: left > right
3236 // -1 if: left < right
3237 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003238 case Primitive::kPrimBoolean:
3239 case Primitive::kPrimByte:
3240 case Primitive::kPrimShort:
3241 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003242 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003243 case Primitive::kPrimLong: {
3244 Register result = OutputRegister(compare);
3245 Register left = InputRegisterAt(compare, 0);
3246 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003247 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003248 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3249 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003250 break;
3251 }
3252 case Primitive::kPrimFloat:
3253 case Primitive::kPrimDouble: {
3254 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003255 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003256 __ Cset(result, ne);
3257 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003258 break;
3259 }
3260 default:
3261 LOG(FATAL) << "Unimplemented compare type " << in_type;
3262 }
3263}
3264
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003265void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003266 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003267
3268 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3269 locations->SetInAt(0, Location::RequiresFpuRegister());
3270 locations->SetInAt(1,
3271 IsFloatingPointZeroConstant(instruction->InputAt(1))
3272 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3273 : Location::RequiresFpuRegister());
3274 } else {
3275 // Integer cases.
3276 locations->SetInAt(0, Location::RequiresRegister());
3277 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3278 }
3279
David Brazdilb3e773e2016-01-26 11:28:37 +00003280 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003281 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003282 }
3283}
3284
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003285void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003286 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003287 return;
3288 }
3289
3290 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003291 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003292 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003293
Roland Levillain7f63c522015-07-13 15:54:55 +00003294 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003295 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003296 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003297 } else {
3298 // Integer cases.
3299 Register lhs = InputRegisterAt(instruction, 0);
3300 Operand rhs = InputOperandAt(instruction, 1);
3301 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003302 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003303 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003304}
3305
3306#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3307 M(Equal) \
3308 M(NotEqual) \
3309 M(LessThan) \
3310 M(LessThanOrEqual) \
3311 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003312 M(GreaterThanOrEqual) \
3313 M(Below) \
3314 M(BelowOrEqual) \
3315 M(Above) \
3316 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003317#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003318void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3319void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003320FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003321#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003322#undef FOR_EACH_CONDITION_INSTRUCTION
3323
Zheng Xuc6667102015-05-15 16:08:45 +08003324void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3325 DCHECK(instruction->IsDiv() || instruction->IsRem());
3326
3327 LocationSummary* locations = instruction->GetLocations();
3328 Location second = locations->InAt(1);
3329 DCHECK(second.IsConstant());
3330
3331 Register out = OutputRegister(instruction);
3332 Register dividend = InputRegisterAt(instruction, 0);
3333 int64_t imm = Int64FromConstant(second.GetConstant());
3334 DCHECK(imm == 1 || imm == -1);
3335
3336 if (instruction->IsRem()) {
3337 __ Mov(out, 0);
3338 } else {
3339 if (imm == 1) {
3340 __ Mov(out, dividend);
3341 } else {
3342 __ Neg(out, dividend);
3343 }
3344 }
3345}
3346
3347void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3348 DCHECK(instruction->IsDiv() || instruction->IsRem());
3349
3350 LocationSummary* locations = instruction->GetLocations();
3351 Location second = locations->InAt(1);
3352 DCHECK(second.IsConstant());
3353
3354 Register out = OutputRegister(instruction);
3355 Register dividend = InputRegisterAt(instruction, 0);
3356 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003357 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003358 int ctz_imm = CTZ(abs_imm);
3359
3360 UseScratchRegisterScope temps(GetVIXLAssembler());
3361 Register temp = temps.AcquireSameSizeAs(out);
3362
3363 if (instruction->IsDiv()) {
3364 __ Add(temp, dividend, abs_imm - 1);
3365 __ Cmp(dividend, 0);
3366 __ Csel(out, temp, dividend, lt);
3367 if (imm > 0) {
3368 __ Asr(out, out, ctz_imm);
3369 } else {
3370 __ Neg(out, Operand(out, ASR, ctz_imm));
3371 }
3372 } else {
3373 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3374 __ Asr(temp, dividend, bits - 1);
3375 __ Lsr(temp, temp, bits - ctz_imm);
3376 __ Add(out, dividend, temp);
3377 __ And(out, out, abs_imm - 1);
3378 __ Sub(out, out, temp);
3379 }
3380}
3381
3382void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3383 DCHECK(instruction->IsDiv() || instruction->IsRem());
3384
3385 LocationSummary* locations = instruction->GetLocations();
3386 Location second = locations->InAt(1);
3387 DCHECK(second.IsConstant());
3388
3389 Register out = OutputRegister(instruction);
3390 Register dividend = InputRegisterAt(instruction, 0);
3391 int64_t imm = Int64FromConstant(second.GetConstant());
3392
3393 Primitive::Type type = instruction->GetResultType();
3394 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3395
3396 int64_t magic;
3397 int shift;
3398 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3399
3400 UseScratchRegisterScope temps(GetVIXLAssembler());
3401 Register temp = temps.AcquireSameSizeAs(out);
3402
3403 // temp = get_high(dividend * magic)
3404 __ Mov(temp, magic);
3405 if (type == Primitive::kPrimLong) {
3406 __ Smulh(temp, dividend, temp);
3407 } else {
3408 __ Smull(temp.X(), dividend, temp);
3409 __ Lsr(temp.X(), temp.X(), 32);
3410 }
3411
3412 if (imm > 0 && magic < 0) {
3413 __ Add(temp, temp, dividend);
3414 } else if (imm < 0 && magic > 0) {
3415 __ Sub(temp, temp, dividend);
3416 }
3417
3418 if (shift != 0) {
3419 __ Asr(temp, temp, shift);
3420 }
3421
3422 if (instruction->IsDiv()) {
3423 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3424 } else {
3425 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3426 // TODO: Strength reduction for msub.
3427 Register temp_imm = temps.AcquireSameSizeAs(out);
3428 __ Mov(temp_imm, imm);
3429 __ Msub(out, temp, temp_imm, dividend);
3430 }
3431}
3432
3433void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3434 DCHECK(instruction->IsDiv() || instruction->IsRem());
3435 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003436 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003437
3438 LocationSummary* locations = instruction->GetLocations();
3439 Register out = OutputRegister(instruction);
3440 Location second = locations->InAt(1);
3441
3442 if (second.IsConstant()) {
3443 int64_t imm = Int64FromConstant(second.GetConstant());
3444
3445 if (imm == 0) {
3446 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3447 } else if (imm == 1 || imm == -1) {
3448 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003449 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003450 DivRemByPowerOfTwo(instruction);
3451 } else {
3452 DCHECK(imm <= -2 || imm >= 2);
3453 GenerateDivRemWithAnyConstant(instruction);
3454 }
3455 } else {
3456 Register dividend = InputRegisterAt(instruction, 0);
3457 Register divisor = InputRegisterAt(instruction, 1);
3458 if (instruction->IsDiv()) {
3459 __ Sdiv(out, dividend, divisor);
3460 } else {
3461 UseScratchRegisterScope temps(GetVIXLAssembler());
3462 Register temp = temps.AcquireSameSizeAs(out);
3463 __ Sdiv(temp, dividend, divisor);
3464 __ Msub(out, temp, divisor, dividend);
3465 }
3466 }
3467}
3468
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003469void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3470 LocationSummary* locations =
3471 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3472 switch (div->GetResultType()) {
3473 case Primitive::kPrimInt:
3474 case Primitive::kPrimLong:
3475 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003476 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003477 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3478 break;
3479
3480 case Primitive::kPrimFloat:
3481 case Primitive::kPrimDouble:
3482 locations->SetInAt(0, Location::RequiresFpuRegister());
3483 locations->SetInAt(1, Location::RequiresFpuRegister());
3484 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3485 break;
3486
3487 default:
3488 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3489 }
3490}
3491
3492void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3493 Primitive::Type type = div->GetResultType();
3494 switch (type) {
3495 case Primitive::kPrimInt:
3496 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003497 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003498 break;
3499
3500 case Primitive::kPrimFloat:
3501 case Primitive::kPrimDouble:
3502 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3503 break;
3504
3505 default:
3506 LOG(FATAL) << "Unexpected div type " << type;
3507 }
3508}
3509
Alexandre Rames67555f72014-11-18 10:55:16 +00003510void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003511 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003512 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003513}
3514
3515void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3516 SlowPathCodeARM64* slow_path =
3517 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3518 codegen_->AddSlowPath(slow_path);
3519 Location value = instruction->GetLocations()->InAt(0);
3520
Alexandre Rames3e69f162014-12-10 10:36:50 +00003521 Primitive::Type type = instruction->GetType();
3522
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003523 if (!Primitive::IsIntegralType(type)) {
3524 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003525 return;
3526 }
3527
Alexandre Rames67555f72014-11-18 10:55:16 +00003528 if (value.IsConstant()) {
3529 int64_t divisor = Int64ConstantFrom(value);
3530 if (divisor == 0) {
3531 __ B(slow_path->GetEntryLabel());
3532 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003533 // A division by a non-null constant is valid. We don't need to perform
3534 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003535 }
3536 } else {
3537 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3538 }
3539}
3540
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003541void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3542 LocationSummary* locations =
3543 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3544 locations->SetOut(Location::ConstantLocation(constant));
3545}
3546
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003547void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3548 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003549 // Will be generated at use site.
3550}
3551
Alexandre Rames5319def2014-10-23 10:03:10 +01003552void LocationsBuilderARM64::VisitExit(HExit* exit) {
3553 exit->SetLocations(nullptr);
3554}
3555
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003556void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003557}
3558
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003559void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3560 LocationSummary* locations =
3561 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3562 locations->SetOut(Location::ConstantLocation(constant));
3563}
3564
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003565void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003566 // Will be generated at use site.
3567}
3568
David Brazdilfc6a86a2015-06-26 10:33:45 +00003569void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003570 DCHECK(!successor->IsExitBlock());
3571 HBasicBlock* block = got->GetBlock();
3572 HInstruction* previous = got->GetPrevious();
3573 HLoopInformation* info = block->GetLoopInformation();
3574
David Brazdil46e2a392015-03-16 17:31:52 +00003575 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003576 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3577 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3578 return;
3579 }
3580 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3581 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3582 }
3583 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003584 __ B(codegen_->GetLabelOf(successor));
3585 }
3586}
3587
David Brazdilfc6a86a2015-06-26 10:33:45 +00003588void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3589 got->SetLocations(nullptr);
3590}
3591
3592void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3593 HandleGoto(got, got->GetSuccessor());
3594}
3595
3596void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3597 try_boundary->SetLocations(nullptr);
3598}
3599
3600void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3601 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3602 if (!successor->IsExitBlock()) {
3603 HandleGoto(try_boundary, successor);
3604 }
3605}
3606
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003607void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003608 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003609 vixl::aarch64::Label* true_target,
3610 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003611 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003612
David Brazdil0debae72015-11-12 18:37:00 +00003613 if (true_target == nullptr && false_target == nullptr) {
3614 // Nothing to do. The code always falls through.
3615 return;
3616 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003617 // Constant condition, statically compared against "true" (integer value 1).
3618 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003619 if (true_target != nullptr) {
3620 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003621 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003622 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003623 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003624 if (false_target != nullptr) {
3625 __ B(false_target);
3626 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003627 }
David Brazdil0debae72015-11-12 18:37:00 +00003628 return;
3629 }
3630
3631 // The following code generates these patterns:
3632 // (1) true_target == nullptr && false_target != nullptr
3633 // - opposite condition true => branch to false_target
3634 // (2) true_target != nullptr && false_target == nullptr
3635 // - condition true => branch to true_target
3636 // (3) true_target != nullptr && false_target != nullptr
3637 // - condition true => branch to true_target
3638 // - branch to false_target
3639 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003640 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003641 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003642 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003643 if (true_target == nullptr) {
3644 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3645 } else {
3646 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3647 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003648 } else {
3649 // The condition instruction has not been materialized, use its inputs as
3650 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003651 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003652
David Brazdil0debae72015-11-12 18:37:00 +00003653 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003654 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003655 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003656 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003657 IfCondition opposite_condition = condition->GetOppositeCondition();
3658 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003659 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003660 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003661 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003662 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003663 // Integer cases.
3664 Register lhs = InputRegisterAt(condition, 0);
3665 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003666
3667 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003668 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003669 if (true_target == nullptr) {
3670 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3671 non_fallthrough_target = false_target;
3672 } else {
3673 arm64_cond = ARM64Condition(condition->GetCondition());
3674 non_fallthrough_target = true_target;
3675 }
3676
Aart Bik086d27e2016-01-20 17:02:00 -08003677 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003678 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003679 switch (arm64_cond) {
3680 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003681 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003682 break;
3683 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003684 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003685 break;
3686 case lt:
3687 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003688 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003689 break;
3690 case ge:
3691 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003692 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003693 break;
3694 default:
3695 // Without the `static_cast` the compiler throws an error for
3696 // `-Werror=sign-promo`.
3697 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3698 }
3699 } else {
3700 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003701 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003702 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003703 }
3704 }
David Brazdil0debae72015-11-12 18:37:00 +00003705
3706 // If neither branch falls through (case 3), the conditional branch to `true_target`
3707 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3708 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003709 __ B(false_target);
3710 }
3711}
3712
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003713void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3714 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003715 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003716 locations->SetInAt(0, Location::RequiresRegister());
3717 }
3718}
3719
3720void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003721 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3722 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003723 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3724 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3725 true_target = nullptr;
3726 }
3727 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3728 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3729 false_target = nullptr;
3730 }
David Brazdil0debae72015-11-12 18:37:00 +00003731 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003732}
3733
3734void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3735 LocationSummary* locations = new (GetGraph()->GetArena())
3736 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003737 InvokeRuntimeCallingConvention calling_convention;
3738 RegisterSet caller_saves = RegisterSet::Empty();
3739 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3740 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003741 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003742 locations->SetInAt(0, Location::RequiresRegister());
3743 }
3744}
3745
3746void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003747 SlowPathCodeARM64* slow_path =
3748 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003749 GenerateTestAndBranch(deoptimize,
3750 /* condition_input_index */ 0,
3751 slow_path->GetEntryLabel(),
3752 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003753}
3754
Mingyao Yang063fc772016-08-02 11:02:54 -07003755void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3756 LocationSummary* locations = new (GetGraph()->GetArena())
3757 LocationSummary(flag, LocationSummary::kNoCall);
3758 locations->SetOut(Location::RequiresRegister());
3759}
3760
3761void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3762 __ Ldr(OutputRegister(flag),
3763 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3764}
3765
David Brazdilc0b601b2016-02-08 14:20:45 +00003766static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3767 return condition->IsCondition() &&
3768 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3769}
3770
Alexandre Rames880f1192016-06-13 16:04:50 +01003771static inline Condition GetConditionForSelect(HCondition* condition) {
3772 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003773 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3774 : ARM64Condition(cond);
3775}
3776
David Brazdil74eb1b22015-12-14 11:44:01 +00003777void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3778 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003779 if (Primitive::IsFloatingPointType(select->GetType())) {
3780 locations->SetInAt(0, Location::RequiresFpuRegister());
3781 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003782 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003783 } else {
3784 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3785 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3786 bool is_true_value_constant = cst_true_value != nullptr;
3787 bool is_false_value_constant = cst_false_value != nullptr;
3788 // Ask VIXL whether we should synthesize constants in registers.
3789 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3790 Operand true_op = is_true_value_constant ?
3791 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3792 Operand false_op = is_false_value_constant ?
3793 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3794 bool true_value_in_register = false;
3795 bool false_value_in_register = false;
3796 MacroAssembler::GetCselSynthesisInformation(
3797 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3798 true_value_in_register |= !is_true_value_constant;
3799 false_value_in_register |= !is_false_value_constant;
3800
3801 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3802 : Location::ConstantLocation(cst_true_value));
3803 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3804 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003805 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003806 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003807
David Brazdil74eb1b22015-12-14 11:44:01 +00003808 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3809 locations->SetInAt(2, Location::RequiresRegister());
3810 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003811}
3812
3813void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003814 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003815 Condition csel_cond;
3816
3817 if (IsBooleanValueOrMaterializedCondition(cond)) {
3818 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003819 // Use the condition flags set by the previous instruction.
3820 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003821 } else {
3822 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003823 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003824 }
3825 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003826 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003827 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003828 } else {
3829 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003830 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003831 }
3832
Alexandre Rames880f1192016-06-13 16:04:50 +01003833 if (Primitive::IsFloatingPointType(select->GetType())) {
3834 __ Fcsel(OutputFPRegister(select),
3835 InputFPRegisterAt(select, 1),
3836 InputFPRegisterAt(select, 0),
3837 csel_cond);
3838 } else {
3839 __ Csel(OutputRegister(select),
3840 InputOperandAt(select, 1),
3841 InputOperandAt(select, 0),
3842 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003843 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003844}
3845
David Srbecky0cf44932015-12-09 14:09:59 +00003846void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3847 new (GetGraph()->GetArena()) LocationSummary(info);
3848}
3849
David Srbeckyd28f4a02016-03-14 17:14:24 +00003850void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3851 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003852}
3853
3854void CodeGeneratorARM64::GenerateNop() {
3855 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003856}
3857
Alexandre Rames5319def2014-10-23 10:03:10 +01003858void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003859 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003860}
3861
3862void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003863 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003864}
3865
3866void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003867 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003868}
3869
3870void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003871 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003872}
3873
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003874// Temp is used for read barrier.
3875static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3876 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003877 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003878 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3879 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3880 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3881 return 1;
3882 }
3883 return 0;
3884}
3885
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003886// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003887// interface pointer, one for loading the current interface.
3888// The other checks have one temp for loading the object's class.
3889static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3890 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3891 return 3;
3892 }
3893 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003894}
3895
Alexandre Rames67555f72014-11-18 10:55:16 +00003896void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003897 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003898 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003899 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003900 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003901 case TypeCheckKind::kExactCheck:
3902 case TypeCheckKind::kAbstractClassCheck:
3903 case TypeCheckKind::kClassHierarchyCheck:
3904 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003905 call_kind =
3906 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003907 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003908 break;
3909 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003910 case TypeCheckKind::kUnresolvedCheck:
3911 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003912 call_kind = LocationSummary::kCallOnSlowPath;
3913 break;
3914 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003915
Alexandre Rames67555f72014-11-18 10:55:16 +00003916 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003917 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003918 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003919 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003920 locations->SetInAt(0, Location::RequiresRegister());
3921 locations->SetInAt(1, Location::RequiresRegister());
3922 // The "out" register is used as a temporary, so it overlaps with the inputs.
3923 // Note that TypeCheckSlowPathARM64 uses this register too.
3924 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003925 // Add temps if necessary for read barriers.
3926 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003927}
3928
3929void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003930 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003931 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003932 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003933 Register obj = InputRegisterAt(instruction, 0);
3934 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003935 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003936 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003937 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3938 DCHECK_LE(num_temps, 1u);
3939 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003940 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3941 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3942 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3943 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003944
Scott Wakeling97c72b72016-06-24 16:19:36 +01003945 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003946 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003947
3948 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003949 // Avoid null check if we know `obj` is not null.
3950 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003951 __ Cbz(obj, &zero);
3952 }
3953
Roland Levillain44015862016-01-22 11:47:17 +00003954 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003955 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003956 // /* HeapReference<Class> */ out = obj->klass_
3957 GenerateReferenceLoadTwoRegisters(instruction,
3958 out_loc,
3959 obj_loc,
3960 class_offset,
3961 maybe_temp_loc,
3962 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003963 __ Cmp(out, cls);
3964 __ Cset(out, eq);
3965 if (zero.IsLinked()) {
3966 __ B(&done);
3967 }
3968 break;
3969 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003970
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003971 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003972 // /* HeapReference<Class> */ out = obj->klass_
3973 GenerateReferenceLoadTwoRegisters(instruction,
3974 out_loc,
3975 obj_loc,
3976 class_offset,
3977 maybe_temp_loc,
3978 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003979 // If the class is abstract, we eagerly fetch the super class of the
3980 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003981 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003982 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003983 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003984 GenerateReferenceLoadOneRegister(instruction,
3985 out_loc,
3986 super_offset,
3987 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003988 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003989 // If `out` is null, we use it for the result, and jump to `done`.
3990 __ Cbz(out, &done);
3991 __ Cmp(out, cls);
3992 __ B(ne, &loop);
3993 __ Mov(out, 1);
3994 if (zero.IsLinked()) {
3995 __ B(&done);
3996 }
3997 break;
3998 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003999
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004000 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004001 // /* HeapReference<Class> */ out = obj->klass_
4002 GenerateReferenceLoadTwoRegisters(instruction,
4003 out_loc,
4004 obj_loc,
4005 class_offset,
4006 maybe_temp_loc,
4007 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004008 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004009 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004010 __ Bind(&loop);
4011 __ Cmp(out, cls);
4012 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004013 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004014 GenerateReferenceLoadOneRegister(instruction,
4015 out_loc,
4016 super_offset,
4017 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004018 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004019 __ Cbnz(out, &loop);
4020 // If `out` is null, we use it for the result, and jump to `done`.
4021 __ B(&done);
4022 __ Bind(&success);
4023 __ Mov(out, 1);
4024 if (zero.IsLinked()) {
4025 __ B(&done);
4026 }
4027 break;
4028 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004029
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004030 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004031 // /* HeapReference<Class> */ out = obj->klass_
4032 GenerateReferenceLoadTwoRegisters(instruction,
4033 out_loc,
4034 obj_loc,
4035 class_offset,
4036 maybe_temp_loc,
4037 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004038 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004039 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004040 __ Cmp(out, cls);
4041 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004042 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004043 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004044 GenerateReferenceLoadOneRegister(instruction,
4045 out_loc,
4046 component_offset,
4047 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004048 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004049 // If `out` is null, we use it for the result, and jump to `done`.
4050 __ Cbz(out, &done);
4051 __ Ldrh(out, HeapOperand(out, primitive_offset));
4052 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4053 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004054 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004055 __ Mov(out, 1);
4056 __ B(&done);
4057 break;
4058 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004059
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004060 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004061 // No read barrier since the slow path will retry upon failure.
4062 // /* HeapReference<Class> */ out = obj->klass_
4063 GenerateReferenceLoadTwoRegisters(instruction,
4064 out_loc,
4065 obj_loc,
4066 class_offset,
4067 maybe_temp_loc,
4068 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004069 __ Cmp(out, cls);
4070 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004071 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4072 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004073 codegen_->AddSlowPath(slow_path);
4074 __ B(ne, slow_path->GetEntryLabel());
4075 __ Mov(out, 1);
4076 if (zero.IsLinked()) {
4077 __ B(&done);
4078 }
4079 break;
4080 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004081
Calin Juravle98893e12015-10-02 21:05:03 +01004082 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004083 case TypeCheckKind::kInterfaceCheck: {
4084 // Note that we indeed only call on slow path, but we always go
4085 // into the slow path for the unresolved and interface check
4086 // cases.
4087 //
4088 // We cannot directly call the InstanceofNonTrivial runtime
4089 // entry point without resorting to a type checking slow path
4090 // here (i.e. by calling InvokeRuntime directly), as it would
4091 // require to assign fixed registers for the inputs of this
4092 // HInstanceOf instruction (following the runtime calling
4093 // convention), which might be cluttered by the potential first
4094 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004095 //
4096 // TODO: Introduce a new runtime entry point taking the object
4097 // to test (instead of its class) as argument, and let it deal
4098 // with the read barrier issues. This will let us refactor this
4099 // case of the `switch` code as it was previously (with a direct
4100 // call to the runtime not using a type checking slow path).
4101 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004102 DCHECK(locations->OnlyCallsOnSlowPath());
4103 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4104 /* is_fatal */ false);
4105 codegen_->AddSlowPath(slow_path);
4106 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004107 if (zero.IsLinked()) {
4108 __ B(&done);
4109 }
4110 break;
4111 }
4112 }
4113
4114 if (zero.IsLinked()) {
4115 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004116 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004117 }
4118
4119 if (done.IsLinked()) {
4120 __ Bind(&done);
4121 }
4122
4123 if (slow_path != nullptr) {
4124 __ Bind(slow_path->GetExitLabel());
4125 }
4126}
4127
4128void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4129 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4130 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4131
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004132 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4133 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004134 case TypeCheckKind::kExactCheck:
4135 case TypeCheckKind::kAbstractClassCheck:
4136 case TypeCheckKind::kClassHierarchyCheck:
4137 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004138 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4139 LocationSummary::kCallOnSlowPath :
4140 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004141 break;
4142 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004143 case TypeCheckKind::kUnresolvedCheck:
4144 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004145 call_kind = LocationSummary::kCallOnSlowPath;
4146 break;
4147 }
4148
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004149 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4150 locations->SetInAt(0, Location::RequiresRegister());
4151 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004152 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4153 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004154}
4155
4156void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004157 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004158 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004159 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004160 Register obj = InputRegisterAt(instruction, 0);
4161 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004162 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4163 DCHECK_GE(num_temps, 1u);
4164 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004165 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004166 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4167 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004168 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004169 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4170 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4171 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4172 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4173 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4174 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4175 const uint32_t object_array_data_offset =
4176 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004177
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004178 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004179 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4180 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4181 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004182 if (!kEmitCompilerReadBarrier) {
4183 is_type_check_slow_path_fatal =
4184 (type_check_kind == TypeCheckKind::kExactCheck ||
4185 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4186 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4187 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4188 !instruction->CanThrowIntoCatchBlock();
4189 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004190 SlowPathCodeARM64* type_check_slow_path =
4191 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4192 is_type_check_slow_path_fatal);
4193 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004194
Scott Wakeling97c72b72016-06-24 16:19:36 +01004195 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004196 // Avoid null check if we know obj is not null.
4197 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004198 __ Cbz(obj, &done);
4199 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004200
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004201 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004202 case TypeCheckKind::kExactCheck:
4203 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004204 // /* HeapReference<Class> */ temp = obj->klass_
4205 GenerateReferenceLoadTwoRegisters(instruction,
4206 temp_loc,
4207 obj_loc,
4208 class_offset,
4209 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004210 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004211
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004212 __ Cmp(temp, cls);
4213 // Jump to slow path for throwing the exception or doing a
4214 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004215 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004216 break;
4217 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004218
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004219 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004220 // /* HeapReference<Class> */ temp = obj->klass_
4221 GenerateReferenceLoadTwoRegisters(instruction,
4222 temp_loc,
4223 obj_loc,
4224 class_offset,
4225 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004226 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004227
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004228 // If the class is abstract, we eagerly fetch the super class of the
4229 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004230 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004231 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004232 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004233 GenerateReferenceLoadOneRegister(instruction,
4234 temp_loc,
4235 super_offset,
4236 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004237 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004238
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004239 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4240 // exception.
4241 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4242 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004243 __ Cmp(temp, cls);
4244 __ B(ne, &loop);
4245 break;
4246 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004247
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004248 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004249 // /* HeapReference<Class> */ temp = obj->klass_
4250 GenerateReferenceLoadTwoRegisters(instruction,
4251 temp_loc,
4252 obj_loc,
4253 class_offset,
4254 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004255 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004256
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004257 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004258 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004259 __ Bind(&loop);
4260 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004261 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004262
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004263 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004264 GenerateReferenceLoadOneRegister(instruction,
4265 temp_loc,
4266 super_offset,
4267 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004268 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004269
4270 // If the class reference currently in `temp` is not null, jump
4271 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004272 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004273 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004274 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004275 break;
4276 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004277
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004278 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004279 // /* HeapReference<Class> */ temp = obj->klass_
4280 GenerateReferenceLoadTwoRegisters(instruction,
4281 temp_loc,
4282 obj_loc,
4283 class_offset,
4284 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004285 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004286
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004287 // Do an exact check.
4288 __ Cmp(temp, cls);
4289 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004290
4291 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004292 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004293 GenerateReferenceLoadOneRegister(instruction,
4294 temp_loc,
4295 component_offset,
4296 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004297 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004298
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004299 // If the component type is null, jump to the slow path to throw the exception.
4300 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4301 // Otherwise, the object is indeed an array. Further check that this component type is not a
4302 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004303 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4304 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004305 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004306 break;
4307 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004308
Calin Juravle98893e12015-10-02 21:05:03 +01004309 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004310 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004311 //
4312 // We cannot directly call the CheckCast runtime entry point
4313 // without resorting to a type checking slow path here (i.e. by
4314 // calling InvokeRuntime directly), as it would require to
4315 // assign fixed registers for the inputs of this HInstanceOf
4316 // instruction (following the runtime calling convention), which
4317 // might be cluttered by the potential first read barrier
4318 // emission at the beginning of this method.
4319 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004320 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004321 case TypeCheckKind::kInterfaceCheck: {
4322 // /* HeapReference<Class> */ temp = obj->klass_
4323 GenerateReferenceLoadTwoRegisters(instruction,
4324 temp_loc,
4325 obj_loc,
4326 class_offset,
4327 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004328 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004329
4330 // /* HeapReference<Class> */ temp = temp->iftable_
4331 GenerateReferenceLoadTwoRegisters(instruction,
4332 temp_loc,
4333 temp_loc,
4334 iftable_offset,
4335 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004336 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004337 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004338 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004339 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004340 vixl::aarch64::Label start_loop;
4341 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004342 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004343 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4344 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004345 // Go to next interface.
4346 __ Add(temp, temp, 2 * kHeapReferenceSize);
4347 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004348 // Compare the classes and continue the loop if they do not match.
4349 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4350 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004351 break;
4352 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004353 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004354 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004355
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004356 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004357}
4358
Alexandre Rames5319def2014-10-23 10:03:10 +01004359void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4360 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4361 locations->SetOut(Location::ConstantLocation(constant));
4362}
4363
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004364void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004365 // Will be generated at use site.
4366}
4367
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004368void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4369 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4370 locations->SetOut(Location::ConstantLocation(constant));
4371}
4372
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004373void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004374 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004375}
4376
Calin Juravle175dc732015-08-25 15:42:32 +01004377void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4378 // The trampoline uses the same calling convention as dex calling conventions,
4379 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4380 // the method_idx.
4381 HandleInvoke(invoke);
4382}
4383
4384void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4385 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4386}
4387
Alexandre Rames5319def2014-10-23 10:03:10 +01004388void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004389 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004390 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004391}
4392
Alexandre Rames67555f72014-11-18 10:55:16 +00004393void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4394 HandleInvoke(invoke);
4395}
4396
4397void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4398 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004399 LocationSummary* locations = invoke->GetLocations();
4400 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004401 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004402 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004403 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004404
4405 // The register ip1 is required to be used for the hidden argument in
4406 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004407 MacroAssembler* masm = GetVIXLAssembler();
4408 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004409 scratch_scope.Exclude(ip1);
4410 __ Mov(ip1, invoke->GetDexMethodIndex());
4411
Artem Serov914d7a82017-02-07 14:33:49 +00004412 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004413 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004414 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004415 {
4416 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4417 // /* HeapReference<Class> */ temp = temp->klass_
4418 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4419 codegen_->MaybeRecordImplicitNullCheck(invoke);
4420 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004421 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004422 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004423 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004424 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004425 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004426 }
Artem Serov914d7a82017-02-07 14:33:49 +00004427
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004428 // Instead of simply (possibly) unpoisoning `temp` here, we should
4429 // emit a read barrier for the previous class reference load.
4430 // However this is not required in practice, as this is an
4431 // intermediate/temporary reference and because the current
4432 // concurrent copying collector keeps the from-space memory
4433 // intact/accessible until the end of the marking phase (the
4434 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004435 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004436 __ Ldr(temp,
4437 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4438 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004439 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004440 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004441 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004442 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004443 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004444
4445 {
4446 // Ensure the pc position is recorded immediately after the `blr` instruction.
4447 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4448
4449 // lr();
4450 __ blr(lr);
4451 DCHECK(!codegen_->IsLeafMethod());
4452 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4453 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004454}
4455
4456void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004457 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004458 if (intrinsic.TryDispatch(invoke)) {
4459 return;
4460 }
4461
Alexandre Rames67555f72014-11-18 10:55:16 +00004462 HandleInvoke(invoke);
4463}
4464
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004465void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004466 // Explicit clinit checks triggered by static invokes must have been pruned by
4467 // art::PrepareForRegisterAllocation.
4468 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004469
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004470 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004471 if (intrinsic.TryDispatch(invoke)) {
4472 return;
4473 }
4474
Alexandre Rames67555f72014-11-18 10:55:16 +00004475 HandleInvoke(invoke);
4476}
4477
Andreas Gampe878d58c2015-01-15 23:24:00 -08004478static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4479 if (invoke->GetLocations()->Intrinsified()) {
4480 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4481 intrinsic.Dispatch(invoke);
4482 return true;
4483 }
4484 return false;
4485}
4486
Vladimir Markodc151b22015-10-15 18:02:30 +01004487HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4488 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004489 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004490 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004491 return desired_dispatch_info;
4492}
4493
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004494void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4495 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004496 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004497 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4498 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004499 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4500 uint32_t offset =
4501 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004502 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004503 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004504 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004505 }
Vladimir Marko58155012015-08-19 12:49:41 +00004506 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004507 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004508 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004509 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4510 DCHECK(GetCompilerOptions().IsBootImage());
4511 // Add ADRP with its PC-relative method patch.
4512 vixl::aarch64::Label* adrp_label = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
4513 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4514 // Add ADD with its PC-relative method patch.
4515 vixl::aarch64::Label* add_label =
4516 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), adrp_label);
4517 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4518 break;
4519 }
Vladimir Marko58155012015-08-19 12:49:41 +00004520 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4521 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004522 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004523 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004524 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko58155012015-08-19 12:49:41 +00004525 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004526 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4527 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004528 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004529 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004530 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004531 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004532 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004533 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004534 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004535 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4536 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4537 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004538 }
4539 }
4540
4541 switch (invoke->GetCodePtrLocation()) {
4542 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004543 {
4544 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4545 ExactAssemblyScope eas(GetVIXLAssembler(),
4546 kInstructionSize,
4547 CodeBufferCheckScope::kExactSize);
4548 __ bl(&frame_entry_label_);
4549 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4550 }
Vladimir Marko58155012015-08-19 12:49:41 +00004551 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004552 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4553 // LR = callee_method->entry_point_from_quick_compiled_code_;
4554 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004555 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004556 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004557 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004558 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004559 ExactAssemblyScope eas(GetVIXLAssembler(),
4560 kInstructionSize,
4561 CodeBufferCheckScope::kExactSize);
4562 // lr()
4563 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004564 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004565 }
Vladimir Marko58155012015-08-19 12:49:41 +00004566 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004567 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004568
Andreas Gampe878d58c2015-01-15 23:24:00 -08004569 DCHECK(!IsLeafMethod());
4570}
4571
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004572void CodeGeneratorARM64::GenerateVirtualCall(
4573 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004574 // Use the calling convention instead of the location of the receiver, as
4575 // intrinsics may have put the receiver in a different register. In the intrinsics
4576 // slow path, the arguments have been moved to the right place, so here we are
4577 // guaranteed that the receiver is the first register of the calling convention.
4578 InvokeDexCallingConvention calling_convention;
4579 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004580 Register temp = XRegisterFrom(temp_in);
4581 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4582 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4583 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004584 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004585
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004586 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004587
4588 {
4589 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4590 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4591 // /* HeapReference<Class> */ temp = receiver->klass_
4592 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4593 MaybeRecordImplicitNullCheck(invoke);
4594 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004595 // Instead of simply (possibly) unpoisoning `temp` here, we should
4596 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004597 // intermediate/temporary reference and because the current
4598 // concurrent copying collector keeps the from-space memory
4599 // intact/accessible until the end of the marking phase (the
4600 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004601 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4602 // temp = temp->GetMethodAt(method_offset);
4603 __ Ldr(temp, MemOperand(temp, method_offset));
4604 // lr = temp->GetEntryPoint();
4605 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004606 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004607 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004608 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4609 // lr();
4610 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004611 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004612 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004613}
4614
Orion Hodsonac141392017-01-13 11:53:47 +00004615void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4616 HandleInvoke(invoke);
4617}
4618
4619void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4620 codegen_->GenerateInvokePolymorphicCall(invoke);
4621}
4622
Vladimir Marko65979462017-05-19 17:25:12 +01004623vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeMethodPatch(
4624 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004625 vixl::aarch64::Label* adrp_label) {
Vladimir Marko65979462017-05-19 17:25:12 +01004626 return NewPcRelativePatch(*target_method.dex_file,
4627 target_method.dex_method_index,
4628 adrp_label,
4629 &pc_relative_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004630}
4631
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004632vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4633 MethodReference target_method,
4634 vixl::aarch64::Label* adrp_label) {
4635 return NewPcRelativePatch(*target_method.dex_file,
4636 target_method.dex_method_index,
4637 adrp_label,
4638 &method_bss_entry_patches_);
4639}
4640
Scott Wakeling97c72b72016-06-24 16:19:36 +01004641vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4642 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004643 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004644 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004645 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004646}
4647
Vladimir Marko1998cd02017-01-13 13:02:58 +00004648vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4649 const DexFile& dex_file,
4650 dex::TypeIndex type_index,
4651 vixl::aarch64::Label* adrp_label) {
4652 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4653}
4654
Vladimir Marko65979462017-05-19 17:25:12 +01004655vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4656 const DexFile& dex_file,
4657 dex::StringIndex string_index,
4658 vixl::aarch64::Label* adrp_label) {
4659 return
4660 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
4661}
4662
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004663vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4664 baker_read_barrier_patches_.emplace_back(custom_data);
4665 return &baker_read_barrier_patches_.back().label;
4666}
4667
Scott Wakeling97c72b72016-06-24 16:19:36 +01004668vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4669 const DexFile& dex_file,
4670 uint32_t offset_or_index,
4671 vixl::aarch64::Label* adrp_label,
4672 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004673 // Add a patch entry and return the label.
4674 patches->emplace_back(dex_file, offset_or_index);
4675 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004676 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004677 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4678 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4679 return label;
4680}
4681
Scott Wakeling97c72b72016-06-24 16:19:36 +01004682vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4683 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004684 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004685}
4686
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004687vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004688 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4689 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4690 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004691 return jit_string_patches_.GetOrCreate(
4692 StringReference(&dex_file, string_index),
4693 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4694}
4695
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004696vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004697 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4698 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4699 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004700 return jit_class_patches_.GetOrCreate(
4701 TypeReference(&dex_file, type_index),
4702 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4703}
4704
Vladimir Markoaad75c62016-10-03 08:46:48 +00004705void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4706 vixl::aarch64::Register reg) {
4707 DCHECK(reg.IsX());
4708 SingleEmissionCheckScope guard(GetVIXLAssembler());
4709 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004710 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004711}
4712
4713void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4714 vixl::aarch64::Register out,
4715 vixl::aarch64::Register base) {
4716 DCHECK(out.IsX());
4717 DCHECK(base.IsX());
4718 SingleEmissionCheckScope guard(GetVIXLAssembler());
4719 __ Bind(fixup_label);
4720 __ add(out, base, Operand(/* offset placeholder */ 0));
4721}
4722
4723void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4724 vixl::aarch64::Register out,
4725 vixl::aarch64::Register base) {
4726 DCHECK(base.IsX());
4727 SingleEmissionCheckScope guard(GetVIXLAssembler());
4728 __ Bind(fixup_label);
4729 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4730}
4731
4732template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4733inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4734 const ArenaDeque<PcRelativePatchInfo>& infos,
4735 ArenaVector<LinkerPatch>* linker_patches) {
4736 for (const PcRelativePatchInfo& info : infos) {
4737 linker_patches->push_back(Factory(info.label.GetLocation(),
4738 &info.target_dex_file,
4739 info.pc_insn_label->GetLocation(),
4740 info.offset_or_index));
4741 }
4742}
4743
Vladimir Marko58155012015-08-19 12:49:41 +00004744void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4745 DCHECK(linker_patches->empty());
4746 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004747 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004748 method_bss_entry_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004749 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004750 type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004751 pc_relative_string_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004752 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004753 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01004754 if (GetCompilerOptions().IsBootImage()) {
4755 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004756 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004757 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4758 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004759 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4760 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004761 } else {
4762 DCHECK(pc_relative_method_patches_.empty());
4763 DCHECK(pc_relative_type_patches_.empty());
4764 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4765 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004766 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004767 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
4768 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004769 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4770 linker_patches);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004771 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
4772 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
4773 info.custom_data));
4774 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004775 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004776}
4777
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004778vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4779 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004780 value,
4781 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4782}
4783
Scott Wakeling97c72b72016-06-24 16:19:36 +01004784vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004785 return uint64_literals_.GetOrCreate(
4786 value,
4787 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004788}
4789
Andreas Gampe878d58c2015-01-15 23:24:00 -08004790void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004791 // Explicit clinit checks triggered by static invokes must have been pruned by
4792 // art::PrepareForRegisterAllocation.
4793 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004794
Andreas Gampe878d58c2015-01-15 23:24:00 -08004795 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4796 return;
4797 }
4798
Artem Serov914d7a82017-02-07 14:33:49 +00004799 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4800 // are no pools emitted.
4801 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004802 LocationSummary* locations = invoke->GetLocations();
4803 codegen_->GenerateStaticOrDirectCall(
4804 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Alexandre Rames5319def2014-10-23 10:03:10 +01004805}
4806
4807void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004808 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4809 return;
4810 }
4811
Artem Serov914d7a82017-02-07 14:33:49 +00004812 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4813 // are no pools emitted.
4814 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004815 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004816 DCHECK(!codegen_->IsLeafMethod());
Alexandre Rames5319def2014-10-23 10:03:10 +01004817}
4818
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004819HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4820 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004821 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004822 case HLoadClass::LoadKind::kInvalid:
4823 LOG(FATAL) << "UNREACHABLE";
4824 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004825 case HLoadClass::LoadKind::kReferrersClass:
4826 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004827 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004828 case HLoadClass::LoadKind::kBssEntry:
4829 DCHECK(!Runtime::Current()->UseJitCompilation());
4830 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004831 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004832 DCHECK(Runtime::Current()->UseJitCompilation());
4833 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004834 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004835 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004836 break;
4837 }
4838 return desired_class_load_kind;
4839}
4840
Alexandre Rames67555f72014-11-18 10:55:16 +00004841void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004842 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004843 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004844 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004845 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004846 cls,
4847 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004848 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004849 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004850 return;
4851 }
Vladimir Marko41559982017-01-06 14:04:23 +00004852 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004853
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004854 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4855 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004856 ? LocationSummary::kCallOnSlowPath
4857 : LocationSummary::kNoCall;
4858 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004859 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004860 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004861 }
4862
Vladimir Marko41559982017-01-06 14:04:23 +00004863 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004864 locations->SetInAt(0, Location::RequiresRegister());
4865 }
4866 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004867 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4868 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4869 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004870 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004871 RegisterSet caller_saves = RegisterSet::Empty();
4872 InvokeRuntimeCallingConvention calling_convention;
4873 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4874 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4875 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4876 Primitive::kPrimNot).GetCode());
4877 locations->SetCustomSlowPathCallerSaves(caller_saves);
4878 } else {
4879 // For non-Baker read barrier we have a temp-clobbering call.
4880 }
4881 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004882}
4883
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004884// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4885// move.
4886void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004887 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004888 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00004889 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004890 return;
4891 }
Vladimir Marko41559982017-01-06 14:04:23 +00004892 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004893
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004894 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004895 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004896 Register bss_entry_temp;
4897 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004898
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004899 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4900 ? kWithoutReadBarrier
4901 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004902 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004903 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004904 case HLoadClass::LoadKind::kReferrersClass: {
4905 DCHECK(!cls->CanCallRuntime());
4906 DCHECK(!cls->MustGenerateClinitCheck());
4907 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4908 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004909 GenerateGcRootFieldLoad(cls,
4910 out_loc,
4911 current_method,
4912 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004913 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004914 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004915 break;
4916 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004917 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004918 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004919 // Add ADRP with its PC-relative type patch.
4920 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004921 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004922 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004923 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004924 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004925 vixl::aarch64::Label* add_label =
4926 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004927 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004928 break;
4929 }
4930 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004931 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004932 uint32_t address = dchecked_integral_cast<uint32_t>(
4933 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4934 DCHECK_NE(address, 0u);
4935 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004936 break;
4937 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004938 case HLoadClass::LoadKind::kBssEntry: {
4939 // Add ADRP with its PC-relative Class .bss entry patch.
4940 const DexFile& dex_file = cls->GetDexFile();
4941 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004942 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004943 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4944 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004945 // Add LDR with its PC-relative Class patch.
4946 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004947 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004948 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4949 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004950 out_loc,
4951 bss_entry_temp,
4952 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004953 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004954 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004955 generate_null_check = true;
4956 break;
4957 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004958 case HLoadClass::LoadKind::kJitTableAddress: {
4959 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4960 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004961 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004962 GenerateGcRootFieldLoad(cls,
4963 out_loc,
4964 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004965 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004966 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004967 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004968 break;
4969 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004970 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004971 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004972 LOG(FATAL) << "UNREACHABLE";
4973 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004974 }
4975
Vladimir Markoea4c1262017-02-06 19:59:33 +00004976 bool do_clinit = cls->MustGenerateClinitCheck();
4977 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004978 DCHECK(cls->CanCallRuntime());
4979 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00004980 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004981 codegen_->AddSlowPath(slow_path);
4982 if (generate_null_check) {
4983 __ Cbz(out, slow_path->GetEntryLabel());
4984 }
4985 if (cls->MustGenerateClinitCheck()) {
4986 GenerateClassInitializationCheck(slow_path, out);
4987 } else {
4988 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004989 }
4990 }
4991}
4992
David Brazdilcb1c0552015-08-04 16:22:25 +01004993static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004994 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004995}
4996
Alexandre Rames67555f72014-11-18 10:55:16 +00004997void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4998 LocationSummary* locations =
4999 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5000 locations->SetOut(Location::RequiresRegister());
5001}
5002
5003void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005004 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5005}
5006
5007void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
5008 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5009}
5010
5011void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5012 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005013}
5014
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005015HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5016 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005017 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005018 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005019 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005020 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005021 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005022 case HLoadString::LoadKind::kJitTableAddress:
5023 DCHECK(Runtime::Current()->UseJitCompilation());
5024 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005025 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005026 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005027 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005028 }
5029 return desired_string_load_kind;
5030}
5031
Alexandre Rames67555f72014-11-18 10:55:16 +00005032void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005033 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005034 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005035 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005036 InvokeRuntimeCallingConvention calling_convention;
5037 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5038 } else {
5039 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005040 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5041 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005042 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005043 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005044 RegisterSet caller_saves = RegisterSet::Empty();
5045 InvokeRuntimeCallingConvention calling_convention;
5046 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5047 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
5048 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
5049 Primitive::kPrimNot).GetCode());
5050 locations->SetCustomSlowPathCallerSaves(caller_saves);
5051 } else {
5052 // For non-Baker read barrier we have a temp-clobbering call.
5053 }
5054 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005055 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005056}
5057
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005058// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5059// move.
5060void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005061 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005062 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005063
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005064 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005065 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005066 // Add ADRP with its PC-relative String patch.
5067 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005068 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005069 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01005070 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005071 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005072 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005073 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005074 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005075 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005076 return; // No dex cache slow path.
5077 }
5078 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005079 uint32_t address = dchecked_integral_cast<uint32_t>(
5080 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5081 DCHECK_NE(address, 0u);
5082 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005083 return; // No dex cache slow path.
5084 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005085 case HLoadString::LoadKind::kBssEntry: {
5086 // Add ADRP with its PC-relative String .bss entry patch.
5087 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005088 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005089 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005090 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005091 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005092 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005093 // Add LDR with its PC-relative String patch.
5094 vixl::aarch64::Label* ldr_label =
5095 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005096 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005097 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005098 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005099 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005100 /* offset placeholder */ 0u,
5101 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005102 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005103 SlowPathCodeARM64* slow_path =
5104 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005105 codegen_->AddSlowPath(slow_path);
5106 __ Cbz(out.X(), slow_path->GetEntryLabel());
5107 __ Bind(slow_path->GetExitLabel());
5108 return;
5109 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005110 case HLoadString::LoadKind::kJitTableAddress: {
5111 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005112 load->GetStringIndex(),
5113 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005114 GenerateGcRootFieldLoad(load,
5115 out_loc,
5116 out.X(),
5117 /* offset */ 0,
5118 /* fixup_label */ nullptr,
5119 kCompilerReadBarrierOption);
5120 return;
5121 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005122 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005123 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005124 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005125
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005126 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005127 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005128 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005129 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005130 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5131 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005132}
5133
Alexandre Rames5319def2014-10-23 10:03:10 +01005134void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5135 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5136 locations->SetOut(Location::ConstantLocation(constant));
5137}
5138
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005139void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005140 // Will be generated at use site.
5141}
5142
Alexandre Rames67555f72014-11-18 10:55:16 +00005143void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5144 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005145 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005146 InvokeRuntimeCallingConvention calling_convention;
5147 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5148}
5149
5150void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005151 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005152 instruction,
5153 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005154 if (instruction->IsEnter()) {
5155 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5156 } else {
5157 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5158 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005159}
5160
Alexandre Rames42d641b2014-10-27 14:00:51 +00005161void LocationsBuilderARM64::VisitMul(HMul* mul) {
5162 LocationSummary* locations =
5163 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5164 switch (mul->GetResultType()) {
5165 case Primitive::kPrimInt:
5166 case Primitive::kPrimLong:
5167 locations->SetInAt(0, Location::RequiresRegister());
5168 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005169 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005170 break;
5171
5172 case Primitive::kPrimFloat:
5173 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005174 locations->SetInAt(0, Location::RequiresFpuRegister());
5175 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005176 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005177 break;
5178
5179 default:
5180 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5181 }
5182}
5183
5184void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5185 switch (mul->GetResultType()) {
5186 case Primitive::kPrimInt:
5187 case Primitive::kPrimLong:
5188 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5189 break;
5190
5191 case Primitive::kPrimFloat:
5192 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005193 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005194 break;
5195
5196 default:
5197 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5198 }
5199}
5200
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005201void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5202 LocationSummary* locations =
5203 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5204 switch (neg->GetResultType()) {
5205 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005206 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005207 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005208 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005209 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005210
5211 case Primitive::kPrimFloat:
5212 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005213 locations->SetInAt(0, Location::RequiresFpuRegister());
5214 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005215 break;
5216
5217 default:
5218 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5219 }
5220}
5221
5222void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5223 switch (neg->GetResultType()) {
5224 case Primitive::kPrimInt:
5225 case Primitive::kPrimLong:
5226 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5227 break;
5228
5229 case Primitive::kPrimFloat:
5230 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005231 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005232 break;
5233
5234 default:
5235 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5236 }
5237}
5238
5239void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5240 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005241 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005242 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005243 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005244 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5245 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005246}
5247
5248void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005249 // Note: if heap poisoning is enabled, the entry point takes cares
5250 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005251 QuickEntrypointEnum entrypoint =
5252 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5253 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005254 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005255}
5256
Alexandre Rames5319def2014-10-23 10:03:10 +01005257void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5258 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005259 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005260 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005261 if (instruction->IsStringAlloc()) {
5262 locations->AddTemp(LocationFrom(kArtMethodRegister));
5263 } else {
5264 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005265 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005266 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5267}
5268
5269void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005270 // Note: if heap poisoning is enabled, the entry point takes cares
5271 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005272 if (instruction->IsStringAlloc()) {
5273 // String is allocated through StringFactory. Call NewEmptyString entry point.
5274 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005275 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005276 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5277 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005278
5279 {
5280 // Ensure the pc position is recorded immediately after the `blr` instruction.
5281 ExactAssemblyScope eas(GetVIXLAssembler(),
5282 kInstructionSize,
5283 CodeBufferCheckScope::kExactSize);
5284 __ blr(lr);
5285 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5286 }
David Brazdil6de19382016-01-08 17:37:10 +00005287 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005288 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005289 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005290 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005291}
5292
5293void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5294 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005295 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005296 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005297}
5298
5299void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005300 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005301 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005302 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005303 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005304 break;
5305
5306 default:
5307 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5308 }
5309}
5310
David Brazdil66d126e2015-04-03 16:02:44 +01005311void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5312 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5313 locations->SetInAt(0, Location::RequiresRegister());
5314 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5315}
5316
5317void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005318 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005319}
5320
Alexandre Rames5319def2014-10-23 10:03:10 +01005321void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005322 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5323 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005324}
5325
Calin Juravle2ae48182016-03-16 14:05:09 +00005326void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5327 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005328 return;
5329 }
Artem Serov914d7a82017-02-07 14:33:49 +00005330 {
5331 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5332 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5333 Location obj = instruction->GetLocations()->InAt(0);
5334 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5335 RecordPcInfo(instruction, instruction->GetDexPc());
5336 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005337}
5338
Calin Juravle2ae48182016-03-16 14:05:09 +00005339void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005340 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005341 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005342
5343 LocationSummary* locations = instruction->GetLocations();
5344 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005345
5346 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005347}
5348
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005349void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005350 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005351}
5352
Alexandre Rames67555f72014-11-18 10:55:16 +00005353void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5354 HandleBinaryOp(instruction);
5355}
5356
5357void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5358 HandleBinaryOp(instruction);
5359}
5360
Alexandre Rames3e69f162014-12-10 10:36:50 +00005361void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5362 LOG(FATAL) << "Unreachable";
5363}
5364
5365void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5366 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5367}
5368
Alexandre Rames5319def2014-10-23 10:03:10 +01005369void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5370 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5371 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5372 if (location.IsStackSlot()) {
5373 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5374 } else if (location.IsDoubleStackSlot()) {
5375 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5376 }
5377 locations->SetOut(location);
5378}
5379
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005380void InstructionCodeGeneratorARM64::VisitParameterValue(
5381 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005382 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005383}
5384
5385void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5386 LocationSummary* locations =
5387 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005388 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005389}
5390
5391void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5392 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5393 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005394}
5395
5396void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5397 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005398 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005399 locations->SetInAt(i, Location::Any());
5400 }
5401 locations->SetOut(Location::Any());
5402}
5403
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005404void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005405 LOG(FATAL) << "Unreachable";
5406}
5407
Serban Constantinescu02164b32014-11-13 14:05:07 +00005408void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005409 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005410 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005411 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5412 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005413 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5414
5415 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005416 case Primitive::kPrimInt:
5417 case Primitive::kPrimLong:
5418 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005419 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005420 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5421 break;
5422
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005423 case Primitive::kPrimFloat:
5424 case Primitive::kPrimDouble: {
5425 InvokeRuntimeCallingConvention calling_convention;
5426 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5427 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5428 locations->SetOut(calling_convention.GetReturnLocation(type));
5429
5430 break;
5431 }
5432
Serban Constantinescu02164b32014-11-13 14:05:07 +00005433 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005434 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005435 }
5436}
5437
5438void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5439 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005440
Serban Constantinescu02164b32014-11-13 14:05:07 +00005441 switch (type) {
5442 case Primitive::kPrimInt:
5443 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005444 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005445 break;
5446 }
5447
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005448 case Primitive::kPrimFloat:
5449 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005450 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5451 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005452 if (type == Primitive::kPrimFloat) {
5453 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5454 } else {
5455 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5456 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005457 break;
5458 }
5459
Serban Constantinescu02164b32014-11-13 14:05:07 +00005460 default:
5461 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005462 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005463 }
5464}
5465
Igor Murashkind01745e2017-04-05 16:40:31 -07005466void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5467 constructor_fence->SetLocations(nullptr);
5468}
5469
5470void InstructionCodeGeneratorARM64::VisitConstructorFence(
5471 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5472 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5473}
5474
Calin Juravle27df7582015-04-17 19:12:31 +01005475void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5476 memory_barrier->SetLocations(nullptr);
5477}
5478
5479void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005480 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005481}
5482
Alexandre Rames5319def2014-10-23 10:03:10 +01005483void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5484 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5485 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005486 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005487}
5488
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005489void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005490 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005491}
5492
5493void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5494 instruction->SetLocations(nullptr);
5495}
5496
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005497void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005498 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005499}
5500
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005501void LocationsBuilderARM64::VisitRor(HRor* ror) {
5502 HandleBinaryOp(ror);
5503}
5504
5505void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5506 HandleBinaryOp(ror);
5507}
5508
Serban Constantinescu02164b32014-11-13 14:05:07 +00005509void LocationsBuilderARM64::VisitShl(HShl* shl) {
5510 HandleShift(shl);
5511}
5512
5513void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5514 HandleShift(shl);
5515}
5516
5517void LocationsBuilderARM64::VisitShr(HShr* shr) {
5518 HandleShift(shr);
5519}
5520
5521void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5522 HandleShift(shr);
5523}
5524
Alexandre Rames5319def2014-10-23 10:03:10 +01005525void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005526 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005527}
5528
5529void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005530 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005531}
5532
Alexandre Rames67555f72014-11-18 10:55:16 +00005533void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005534 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005535}
5536
5537void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005538 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005539}
5540
5541void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005542 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005543}
5544
Alexandre Rames67555f72014-11-18 10:55:16 +00005545void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005546 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005547}
5548
Calin Juravlee460d1d2015-09-29 04:52:17 +01005549void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5550 HUnresolvedInstanceFieldGet* instruction) {
5551 FieldAccessCallingConventionARM64 calling_convention;
5552 codegen_->CreateUnresolvedFieldLocationSummary(
5553 instruction, instruction->GetFieldType(), calling_convention);
5554}
5555
5556void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5557 HUnresolvedInstanceFieldGet* instruction) {
5558 FieldAccessCallingConventionARM64 calling_convention;
5559 codegen_->GenerateUnresolvedFieldAccess(instruction,
5560 instruction->GetFieldType(),
5561 instruction->GetFieldIndex(),
5562 instruction->GetDexPc(),
5563 calling_convention);
5564}
5565
5566void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5567 HUnresolvedInstanceFieldSet* instruction) {
5568 FieldAccessCallingConventionARM64 calling_convention;
5569 codegen_->CreateUnresolvedFieldLocationSummary(
5570 instruction, instruction->GetFieldType(), calling_convention);
5571}
5572
5573void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5574 HUnresolvedInstanceFieldSet* instruction) {
5575 FieldAccessCallingConventionARM64 calling_convention;
5576 codegen_->GenerateUnresolvedFieldAccess(instruction,
5577 instruction->GetFieldType(),
5578 instruction->GetFieldIndex(),
5579 instruction->GetDexPc(),
5580 calling_convention);
5581}
5582
5583void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5584 HUnresolvedStaticFieldGet* instruction) {
5585 FieldAccessCallingConventionARM64 calling_convention;
5586 codegen_->CreateUnresolvedFieldLocationSummary(
5587 instruction, instruction->GetFieldType(), calling_convention);
5588}
5589
5590void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5591 HUnresolvedStaticFieldGet* instruction) {
5592 FieldAccessCallingConventionARM64 calling_convention;
5593 codegen_->GenerateUnresolvedFieldAccess(instruction,
5594 instruction->GetFieldType(),
5595 instruction->GetFieldIndex(),
5596 instruction->GetDexPc(),
5597 calling_convention);
5598}
5599
5600void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5601 HUnresolvedStaticFieldSet* instruction) {
5602 FieldAccessCallingConventionARM64 calling_convention;
5603 codegen_->CreateUnresolvedFieldLocationSummary(
5604 instruction, instruction->GetFieldType(), calling_convention);
5605}
5606
5607void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5608 HUnresolvedStaticFieldSet* instruction) {
5609 FieldAccessCallingConventionARM64 calling_convention;
5610 codegen_->GenerateUnresolvedFieldAccess(instruction,
5611 instruction->GetFieldType(),
5612 instruction->GetFieldIndex(),
5613 instruction->GetDexPc(),
5614 calling_convention);
5615}
5616
Alexandre Rames5319def2014-10-23 10:03:10 +01005617void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005618 LocationSummary* locations =
5619 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005620 // In suspend check slow path, usually there are no caller-save registers at all.
5621 // If SIMD instructions are present, however, we force spilling all live SIMD
5622 // registers in full width (since the runtime only saves/restores lower part).
5623 locations->SetCustomSlowPathCallerSaves(
5624 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005625}
5626
5627void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005628 HBasicBlock* block = instruction->GetBlock();
5629 if (block->GetLoopInformation() != nullptr) {
5630 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5631 // The back edge will generate the suspend check.
5632 return;
5633 }
5634 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5635 // The goto will generate the suspend check.
5636 return;
5637 }
5638 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005639}
5640
Alexandre Rames67555f72014-11-18 10:55:16 +00005641void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5642 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005643 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005644 InvokeRuntimeCallingConvention calling_convention;
5645 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5646}
5647
5648void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005649 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005650 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005651}
5652
5653void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5654 LocationSummary* locations =
5655 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5656 Primitive::Type input_type = conversion->GetInputType();
5657 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005658 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005659 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5660 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5661 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5662 }
5663
Alexandre Rames542361f2015-01-29 16:57:31 +00005664 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005665 locations->SetInAt(0, Location::RequiresFpuRegister());
5666 } else {
5667 locations->SetInAt(0, Location::RequiresRegister());
5668 }
5669
Alexandre Rames542361f2015-01-29 16:57:31 +00005670 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005671 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5672 } else {
5673 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5674 }
5675}
5676
5677void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5678 Primitive::Type result_type = conversion->GetResultType();
5679 Primitive::Type input_type = conversion->GetInputType();
5680
5681 DCHECK_NE(input_type, result_type);
5682
Alexandre Rames542361f2015-01-29 16:57:31 +00005683 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005684 int result_size = Primitive::ComponentSize(result_type);
5685 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005686 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005687 Register output = OutputRegister(conversion);
5688 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005689 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005690 // 'int' values are used directly as W registers, discarding the top
5691 // bits, so we don't need to sign-extend and can just perform a move.
5692 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5693 // top 32 bits of the target register. We theoretically could leave those
5694 // bits unchanged, but we would have to make sure that no code uses a
5695 // 32bit input value as a 64bit value assuming that the top 32 bits are
5696 // zero.
5697 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005698 } else if (result_type == Primitive::kPrimChar ||
5699 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5700 __ Ubfx(output,
5701 output.IsX() ? source.X() : source.W(),
5702 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005703 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005704 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005705 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005706 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005707 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005708 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005709 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5710 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005711 } else if (Primitive::IsFloatingPointType(result_type) &&
5712 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005713 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5714 } else {
5715 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5716 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005717 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005718}
Alexandre Rames67555f72014-11-18 10:55:16 +00005719
Serban Constantinescu02164b32014-11-13 14:05:07 +00005720void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5721 HandleShift(ushr);
5722}
5723
5724void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5725 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005726}
5727
5728void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5729 HandleBinaryOp(instruction);
5730}
5731
5732void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5733 HandleBinaryOp(instruction);
5734}
5735
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005736void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005737 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005738 LOG(FATAL) << "Unreachable";
5739}
5740
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005741void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005742 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005743 LOG(FATAL) << "Unreachable";
5744}
5745
Mark Mendellfe57faa2015-09-18 09:26:15 -04005746// Simple implementation of packed switch - generate cascaded compare/jumps.
5747void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5748 LocationSummary* locations =
5749 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5750 locations->SetInAt(0, Location::RequiresRegister());
5751}
5752
5753void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5754 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005755 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005756 Register value_reg = InputRegisterAt(switch_instr, 0);
5757 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5758
Zheng Xu3927c8b2015-11-18 17:46:25 +08005759 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005760 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005761 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5762 // make sure we don't emit it if the target may run out of range.
5763 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5764 // ranges and emit the tables only as required.
5765 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005766
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005767 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005768 // Current instruction id is an upper bound of the number of HIRs in the graph.
5769 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5770 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005771 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5772 Register temp = temps.AcquireW();
5773 __ Subs(temp, value_reg, Operand(lower_bound));
5774
Zheng Xu3927c8b2015-11-18 17:46:25 +08005775 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005776 // Jump to successors[0] if value == lower_bound.
5777 __ B(eq, codegen_->GetLabelOf(successors[0]));
5778 int32_t last_index = 0;
5779 for (; num_entries - last_index > 2; last_index += 2) {
5780 __ Subs(temp, temp, Operand(2));
5781 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5782 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5783 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5784 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5785 }
5786 if (num_entries - last_index == 2) {
5787 // The last missing case_value.
5788 __ Cmp(temp, Operand(1));
5789 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005790 }
5791
5792 // And the default for any other value.
5793 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5794 __ B(codegen_->GetLabelOf(default_block));
5795 }
5796 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005797 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005798
5799 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5800
5801 // Below instructions should use at most one blocked register. Since there are two blocked
5802 // registers, we are free to block one.
5803 Register temp_w = temps.AcquireW();
5804 Register index;
5805 // Remove the bias.
5806 if (lower_bound != 0) {
5807 index = temp_w;
5808 __ Sub(index, value_reg, Operand(lower_bound));
5809 } else {
5810 index = value_reg;
5811 }
5812
5813 // Jump to default block if index is out of the range.
5814 __ Cmp(index, Operand(num_entries));
5815 __ B(hs, codegen_->GetLabelOf(default_block));
5816
5817 // In current VIXL implementation, it won't require any blocked registers to encode the
5818 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5819 // register pressure.
5820 Register table_base = temps.AcquireX();
5821 // Load jump offset from the table.
5822 __ Adr(table_base, jump_table->GetTableStartLabel());
5823 Register jump_offset = temp_w;
5824 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5825
5826 // Jump to target block by branching to table_base(pc related) + offset.
5827 Register target_address = table_base;
5828 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5829 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005830 }
5831}
5832
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005833void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5834 HInstruction* instruction,
5835 Location out,
5836 uint32_t offset,
5837 Location maybe_temp,
5838 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005839 Primitive::Type type = Primitive::kPrimNot;
5840 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005841 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005842 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005843 if (kUseBakerReadBarrier) {
5844 // Load with fast path based Baker's read barrier.
5845 // /* HeapReference<Object> */ out = *(out + offset)
5846 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5847 out,
5848 out_reg,
5849 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005850 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005851 /* needs_null_check */ false,
5852 /* use_load_acquire */ false);
5853 } else {
5854 // Load with slow path based read barrier.
5855 // Save the value of `out` into `maybe_temp` before overwriting it
5856 // in the following move operation, as we will need it for the
5857 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005858 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005859 __ Mov(temp_reg, out_reg);
5860 // /* HeapReference<Object> */ out = *(out + offset)
5861 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5862 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5863 }
5864 } else {
5865 // Plain load with no read barrier.
5866 // /* HeapReference<Object> */ out = *(out + offset)
5867 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5868 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5869 }
5870}
5871
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005872void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5873 HInstruction* instruction,
5874 Location out,
5875 Location obj,
5876 uint32_t offset,
5877 Location maybe_temp,
5878 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005879 Primitive::Type type = Primitive::kPrimNot;
5880 Register out_reg = RegisterFrom(out, type);
5881 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005882 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005883 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005884 if (kUseBakerReadBarrier) {
5885 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005886 // /* HeapReference<Object> */ out = *(obj + offset)
5887 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5888 out,
5889 obj_reg,
5890 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005891 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005892 /* needs_null_check */ false,
5893 /* use_load_acquire */ false);
5894 } else {
5895 // Load with slow path based read barrier.
5896 // /* HeapReference<Object> */ out = *(obj + offset)
5897 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5898 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5899 }
5900 } else {
5901 // Plain load with no read barrier.
5902 // /* HeapReference<Object> */ out = *(obj + offset)
5903 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5904 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5905 }
5906}
5907
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005908void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5909 HInstruction* instruction,
5910 Location root,
5911 Register obj,
5912 uint32_t offset,
5913 vixl::aarch64::Label* fixup_label,
5914 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005915 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005916 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005917 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005918 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005919 if (kUseBakerReadBarrier) {
5920 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005921 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005922 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5923 !Runtime::Current()->UseJitCompilation()) {
5924 // Note that we do not actually check the value of `GetIsGcMarking()`
5925 // to decide whether to mark the loaded GC root or not. Instead, we
Vladimir Marko66d691d2017-04-07 17:53:39 +01005926 // load into `temp` (actually IP1) the read barrier mark introspection
5927 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
5928 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005929 //
5930 // We use link-time generated thunks for the slow path. That thunk
5931 // checks the reference and jumps to the entrypoint if needed.
5932 //
5933 // temp = Thread::Current()->pReadBarrierMarkIntrospection
5934 // lr = &return_address;
5935 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5936 // if (temp != nullptr) {
5937 // goto gc_root_thunk<root_reg>(lr)
5938 // }
5939 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00005940
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005941 UseScratchRegisterScope temps(GetVIXLAssembler());
5942 DCHECK(temps.IsAvailable(ip0));
5943 DCHECK(temps.IsAvailable(ip1));
5944 temps.Exclude(ip0, ip1);
5945 uint32_t custom_data =
5946 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
5947 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00005948
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005949 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
5950 DCHECK_EQ(ip0.GetCode(), 16u);
5951 const int32_t entry_point_offset =
5952 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
5953 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
5954 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5955 vixl::aarch64::Label return_address;
5956 __ adr(lr, &return_address);
5957 if (fixup_label != nullptr) {
5958 __ Bind(fixup_label);
5959 }
5960 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
5961 "GC root LDR must be 2 instruction (8B) before the return address label.");
5962 __ ldr(root_reg, MemOperand(obj.X(), offset));
5963 __ Bind(cbnz_label);
5964 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
5965 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005966 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005967 // Note that we do not actually check the value of
5968 // `GetIsGcMarking()` to decide whether to mark the loaded GC
5969 // root or not. Instead, we load into `temp` the read barrier
5970 // mark entry point corresponding to register `root`. If `temp`
5971 // is null, it means that `GetIsGcMarking()` is false, and vice
5972 // versa.
5973 //
5974 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5975 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5976 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
5977 // // Slow path.
5978 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
5979 // }
Roland Levillain44015862016-01-22 11:47:17 +00005980
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005981 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
5982 Register temp = lr;
5983 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
5984 instruction, root, /* entrypoint */ LocationFrom(temp));
5985 codegen_->AddSlowPath(slow_path);
5986
5987 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5988 const int32_t entry_point_offset =
5989 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5990 // Loading the entrypoint does not require a load acquire since it is only changed when
5991 // threads are suspended or running a checkpoint.
5992 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5993
5994 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5995 if (fixup_label == nullptr) {
5996 __ Ldr(root_reg, MemOperand(obj, offset));
5997 } else {
5998 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
5999 }
6000 static_assert(
6001 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6002 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6003 "have different sizes.");
6004 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6005 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6006 "have different sizes.");
6007
6008 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6009 // checking GetIsGcMarking.
6010 __ Cbnz(temp, slow_path->GetEntryLabel());
6011 __ Bind(slow_path->GetExitLabel());
6012 }
Roland Levillain44015862016-01-22 11:47:17 +00006013 } else {
6014 // GC root loaded through a slow path for read barriers other
6015 // than Baker's.
6016 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006017 if (fixup_label == nullptr) {
6018 __ Add(root_reg.X(), obj.X(), offset);
6019 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006020 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006021 }
Roland Levillain44015862016-01-22 11:47:17 +00006022 // /* mirror::Object* */ root = root->Read()
6023 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6024 }
6025 } else {
6026 // Plain GC root load with no read barrier.
6027 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006028 if (fixup_label == nullptr) {
6029 __ Ldr(root_reg, MemOperand(obj, offset));
6030 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006031 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006032 }
Roland Levillain44015862016-01-22 11:47:17 +00006033 // Note that GC roots are not affected by heap poisoning, thus we
6034 // do not have to unpoison `root_reg` here.
6035 }
6036}
6037
6038void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6039 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006040 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006041 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006042 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006043 bool needs_null_check,
6044 bool use_load_acquire) {
6045 DCHECK(kEmitCompilerReadBarrier);
6046 DCHECK(kUseBakerReadBarrier);
6047
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006048 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6049 !use_load_acquire &&
6050 !Runtime::Current()->UseJitCompilation()) {
6051 // Note that we do not actually check the value of `GetIsGcMarking()`
Vladimir Marko66d691d2017-04-07 17:53:39 +01006052 // to decide whether to mark the loaded reference or not. Instead, we
6053 // load into `temp` (actually IP1) the read barrier mark introspection
6054 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6055 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006056 //
6057 // We use link-time generated thunks for the slow path. That thunk checks
6058 // the holder and jumps to the entrypoint if needed. If the holder is not
6059 // gray, it creates a fake dependency and returns to the LDR instruction.
6060 //
6061 // temp = Thread::Current()->pReadBarrierMarkIntrospection
Vladimir Marko66d691d2017-04-07 17:53:39 +01006062 // lr = &gray_return_address;
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006063 // if (temp != nullptr) {
6064 // goto field_thunk<holder_reg, base_reg>(lr)
6065 // }
6066 // not_gray_return_address:
6067 // // Original reference load. If the offset is too large to fit
6068 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006069 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006070 // gray_return_address:
6071
6072 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6073 Register base = obj;
6074 if (offset >= kReferenceLoadMinFarOffset) {
6075 DCHECK(maybe_temp.IsRegister());
6076 base = WRegisterFrom(maybe_temp);
6077 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6078 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6079 offset &= (kReferenceLoadMinFarOffset - 1u);
6080 }
6081 UseScratchRegisterScope temps(GetVIXLAssembler());
6082 DCHECK(temps.IsAvailable(ip0));
6083 DCHECK(temps.IsAvailable(ip1));
6084 temps.Exclude(ip0, ip1);
6085 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6086 base.GetCode(),
6087 obj.GetCode());
6088 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6089
6090 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6091 DCHECK_EQ(ip0.GetCode(), 16u);
6092 const int32_t entry_point_offset =
6093 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6094 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
Vladimir Markod1ef8732017-04-18 13:55:13 +01006095 EmissionCheckScope guard(GetVIXLAssembler(),
6096 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006097 vixl::aarch64::Label return_address;
6098 __ adr(lr, &return_address);
6099 __ Bind(cbnz_label);
6100 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markod1ef8732017-04-18 13:55:13 +01006101 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6102 "Field LDR must be 1 instruction (4B) before the return address label; "
6103 " 2 instructions (8B) for heap poisoning.");
6104 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6105 __ ldr(ref_reg, MemOperand(base.X(), offset));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006106 if (needs_null_check) {
6107 MaybeRecordImplicitNullCheck(instruction);
6108 }
Vladimir Markod1ef8732017-04-18 13:55:13 +01006109 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006110 __ Bind(&return_address);
6111 return;
6112 }
6113
Roland Levillain44015862016-01-22 11:47:17 +00006114 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006115 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006116 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006117 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006118 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6119 ref,
6120 obj,
6121 offset,
6122 no_index,
6123 no_scale_factor,
6124 temp,
6125 needs_null_check,
6126 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006127}
6128
6129void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6130 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006131 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006132 uint32_t data_offset,
6133 Location index,
6134 Register temp,
6135 bool needs_null_check) {
6136 DCHECK(kEmitCompilerReadBarrier);
6137 DCHECK(kUseBakerReadBarrier);
6138
Vladimir Marko66d691d2017-04-07 17:53:39 +01006139 static_assert(
6140 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6141 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6142 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
6143
6144 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6145 !Runtime::Current()->UseJitCompilation()) {
6146 // Note that we do not actually check the value of `GetIsGcMarking()`
6147 // to decide whether to mark the loaded reference or not. Instead, we
6148 // load into `temp` (actually IP1) the read barrier mark introspection
6149 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6150 // false, and vice versa.
6151 //
6152 // We use link-time generated thunks for the slow path. That thunk checks
6153 // the holder and jumps to the entrypoint if needed. If the holder is not
6154 // gray, it creates a fake dependency and returns to the LDR instruction.
6155 //
6156 // temp = Thread::Current()->pReadBarrierMarkIntrospection
6157 // lr = &gray_return_address;
6158 // if (temp != nullptr) {
6159 // goto field_thunk<holder_reg, base_reg>(lr)
6160 // }
6161 // not_gray_return_address:
6162 // // Original reference load. If the offset is too large to fit
6163 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006164 // HeapReference<mirror::Object> reference = data[index];
Vladimir Marko66d691d2017-04-07 17:53:39 +01006165 // gray_return_address:
6166
6167 DCHECK(index.IsValid());
6168 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
6169 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6170
6171 UseScratchRegisterScope temps(GetVIXLAssembler());
6172 DCHECK(temps.IsAvailable(ip0));
6173 DCHECK(temps.IsAvailable(ip1));
6174 temps.Exclude(ip0, ip1);
6175 uint32_t custom_data =
6176 linker::Arm64RelativePatcher::EncodeBakerReadBarrierArrayData(temp.GetCode());
6177 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6178
6179 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6180 DCHECK_EQ(ip0.GetCode(), 16u);
6181 const int32_t entry_point_offset =
6182 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6183 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
6184 __ Add(temp.X(), obj.X(), Operand(data_offset));
6185 EmissionCheckScope guard(GetVIXLAssembler(),
6186 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6187 vixl::aarch64::Label return_address;
6188 __ adr(lr, &return_address);
6189 __ Bind(cbnz_label);
6190 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6191 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6192 "Array LDR must be 1 instruction (4B) before the return address label; "
6193 " 2 instructions (8B) for heap poisoning.");
6194 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6195 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6196 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6197 __ Bind(&return_address);
6198 return;
6199 }
6200
Roland Levillain44015862016-01-22 11:47:17 +00006201 // Array cells are never volatile variables, therefore array loads
6202 // never use Load-Acquire instructions on ARM64.
6203 const bool use_load_acquire = false;
6204
6205 // /* HeapReference<Object> */ ref =
6206 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006207 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6208 ref,
6209 obj,
6210 data_offset,
6211 index,
6212 scale_factor,
6213 temp,
6214 needs_null_check,
6215 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006216}
6217
6218void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6219 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006220 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006221 uint32_t offset,
6222 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006223 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006224 Register temp,
6225 bool needs_null_check,
Roland Levillainff487002017-03-07 16:50:01 +00006226 bool use_load_acquire) {
Roland Levillain44015862016-01-22 11:47:17 +00006227 DCHECK(kEmitCompilerReadBarrier);
6228 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006229 // If we are emitting an array load, we should not be using a
6230 // Load Acquire instruction. In other words:
6231 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6232 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006233
Roland Levillain54f869e2017-03-06 13:54:11 +00006234 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6235 // whether we need to enter the slow path to mark the reference.
6236 // Then, in the slow path, check the gray bit in the lock word of
6237 // the reference's holder (`obj`) to decide whether to mark `ref` or
6238 // not.
Roland Levillain44015862016-01-22 11:47:17 +00006239 //
Roland Levillainba650a42017-03-06 13:52:32 +00006240 // Note that we do not actually check the value of `GetIsGcMarking()`;
6241 // instead, we load into `temp2` the read barrier mark entry point
6242 // corresponding to register `ref`. If `temp2` is null, it means
6243 // that `GetIsGcMarking()` is false, and vice versa.
6244 //
6245 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00006246 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6247 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006248 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6249 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6250 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6251 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6252 // if (is_gray) {
6253 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6254 // }
6255 // } else {
6256 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006257 // }
Roland Levillain44015862016-01-22 11:47:17 +00006258
Roland Levillainba650a42017-03-06 13:52:32 +00006259 // Slow path marking the object `ref` when the GC is marking. The
6260 // entrypoint will already be loaded in `temp2`.
6261 Register temp2 = lr;
6262 Location temp2_loc = LocationFrom(temp2);
Roland Levillainff487002017-03-07 16:50:01 +00006263 SlowPathCodeARM64* slow_path =
6264 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6265 instruction,
6266 ref,
6267 obj,
6268 offset,
6269 index,
6270 scale_factor,
6271 needs_null_check,
6272 use_load_acquire,
6273 temp,
6274 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006275 AddSlowPath(slow_path);
6276
6277 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6278 const int32_t entry_point_offset =
6279 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6280 // Loading the entrypoint does not require a load acquire since it is only changed when
6281 // threads are suspended or running a checkpoint.
6282 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006283 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6284 // checking GetIsGcMarking.
6285 __ Cbnz(temp2, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006286 // Fast path: the GC is not marking: just load the reference.
Roland Levillain54f869e2017-03-06 13:54:11 +00006287 GenerateRawReferenceLoad(
6288 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006289 __ Bind(slow_path->GetExitLabel());
6290}
6291
Roland Levillainff487002017-03-07 16:50:01 +00006292void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6293 Location ref,
6294 Register obj,
6295 Location field_offset,
6296 Register temp,
6297 bool needs_null_check,
6298 bool use_load_acquire) {
6299 DCHECK(kEmitCompilerReadBarrier);
6300 DCHECK(kUseBakerReadBarrier);
6301 // If we are emitting an array load, we should not be using a
6302 // Load Acquire instruction. In other words:
6303 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6304 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6305
6306 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6307 // whether we need to enter the slow path to update the reference
6308 // field within `obj`. Then, in the slow path, check the gray bit
6309 // in the lock word of the reference's holder (`obj`) to decide
6310 // whether to mark `ref` and update the field or not.
6311 //
6312 // Note that we do not actually check the value of `GetIsGcMarking()`;
6313 // instead, we load into `temp2` the read barrier mark entry point
6314 // corresponding to register `ref`. If `temp2` is null, it means
6315 // that `GetIsGcMarking()` is false, and vice versa.
6316 //
6317 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6318 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6319 // // Slow path.
6320 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6321 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6322 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6323 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6324 // if (is_gray) {
6325 // old_ref = ref;
6326 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6327 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6328 // }
6329 // }
6330
6331 // Slow path updating the object reference at address `obj + field_offset`
6332 // when the GC is marking. The entrypoint will already be loaded in `temp2`.
6333 Register temp2 = lr;
6334 Location temp2_loc = LocationFrom(temp2);
6335 SlowPathCodeARM64* slow_path =
6336 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6337 instruction,
6338 ref,
6339 obj,
6340 /* offset */ 0u,
6341 /* index */ field_offset,
6342 /* scale_factor */ 0u /* "times 1" */,
6343 needs_null_check,
6344 use_load_acquire,
6345 temp,
6346 /* entrypoint */ temp2_loc);
6347 AddSlowPath(slow_path);
6348
6349 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6350 const int32_t entry_point_offset =
6351 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6352 // Loading the entrypoint does not require a load acquire since it is only changed when
6353 // threads are suspended or running a checkpoint.
6354 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
6355 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6356 // checking GetIsGcMarking.
6357 __ Cbnz(temp2, slow_path->GetEntryLabel());
6358 // Fast path: the GC is not marking: nothing to do (the field is
6359 // up-to-date, and we don't need to load the reference).
6360 __ Bind(slow_path->GetExitLabel());
6361}
6362
Roland Levillainba650a42017-03-06 13:52:32 +00006363void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6364 Location ref,
6365 Register obj,
6366 uint32_t offset,
6367 Location index,
6368 size_t scale_factor,
6369 bool needs_null_check,
6370 bool use_load_acquire) {
6371 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006372 Primitive::Type type = Primitive::kPrimNot;
6373 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006374
Roland Levillainba650a42017-03-06 13:52:32 +00006375 // If needed, vixl::EmissionCheckScope guards are used to ensure
6376 // that no pools are emitted between the load (macro) instruction
6377 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006378
Roland Levillain44015862016-01-22 11:47:17 +00006379 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006380 // Load types involving an "index": ArrayGet,
6381 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6382 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006383 if (use_load_acquire) {
6384 // UnsafeGetObjectVolatile intrinsic case.
6385 // Register `index` is not an index in an object array, but an
6386 // offset to an object reference field within object `obj`.
6387 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6388 DCHECK(instruction->GetLocations()->Intrinsified());
6389 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6390 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006391 DCHECK_EQ(offset, 0u);
6392 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006393 DCHECK_EQ(needs_null_check, false);
6394 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006395 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6396 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006397 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006398 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6399 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006400 if (index.IsConstant()) {
6401 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006402 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006403 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006404 if (needs_null_check) {
6405 MaybeRecordImplicitNullCheck(instruction);
6406 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006407 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006408 UseScratchRegisterScope temps(GetVIXLAssembler());
6409 Register temp = temps.AcquireW();
6410 __ Add(temp, obj, offset);
6411 {
6412 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6413 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6414 if (needs_null_check) {
6415 MaybeRecordImplicitNullCheck(instruction);
6416 }
6417 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006418 }
Roland Levillain44015862016-01-22 11:47:17 +00006419 }
Roland Levillain44015862016-01-22 11:47:17 +00006420 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006421 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006422 MemOperand field = HeapOperand(obj, offset);
6423 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006424 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6425 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006426 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006427 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006428 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006429 if (needs_null_check) {
6430 MaybeRecordImplicitNullCheck(instruction);
6431 }
Roland Levillain44015862016-01-22 11:47:17 +00006432 }
6433 }
6434
6435 // Object* ref = ref_addr->AsMirrorPtr()
6436 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006437}
6438
6439void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6440 Location out,
6441 Location ref,
6442 Location obj,
6443 uint32_t offset,
6444 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006445 DCHECK(kEmitCompilerReadBarrier);
6446
Roland Levillain44015862016-01-22 11:47:17 +00006447 // Insert a slow path based read barrier *after* the reference load.
6448 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006449 // If heap poisoning is enabled, the unpoisoning of the loaded
6450 // reference will be carried out by the runtime within the slow
6451 // path.
6452 //
6453 // Note that `ref` currently does not get unpoisoned (when heap
6454 // poisoning is enabled), which is alright as the `ref` argument is
6455 // not used by the artReadBarrierSlow entry point.
6456 //
6457 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6458 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6459 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6460 AddSlowPath(slow_path);
6461
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006462 __ B(slow_path->GetEntryLabel());
6463 __ Bind(slow_path->GetExitLabel());
6464}
6465
Roland Levillain44015862016-01-22 11:47:17 +00006466void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6467 Location out,
6468 Location ref,
6469 Location obj,
6470 uint32_t offset,
6471 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006472 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006473 // Baker's read barriers shall be handled by the fast path
6474 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6475 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006476 // If heap poisoning is enabled, unpoisoning will be taken care of
6477 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006478 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006479 } else if (kPoisonHeapReferences) {
6480 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6481 }
6482}
6483
Roland Levillain44015862016-01-22 11:47:17 +00006484void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6485 Location out,
6486 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006487 DCHECK(kEmitCompilerReadBarrier);
6488
Roland Levillain44015862016-01-22 11:47:17 +00006489 // Insert a slow path based read barrier *after* the GC root load.
6490 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006491 // Note that GC roots are not affected by heap poisoning, so we do
6492 // not need to do anything special for this here.
6493 SlowPathCodeARM64* slow_path =
6494 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6495 AddSlowPath(slow_path);
6496
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006497 __ B(slow_path->GetEntryLabel());
6498 __ Bind(slow_path->GetExitLabel());
6499}
6500
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006501void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6502 LocationSummary* locations =
6503 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6504 locations->SetInAt(0, Location::RequiresRegister());
6505 locations->SetOut(Location::RequiresRegister());
6506}
6507
6508void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6509 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006510 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006511 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006512 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006513 __ Ldr(XRegisterFrom(locations->Out()),
6514 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006515 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006516 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006517 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006518 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6519 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006520 __ Ldr(XRegisterFrom(locations->Out()),
6521 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006522 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006523}
6524
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006525static void PatchJitRootUse(uint8_t* code,
6526 const uint8_t* roots_data,
6527 vixl::aarch64::Literal<uint32_t>* literal,
6528 uint64_t index_in_table) {
6529 uint32_t literal_offset = literal->GetOffset();
6530 uintptr_t address =
6531 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6532 uint8_t* data = code + literal_offset;
6533 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6534}
6535
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006536void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6537 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006538 const StringReference& string_reference = entry.first;
6539 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
6540 const auto it = jit_string_roots_.find(string_reference);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006541 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006542 uint64_t index_in_table = it->second;
6543 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006544 }
6545 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006546 const TypeReference& type_reference = entry.first;
6547 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
6548 const auto it = jit_class_roots_.find(type_reference);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006549 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006550 uint64_t index_in_table = it->second;
6551 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006552 }
6553}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006554
Alexandre Rames67555f72014-11-18 10:55:16 +00006555#undef __
6556#undef QUICK_ENTRY_POINT
6557
Alexandre Rames5319def2014-10-23 10:03:10 +01006558} // namespace arm64
6559} // namespace art