blob: 0c198b123dda71dd779118312bdee2b74e953956 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Vladimir Markof4f2daa2017-03-20 18:26:59 +000019#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000020#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080022#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010024#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080025#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010026#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080027#include "intrinsics.h"
28#include "intrinsics_arm64.h"
Vladimir Markof4f2daa2017-03-20 18:26:59 +000029#include "linker/arm64/relative_patcher_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010030#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000032#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010033#include "thread.h"
34#include "utils/arm64/assembler_arm64.h"
35#include "utils/assembler.h"
36#include "utils/stack_checks.h"
37
Scott Wakeling97c72b72016-06-24 16:19:36 +010038using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000039using vixl::ExactAssemblyScope;
40using vixl::CodeBufferCheckScope;
41using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010042
43#ifdef __
44#error "ARM64 Codegen VIXL macro-assembler macro already defined."
45#endif
46
Alexandre Rames5319def2014-10-23 10:03:10 +010047namespace art {
48
Roland Levillain22ccc3a2015-11-24 13:10:05 +000049template<class MirrorType>
50class GcRoot;
51
Alexandre Rames5319def2014-10-23 10:03:10 +010052namespace arm64 {
53
Alexandre Ramesbe919d92016-08-23 18:33:36 +010054using helpers::ARM64EncodableConstantOrRegister;
55using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080056using helpers::CPURegisterFrom;
57using helpers::DRegisterFrom;
58using helpers::FPRegisterFrom;
59using helpers::HeapOperand;
60using helpers::HeapOperandFrom;
61using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010062using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080063using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080064using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010065using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080066using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010067using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080068using helpers::LocationFrom;
69using helpers::OperandFromMemOperand;
70using helpers::OutputCPURegister;
71using helpers::OutputFPRegister;
72using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010073using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080074using helpers::RegisterFrom;
75using helpers::StackOperandFrom;
76using helpers::VIXLRegCodeFromART;
77using helpers::WRegisterFrom;
78using helpers::XRegisterFrom;
79
Alexandre Rames5319def2014-10-23 10:03:10 +010080static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000081// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080082// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
83// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000084static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010085
Vladimir Markof4f2daa2017-03-20 18:26:59 +000086// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
87// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
88// For the Baker read barrier implementation using link-generated thunks we need to split
89// the offset explicitly.
90constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
91
92// Flags controlling the use of link-time generated thunks for Baker read barriers.
Vladimir Markod1ef8732017-04-18 13:55:13 +010093constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
Vladimir Marko66d691d2017-04-07 17:53:39 +010094constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
Vladimir Markod1ef8732017-04-18 13:55:13 +010095constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
Vladimir Markof4f2daa2017-03-20 18:26:59 +000096
97// Some instructions have special requirements for a temporary, for example
98// LoadClass/kBssEntry and LoadString/kBssEntry for Baker read barrier require
99// temp that's not an R0 (to avoid an extra move) and Baker read barrier field
100// loads with large offsets need a fixed register to limit the number of link-time
101// thunks we generate. For these and similar cases, we want to reserve a specific
102// register that's neither callee-save nor an argument register. We choose x15.
103inline Location FixedTempLocation() {
104 return Location::RegisterLocation(x15.GetCode());
105}
106
Alexandre Rames5319def2014-10-23 10:03:10 +0100107inline Condition ARM64Condition(IfCondition cond) {
108 switch (cond) {
109 case kCondEQ: return eq;
110 case kCondNE: return ne;
111 case kCondLT: return lt;
112 case kCondLE: return le;
113 case kCondGT: return gt;
114 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700115 case kCondB: return lo;
116 case kCondBE: return ls;
117 case kCondA: return hi;
118 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100119 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000120 LOG(FATAL) << "Unreachable";
121 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100122}
123
Vladimir Markod6e069b2016-01-18 11:11:01 +0000124inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
125 // The ARM64 condition codes can express all the necessary branches, see the
126 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
127 // There is no dex instruction or HIR that would need the missing conditions
128 // "equal or unordered" or "not equal".
129 switch (cond) {
130 case kCondEQ: return eq;
131 case kCondNE: return ne /* unordered */;
132 case kCondLT: return gt_bias ? cc : lt /* unordered */;
133 case kCondLE: return gt_bias ? ls : le /* unordered */;
134 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
135 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
136 default:
137 LOG(FATAL) << "UNREACHABLE";
138 UNREACHABLE();
139 }
140}
141
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000142Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000143 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
144 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
145 // but we use the exact registers for clarity.
146 if (return_type == Primitive::kPrimFloat) {
147 return LocationFrom(s0);
148 } else if (return_type == Primitive::kPrimDouble) {
149 return LocationFrom(d0);
150 } else if (return_type == Primitive::kPrimLong) {
151 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100152 } else if (return_type == Primitive::kPrimVoid) {
153 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000154 } else {
155 return LocationFrom(w0);
156 }
157}
158
Alexandre Rames5319def2014-10-23 10:03:10 +0100159Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000160 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100161}
162
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100163// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
164#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700165#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100166
Zheng Xuda403092015-04-24 17:35:39 +0800167// Calculate memory accessing operand for save/restore live registers.
168static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100169 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800170 int64_t spill_offset,
171 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100172 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
173 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
174 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800175 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100176 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800177 codegen->GetNumberOfFloatingPointRegisters()));
178
Vladimir Marko804b03f2016-09-14 16:26:36 +0100179 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100180 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
181 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800182
183 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
184 UseScratchRegisterScope temps(masm);
185
186 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100187 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
188 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800189 int64_t reg_size = kXRegSizeInBytes;
190 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
191 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100192 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800193 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
194 // If the offset does not fit in the instruction's immediate field, use an alternate register
195 // to compute the base address(float point registers spill base address).
196 Register new_base = temps.AcquireSameSizeAs(base);
197 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
198 base = new_base;
199 spill_offset = -core_spill_size;
200 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
201 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
202 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
203 }
204
205 if (is_save) {
206 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
207 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
208 } else {
209 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
210 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
211 }
212}
213
214void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800215 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100216 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
217 for (uint32_t i : LowToHighBits(core_spills)) {
218 // If the register holds an object, update the stack mask.
219 if (locations->RegisterContainsObject(i)) {
220 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800221 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100222 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
223 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
224 saved_core_stack_offsets_[i] = stack_offset;
225 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800226 }
227
Vladimir Marko804b03f2016-09-14 16:26:36 +0100228 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
229 for (uint32_t i : LowToHighBits(fp_spills)) {
230 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
231 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
232 saved_fpu_stack_offsets_[i] = stack_offset;
233 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800234 }
235
Vladimir Marko804b03f2016-09-14 16:26:36 +0100236 SaveRestoreLiveRegistersHelper(codegen,
237 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800238 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
239}
240
241void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100242 SaveRestoreLiveRegistersHelper(codegen,
243 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800244 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
245}
246
Alexandre Rames5319def2014-10-23 10:03:10 +0100247class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
248 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000249 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100250
Alexandre Rames67555f72014-11-18 10:55:16 +0000251 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100252 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000253 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100254
Alexandre Rames5319def2014-10-23 10:03:10 +0100255 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000256 if (instruction_->CanThrowIntoCatchBlock()) {
257 // Live registers will be restored in the catch block if caught.
258 SaveLiveRegisters(codegen, instruction_->GetLocations());
259 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000260 // We're moving two locations to locations that could overlap, so we need a parallel
261 // move resolver.
262 InvokeRuntimeCallingConvention calling_convention;
263 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100264 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
265 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000266 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
267 ? kQuickThrowStringBounds
268 : kQuickThrowArrayBounds;
269 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100270 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800271 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100272 }
273
Alexandre Rames8158f282015-08-07 10:26:17 +0100274 bool IsFatal() const OVERRIDE { return true; }
275
Alexandre Rames9931f312015-06-19 14:47:01 +0100276 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
277
Alexandre Rames5319def2014-10-23 10:03:10 +0100278 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100279 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
280};
281
Alexandre Rames67555f72014-11-18 10:55:16 +0000282class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
283 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000284 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000285
286 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
287 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
288 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000289 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800290 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000291 }
292
Alexandre Rames8158f282015-08-07 10:26:17 +0100293 bool IsFatal() const OVERRIDE { return true; }
294
Alexandre Rames9931f312015-06-19 14:47:01 +0100295 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
296
Alexandre Rames67555f72014-11-18 10:55:16 +0000297 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
299};
300
301class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
302 public:
303 LoadClassSlowPathARM64(HLoadClass* cls,
304 HInstruction* at,
305 uint32_t dex_pc,
Vladimir Markoea4c1262017-02-06 19:59:33 +0000306 bool do_clinit,
307 vixl::aarch64::Register bss_entry_temp = vixl::aarch64::Register(),
308 vixl::aarch64::Label* bss_entry_adrp_label = nullptr)
309 : SlowPathCodeARM64(at),
310 cls_(cls),
311 dex_pc_(dex_pc),
312 do_clinit_(do_clinit),
313 bss_entry_temp_(bss_entry_temp),
314 bss_entry_adrp_label_(bss_entry_adrp_label) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000315 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
316 }
317
318 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000319 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000320 Location out = locations->Out();
321 constexpr bool call_saves_everything_except_r0_ip0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexandre Rames67555f72014-11-18 10:55:16 +0000322 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
323
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000324 InvokeRuntimeCallingConvention calling_convention;
325 // For HLoadClass/kBssEntry/kSaveEverything, the page address of the entry is in a temp
326 // register, make sure it's not clobbered by the call or by saving/restoring registers.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000327 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
328 bool is_load_class_bss_entry =
329 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000330 if (is_load_class_bss_entry) {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000331 DCHECK(bss_entry_temp_.IsValid());
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000332 DCHECK(!bss_entry_temp_.Is(calling_convention.GetRegisterAt(0)));
333 DCHECK(
334 !UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(bss_entry_temp_));
Vladimir Markoea4c1262017-02-06 19:59:33 +0000335 }
336
Alexandre Rames67555f72014-11-18 10:55:16 +0000337 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000338 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000339
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000340 dex::TypeIndex type_index = cls_->GetTypeIndex();
341 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000342 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
343 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000344 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800345 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100346 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800347 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100348 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800349 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000350
351 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 if (out.IsValid()) {
353 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000354 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000355 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000357 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000358 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000359 if (is_load_class_bss_entry) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000360 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000361 const DexFile& dex_file = cls_->GetDexFile();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000362 if (call_saves_everything_except_r0_ip0) {
363 // The class entry page address was preserved in bss_entry_temp_ thanks to kSaveEverything.
364 } else {
365 // For non-Baker read barrier, we need to re-calculate the address of the class entry page.
366 bss_entry_adrp_label_ = arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
367 arm64_codegen->EmitAdrpPlaceholder(bss_entry_adrp_label_, bss_entry_temp_);
368 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000369 vixl::aarch64::Label* strp_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +0000370 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label_);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000371 {
372 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
373 __ Bind(strp_label);
374 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Markoea4c1262017-02-06 19:59:33 +0000375 MemOperand(bss_entry_temp_, /* offset placeholder */ 0));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000376 }
377 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000378 __ B(GetExitLabel());
379 }
380
Alexandre Rames9931f312015-06-19 14:47:01 +0100381 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
382
Alexandre Rames67555f72014-11-18 10:55:16 +0000383 private:
384 // The class this slow path will load.
385 HLoadClass* const cls_;
386
Alexandre Rames67555f72014-11-18 10:55:16 +0000387 // The dex PC of `at_`.
388 const uint32_t dex_pc_;
389
390 // Whether to initialize the class.
391 const bool do_clinit_;
392
Vladimir Markoea4c1262017-02-06 19:59:33 +0000393 // For HLoadClass/kBssEntry, the temp register and the label of the ADRP where it was loaded.
394 vixl::aarch64::Register bss_entry_temp_;
395 vixl::aarch64::Label* bss_entry_adrp_label_;
396
Alexandre Rames67555f72014-11-18 10:55:16 +0000397 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
398};
399
Vladimir Markoaad75c62016-10-03 08:46:48 +0000400class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
401 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100402 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
403 : SlowPathCodeARM64(instruction),
404 temp_(temp),
405 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000406
407 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
408 LocationSummary* locations = instruction_->GetLocations();
409 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
410 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
411
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000412 InvokeRuntimeCallingConvention calling_convention;
413 // Make sure `temp_` is not clobbered by the call or by saving/restoring registers.
414 DCHECK(temp_.IsValid());
415 DCHECK(!temp_.Is(calling_convention.GetRegisterAt(0)));
416 DCHECK(!UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(temp_));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100417
Vladimir Markoaad75c62016-10-03 08:46:48 +0000418 __ Bind(GetEntryLabel());
419 SaveLiveRegisters(codegen, locations);
420
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000421 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
422 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000423 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
424 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
425 Primitive::Type type = instruction_->GetType();
426 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
427
428 RestoreLiveRegisters(codegen, locations);
429
430 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000431 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100432 if (!kUseReadBarrier || kUseBakerReadBarrier) {
433 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
434 } else {
435 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
436 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
437 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
438 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000439 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100440 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000441 {
442 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
443 __ Bind(strp_label);
444 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100445 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000446 }
447
448 __ B(GetExitLabel());
449 }
450
451 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
452
453 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100454 const Register temp_;
455 vixl::aarch64::Label* adrp_label_;
456
Vladimir Markoaad75c62016-10-03 08:46:48 +0000457 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
458};
459
Alexandre Rames5319def2014-10-23 10:03:10 +0100460class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
461 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000462 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100463
Alexandre Rames67555f72014-11-18 10:55:16 +0000464 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
465 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100466 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000467 if (instruction_->CanThrowIntoCatchBlock()) {
468 // Live registers will be restored in the catch block if caught.
469 SaveLiveRegisters(codegen, instruction_->GetLocations());
470 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000471 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
472 instruction_,
473 instruction_->GetDexPc(),
474 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800475 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100476 }
477
Alexandre Rames8158f282015-08-07 10:26:17 +0100478 bool IsFatal() const OVERRIDE { return true; }
479
Alexandre Rames9931f312015-06-19 14:47:01 +0100480 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
481
Alexandre Rames5319def2014-10-23 10:03:10 +0100482 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100483 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
484};
485
486class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
487 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100488 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000489 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100490
Alexandre Rames67555f72014-11-18 10:55:16 +0000491 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100492 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000493 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100494 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100495 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000496 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800497 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100498 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000499 if (successor_ == nullptr) {
500 __ B(GetReturnLabel());
501 } else {
502 __ B(arm64_codegen->GetLabelOf(successor_));
503 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100504 }
505
Scott Wakeling97c72b72016-06-24 16:19:36 +0100506 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100507 DCHECK(successor_ == nullptr);
508 return &return_label_;
509 }
510
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100511 HBasicBlock* GetSuccessor() const {
512 return successor_;
513 }
514
Alexandre Rames9931f312015-06-19 14:47:01 +0100515 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
516
Alexandre Rames5319def2014-10-23 10:03:10 +0100517 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100518 // If not null, the block to branch to after the suspend check.
519 HBasicBlock* const successor_;
520
521 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100522 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100523
524 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
525};
526
Alexandre Rames67555f72014-11-18 10:55:16 +0000527class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
528 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000529 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000530 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000531
532 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000533 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800534
Alexandre Rames3e69f162014-12-10 10:36:50 +0000535 DCHECK(instruction_->IsCheckCast()
536 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
537 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100538 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000539
Alexandre Rames67555f72014-11-18 10:55:16 +0000540 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000541
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000542 if (!is_fatal_) {
543 SaveLiveRegisters(codegen, locations);
544 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000545
546 // We're moving two locations to locations that could overlap, so we need a parallel
547 // move resolver.
548 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800549 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800550 LocationFrom(calling_convention.GetRegisterAt(0)),
551 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800552 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800553 LocationFrom(calling_convention.GetRegisterAt(1)),
554 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000555 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000556 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800557 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000558 Primitive::Type ret_type = instruction_->GetType();
559 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
560 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
561 } else {
562 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800563 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
564 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000565 }
566
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000567 if (!is_fatal_) {
568 RestoreLiveRegisters(codegen, locations);
569 __ B(GetExitLabel());
570 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000571 }
572
Alexandre Rames9931f312015-06-19 14:47:01 +0100573 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100574 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100575
Alexandre Rames67555f72014-11-18 10:55:16 +0000576 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000577 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000578
Alexandre Rames67555f72014-11-18 10:55:16 +0000579 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
580};
581
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700582class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
583 public:
Aart Bik42249c32016-01-07 15:33:50 -0800584 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000585 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700586
587 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800588 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700589 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100590 LocationSummary* locations = instruction_->GetLocations();
591 SaveLiveRegisters(codegen, locations);
592 InvokeRuntimeCallingConvention calling_convention;
593 __ Mov(calling_convention.GetRegisterAt(0),
594 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000595 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100596 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700597 }
598
Alexandre Rames9931f312015-06-19 14:47:01 +0100599 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
600
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700601 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700602 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
603};
604
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100605class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
606 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000607 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100608
609 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
610 LocationSummary* locations = instruction_->GetLocations();
611 __ Bind(GetEntryLabel());
612 SaveLiveRegisters(codegen, locations);
613
614 InvokeRuntimeCallingConvention calling_convention;
615 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
616 parallel_move.AddMove(
617 locations->InAt(0),
618 LocationFrom(calling_convention.GetRegisterAt(0)),
619 Primitive::kPrimNot,
620 nullptr);
621 parallel_move.AddMove(
622 locations->InAt(1),
623 LocationFrom(calling_convention.GetRegisterAt(1)),
624 Primitive::kPrimInt,
625 nullptr);
626 parallel_move.AddMove(
627 locations->InAt(2),
628 LocationFrom(calling_convention.GetRegisterAt(2)),
629 Primitive::kPrimNot,
630 nullptr);
631 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
632
633 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000634 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100635 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
636 RestoreLiveRegisters(codegen, locations);
637 __ B(GetExitLabel());
638 }
639
640 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
641
642 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100643 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
644};
645
Zheng Xu3927c8b2015-11-18 17:46:25 +0800646void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
647 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000648 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800649
650 // We are about to use the assembler to place literals directly. Make sure we have enough
651 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000652 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
653 num_entries * sizeof(int32_t),
654 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800655
656 __ Bind(&table_start_);
657 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
658 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100659 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800660 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100661 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800662 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
663 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
664 Literal<int32_t> literal(jump_offset);
665 __ place(&literal);
666 }
667}
668
Roland Levillain54f869e2017-03-06 13:54:11 +0000669// Abstract base class for read barrier slow paths marking a reference
670// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000671//
Roland Levillain54f869e2017-03-06 13:54:11 +0000672// Argument `entrypoint` must be a register location holding the read
673// barrier marking runtime entry point to be invoked.
674class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
675 protected:
676 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
677 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000678 DCHECK(kEmitCompilerReadBarrier);
679 }
680
Roland Levillain54f869e2017-03-06 13:54:11 +0000681 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000682
Roland Levillain54f869e2017-03-06 13:54:11 +0000683 // Generate assembly code calling the read barrier marking runtime
684 // entry point (ReadBarrierMarkRegX).
685 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000686 // No need to save live registers; it's taken care of by the
687 // entrypoint. Also, there is no need to update the stack mask,
688 // as this runtime call will not trigger a garbage collection.
689 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
690 DCHECK_NE(ref_.reg(), LR);
691 DCHECK_NE(ref_.reg(), WSP);
692 DCHECK_NE(ref_.reg(), WZR);
693 // IP0 is used internally by the ReadBarrierMarkRegX entry point
694 // as a temporary, it cannot be the entry point's input/output.
695 DCHECK_NE(ref_.reg(), IP0);
696 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
697 // "Compact" slow path, saving two moves.
698 //
699 // Instead of using the standard runtime calling convention (input
700 // and output in W0):
701 //
702 // W0 <- ref
703 // W0 <- ReadBarrierMark(W0)
704 // ref <- W0
705 //
706 // we just use rX (the register containing `ref`) as input and output
707 // of a dedicated entrypoint:
708 //
709 // rX <- ReadBarrierMarkRegX(rX)
710 //
711 if (entrypoint_.IsValid()) {
712 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
713 __ Blr(XRegisterFrom(entrypoint_));
714 } else {
715 // Entrypoint is not already loaded, load from the thread.
716 int32_t entry_point_offset =
717 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
718 // This runtime call does not require a stack map.
719 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
720 }
721 }
722
723 // The location (register) of the marked object reference.
724 const Location ref_;
725
726 // The location of the entrypoint if it is already loaded.
727 const Location entrypoint_;
728
Roland Levillain54f869e2017-03-06 13:54:11 +0000729 private:
730 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
731};
732
Alexandre Rames5319def2014-10-23 10:03:10 +0100733// Slow path marking an object reference `ref` during a read
734// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000735// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100736//
737// This means that after the execution of this slow path, `ref` will
738// always be up-to-date, but `obj.field` may not; i.e., after the
739// flip, `ref` will be a to-space reference, but `obj.field` will
740// probably still be a from-space reference (unless it gets updated by
741// another thread, or if another thread installed another object
742// reference (different from `ref`) in `obj.field`).
743//
744// If `entrypoint` is a valid location it is assumed to already be
745// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000746// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000747class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100748 public:
749 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
750 Location ref,
751 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000752 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100753 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100754 }
755
756 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
757
758 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000759 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100760 DCHECK(locations->CanCall());
761 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000762 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000763 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
764 << "Unexpected instruction in read barrier marking slow path: "
765 << instruction_->DebugName();
766
767 __ Bind(GetEntryLabel());
768 GenerateReadBarrierMarkRuntimeCall(codegen);
769 __ B(GetExitLabel());
770 }
771
772 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000773 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
774};
775
Roland Levillain54f869e2017-03-06 13:54:11 +0000776// Slow path loading `obj`'s lock word, loading a reference from
777// object `*(obj + offset + (index << scale_factor))` into `ref`, and
778// marking `ref` if `obj` is gray according to the lock word (Baker
779// read barrier). The field `obj.field` in the object `obj` holding
780// this reference does not get updated by this slow path after marking
781// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
782// below for that).
783//
784// This means that after the execution of this slow path, `ref` will
785// always be up-to-date, but `obj.field` may not; i.e., after the
786// flip, `ref` will be a to-space reference, but `obj.field` will
787// probably still be a from-space reference (unless it gets updated by
788// another thread, or if another thread installed another object
789// reference (different from `ref`) in `obj.field`).
790//
791// Argument `entrypoint` must be a register location holding the read
792// barrier marking runtime entry point to be invoked.
793class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
794 public:
795 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
796 Location ref,
797 Register obj,
798 uint32_t offset,
799 Location index,
800 size_t scale_factor,
801 bool needs_null_check,
802 bool use_load_acquire,
803 Register temp,
804 Location entrypoint)
805 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
806 obj_(obj),
807 offset_(offset),
808 index_(index),
809 scale_factor_(scale_factor),
810 needs_null_check_(needs_null_check),
811 use_load_acquire_(use_load_acquire),
812 temp_(temp) {
813 DCHECK(kEmitCompilerReadBarrier);
814 DCHECK(kUseBakerReadBarrier);
815 }
816
817 const char* GetDescription() const OVERRIDE {
818 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
819 }
820
821 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
822 LocationSummary* locations = instruction_->GetLocations();
823 DCHECK(locations->CanCall());
824 DCHECK(ref_.IsRegister()) << ref_;
825 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
826 DCHECK(obj_.IsW());
827 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100828 DCHECK(instruction_->IsInstanceFieldGet() ||
829 instruction_->IsStaticFieldGet() ||
830 instruction_->IsArrayGet() ||
831 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100832 instruction_->IsInstanceOf() ||
833 instruction_->IsCheckCast() ||
834 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
835 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
836 << "Unexpected instruction in read barrier marking slow path: "
837 << instruction_->DebugName();
838 // The read barrier instrumentation of object ArrayGet
839 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000840 // instruction.
841 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000842 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
843
Roland Levillain54f869e2017-03-06 13:54:11 +0000844 // Temporary register `temp_`, used to store the lock word, must
845 // not be IP0 nor IP1, as we may use them to emit the reference
846 // load (in the call to GenerateRawReferenceLoad below), and we
847 // need the lock word to still be in `temp_` after the reference
848 // load.
849 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
850 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
851
Alexandre Rames5319def2014-10-23 10:03:10 +0100852 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000853
854 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
855 // inserted after the original load. However, in fast path based
856 // Baker's read barriers, we need to perform the load of
857 // mirror::Object::monitor_ *before* the original reference load.
858 // This load-load ordering is required by the read barrier.
859 // The fast path/slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100860 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000861 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
862 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
863 // HeapReference<mirror::Object> ref = *src; // Original reference load.
864 // bool is_gray = (rb_state == ReadBarrier::GrayState());
865 // if (is_gray) {
866 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
867 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000868 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000869 // Note: the original implementation in ReadBarrier::Barrier is
870 // slightly more complex as it performs additional checks that we do
871 // not do here for performance reasons.
872
873 // /* int32_t */ monitor = obj->monitor_
874 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
875 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
876 if (needs_null_check_) {
877 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100878 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000879 // /* LockWord */ lock_word = LockWord(monitor)
880 static_assert(sizeof(LockWord) == sizeof(int32_t),
881 "art::LockWord and int32_t have different sizes.");
882
883 // Introduce a dependency on the lock_word including rb_state,
884 // to prevent load-load reordering, and without using
885 // a memory barrier (which would be more expensive).
886 // `obj` is unchanged by this operation, but its value now depends
887 // on `temp`.
888 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
889
890 // The actual reference load.
891 // A possible implicit null check has already been handled above.
892 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
893 arm64_codegen->GenerateRawReferenceLoad(instruction_,
894 ref_,
895 obj_,
896 offset_,
897 index_,
898 scale_factor_,
899 /* needs_null_check */ false,
900 use_load_acquire_);
901
902 // Mark the object `ref` when `obj` is gray.
903 //
904 // if (rb_state == ReadBarrier::GrayState())
905 // ref = ReadBarrier::Mark(ref);
906 //
907 // Given the numeric representation, it's enough to check the low bit of the rb_state.
908 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
909 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
910 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
911 GenerateReadBarrierMarkRuntimeCall(codegen);
912
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000913 __ B(GetExitLabel());
914 }
915
916 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000917 // The register containing the object holding the marked object reference field.
918 Register obj_;
919 // The offset, index and scale factor to access the reference in `obj_`.
920 uint32_t offset_;
921 Location index_;
922 size_t scale_factor_;
923 // Is a null check required?
924 bool needs_null_check_;
925 // Should this reference load use Load-Acquire semantics?
926 bool use_load_acquire_;
927 // A temporary register used to hold the lock word of `obj_`.
928 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000929
Roland Levillain54f869e2017-03-06 13:54:11 +0000930 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000931};
932
Roland Levillain54f869e2017-03-06 13:54:11 +0000933// Slow path loading `obj`'s lock word, loading a reference from
934// object `*(obj + offset + (index << scale_factor))` into `ref`, and
935// marking `ref` if `obj` is gray according to the lock word (Baker
936// read barrier). If needed, this slow path also atomically updates
937// the field `obj.field` in the object `obj` holding this reference
938// after marking (contrary to
939// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
940// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100941//
942// This means that after the execution of this slow path, both `ref`
943// and `obj.field` will be up-to-date; i.e., after the flip, both will
944// hold the same to-space reference (unless another thread installed
945// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000946//
Roland Levillain54f869e2017-03-06 13:54:11 +0000947// Argument `entrypoint` must be a register location holding the read
948// barrier marking runtime entry point to be invoked.
949class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
950 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100951 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000952 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(HInstruction* instruction,
953 Location ref,
954 Register obj,
955 uint32_t offset,
956 Location index,
957 size_t scale_factor,
958 bool needs_null_check,
959 bool use_load_acquire,
960 Register temp,
961 Location entrypoint)
962 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100963 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000964 offset_(offset),
965 index_(index),
966 scale_factor_(scale_factor),
967 needs_null_check_(needs_null_check),
968 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000969 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100970 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000971 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100972 }
973
974 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000975 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100976 }
977
978 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
979 LocationSummary* locations = instruction_->GetLocations();
980 Register ref_reg = WRegisterFrom(ref_);
981 DCHECK(locations->CanCall());
982 DCHECK(ref_.IsRegister()) << ref_;
983 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000984 DCHECK(obj_.IsW());
985 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
986
987 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100988 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
989 << "Unexpected instruction in read barrier marking and field updating slow path: "
990 << instruction_->DebugName();
991 DCHECK(instruction_->GetLocations()->Intrinsified());
992 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000993 DCHECK_EQ(offset_, 0u);
994 DCHECK_EQ(scale_factor_, 0u);
995 DCHECK_EQ(use_load_acquire_, false);
996 // The location of the offset of the marked reference field within `obj_`.
997 Location field_offset = index_;
998 DCHECK(field_offset.IsRegister()) << field_offset;
999
1000 // Temporary register `temp_`, used to store the lock word, must
1001 // not be IP0 nor IP1, as we may use them to emit the reference
1002 // load (in the call to GenerateRawReferenceLoad below), and we
1003 // need the lock word to still be in `temp_` after the reference
1004 // load.
1005 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1006 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001007
1008 __ Bind(GetEntryLabel());
1009
Roland Levillain54f869e2017-03-06 13:54:11 +00001010 // /* int32_t */ monitor = obj->monitor_
1011 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1012 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
1013 if (needs_null_check_) {
1014 codegen->MaybeRecordImplicitNullCheck(instruction_);
1015 }
1016 // /* LockWord */ lock_word = LockWord(monitor)
1017 static_assert(sizeof(LockWord) == sizeof(int32_t),
1018 "art::LockWord and int32_t have different sizes.");
1019
1020 // Introduce a dependency on the lock_word including rb_state,
1021 // to prevent load-load reordering, and without using
1022 // a memory barrier (which would be more expensive).
1023 // `obj` is unchanged by this operation, but its value now depends
1024 // on `temp`.
1025 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
1026
1027 // The actual reference load.
1028 // A possible implicit null check has already been handled above.
1029 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1030 arm64_codegen->GenerateRawReferenceLoad(instruction_,
1031 ref_,
1032 obj_,
1033 offset_,
1034 index_,
1035 scale_factor_,
1036 /* needs_null_check */ false,
1037 use_load_acquire_);
1038
1039 // Mark the object `ref` when `obj` is gray.
1040 //
1041 // if (rb_state == ReadBarrier::GrayState())
1042 // ref = ReadBarrier::Mark(ref);
1043 //
1044 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1045 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1046 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1047 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1048
1049 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001050 // Note that we cannot use IP to save the old reference, as IP is
1051 // used internally by the ReadBarrierMarkRegX entry point, and we
1052 // need the old reference after the call to that entry point.
1053 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1054 __ Mov(temp_.W(), ref_reg);
1055
Roland Levillain54f869e2017-03-06 13:54:11 +00001056 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001057
1058 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001059 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001060 //
1061 // Note that this field could also hold a different object, if
1062 // another thread had concurrently changed it. In that case, the
1063 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1064 // (CAS) operation below would abort the CAS, leaving the field
1065 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001066 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001067 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001068
1069 // Update the the holder's field atomically. This may fail if
1070 // mutator updates before us, but it's OK. This is achieved
1071 // using a strong compare-and-set (CAS) operation with relaxed
1072 // memory synchronization ordering, where the expected value is
1073 // the old reference and the desired value is the new reference.
1074
1075 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1076 UseScratchRegisterScope temps(masm);
1077
1078 // Convenience aliases.
1079 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001080 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001081 Register expected = temp_.W();
1082 Register value = ref_reg;
1083 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1084 Register tmp_value = temps.AcquireW(); // Value in memory.
1085
1086 __ Add(tmp_ptr, base.X(), Operand(offset));
1087
1088 if (kPoisonHeapReferences) {
1089 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1090 if (value.Is(expected)) {
1091 // Do not poison `value`, as it is the same register as
1092 // `expected`, which has just been poisoned.
1093 } else {
1094 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1095 }
1096 }
1097
1098 // do {
1099 // tmp_value = [tmp_ptr] - expected;
1100 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1101
Roland Levillain24a4d112016-10-26 13:10:46 +01001102 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001103 __ Bind(&loop_head);
1104 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1105 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001106 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001107 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1108 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001109 __ B(&exit_loop);
1110 __ Bind(&comparison_failed);
1111 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001112 __ Bind(&exit_loop);
1113
1114 if (kPoisonHeapReferences) {
1115 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1116 if (value.Is(expected)) {
1117 // Do not unpoison `value`, as it is the same register as
1118 // `expected`, which has just been unpoisoned.
1119 } else {
1120 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1121 }
1122 }
1123
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001124 __ B(GetExitLabel());
1125 }
1126
1127 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001128 // The register containing the object holding the marked object reference field.
1129 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001130 // The offset, index and scale factor to access the reference in `obj_`.
1131 uint32_t offset_;
1132 Location index_;
1133 size_t scale_factor_;
1134 // Is a null check required?
1135 bool needs_null_check_;
1136 // Should this reference load use Load-Acquire semantics?
1137 bool use_load_acquire_;
1138 // A temporary register used to hold the lock word of `obj_`; and
1139 // also to hold the original reference value, when the reference is
1140 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001141 const Register temp_;
1142
Roland Levillain54f869e2017-03-06 13:54:11 +00001143 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001144};
1145
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001146// Slow path generating a read barrier for a heap reference.
1147class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1148 public:
1149 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1150 Location out,
1151 Location ref,
1152 Location obj,
1153 uint32_t offset,
1154 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001155 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001156 out_(out),
1157 ref_(ref),
1158 obj_(obj),
1159 offset_(offset),
1160 index_(index) {
1161 DCHECK(kEmitCompilerReadBarrier);
1162 // If `obj` is equal to `out` or `ref`, it means the initial object
1163 // has been overwritten by (or after) the heap object reference load
1164 // to be instrumented, e.g.:
1165 //
1166 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001167 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001168 //
1169 // In that case, we have lost the information about the original
1170 // object, and the emitted read barrier cannot work properly.
1171 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1172 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1173 }
1174
1175 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1176 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1177 LocationSummary* locations = instruction_->GetLocations();
1178 Primitive::Type type = Primitive::kPrimNot;
1179 DCHECK(locations->CanCall());
1180 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001181 DCHECK(instruction_->IsInstanceFieldGet() ||
1182 instruction_->IsStaticFieldGet() ||
1183 instruction_->IsArrayGet() ||
1184 instruction_->IsInstanceOf() ||
1185 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001186 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001187 << "Unexpected instruction in read barrier for heap reference slow path: "
1188 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001189 // The read barrier instrumentation of object ArrayGet
1190 // instructions does not support the HIntermediateAddress
1191 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001192 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001193 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001194
1195 __ Bind(GetEntryLabel());
1196
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001197 SaveLiveRegisters(codegen, locations);
1198
1199 // We may have to change the index's value, but as `index_` is a
1200 // constant member (like other "inputs" of this slow path),
1201 // introduce a copy of it, `index`.
1202 Location index = index_;
1203 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001204 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001205 if (instruction_->IsArrayGet()) {
1206 // Compute the actual memory offset and store it in `index`.
1207 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
1208 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1209 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1210 // We are about to change the value of `index_reg` (see the
1211 // calls to vixl::MacroAssembler::Lsl and
1212 // vixl::MacroAssembler::Mov below), but it has
1213 // not been saved by the previous call to
1214 // art::SlowPathCode::SaveLiveRegisters, as it is a
1215 // callee-save register --
1216 // art::SlowPathCode::SaveLiveRegisters does not consider
1217 // callee-save registers, as it has been designed with the
1218 // assumption that callee-save registers are supposed to be
1219 // handled by the called function. So, as a callee-save
1220 // register, `index_reg` _would_ eventually be saved onto
1221 // the stack, but it would be too late: we would have
1222 // changed its value earlier. Therefore, we manually save
1223 // it here into another freely available register,
1224 // `free_reg`, chosen of course among the caller-save
1225 // registers (as a callee-save `free_reg` register would
1226 // exhibit the same problem).
1227 //
1228 // Note we could have requested a temporary register from
1229 // the register allocator instead; but we prefer not to, as
1230 // this is a slow path, and we know we can find a
1231 // caller-save register that is available.
1232 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1233 __ Mov(free_reg.W(), index_reg);
1234 index_reg = free_reg;
1235 index = LocationFrom(index_reg);
1236 } else {
1237 // The initial register stored in `index_` has already been
1238 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1239 // (as it is not a callee-save register), so we can freely
1240 // use it.
1241 }
1242 // Shifting the index value contained in `index_reg` by the scale
1243 // factor (2) cannot overflow in practice, as the runtime is
1244 // unable to allocate object arrays with a size larger than
1245 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1246 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1247 static_assert(
1248 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1249 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1250 __ Add(index_reg, index_reg, Operand(offset_));
1251 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001252 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1253 // intrinsics, `index_` is not shifted by a scale factor of 2
1254 // (as in the case of ArrayGet), as it is actually an offset
1255 // to an object field within an object.
1256 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001257 DCHECK(instruction_->GetLocations()->Intrinsified());
1258 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1259 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1260 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001261 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001262 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001263 }
1264 }
1265
1266 // We're moving two or three locations to locations that could
1267 // overlap, so we need a parallel move resolver.
1268 InvokeRuntimeCallingConvention calling_convention;
1269 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1270 parallel_move.AddMove(ref_,
1271 LocationFrom(calling_convention.GetRegisterAt(0)),
1272 type,
1273 nullptr);
1274 parallel_move.AddMove(obj_,
1275 LocationFrom(calling_convention.GetRegisterAt(1)),
1276 type,
1277 nullptr);
1278 if (index.IsValid()) {
1279 parallel_move.AddMove(index,
1280 LocationFrom(calling_convention.GetRegisterAt(2)),
1281 Primitive::kPrimInt,
1282 nullptr);
1283 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1284 } else {
1285 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1286 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1287 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001288 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001289 instruction_,
1290 instruction_->GetDexPc(),
1291 this);
1292 CheckEntrypointTypes<
1293 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1294 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1295
1296 RestoreLiveRegisters(codegen, locations);
1297
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001298 __ B(GetExitLabel());
1299 }
1300
1301 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1302
1303 private:
1304 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001305 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1306 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001307 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1308 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1309 return Register(VIXLRegCodeFromART(i), kXRegSize);
1310 }
1311 }
1312 // We shall never fail to find a free caller-save register, as
1313 // there are more than two core caller-save registers on ARM64
1314 // (meaning it is possible to find one which is different from
1315 // `ref` and `obj`).
1316 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1317 LOG(FATAL) << "Could not find a free register";
1318 UNREACHABLE();
1319 }
1320
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001321 const Location out_;
1322 const Location ref_;
1323 const Location obj_;
1324 const uint32_t offset_;
1325 // An additional location containing an index to an array.
1326 // Only used for HArrayGet and the UnsafeGetObject &
1327 // UnsafeGetObjectVolatile intrinsics.
1328 const Location index_;
1329
1330 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1331};
1332
1333// Slow path generating a read barrier for a GC root.
1334class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1335 public:
1336 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001337 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001338 DCHECK(kEmitCompilerReadBarrier);
1339 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001340
1341 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1342 LocationSummary* locations = instruction_->GetLocations();
1343 Primitive::Type type = Primitive::kPrimNot;
1344 DCHECK(locations->CanCall());
1345 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001346 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1347 << "Unexpected instruction in read barrier for GC root slow path: "
1348 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001349
1350 __ Bind(GetEntryLabel());
1351 SaveLiveRegisters(codegen, locations);
1352
1353 InvokeRuntimeCallingConvention calling_convention;
1354 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1355 // The argument of the ReadBarrierForRootSlow is not a managed
1356 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1357 // thus we need a 64-bit move here, and we cannot use
1358 //
1359 // arm64_codegen->MoveLocation(
1360 // LocationFrom(calling_convention.GetRegisterAt(0)),
1361 // root_,
1362 // type);
1363 //
1364 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1365 // reference type (`Primitive::kPrimNot`).
1366 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001367 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001368 instruction_,
1369 instruction_->GetDexPc(),
1370 this);
1371 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1372 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1373
1374 RestoreLiveRegisters(codegen, locations);
1375 __ B(GetExitLabel());
1376 }
1377
1378 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1379
1380 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001381 const Location out_;
1382 const Location root_;
1383
1384 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1385};
1386
Alexandre Rames5319def2014-10-23 10:03:10 +01001387#undef __
1388
1389Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
1390 Location next_location;
1391 if (type == Primitive::kPrimVoid) {
1392 LOG(FATAL) << "Unreachable type " << type;
1393 }
1394
1395 if (Primitive::IsFloatingPointType(type) &&
1396 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001397 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1398 } else if (!Primitive::IsFloatingPointType(type) &&
1399 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1400 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1401 } else {
1402 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001403 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1404 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001405 }
1406
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001407 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001408 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001409 return next_location;
1410}
1411
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001412Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001413 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001414}
1415
Serban Constantinescu579885a2015-02-22 20:51:33 +00001416CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1417 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001418 const CompilerOptions& compiler_options,
1419 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001420 : CodeGenerator(graph,
1421 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001422 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001423 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001424 callee_saved_core_registers.GetList(),
1425 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001426 compiler_options,
1427 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001428 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001429 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001430 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001431 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001432 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001433 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001434 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001435 uint32_literals_(std::less<uint32_t>(),
1436 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001437 uint64_literals_(std::less<uint64_t>(),
1438 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001439 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1440 boot_image_string_patches_(StringReferenceValueComparator(),
1441 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1442 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001443 boot_image_type_patches_(TypeReferenceValueComparator(),
1444 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1445 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001446 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001447 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001448 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001449 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1450 jit_class_patches_(TypeReferenceValueComparator(),
1451 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001452 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001453 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001454}
Alexandre Rames5319def2014-10-23 10:03:10 +01001455
Alexandre Rames67555f72014-11-18 10:55:16 +00001456#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001457
Zheng Xu3927c8b2015-11-18 17:46:25 +08001458void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001459 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001460 jump_table->EmitTable(this);
1461 }
1462}
1463
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001464void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001465 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001466 // Ensure we emit the literal pool.
1467 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001468
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001469 CodeGenerator::Finalize(allocator);
1470}
1471
Zheng Xuad4450e2015-04-17 18:48:56 +08001472void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1473 // Note: There are 6 kinds of moves:
1474 // 1. constant -> GPR/FPR (non-cycle)
1475 // 2. constant -> stack (non-cycle)
1476 // 3. GPR/FPR -> GPR/FPR
1477 // 4. GPR/FPR -> stack
1478 // 5. stack -> GPR/FPR
1479 // 6. stack -> stack (non-cycle)
1480 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1481 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1482 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1483 // dependency.
1484 vixl_temps_.Open(GetVIXLAssembler());
1485}
1486
1487void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1488 vixl_temps_.Close();
1489}
1490
1491Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001492 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1493 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1494 || kind == Location::kSIMDStackSlot);
1495 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1496 ? Location::kFpuRegister
1497 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001498 Location scratch = GetScratchLocation(kind);
1499 if (!scratch.Equals(Location::NoLocation())) {
1500 return scratch;
1501 }
1502 // Allocate from VIXL temp registers.
1503 if (kind == Location::kRegister) {
1504 scratch = LocationFrom(vixl_temps_.AcquireX());
1505 } else {
1506 DCHECK(kind == Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001507 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1508 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1509 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001510 }
1511 AddScratchLocation(scratch);
1512 return scratch;
1513}
1514
1515void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1516 if (loc.IsRegister()) {
1517 vixl_temps_.Release(XRegisterFrom(loc));
1518 } else {
1519 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001520 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001521 }
1522 RemoveScratchLocation(loc);
1523}
1524
Alexandre Rames3e69f162014-12-10 10:36:50 +00001525void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001526 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001527 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001528}
1529
Alexandre Rames5319def2014-10-23 10:03:10 +01001530void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001531 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001532 __ Bind(&frame_entry_label_);
1533
Serban Constantinescu02164b32014-11-13 14:05:07 +00001534 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1535 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001536 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001537 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001538 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001539 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001540 {
1541 // Ensure that between load and RecordPcInfo there are no pools emitted.
1542 ExactAssemblyScope eas(GetVIXLAssembler(),
1543 kInstructionSize,
1544 CodeBufferCheckScope::kExactSize);
1545 __ ldr(wzr, MemOperand(temp, 0));
1546 RecordPcInfo(nullptr, 0);
1547 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001548 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001549
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001550 if (!HasEmptyFrame()) {
1551 int frame_size = GetFrameSize();
1552 // Stack layout:
1553 // sp[frame_size - 8] : lr.
1554 // ... : other preserved core registers.
1555 // ... : other preserved fp registers.
1556 // ... : reserved frame space.
1557 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001558
1559 // Save the current method if we need it. Note that we do not
1560 // do this in HCurrentMethod, as the instruction might have been removed
1561 // in the SSA graph.
1562 if (RequiresCurrentMethod()) {
1563 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001564 } else {
1565 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001566 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001567 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001568 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1569 frame_size - GetCoreSpillSize());
1570 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1571 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001572
1573 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1574 // Initialize should_deoptimize flag to 0.
1575 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1576 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1577 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001578 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001579}
1580
1581void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001582 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001583 if (!HasEmptyFrame()) {
1584 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001585 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1586 frame_size - FrameEntrySpillSize());
1587 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1588 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001589 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001590 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001591 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001592 __ Ret();
1593 GetAssembler()->cfi().RestoreState();
1594 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001595}
1596
Scott Wakeling97c72b72016-06-24 16:19:36 +01001597CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001598 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001599 return CPURegList(CPURegister::kRegister, kXRegSize,
1600 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001601}
1602
Scott Wakeling97c72b72016-06-24 16:19:36 +01001603CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001604 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1605 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001606 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1607 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001608}
1609
Alexandre Rames5319def2014-10-23 10:03:10 +01001610void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1611 __ Bind(GetLabelOf(block));
1612}
1613
Calin Juravle175dc732015-08-25 15:42:32 +01001614void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1615 DCHECK(location.IsRegister());
1616 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1617}
1618
Calin Juravlee460d1d2015-09-29 04:52:17 +01001619void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1620 if (location.IsRegister()) {
1621 locations->AddTemp(location);
1622 } else {
1623 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1624 }
1625}
1626
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001627void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001628 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001629 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001630 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001631 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001632 if (value_can_be_null) {
1633 __ Cbz(value, &done);
1634 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001635 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001636 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001637 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001638 if (value_can_be_null) {
1639 __ Bind(&done);
1640 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001641}
1642
David Brazdil58282f42016-01-14 12:45:10 +00001643void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001644 // Blocked core registers:
1645 // lr : Runtime reserved.
1646 // tr : Runtime reserved.
1647 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1648 // ip1 : VIXL core temp.
1649 // ip0 : VIXL core temp.
1650 //
1651 // Blocked fp registers:
1652 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001653 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1654 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001655 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001656 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001657 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001658
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001659 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001660 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001661 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001662 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001663
David Brazdil58282f42016-01-14 12:45:10 +00001664 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001665 // Stubs do not save callee-save floating point registers. If the graph
1666 // is debuggable, we need to deal with these registers differently. For
1667 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001668 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1669 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001670 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001671 }
1672 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001673}
1674
Alexandre Rames3e69f162014-12-10 10:36:50 +00001675size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1676 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1677 __ Str(reg, MemOperand(sp, stack_index));
1678 return kArm64WordSize;
1679}
1680
1681size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1682 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1683 __ Ldr(reg, MemOperand(sp, stack_index));
1684 return kArm64WordSize;
1685}
1686
1687size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1688 FPRegister reg = FPRegister(reg_id, kDRegSize);
1689 __ Str(reg, MemOperand(sp, stack_index));
1690 return kArm64WordSize;
1691}
1692
1693size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1694 FPRegister reg = FPRegister(reg_id, kDRegSize);
1695 __ Ldr(reg, MemOperand(sp, stack_index));
1696 return kArm64WordSize;
1697}
1698
Alexandre Rames5319def2014-10-23 10:03:10 +01001699void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001700 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001701}
1702
1703void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001704 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001705}
1706
Alexandre Rames67555f72014-11-18 10:55:16 +00001707void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001708 if (constant->IsIntConstant()) {
1709 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1710 } else if (constant->IsLongConstant()) {
1711 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1712 } else if (constant->IsNullConstant()) {
1713 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001714 } else if (constant->IsFloatConstant()) {
1715 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1716 } else {
1717 DCHECK(constant->IsDoubleConstant());
1718 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1719 }
1720}
1721
Alexandre Rames3e69f162014-12-10 10:36:50 +00001722
1723static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1724 DCHECK(constant.IsConstant());
1725 HConstant* cst = constant.GetConstant();
1726 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001727 // Null is mapped to a core W register, which we associate with kPrimInt.
1728 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001729 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1730 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1731 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1732}
1733
Roland Levillain558dea12017-01-27 19:40:44 +00001734// Allocate a scratch register from the VIXL pool, querying first into
1735// the floating-point register pool, and then the the core register
1736// pool. This is essentially a reimplementation of
1737// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1738// using a different allocation strategy.
1739static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1740 vixl::aarch64::UseScratchRegisterScope* temps,
1741 int size_in_bits) {
1742 return masm->GetScratchFPRegisterList()->IsEmpty()
1743 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1744 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1745}
1746
Calin Juravlee460d1d2015-09-29 04:52:17 +01001747void CodeGeneratorARM64::MoveLocation(Location destination,
1748 Location source,
1749 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001750 if (source.Equals(destination)) {
1751 return;
1752 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001753
1754 // A valid move can always be inferred from the destination and source
1755 // locations. When moving from and to a register, the argument type can be
1756 // used to generate 32bit instead of 64bit moves. In debug mode we also
1757 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001758 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001759
1760 if (destination.IsRegister() || destination.IsFpuRegister()) {
1761 if (unspecified_type) {
1762 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1763 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001764 (src_cst != nullptr && (src_cst->IsIntConstant()
1765 || src_cst->IsFloatConstant()
1766 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001767 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001768 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001769 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001770 // If the source is a double stack slot or a 64bit constant, a 64bit
1771 // type is appropriate. Else the source is a register, and since the
1772 // type has not been specified, we chose a 64bit type to force a 64bit
1773 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001774 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001775 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001776 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001777 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1778 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1779 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001780 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1781 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1782 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001783 } else if (source.IsSIMDStackSlot()) {
1784 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001785 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001786 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001787 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001788 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001789 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001790 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001791 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001792 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001793 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1794 ? Primitive::kPrimLong
1795 : Primitive::kPrimInt;
1796 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1797 }
1798 } else {
1799 DCHECK(source.IsFpuRegister());
1800 if (destination.IsRegister()) {
1801 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1802 ? Primitive::kPrimDouble
1803 : Primitive::kPrimFloat;
1804 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1805 } else {
1806 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001807 if (GetGraph()->HasSIMD()) {
1808 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1809 } else {
1810 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1811 }
1812 }
1813 }
1814 } else if (destination.IsSIMDStackSlot()) {
1815 if (source.IsFpuRegister()) {
1816 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1817 } else {
1818 DCHECK(source.IsSIMDStackSlot());
1819 UseScratchRegisterScope temps(GetVIXLAssembler());
1820 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1821 Register temp = temps.AcquireX();
1822 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1823 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1824 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1825 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1826 } else {
1827 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1828 __ Ldr(temp, StackOperandFrom(source));
1829 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001830 }
1831 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001832 } else { // The destination is not a register. It must be a stack slot.
1833 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1834 if (source.IsRegister() || source.IsFpuRegister()) {
1835 if (unspecified_type) {
1836 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001837 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001838 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001839 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001840 }
1841 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001842 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1843 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1844 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001845 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001846 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1847 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001848 UseScratchRegisterScope temps(GetVIXLAssembler());
1849 HConstant* src_cst = source.GetConstant();
1850 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001851 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001852 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1853 ? Register(xzr)
1854 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001855 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001856 if (src_cst->IsIntConstant()) {
1857 temp = temps.AcquireW();
1858 } else if (src_cst->IsLongConstant()) {
1859 temp = temps.AcquireX();
1860 } else if (src_cst->IsFloatConstant()) {
1861 temp = temps.AcquireS();
1862 } else {
1863 DCHECK(src_cst->IsDoubleConstant());
1864 temp = temps.AcquireD();
1865 }
1866 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001867 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001868 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001869 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001870 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001871 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001872 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001873 // Use any scratch register (a core or a floating-point one)
1874 // from VIXL scratch register pools as a temporary.
1875 //
1876 // We used to only use the FP scratch register pool, but in some
1877 // rare cases the only register from this pool (D31) would
1878 // already be used (e.g. within a ParallelMove instruction, when
1879 // a move is blocked by a another move requiring a scratch FP
1880 // register, which would reserve D31). To prevent this issue, we
1881 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001882 //
1883 // Also, we start by asking for a FP scratch register first, as the
1884 // demand of scratch core registers is higher. This is why we
1885 // use AcquireFPOrCoreCPURegisterOfSize instead of
1886 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1887 // allocates core scratch registers first.
1888 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1889 GetVIXLAssembler(),
1890 &temps,
1891 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001892 __ Ldr(temp, StackOperandFrom(source));
1893 __ Str(temp, StackOperandFrom(destination));
1894 }
1895 }
1896}
1897
1898void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001899 CPURegister dst,
1900 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001901 switch (type) {
1902 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001903 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001904 break;
1905 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001906 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001907 break;
1908 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001909 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001910 break;
1911 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001912 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001913 break;
1914 case Primitive::kPrimInt:
1915 case Primitive::kPrimNot:
1916 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001917 case Primitive::kPrimFloat:
1918 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001919 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001920 __ Ldr(dst, src);
1921 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001922 case Primitive::kPrimVoid:
1923 LOG(FATAL) << "Unreachable type " << type;
1924 }
1925}
1926
Calin Juravle77520bc2015-01-12 18:45:46 +00001927void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001928 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001929 const MemOperand& src,
1930 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001931 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001932 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001933 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001934 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001935
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001936 DCHECK(!src.IsPreIndex());
1937 DCHECK(!src.IsPostIndex());
1938
1939 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001940 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001941 {
1942 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1943 MemOperand base = MemOperand(temp_base);
1944 switch (type) {
1945 case Primitive::kPrimBoolean:
1946 {
1947 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1948 __ ldarb(Register(dst), base);
1949 if (needs_null_check) {
1950 MaybeRecordImplicitNullCheck(instruction);
1951 }
1952 }
1953 break;
1954 case Primitive::kPrimByte:
1955 {
1956 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1957 __ ldarb(Register(dst), base);
1958 if (needs_null_check) {
1959 MaybeRecordImplicitNullCheck(instruction);
1960 }
1961 }
1962 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1963 break;
1964 case Primitive::kPrimChar:
1965 {
1966 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1967 __ ldarh(Register(dst), base);
1968 if (needs_null_check) {
1969 MaybeRecordImplicitNullCheck(instruction);
1970 }
1971 }
1972 break;
1973 case Primitive::kPrimShort:
1974 {
1975 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1976 __ ldarh(Register(dst), base);
1977 if (needs_null_check) {
1978 MaybeRecordImplicitNullCheck(instruction);
1979 }
1980 }
1981 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1982 break;
1983 case Primitive::kPrimInt:
1984 case Primitive::kPrimNot:
1985 case Primitive::kPrimLong:
1986 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
1987 {
1988 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1989 __ ldar(Register(dst), base);
1990 if (needs_null_check) {
1991 MaybeRecordImplicitNullCheck(instruction);
1992 }
1993 }
1994 break;
1995 case Primitive::kPrimFloat:
1996 case Primitive::kPrimDouble: {
1997 DCHECK(dst.IsFPRegister());
1998 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001999
Artem Serov914d7a82017-02-07 14:33:49 +00002000 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2001 {
2002 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2003 __ ldar(temp, base);
2004 if (needs_null_check) {
2005 MaybeRecordImplicitNullCheck(instruction);
2006 }
2007 }
2008 __ Fmov(FPRegister(dst), temp);
2009 break;
Roland Levillain44015862016-01-22 11:47:17 +00002010 }
Artem Serov914d7a82017-02-07 14:33:49 +00002011 case Primitive::kPrimVoid:
2012 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002013 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002014 }
2015}
2016
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002017void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002018 CPURegister src,
2019 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002020 switch (type) {
2021 case Primitive::kPrimBoolean:
2022 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002023 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002024 break;
2025 case Primitive::kPrimChar:
2026 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002027 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002028 break;
2029 case Primitive::kPrimInt:
2030 case Primitive::kPrimNot:
2031 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002032 case Primitive::kPrimFloat:
2033 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00002034 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002035 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002036 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002037 case Primitive::kPrimVoid:
2038 LOG(FATAL) << "Unreachable type " << type;
2039 }
2040}
2041
Artem Serov914d7a82017-02-07 14:33:49 +00002042void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
2043 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002044 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002045 const MemOperand& dst,
2046 bool needs_null_check) {
2047 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002048 UseScratchRegisterScope temps(GetVIXLAssembler());
2049 Register temp_base = temps.AcquireX();
2050
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002051 DCHECK(!dst.IsPreIndex());
2052 DCHECK(!dst.IsPostIndex());
2053
2054 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002055 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002056 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002057 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002058 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002059 switch (type) {
2060 case Primitive::kPrimBoolean:
2061 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00002062 {
2063 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2064 __ stlrb(Register(src), base);
2065 if (needs_null_check) {
2066 MaybeRecordImplicitNullCheck(instruction);
2067 }
2068 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002069 break;
2070 case Primitive::kPrimChar:
2071 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00002072 {
2073 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2074 __ stlrh(Register(src), base);
2075 if (needs_null_check) {
2076 MaybeRecordImplicitNullCheck(instruction);
2077 }
2078 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002079 break;
2080 case Primitive::kPrimInt:
2081 case Primitive::kPrimNot:
2082 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002083 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002084 {
2085 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2086 __ stlr(Register(src), base);
2087 if (needs_null_check) {
2088 MaybeRecordImplicitNullCheck(instruction);
2089 }
2090 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002091 break;
2092 case Primitive::kPrimFloat:
2093 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002094 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002095 Register temp_src;
2096 if (src.IsZero()) {
2097 // The zero register is used to avoid synthesizing zero constants.
2098 temp_src = Register(src);
2099 } else {
2100 DCHECK(src.IsFPRegister());
2101 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2102 __ Fmov(temp_src, FPRegister(src));
2103 }
Artem Serov914d7a82017-02-07 14:33:49 +00002104 {
2105 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2106 __ stlr(temp_src, base);
2107 if (needs_null_check) {
2108 MaybeRecordImplicitNullCheck(instruction);
2109 }
2110 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002111 break;
2112 }
2113 case Primitive::kPrimVoid:
2114 LOG(FATAL) << "Unreachable type " << type;
2115 }
2116}
2117
Calin Juravle175dc732015-08-25 15:42:32 +01002118void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2119 HInstruction* instruction,
2120 uint32_t dex_pc,
2121 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002122 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002123
2124 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2125 {
2126 // Ensure the pc position is recorded immediately after the `blr` instruction.
2127 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2128 __ blr(lr);
2129 if (EntrypointRequiresStackMap(entrypoint)) {
2130 RecordPcInfo(instruction, dex_pc, slow_path);
2131 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002132 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002133}
2134
Roland Levillaindec8f632016-07-22 17:10:06 +01002135void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2136 HInstruction* instruction,
2137 SlowPathCode* slow_path) {
2138 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002139 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2140 __ Blr(lr);
2141}
2142
Alexandre Rames67555f72014-11-18 10:55:16 +00002143void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002144 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002145 UseScratchRegisterScope temps(GetVIXLAssembler());
2146 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002147 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2148
Serban Constantinescu02164b32014-11-13 14:05:07 +00002149 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002150 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2151 __ Add(temp, class_reg, status_offset);
2152 __ Ldar(temp, HeapOperand(temp));
2153 __ Cmp(temp, mirror::Class::kStatusInitialized);
2154 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002155 __ Bind(slow_path->GetExitLabel());
2156}
Alexandre Rames5319def2014-10-23 10:03:10 +01002157
Roland Levillain44015862016-01-22 11:47:17 +00002158void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002159 BarrierType type = BarrierAll;
2160
2161 switch (kind) {
2162 case MemBarrierKind::kAnyAny:
2163 case MemBarrierKind::kAnyStore: {
2164 type = BarrierAll;
2165 break;
2166 }
2167 case MemBarrierKind::kLoadAny: {
2168 type = BarrierReads;
2169 break;
2170 }
2171 case MemBarrierKind::kStoreStore: {
2172 type = BarrierWrites;
2173 break;
2174 }
2175 default:
2176 LOG(FATAL) << "Unexpected memory barrier " << kind;
2177 }
2178 __ Dmb(InnerShareable, type);
2179}
2180
Serban Constantinescu02164b32014-11-13 14:05:07 +00002181void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2182 HBasicBlock* successor) {
2183 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002184 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2185 if (slow_path == nullptr) {
2186 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2187 instruction->SetSlowPath(slow_path);
2188 codegen_->AddSlowPath(slow_path);
2189 if (successor != nullptr) {
2190 DCHECK(successor->IsLoopHeader());
2191 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2192 }
2193 } else {
2194 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2195 }
2196
Serban Constantinescu02164b32014-11-13 14:05:07 +00002197 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2198 Register temp = temps.AcquireW();
2199
Andreas Gampe542451c2016-07-26 09:02:02 -07002200 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002201 if (successor == nullptr) {
2202 __ Cbnz(temp, slow_path->GetEntryLabel());
2203 __ Bind(slow_path->GetReturnLabel());
2204 } else {
2205 __ Cbz(temp, codegen_->GetLabelOf(successor));
2206 __ B(slow_path->GetEntryLabel());
2207 // slow_path will return to GetLabelOf(successor).
2208 }
2209}
2210
Alexandre Rames5319def2014-10-23 10:03:10 +01002211InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2212 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002213 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002214 assembler_(codegen->GetAssembler()),
2215 codegen_(codegen) {}
2216
2217#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002218 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002219
2220#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2221
2222enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002223 // Using a base helps identify when we hit such breakpoints.
2224 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002225#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2226 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2227#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2228};
2229
2230#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002231 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002232 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2233 } \
2234 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2235 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2236 locations->SetOut(Location::Any()); \
2237 }
2238 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2239#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2240
2241#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002242#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002243
Alexandre Rames67555f72014-11-18 10:55:16 +00002244void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002245 DCHECK_EQ(instr->InputCount(), 2U);
2246 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2247 Primitive::Type type = instr->GetResultType();
2248 switch (type) {
2249 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002250 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002251 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002252 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002253 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002254 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002255
2256 case Primitive::kPrimFloat:
2257 case Primitive::kPrimDouble:
2258 locations->SetInAt(0, Location::RequiresFpuRegister());
2259 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002260 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002261 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002262
Alexandre Rames5319def2014-10-23 10:03:10 +01002263 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002264 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002265 }
2266}
2267
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002268void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2269 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002270 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2271
2272 bool object_field_get_with_read_barrier =
2273 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002274 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002275 new (GetGraph()->GetArena()) LocationSummary(instruction,
2276 object_field_get_with_read_barrier ?
2277 LocationSummary::kCallOnSlowPath :
2278 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002279 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002280 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002281 // We need a temporary register for the read barrier marking slow
2282 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002283 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2284 !Runtime::Current()->UseJitCompilation() &&
2285 !field_info.IsVolatile()) {
2286 // If link-time thunks for the Baker read barrier are enabled, for AOT
2287 // non-volatile loads we need a temporary only if the offset is too big.
2288 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2289 locations->AddTemp(FixedTempLocation());
2290 }
2291 } else {
2292 locations->AddTemp(Location::RequiresRegister());
2293 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002294 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002295 locations->SetInAt(0, Location::RequiresRegister());
2296 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2297 locations->SetOut(Location::RequiresFpuRegister());
2298 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002299 // The output overlaps for an object field get when read barriers
2300 // are enabled: we do not want the load to overwrite the object's
2301 // location, as we need it to emit the read barrier.
2302 locations->SetOut(
2303 Location::RequiresRegister(),
2304 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002305 }
2306}
2307
2308void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2309 const FieldInfo& field_info) {
2310 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002311 LocationSummary* locations = instruction->GetLocations();
2312 Location base_loc = locations->InAt(0);
2313 Location out = locations->Out();
2314 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002315 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002316 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002317
Roland Levillain44015862016-01-22 11:47:17 +00002318 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2319 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002320 // /* HeapReference<Object> */ out = *(base + offset)
2321 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002322 Location maybe_temp =
2323 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002324 // Note that potential implicit null checks are handled in this
2325 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2326 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2327 instruction,
2328 out,
2329 base,
2330 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002331 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002332 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002333 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002334 } else {
2335 // General case.
2336 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002337 // Note that a potential implicit null check is handled in this
2338 // CodeGeneratorARM64::LoadAcquire call.
2339 // NB: LoadAcquire will record the pc info if needed.
2340 codegen_->LoadAcquire(
2341 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002342 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002343 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2344 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002345 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002346 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002347 }
Roland Levillain44015862016-01-22 11:47:17 +00002348 if (field_type == Primitive::kPrimNot) {
2349 // If read barriers are enabled, emit read barriers other than
2350 // Baker's using a slow path (and also unpoison the loaded
2351 // reference, if heap poisoning is enabled).
2352 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2353 }
Roland Levillain4d027112015-07-01 15:41:14 +01002354 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002355}
2356
2357void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2358 LocationSummary* locations =
2359 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2360 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002361 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2362 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2363 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002364 locations->SetInAt(1, Location::RequiresFpuRegister());
2365 } else {
2366 locations->SetInAt(1, Location::RequiresRegister());
2367 }
2368}
2369
2370void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002371 const FieldInfo& field_info,
2372 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002373 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2374
2375 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002376 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002377 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002378 Offset offset = field_info.GetFieldOffset();
2379 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002380
Roland Levillain4d027112015-07-01 15:41:14 +01002381 {
2382 // We use a block to end the scratch scope before the write barrier, thus
2383 // freeing the temporary registers so they can be used in `MarkGCCard`.
2384 UseScratchRegisterScope temps(GetVIXLAssembler());
2385
2386 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2387 DCHECK(value.IsW());
2388 Register temp = temps.AcquireW();
2389 __ Mov(temp, value.W());
2390 GetAssembler()->PoisonHeapReference(temp.W());
2391 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002392 }
Roland Levillain4d027112015-07-01 15:41:14 +01002393
2394 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002395 codegen_->StoreRelease(
2396 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002397 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002398 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2399 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002400 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2401 codegen_->MaybeRecordImplicitNullCheck(instruction);
2402 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002403 }
2404
2405 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002406 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002407 }
2408}
2409
Alexandre Rames67555f72014-11-18 10:55:16 +00002410void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002411 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002412
2413 switch (type) {
2414 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002415 case Primitive::kPrimLong: {
2416 Register dst = OutputRegister(instr);
2417 Register lhs = InputRegisterAt(instr, 0);
2418 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002419 if (instr->IsAdd()) {
2420 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002421 } else if (instr->IsAnd()) {
2422 __ And(dst, lhs, rhs);
2423 } else if (instr->IsOr()) {
2424 __ Orr(dst, lhs, rhs);
2425 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002426 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002427 } else if (instr->IsRor()) {
2428 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002429 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002430 __ Ror(dst, lhs, shift);
2431 } else {
2432 // Ensure shift distance is in the same size register as the result. If
2433 // we are rotating a long and the shift comes in a w register originally,
2434 // we don't need to sxtw for use as an x since the shift distances are
2435 // all & reg_bits - 1.
2436 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2437 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002438 } else {
2439 DCHECK(instr->IsXor());
2440 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002441 }
2442 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002443 }
2444 case Primitive::kPrimFloat:
2445 case Primitive::kPrimDouble: {
2446 FPRegister dst = OutputFPRegister(instr);
2447 FPRegister lhs = InputFPRegisterAt(instr, 0);
2448 FPRegister rhs = InputFPRegisterAt(instr, 1);
2449 if (instr->IsAdd()) {
2450 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002451 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002452 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002453 } else {
2454 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002455 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002456 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002457 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002458 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002459 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002460 }
2461}
2462
Serban Constantinescu02164b32014-11-13 14:05:07 +00002463void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2464 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2465
2466 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2467 Primitive::Type type = instr->GetResultType();
2468 switch (type) {
2469 case Primitive::kPrimInt:
2470 case Primitive::kPrimLong: {
2471 locations->SetInAt(0, Location::RequiresRegister());
2472 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002473 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002474 break;
2475 }
2476 default:
2477 LOG(FATAL) << "Unexpected shift type " << type;
2478 }
2479}
2480
2481void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2482 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2483
2484 Primitive::Type type = instr->GetType();
2485 switch (type) {
2486 case Primitive::kPrimInt:
2487 case Primitive::kPrimLong: {
2488 Register dst = OutputRegister(instr);
2489 Register lhs = InputRegisterAt(instr, 0);
2490 Operand rhs = InputOperandAt(instr, 1);
2491 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002492 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002493 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002494 if (instr->IsShl()) {
2495 __ Lsl(dst, lhs, shift_value);
2496 } else if (instr->IsShr()) {
2497 __ Asr(dst, lhs, shift_value);
2498 } else {
2499 __ Lsr(dst, lhs, shift_value);
2500 }
2501 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002502 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002503
2504 if (instr->IsShl()) {
2505 __ Lsl(dst, lhs, rhs_reg);
2506 } else if (instr->IsShr()) {
2507 __ Asr(dst, lhs, rhs_reg);
2508 } else {
2509 __ Lsr(dst, lhs, rhs_reg);
2510 }
2511 }
2512 break;
2513 }
2514 default:
2515 LOG(FATAL) << "Unexpected shift operation type " << type;
2516 }
2517}
2518
Alexandre Rames5319def2014-10-23 10:03:10 +01002519void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002520 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002521}
2522
2523void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002524 HandleBinaryOp(instruction);
2525}
2526
2527void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2528 HandleBinaryOp(instruction);
2529}
2530
2531void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2532 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002533}
2534
Artem Serov7fc63502016-02-09 17:15:29 +00002535void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002536 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2537 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2538 locations->SetInAt(0, Location::RequiresRegister());
2539 // There is no immediate variant of negated bitwise instructions in AArch64.
2540 locations->SetInAt(1, Location::RequiresRegister());
2541 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2542}
2543
Artem Serov7fc63502016-02-09 17:15:29 +00002544void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002545 Register dst = OutputRegister(instr);
2546 Register lhs = InputRegisterAt(instr, 0);
2547 Register rhs = InputRegisterAt(instr, 1);
2548
2549 switch (instr->GetOpKind()) {
2550 case HInstruction::kAnd:
2551 __ Bic(dst, lhs, rhs);
2552 break;
2553 case HInstruction::kOr:
2554 __ Orn(dst, lhs, rhs);
2555 break;
2556 case HInstruction::kXor:
2557 __ Eon(dst, lhs, rhs);
2558 break;
2559 default:
2560 LOG(FATAL) << "Unreachable";
2561 }
2562}
2563
Anton Kirilov74234da2017-01-13 14:42:47 +00002564void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2565 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002566 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2567 instruction->GetType() == Primitive::kPrimLong);
2568 LocationSummary* locations =
2569 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2570 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2571 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2572 } else {
2573 locations->SetInAt(0, Location::RequiresRegister());
2574 }
2575 locations->SetInAt(1, Location::RequiresRegister());
2576 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2577}
2578
Anton Kirilov74234da2017-01-13 14:42:47 +00002579void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2580 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002581 Primitive::Type type = instruction->GetType();
2582 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2583 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2584 Register out = OutputRegister(instruction);
2585 Register left;
2586 if (kind != HInstruction::kNeg) {
2587 left = InputRegisterAt(instruction, 0);
2588 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002589 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002590 // shifter operand operation, the IR generating `right_reg` (input to the type
2591 // conversion) can have a different type from the current instruction's type,
2592 // so we manually indicate the type.
2593 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002594 Operand right_operand(0);
2595
Anton Kirilov74234da2017-01-13 14:42:47 +00002596 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2597 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002598 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2599 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002600 right_operand = Operand(right_reg,
2601 helpers::ShiftFromOpKind(op_kind),
2602 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002603 }
2604
2605 // Logical binary operations do not support extension operations in the
2606 // operand. Note that VIXL would still manage if it was passed by generating
2607 // the extension as a separate instruction.
2608 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2609 DCHECK(!right_operand.IsExtendedRegister() ||
2610 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2611 kind != HInstruction::kNeg));
2612 switch (kind) {
2613 case HInstruction::kAdd:
2614 __ Add(out, left, right_operand);
2615 break;
2616 case HInstruction::kAnd:
2617 __ And(out, left, right_operand);
2618 break;
2619 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002620 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002621 __ Neg(out, right_operand);
2622 break;
2623 case HInstruction::kOr:
2624 __ Orr(out, left, right_operand);
2625 break;
2626 case HInstruction::kSub:
2627 __ Sub(out, left, right_operand);
2628 break;
2629 case HInstruction::kXor:
2630 __ Eor(out, left, right_operand);
2631 break;
2632 default:
2633 LOG(FATAL) << "Unexpected operation kind: " << kind;
2634 UNREACHABLE();
2635 }
2636}
2637
Artem Serov328429f2016-07-06 16:23:04 +01002638void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002639 LocationSummary* locations =
2640 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2641 locations->SetInAt(0, Location::RequiresRegister());
2642 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002643 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002644}
2645
Roland Levillain19c54192016-11-04 13:44:09 +00002646void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002647 __ Add(OutputRegister(instruction),
2648 InputRegisterAt(instruction, 0),
2649 Operand(InputOperandAt(instruction, 1)));
2650}
2651
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002652void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002653 LocationSummary* locations =
2654 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002655 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2656 if (instr->GetOpKind() == HInstruction::kSub &&
2657 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002658 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002659 // Don't allocate register for Mneg instruction.
2660 } else {
2661 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2662 Location::RequiresRegister());
2663 }
2664 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2665 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002666 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2667}
2668
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002669void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002670 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002671 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2672 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002673
2674 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2675 // This fixup should be carried out for all multiply-accumulate instructions:
2676 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2677 if (instr->GetType() == Primitive::kPrimLong &&
2678 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2679 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002680 vixl::aarch64::Instruction* prev =
2681 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002682 if (prev->IsLoadOrStore()) {
2683 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002684 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002685 __ nop();
2686 }
2687 }
2688
2689 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002690 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002691 __ Madd(res, mul_left, mul_right, accumulator);
2692 } else {
2693 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002694 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002695 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002696 __ Mneg(res, mul_left, mul_right);
2697 } else {
2698 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2699 __ Msub(res, mul_left, mul_right, accumulator);
2700 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002701 }
2702}
2703
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002704void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002705 bool object_array_get_with_read_barrier =
2706 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002707 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002708 new (GetGraph()->GetArena()) LocationSummary(instruction,
2709 object_array_get_with_read_barrier ?
2710 LocationSummary::kCallOnSlowPath :
2711 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002712 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002713 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002714 // We need a temporary register for the read barrier marking slow
2715 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002716 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2717 !Runtime::Current()->UseJitCompilation() &&
2718 instruction->GetIndex()->IsConstant()) {
2719 // Array loads with constant index are treated as field loads.
2720 // If link-time thunks for the Baker read barrier are enabled, for AOT
2721 // constant index loads we need a temporary only if the offset is too big.
2722 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2723 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
2724 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
2725 if (offset >= kReferenceLoadMinFarOffset) {
2726 locations->AddTemp(FixedTempLocation());
2727 }
2728 } else {
2729 locations->AddTemp(Location::RequiresRegister());
2730 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002731 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002732 locations->SetInAt(0, Location::RequiresRegister());
2733 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002734 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2735 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2736 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002737 // The output overlaps in the case of an object array get with
2738 // read barriers enabled: we do not want the move to overwrite the
2739 // array's location, as we need it to emit the read barrier.
2740 locations->SetOut(
2741 Location::RequiresRegister(),
2742 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002743 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002744}
2745
2746void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002747 Primitive::Type type = instruction->GetType();
2748 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002749 LocationSummary* locations = instruction->GetLocations();
2750 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002751 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002752 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002753 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2754 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002755 MacroAssembler* masm = GetVIXLAssembler();
2756 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002757
Roland Levillain19c54192016-11-04 13:44:09 +00002758 // The read barrier instrumentation of object ArrayGet instructions
2759 // does not support the HIntermediateAddress instruction.
2760 DCHECK(!((type == Primitive::kPrimNot) &&
2761 instruction->GetArray()->IsIntermediateAddress() &&
2762 kEmitCompilerReadBarrier));
2763
Roland Levillain44015862016-01-22 11:47:17 +00002764 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2765 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002766 // Note that a potential implicit null check is handled in the
2767 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002768 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002769 if (index.IsConstant()) {
2770 // Array load with a constant index can be treated as a field load.
2771 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2772 Location maybe_temp =
2773 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2774 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2775 out,
2776 obj.W(),
2777 offset,
2778 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002779 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002780 /* use_load_acquire */ false);
2781 } else {
2782 Register temp = WRegisterFrom(locations->GetTemp(0));
2783 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002784 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002785 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002786 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002787 // General case.
2788 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002789 Register length;
2790 if (maybe_compressed_char_at) {
2791 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2792 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002793 {
2794 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2795 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2796
2797 if (instruction->GetArray()->IsIntermediateAddress()) {
2798 DCHECK_LT(count_offset, offset);
2799 int64_t adjusted_offset =
2800 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2801 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2802 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2803 } else {
2804 __ Ldr(length, HeapOperand(obj, count_offset));
2805 }
2806 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002807 }
jessicahandojo05765752016-09-09 19:01:32 -07002808 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002809 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002810 if (maybe_compressed_char_at) {
2811 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002812 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2813 "Expecting 0=compressed, 1=uncompressed");
2814 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002815 __ Ldrb(Register(OutputCPURegister(instruction)),
2816 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2817 __ B(&done);
2818 __ Bind(&uncompressed_load);
2819 __ Ldrh(Register(OutputCPURegister(instruction)),
2820 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2821 __ Bind(&done);
2822 } else {
2823 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2824 source = HeapOperand(obj, offset);
2825 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002826 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002827 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002828 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002829 // We do not need to compute the intermediate address from the array: the
2830 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002831 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002832 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002833 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002834 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2835 }
2836 temp = obj;
2837 } else {
2838 __ Add(temp, obj, offset);
2839 }
jessicahandojo05765752016-09-09 19:01:32 -07002840 if (maybe_compressed_char_at) {
2841 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002842 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2843 "Expecting 0=compressed, 1=uncompressed");
2844 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002845 __ Ldrb(Register(OutputCPURegister(instruction)),
2846 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2847 __ B(&done);
2848 __ Bind(&uncompressed_load);
2849 __ Ldrh(Register(OutputCPURegister(instruction)),
2850 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2851 __ Bind(&done);
2852 } else {
2853 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2854 }
Roland Levillain44015862016-01-22 11:47:17 +00002855 }
jessicahandojo05765752016-09-09 19:01:32 -07002856 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002857 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2858 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002859 codegen_->Load(type, OutputCPURegister(instruction), source);
2860 codegen_->MaybeRecordImplicitNullCheck(instruction);
2861 }
Roland Levillain44015862016-01-22 11:47:17 +00002862
2863 if (type == Primitive::kPrimNot) {
2864 static_assert(
2865 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2866 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2867 Location obj_loc = locations->InAt(0);
2868 if (index.IsConstant()) {
2869 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2870 } else {
2871 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2872 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002873 }
Roland Levillain4d027112015-07-01 15:41:14 +01002874 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002875}
2876
Alexandre Rames5319def2014-10-23 10:03:10 +01002877void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2878 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2879 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002880 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002881}
2882
2883void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002884 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002885 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002886 {
2887 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2888 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2889 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2890 codegen_->MaybeRecordImplicitNullCheck(instruction);
2891 }
jessicahandojo05765752016-09-09 19:01:32 -07002892 // Mask out compression flag from String's array length.
2893 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002894 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002895 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002896}
2897
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002898void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002899 Primitive::Type value_type = instruction->GetComponentType();
2900
2901 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002902 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2903 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002904 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002905 LocationSummary::kCallOnSlowPath :
2906 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002907 locations->SetInAt(0, Location::RequiresRegister());
2908 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002909 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2910 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2911 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002912 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002913 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002914 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002915 }
2916}
2917
2918void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2919 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002920 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002921 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002922 bool needs_write_barrier =
2923 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002924
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002925 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002926 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002927 CPURegister source = value;
2928 Location index = locations->InAt(1);
2929 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2930 MemOperand destination = HeapOperand(array);
2931 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002932
2933 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002934 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002935 if (index.IsConstant()) {
2936 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2937 destination = HeapOperand(array, offset);
2938 } else {
2939 UseScratchRegisterScope temps(masm);
2940 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002941 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002942 // We do not need to compute the intermediate address from the array: the
2943 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002944 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002945 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002946 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002947 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2948 }
2949 temp = array;
2950 } else {
2951 __ Add(temp, array, offset);
2952 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002953 destination = HeapOperand(temp,
2954 XRegisterFrom(index),
2955 LSL,
2956 Primitive::ComponentSizeShift(value_type));
2957 }
Artem Serov914d7a82017-02-07 14:33:49 +00002958 {
2959 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2960 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2961 codegen_->Store(value_type, value, destination);
2962 codegen_->MaybeRecordImplicitNullCheck(instruction);
2963 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002964 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002965 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002966 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002967 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002968 {
2969 // We use a block to end the scratch scope before the write barrier, thus
2970 // freeing the temporary registers so they can be used in `MarkGCCard`.
2971 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002972 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002973 if (index.IsConstant()) {
2974 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002975 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002976 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002977 destination = HeapOperand(temp,
2978 XRegisterFrom(index),
2979 LSL,
2980 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002981 }
2982
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002983 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2984 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2985 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2986
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002987 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002988 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2989 codegen_->AddSlowPath(slow_path);
2990 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002991 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002992 __ Cbnz(Register(value), &non_zero);
2993 if (!index.IsConstant()) {
2994 __ Add(temp, array, offset);
2995 }
Artem Serov914d7a82017-02-07 14:33:49 +00002996 {
2997 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
2998 // emitted.
2999 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3000 __ Str(wzr, destination);
3001 codegen_->MaybeRecordImplicitNullCheck(instruction);
3002 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003003 __ B(&done);
3004 __ Bind(&non_zero);
3005 }
3006
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003007 // Note that when Baker read barriers are enabled, the type
3008 // checks are performed without read barriers. This is fine,
3009 // even in the case where a class object is in the from-space
3010 // after the flip, as a comparison involving such a type would
3011 // not produce a false positive; it may of course produce a
3012 // false negative, in which case we would take the ArraySet
3013 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003014
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003015 Register temp2 = temps.AcquireSameSizeAs(array);
3016 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003017 {
3018 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3019 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3020 __ Ldr(temp, HeapOperand(array, class_offset));
3021 codegen_->MaybeRecordImplicitNullCheck(instruction);
3022 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003023 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003024
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003025 // /* HeapReference<Class> */ temp = temp->component_type_
3026 __ Ldr(temp, HeapOperand(temp, component_offset));
3027 // /* HeapReference<Class> */ temp2 = value->klass_
3028 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3029 // If heap poisoning is enabled, no need to unpoison `temp`
3030 // nor `temp2`, as we are comparing two poisoned references.
3031 __ Cmp(temp, temp2);
3032 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003033
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003034 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3035 vixl::aarch64::Label do_put;
3036 __ B(eq, &do_put);
3037 // If heap poisoning is enabled, the `temp` reference has
3038 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003039 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3040
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003041 // /* HeapReference<Class> */ temp = temp->super_class_
3042 __ Ldr(temp, HeapOperand(temp, super_offset));
3043 // If heap poisoning is enabled, no need to unpoison
3044 // `temp`, as we are comparing against null below.
3045 __ Cbnz(temp, slow_path->GetEntryLabel());
3046 __ Bind(&do_put);
3047 } else {
3048 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003049 }
3050 }
3051
3052 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003053 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003054 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003055 __ Mov(temp2, value.W());
3056 GetAssembler()->PoisonHeapReference(temp2);
3057 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003058 }
3059
3060 if (!index.IsConstant()) {
3061 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003062 } else {
3063 // We no longer need the `temp` here so release it as the store below may
3064 // need a scratch register (if the constant index makes the offset too large)
3065 // and the poisoned `source` could be using the other scratch register.
3066 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003067 }
Artem Serov914d7a82017-02-07 14:33:49 +00003068 {
3069 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3070 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3071 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003072
Artem Serov914d7a82017-02-07 14:33:49 +00003073 if (!may_need_runtime_call_for_type_check) {
3074 codegen_->MaybeRecordImplicitNullCheck(instruction);
3075 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003076 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003077 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003078
3079 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3080
3081 if (done.IsLinked()) {
3082 __ Bind(&done);
3083 }
3084
3085 if (slow_path != nullptr) {
3086 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003087 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003088 }
3089}
3090
Alexandre Rames67555f72014-11-18 10:55:16 +00003091void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003092 RegisterSet caller_saves = RegisterSet::Empty();
3093 InvokeRuntimeCallingConvention calling_convention;
3094 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3095 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3096 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003097 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003098 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003099}
3100
3101void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003102 BoundsCheckSlowPathARM64* slow_path =
3103 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003104 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003105 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3106 __ B(slow_path->GetEntryLabel(), hs);
3107}
3108
Alexandre Rames67555f72014-11-18 10:55:16 +00003109void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3110 LocationSummary* locations =
3111 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3112 locations->SetInAt(0, Location::RequiresRegister());
3113 if (check->HasUses()) {
3114 locations->SetOut(Location::SameAsFirstInput());
3115 }
3116}
3117
3118void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3119 // We assume the class is not null.
3120 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3121 check->GetLoadClass(), check, check->GetDexPc(), true);
3122 codegen_->AddSlowPath(slow_path);
3123 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3124}
3125
Roland Levillain1a653882016-03-18 18:05:57 +00003126static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3127 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3128 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3129}
3130
3131void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3132 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3133 Location rhs_loc = instruction->GetLocations()->InAt(1);
3134 if (rhs_loc.IsConstant()) {
3135 // 0.0 is the only immediate that can be encoded directly in
3136 // an FCMP instruction.
3137 //
3138 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3139 // specify that in a floating-point comparison, positive zero
3140 // and negative zero are considered equal, so we can use the
3141 // literal 0.0 for both cases here.
3142 //
3143 // Note however that some methods (Float.equal, Float.compare,
3144 // Float.compareTo, Double.equal, Double.compare,
3145 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3146 // StrictMath.min) consider 0.0 to be (strictly) greater than
3147 // -0.0. So if we ever translate calls to these methods into a
3148 // HCompare instruction, we must handle the -0.0 case with
3149 // care here.
3150 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3151 __ Fcmp(lhs_reg, 0.0);
3152 } else {
3153 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3154 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003155}
3156
Serban Constantinescu02164b32014-11-13 14:05:07 +00003157void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003158 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003159 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3160 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003161 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003162 case Primitive::kPrimBoolean:
3163 case Primitive::kPrimByte:
3164 case Primitive::kPrimShort:
3165 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003166 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003167 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003168 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003169 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003170 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3171 break;
3172 }
3173 case Primitive::kPrimFloat:
3174 case Primitive::kPrimDouble: {
3175 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003176 locations->SetInAt(1,
3177 IsFloatingPointZeroConstant(compare->InputAt(1))
3178 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3179 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003180 locations->SetOut(Location::RequiresRegister());
3181 break;
3182 }
3183 default:
3184 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3185 }
3186}
3187
3188void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3189 Primitive::Type in_type = compare->InputAt(0)->GetType();
3190
3191 // 0 if: left == right
3192 // 1 if: left > right
3193 // -1 if: left < right
3194 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003195 case Primitive::kPrimBoolean:
3196 case Primitive::kPrimByte:
3197 case Primitive::kPrimShort:
3198 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003199 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003200 case Primitive::kPrimLong: {
3201 Register result = OutputRegister(compare);
3202 Register left = InputRegisterAt(compare, 0);
3203 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003204 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003205 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3206 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003207 break;
3208 }
3209 case Primitive::kPrimFloat:
3210 case Primitive::kPrimDouble: {
3211 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003212 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003213 __ Cset(result, ne);
3214 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003215 break;
3216 }
3217 default:
3218 LOG(FATAL) << "Unimplemented compare type " << in_type;
3219 }
3220}
3221
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003222void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003223 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003224
3225 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3226 locations->SetInAt(0, Location::RequiresFpuRegister());
3227 locations->SetInAt(1,
3228 IsFloatingPointZeroConstant(instruction->InputAt(1))
3229 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3230 : Location::RequiresFpuRegister());
3231 } else {
3232 // Integer cases.
3233 locations->SetInAt(0, Location::RequiresRegister());
3234 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3235 }
3236
David Brazdilb3e773e2016-01-26 11:28:37 +00003237 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003238 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003239 }
3240}
3241
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003242void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003243 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003244 return;
3245 }
3246
3247 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003248 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003249 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003250
Roland Levillain7f63c522015-07-13 15:54:55 +00003251 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003252 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003253 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003254 } else {
3255 // Integer cases.
3256 Register lhs = InputRegisterAt(instruction, 0);
3257 Operand rhs = InputOperandAt(instruction, 1);
3258 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003259 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003260 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003261}
3262
3263#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3264 M(Equal) \
3265 M(NotEqual) \
3266 M(LessThan) \
3267 M(LessThanOrEqual) \
3268 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003269 M(GreaterThanOrEqual) \
3270 M(Below) \
3271 M(BelowOrEqual) \
3272 M(Above) \
3273 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003274#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003275void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3276void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003277FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003278#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003279#undef FOR_EACH_CONDITION_INSTRUCTION
3280
Zheng Xuc6667102015-05-15 16:08:45 +08003281void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3282 DCHECK(instruction->IsDiv() || instruction->IsRem());
3283
3284 LocationSummary* locations = instruction->GetLocations();
3285 Location second = locations->InAt(1);
3286 DCHECK(second.IsConstant());
3287
3288 Register out = OutputRegister(instruction);
3289 Register dividend = InputRegisterAt(instruction, 0);
3290 int64_t imm = Int64FromConstant(second.GetConstant());
3291 DCHECK(imm == 1 || imm == -1);
3292
3293 if (instruction->IsRem()) {
3294 __ Mov(out, 0);
3295 } else {
3296 if (imm == 1) {
3297 __ Mov(out, dividend);
3298 } else {
3299 __ Neg(out, dividend);
3300 }
3301 }
3302}
3303
3304void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3305 DCHECK(instruction->IsDiv() || instruction->IsRem());
3306
3307 LocationSummary* locations = instruction->GetLocations();
3308 Location second = locations->InAt(1);
3309 DCHECK(second.IsConstant());
3310
3311 Register out = OutputRegister(instruction);
3312 Register dividend = InputRegisterAt(instruction, 0);
3313 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003314 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003315 int ctz_imm = CTZ(abs_imm);
3316
3317 UseScratchRegisterScope temps(GetVIXLAssembler());
3318 Register temp = temps.AcquireSameSizeAs(out);
3319
3320 if (instruction->IsDiv()) {
3321 __ Add(temp, dividend, abs_imm - 1);
3322 __ Cmp(dividend, 0);
3323 __ Csel(out, temp, dividend, lt);
3324 if (imm > 0) {
3325 __ Asr(out, out, ctz_imm);
3326 } else {
3327 __ Neg(out, Operand(out, ASR, ctz_imm));
3328 }
3329 } else {
3330 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3331 __ Asr(temp, dividend, bits - 1);
3332 __ Lsr(temp, temp, bits - ctz_imm);
3333 __ Add(out, dividend, temp);
3334 __ And(out, out, abs_imm - 1);
3335 __ Sub(out, out, temp);
3336 }
3337}
3338
3339void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3340 DCHECK(instruction->IsDiv() || instruction->IsRem());
3341
3342 LocationSummary* locations = instruction->GetLocations();
3343 Location second = locations->InAt(1);
3344 DCHECK(second.IsConstant());
3345
3346 Register out = OutputRegister(instruction);
3347 Register dividend = InputRegisterAt(instruction, 0);
3348 int64_t imm = Int64FromConstant(second.GetConstant());
3349
3350 Primitive::Type type = instruction->GetResultType();
3351 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3352
3353 int64_t magic;
3354 int shift;
3355 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3356
3357 UseScratchRegisterScope temps(GetVIXLAssembler());
3358 Register temp = temps.AcquireSameSizeAs(out);
3359
3360 // temp = get_high(dividend * magic)
3361 __ Mov(temp, magic);
3362 if (type == Primitive::kPrimLong) {
3363 __ Smulh(temp, dividend, temp);
3364 } else {
3365 __ Smull(temp.X(), dividend, temp);
3366 __ Lsr(temp.X(), temp.X(), 32);
3367 }
3368
3369 if (imm > 0 && magic < 0) {
3370 __ Add(temp, temp, dividend);
3371 } else if (imm < 0 && magic > 0) {
3372 __ Sub(temp, temp, dividend);
3373 }
3374
3375 if (shift != 0) {
3376 __ Asr(temp, temp, shift);
3377 }
3378
3379 if (instruction->IsDiv()) {
3380 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3381 } else {
3382 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3383 // TODO: Strength reduction for msub.
3384 Register temp_imm = temps.AcquireSameSizeAs(out);
3385 __ Mov(temp_imm, imm);
3386 __ Msub(out, temp, temp_imm, dividend);
3387 }
3388}
3389
3390void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3391 DCHECK(instruction->IsDiv() || instruction->IsRem());
3392 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003393 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003394
3395 LocationSummary* locations = instruction->GetLocations();
3396 Register out = OutputRegister(instruction);
3397 Location second = locations->InAt(1);
3398
3399 if (second.IsConstant()) {
3400 int64_t imm = Int64FromConstant(second.GetConstant());
3401
3402 if (imm == 0) {
3403 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3404 } else if (imm == 1 || imm == -1) {
3405 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003406 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003407 DivRemByPowerOfTwo(instruction);
3408 } else {
3409 DCHECK(imm <= -2 || imm >= 2);
3410 GenerateDivRemWithAnyConstant(instruction);
3411 }
3412 } else {
3413 Register dividend = InputRegisterAt(instruction, 0);
3414 Register divisor = InputRegisterAt(instruction, 1);
3415 if (instruction->IsDiv()) {
3416 __ Sdiv(out, dividend, divisor);
3417 } else {
3418 UseScratchRegisterScope temps(GetVIXLAssembler());
3419 Register temp = temps.AcquireSameSizeAs(out);
3420 __ Sdiv(temp, dividend, divisor);
3421 __ Msub(out, temp, divisor, dividend);
3422 }
3423 }
3424}
3425
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003426void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3427 LocationSummary* locations =
3428 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3429 switch (div->GetResultType()) {
3430 case Primitive::kPrimInt:
3431 case Primitive::kPrimLong:
3432 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003433 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003434 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3435 break;
3436
3437 case Primitive::kPrimFloat:
3438 case Primitive::kPrimDouble:
3439 locations->SetInAt(0, Location::RequiresFpuRegister());
3440 locations->SetInAt(1, Location::RequiresFpuRegister());
3441 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3442 break;
3443
3444 default:
3445 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3446 }
3447}
3448
3449void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3450 Primitive::Type type = div->GetResultType();
3451 switch (type) {
3452 case Primitive::kPrimInt:
3453 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003454 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003455 break;
3456
3457 case Primitive::kPrimFloat:
3458 case Primitive::kPrimDouble:
3459 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3460 break;
3461
3462 default:
3463 LOG(FATAL) << "Unexpected div type " << type;
3464 }
3465}
3466
Alexandre Rames67555f72014-11-18 10:55:16 +00003467void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003468 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003469 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003470}
3471
3472void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3473 SlowPathCodeARM64* slow_path =
3474 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3475 codegen_->AddSlowPath(slow_path);
3476 Location value = instruction->GetLocations()->InAt(0);
3477
Alexandre Rames3e69f162014-12-10 10:36:50 +00003478 Primitive::Type type = instruction->GetType();
3479
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003480 if (!Primitive::IsIntegralType(type)) {
3481 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003482 return;
3483 }
3484
Alexandre Rames67555f72014-11-18 10:55:16 +00003485 if (value.IsConstant()) {
3486 int64_t divisor = Int64ConstantFrom(value);
3487 if (divisor == 0) {
3488 __ B(slow_path->GetEntryLabel());
3489 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003490 // A division by a non-null constant is valid. We don't need to perform
3491 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003492 }
3493 } else {
3494 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3495 }
3496}
3497
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003498void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3499 LocationSummary* locations =
3500 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3501 locations->SetOut(Location::ConstantLocation(constant));
3502}
3503
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003504void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3505 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003506 // Will be generated at use site.
3507}
3508
Alexandre Rames5319def2014-10-23 10:03:10 +01003509void LocationsBuilderARM64::VisitExit(HExit* exit) {
3510 exit->SetLocations(nullptr);
3511}
3512
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003513void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003514}
3515
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003516void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3517 LocationSummary* locations =
3518 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3519 locations->SetOut(Location::ConstantLocation(constant));
3520}
3521
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003522void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003523 // Will be generated at use site.
3524}
3525
David Brazdilfc6a86a2015-06-26 10:33:45 +00003526void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003527 DCHECK(!successor->IsExitBlock());
3528 HBasicBlock* block = got->GetBlock();
3529 HInstruction* previous = got->GetPrevious();
3530 HLoopInformation* info = block->GetLoopInformation();
3531
David Brazdil46e2a392015-03-16 17:31:52 +00003532 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003533 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3534 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3535 return;
3536 }
3537 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3538 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3539 }
3540 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003541 __ B(codegen_->GetLabelOf(successor));
3542 }
3543}
3544
David Brazdilfc6a86a2015-06-26 10:33:45 +00003545void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3546 got->SetLocations(nullptr);
3547}
3548
3549void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3550 HandleGoto(got, got->GetSuccessor());
3551}
3552
3553void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3554 try_boundary->SetLocations(nullptr);
3555}
3556
3557void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3558 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3559 if (!successor->IsExitBlock()) {
3560 HandleGoto(try_boundary, successor);
3561 }
3562}
3563
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003564void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003565 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003566 vixl::aarch64::Label* true_target,
3567 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003568 // FP branching requires both targets to be explicit. If either of the targets
3569 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003570 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003571 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003572
David Brazdil0debae72015-11-12 18:37:00 +00003573 if (true_target == nullptr && false_target == nullptr) {
3574 // Nothing to do. The code always falls through.
3575 return;
3576 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003577 // Constant condition, statically compared against "true" (integer value 1).
3578 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003579 if (true_target != nullptr) {
3580 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003581 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003582 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003583 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003584 if (false_target != nullptr) {
3585 __ B(false_target);
3586 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003587 }
David Brazdil0debae72015-11-12 18:37:00 +00003588 return;
3589 }
3590
3591 // The following code generates these patterns:
3592 // (1) true_target == nullptr && false_target != nullptr
3593 // - opposite condition true => branch to false_target
3594 // (2) true_target != nullptr && false_target == nullptr
3595 // - condition true => branch to true_target
3596 // (3) true_target != nullptr && false_target != nullptr
3597 // - condition true => branch to true_target
3598 // - branch to false_target
3599 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003600 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003601 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003602 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003603 if (true_target == nullptr) {
3604 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3605 } else {
3606 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3607 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003608 } else {
3609 // The condition instruction has not been materialized, use its inputs as
3610 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003611 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003612
David Brazdil0debae72015-11-12 18:37:00 +00003613 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003614 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003615 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003616 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003617 IfCondition opposite_condition = condition->GetOppositeCondition();
3618 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003619 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003620 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003621 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003622 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003623 // Integer cases.
3624 Register lhs = InputRegisterAt(condition, 0);
3625 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003626
3627 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003628 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003629 if (true_target == nullptr) {
3630 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3631 non_fallthrough_target = false_target;
3632 } else {
3633 arm64_cond = ARM64Condition(condition->GetCondition());
3634 non_fallthrough_target = true_target;
3635 }
3636
Aart Bik086d27e2016-01-20 17:02:00 -08003637 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003638 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003639 switch (arm64_cond) {
3640 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003641 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003642 break;
3643 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003644 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003645 break;
3646 case lt:
3647 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003648 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003649 break;
3650 case ge:
3651 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003652 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003653 break;
3654 default:
3655 // Without the `static_cast` the compiler throws an error for
3656 // `-Werror=sign-promo`.
3657 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3658 }
3659 } else {
3660 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003661 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003662 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003663 }
3664 }
David Brazdil0debae72015-11-12 18:37:00 +00003665
3666 // If neither branch falls through (case 3), the conditional branch to `true_target`
3667 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3668 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003669 __ B(false_target);
3670 }
David Brazdil0debae72015-11-12 18:37:00 +00003671
3672 if (fallthrough_target.IsLinked()) {
3673 __ Bind(&fallthrough_target);
3674 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003675}
3676
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003677void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3678 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003679 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003680 locations->SetInAt(0, Location::RequiresRegister());
3681 }
3682}
3683
3684void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003685 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3686 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003687 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3688 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3689 true_target = nullptr;
3690 }
3691 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3692 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3693 false_target = nullptr;
3694 }
David Brazdil0debae72015-11-12 18:37:00 +00003695 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003696}
3697
3698void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3699 LocationSummary* locations = new (GetGraph()->GetArena())
3700 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003701 InvokeRuntimeCallingConvention calling_convention;
3702 RegisterSet caller_saves = RegisterSet::Empty();
3703 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3704 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003705 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003706 locations->SetInAt(0, Location::RequiresRegister());
3707 }
3708}
3709
3710void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003711 SlowPathCodeARM64* slow_path =
3712 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003713 GenerateTestAndBranch(deoptimize,
3714 /* condition_input_index */ 0,
3715 slow_path->GetEntryLabel(),
3716 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003717}
3718
Mingyao Yang063fc772016-08-02 11:02:54 -07003719void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3720 LocationSummary* locations = new (GetGraph()->GetArena())
3721 LocationSummary(flag, LocationSummary::kNoCall);
3722 locations->SetOut(Location::RequiresRegister());
3723}
3724
3725void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3726 __ Ldr(OutputRegister(flag),
3727 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3728}
3729
David Brazdilc0b601b2016-02-08 14:20:45 +00003730static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3731 return condition->IsCondition() &&
3732 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3733}
3734
Alexandre Rames880f1192016-06-13 16:04:50 +01003735static inline Condition GetConditionForSelect(HCondition* condition) {
3736 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003737 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3738 : ARM64Condition(cond);
3739}
3740
David Brazdil74eb1b22015-12-14 11:44:01 +00003741void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3742 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003743 if (Primitive::IsFloatingPointType(select->GetType())) {
3744 locations->SetInAt(0, Location::RequiresFpuRegister());
3745 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003746 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003747 } else {
3748 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3749 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3750 bool is_true_value_constant = cst_true_value != nullptr;
3751 bool is_false_value_constant = cst_false_value != nullptr;
3752 // Ask VIXL whether we should synthesize constants in registers.
3753 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3754 Operand true_op = is_true_value_constant ?
3755 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3756 Operand false_op = is_false_value_constant ?
3757 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3758 bool true_value_in_register = false;
3759 bool false_value_in_register = false;
3760 MacroAssembler::GetCselSynthesisInformation(
3761 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3762 true_value_in_register |= !is_true_value_constant;
3763 false_value_in_register |= !is_false_value_constant;
3764
3765 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3766 : Location::ConstantLocation(cst_true_value));
3767 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3768 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003769 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003770 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003771
David Brazdil74eb1b22015-12-14 11:44:01 +00003772 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3773 locations->SetInAt(2, Location::RequiresRegister());
3774 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003775}
3776
3777void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003778 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003779 Condition csel_cond;
3780
3781 if (IsBooleanValueOrMaterializedCondition(cond)) {
3782 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003783 // Use the condition flags set by the previous instruction.
3784 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003785 } else {
3786 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003787 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003788 }
3789 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003790 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003791 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003792 } else {
3793 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003794 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003795 }
3796
Alexandre Rames880f1192016-06-13 16:04:50 +01003797 if (Primitive::IsFloatingPointType(select->GetType())) {
3798 __ Fcsel(OutputFPRegister(select),
3799 InputFPRegisterAt(select, 1),
3800 InputFPRegisterAt(select, 0),
3801 csel_cond);
3802 } else {
3803 __ Csel(OutputRegister(select),
3804 InputOperandAt(select, 1),
3805 InputOperandAt(select, 0),
3806 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003807 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003808}
3809
David Srbecky0cf44932015-12-09 14:09:59 +00003810void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3811 new (GetGraph()->GetArena()) LocationSummary(info);
3812}
3813
David Srbeckyd28f4a02016-03-14 17:14:24 +00003814void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3815 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003816}
3817
3818void CodeGeneratorARM64::GenerateNop() {
3819 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003820}
3821
Alexandre Rames5319def2014-10-23 10:03:10 +01003822void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003823 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003824}
3825
3826void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003827 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003828}
3829
3830void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003831 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003832}
3833
3834void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003835 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003836}
3837
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003838// Temp is used for read barrier.
3839static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3840 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003841 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003842 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3843 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3844 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3845 return 1;
3846 }
3847 return 0;
3848}
3849
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003850// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003851// interface pointer, one for loading the current interface.
3852// The other checks have one temp for loading the object's class.
3853static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3854 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3855 return 3;
3856 }
3857 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003858}
3859
Alexandre Rames67555f72014-11-18 10:55:16 +00003860void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003861 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003862 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003863 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003864 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003865 case TypeCheckKind::kExactCheck:
3866 case TypeCheckKind::kAbstractClassCheck:
3867 case TypeCheckKind::kClassHierarchyCheck:
3868 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003869 call_kind =
3870 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003871 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003872 break;
3873 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003874 case TypeCheckKind::kUnresolvedCheck:
3875 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003876 call_kind = LocationSummary::kCallOnSlowPath;
3877 break;
3878 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003879
Alexandre Rames67555f72014-11-18 10:55:16 +00003880 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003881 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003882 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003883 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003884 locations->SetInAt(0, Location::RequiresRegister());
3885 locations->SetInAt(1, Location::RequiresRegister());
3886 // The "out" register is used as a temporary, so it overlaps with the inputs.
3887 // Note that TypeCheckSlowPathARM64 uses this register too.
3888 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003889 // Add temps if necessary for read barriers.
3890 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003891}
3892
3893void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003894 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003895 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003896 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003897 Register obj = InputRegisterAt(instruction, 0);
3898 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003899 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003900 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003901 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3902 DCHECK_LE(num_temps, 1u);
3903 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003904 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3905 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3906 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3907 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003908
Scott Wakeling97c72b72016-06-24 16:19:36 +01003909 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003910 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003911
3912 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003913 // Avoid null check if we know `obj` is not null.
3914 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003915 __ Cbz(obj, &zero);
3916 }
3917
Roland Levillain44015862016-01-22 11:47:17 +00003918 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003919 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003920 // /* HeapReference<Class> */ out = obj->klass_
3921 GenerateReferenceLoadTwoRegisters(instruction,
3922 out_loc,
3923 obj_loc,
3924 class_offset,
3925 maybe_temp_loc,
3926 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003927 __ Cmp(out, cls);
3928 __ Cset(out, eq);
3929 if (zero.IsLinked()) {
3930 __ B(&done);
3931 }
3932 break;
3933 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003934
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003935 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003936 // /* HeapReference<Class> */ out = obj->klass_
3937 GenerateReferenceLoadTwoRegisters(instruction,
3938 out_loc,
3939 obj_loc,
3940 class_offset,
3941 maybe_temp_loc,
3942 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003943 // If the class is abstract, we eagerly fetch the super class of the
3944 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003945 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003946 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003947 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003948 GenerateReferenceLoadOneRegister(instruction,
3949 out_loc,
3950 super_offset,
3951 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003952 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003953 // If `out` is null, we use it for the result, and jump to `done`.
3954 __ Cbz(out, &done);
3955 __ Cmp(out, cls);
3956 __ B(ne, &loop);
3957 __ Mov(out, 1);
3958 if (zero.IsLinked()) {
3959 __ B(&done);
3960 }
3961 break;
3962 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003963
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003964 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003965 // /* HeapReference<Class> */ out = obj->klass_
3966 GenerateReferenceLoadTwoRegisters(instruction,
3967 out_loc,
3968 obj_loc,
3969 class_offset,
3970 maybe_temp_loc,
3971 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003972 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003973 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003974 __ Bind(&loop);
3975 __ Cmp(out, cls);
3976 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003977 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003978 GenerateReferenceLoadOneRegister(instruction,
3979 out_loc,
3980 super_offset,
3981 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003982 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003983 __ Cbnz(out, &loop);
3984 // If `out` is null, we use it for the result, and jump to `done`.
3985 __ B(&done);
3986 __ Bind(&success);
3987 __ Mov(out, 1);
3988 if (zero.IsLinked()) {
3989 __ B(&done);
3990 }
3991 break;
3992 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003993
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003994 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003995 // /* HeapReference<Class> */ out = obj->klass_
3996 GenerateReferenceLoadTwoRegisters(instruction,
3997 out_loc,
3998 obj_loc,
3999 class_offset,
4000 maybe_temp_loc,
4001 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004002 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004003 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004004 __ Cmp(out, cls);
4005 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004006 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004007 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004008 GenerateReferenceLoadOneRegister(instruction,
4009 out_loc,
4010 component_offset,
4011 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004012 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004013 // If `out` is null, we use it for the result, and jump to `done`.
4014 __ Cbz(out, &done);
4015 __ Ldrh(out, HeapOperand(out, primitive_offset));
4016 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4017 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004018 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004019 __ Mov(out, 1);
4020 __ B(&done);
4021 break;
4022 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004023
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004024 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004025 // No read barrier since the slow path will retry upon failure.
4026 // /* HeapReference<Class> */ out = obj->klass_
4027 GenerateReferenceLoadTwoRegisters(instruction,
4028 out_loc,
4029 obj_loc,
4030 class_offset,
4031 maybe_temp_loc,
4032 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004033 __ Cmp(out, cls);
4034 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004035 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4036 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004037 codegen_->AddSlowPath(slow_path);
4038 __ B(ne, slow_path->GetEntryLabel());
4039 __ Mov(out, 1);
4040 if (zero.IsLinked()) {
4041 __ B(&done);
4042 }
4043 break;
4044 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004045
Calin Juravle98893e12015-10-02 21:05:03 +01004046 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004047 case TypeCheckKind::kInterfaceCheck: {
4048 // Note that we indeed only call on slow path, but we always go
4049 // into the slow path for the unresolved and interface check
4050 // cases.
4051 //
4052 // We cannot directly call the InstanceofNonTrivial runtime
4053 // entry point without resorting to a type checking slow path
4054 // here (i.e. by calling InvokeRuntime directly), as it would
4055 // require to assign fixed registers for the inputs of this
4056 // HInstanceOf instruction (following the runtime calling
4057 // convention), which might be cluttered by the potential first
4058 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004059 //
4060 // TODO: Introduce a new runtime entry point taking the object
4061 // to test (instead of its class) as argument, and let it deal
4062 // with the read barrier issues. This will let us refactor this
4063 // case of the `switch` code as it was previously (with a direct
4064 // call to the runtime not using a type checking slow path).
4065 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004066 DCHECK(locations->OnlyCallsOnSlowPath());
4067 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4068 /* is_fatal */ false);
4069 codegen_->AddSlowPath(slow_path);
4070 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004071 if (zero.IsLinked()) {
4072 __ B(&done);
4073 }
4074 break;
4075 }
4076 }
4077
4078 if (zero.IsLinked()) {
4079 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004080 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004081 }
4082
4083 if (done.IsLinked()) {
4084 __ Bind(&done);
4085 }
4086
4087 if (slow_path != nullptr) {
4088 __ Bind(slow_path->GetExitLabel());
4089 }
4090}
4091
4092void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4093 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4094 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4095
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004096 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4097 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004098 case TypeCheckKind::kExactCheck:
4099 case TypeCheckKind::kAbstractClassCheck:
4100 case TypeCheckKind::kClassHierarchyCheck:
4101 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004102 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4103 LocationSummary::kCallOnSlowPath :
4104 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004105 break;
4106 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004107 case TypeCheckKind::kUnresolvedCheck:
4108 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004109 call_kind = LocationSummary::kCallOnSlowPath;
4110 break;
4111 }
4112
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004113 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4114 locations->SetInAt(0, Location::RequiresRegister());
4115 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004116 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4117 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004118}
4119
4120void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004121 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004122 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004123 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004124 Register obj = InputRegisterAt(instruction, 0);
4125 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004126 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4127 DCHECK_GE(num_temps, 1u);
4128 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004129 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004130 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4131 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004132 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004133 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4134 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4135 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4136 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4137 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4138 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4139 const uint32_t object_array_data_offset =
4140 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004141
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004142 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004143 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4144 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4145 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004146 if (!kEmitCompilerReadBarrier) {
4147 is_type_check_slow_path_fatal =
4148 (type_check_kind == TypeCheckKind::kExactCheck ||
4149 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4150 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4151 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4152 !instruction->CanThrowIntoCatchBlock();
4153 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004154 SlowPathCodeARM64* type_check_slow_path =
4155 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4156 is_type_check_slow_path_fatal);
4157 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004158
Scott Wakeling97c72b72016-06-24 16:19:36 +01004159 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004160 // Avoid null check if we know obj is not null.
4161 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004162 __ Cbz(obj, &done);
4163 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004164
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004165 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004166 case TypeCheckKind::kExactCheck:
4167 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004168 // /* HeapReference<Class> */ temp = obj->klass_
4169 GenerateReferenceLoadTwoRegisters(instruction,
4170 temp_loc,
4171 obj_loc,
4172 class_offset,
4173 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004174 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004175
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004176 __ Cmp(temp, cls);
4177 // Jump to slow path for throwing the exception or doing a
4178 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004179 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004180 break;
4181 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004182
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004183 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004184 // /* HeapReference<Class> */ temp = obj->klass_
4185 GenerateReferenceLoadTwoRegisters(instruction,
4186 temp_loc,
4187 obj_loc,
4188 class_offset,
4189 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004190 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004191
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004192 // If the class is abstract, we eagerly fetch the super class of the
4193 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004194 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004195 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004196 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004197 GenerateReferenceLoadOneRegister(instruction,
4198 temp_loc,
4199 super_offset,
4200 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004201 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004202
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004203 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4204 // exception.
4205 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4206 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004207 __ Cmp(temp, cls);
4208 __ B(ne, &loop);
4209 break;
4210 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004211
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004212 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004213 // /* HeapReference<Class> */ temp = obj->klass_
4214 GenerateReferenceLoadTwoRegisters(instruction,
4215 temp_loc,
4216 obj_loc,
4217 class_offset,
4218 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004219 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004220
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004221 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004222 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004223 __ Bind(&loop);
4224 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004225 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004226
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004227 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004228 GenerateReferenceLoadOneRegister(instruction,
4229 temp_loc,
4230 super_offset,
4231 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004232 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004233
4234 // If the class reference currently in `temp` is not null, jump
4235 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004236 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004237 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004238 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004239 break;
4240 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004241
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004242 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004243 // /* HeapReference<Class> */ temp = obj->klass_
4244 GenerateReferenceLoadTwoRegisters(instruction,
4245 temp_loc,
4246 obj_loc,
4247 class_offset,
4248 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004249 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004250
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004251 // Do an exact check.
4252 __ Cmp(temp, cls);
4253 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004254
4255 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004256 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004257 GenerateReferenceLoadOneRegister(instruction,
4258 temp_loc,
4259 component_offset,
4260 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004261 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004262
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004263 // If the component type is null, jump to the slow path to throw the exception.
4264 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4265 // Otherwise, the object is indeed an array. Further check that this component type is not a
4266 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004267 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4268 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004269 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004270 break;
4271 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004272
Calin Juravle98893e12015-10-02 21:05:03 +01004273 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004274 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004275 //
4276 // We cannot directly call the CheckCast runtime entry point
4277 // without resorting to a type checking slow path here (i.e. by
4278 // calling InvokeRuntime directly), as it would require to
4279 // assign fixed registers for the inputs of this HInstanceOf
4280 // instruction (following the runtime calling convention), which
4281 // might be cluttered by the potential first read barrier
4282 // emission at the beginning of this method.
4283 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004284 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004285 case TypeCheckKind::kInterfaceCheck: {
4286 // /* HeapReference<Class> */ temp = obj->klass_
4287 GenerateReferenceLoadTwoRegisters(instruction,
4288 temp_loc,
4289 obj_loc,
4290 class_offset,
4291 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004292 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004293
4294 // /* HeapReference<Class> */ temp = temp->iftable_
4295 GenerateReferenceLoadTwoRegisters(instruction,
4296 temp_loc,
4297 temp_loc,
4298 iftable_offset,
4299 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004300 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004301 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004302 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004303 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004304 vixl::aarch64::Label start_loop;
4305 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004306 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004307 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4308 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004309 // Go to next interface.
4310 __ Add(temp, temp, 2 * kHeapReferenceSize);
4311 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004312 // Compare the classes and continue the loop if they do not match.
4313 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4314 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004315 break;
4316 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004317 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004318 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004319
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004320 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004321}
4322
Alexandre Rames5319def2014-10-23 10:03:10 +01004323void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4324 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4325 locations->SetOut(Location::ConstantLocation(constant));
4326}
4327
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004328void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004329 // Will be generated at use site.
4330}
4331
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004332void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4333 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4334 locations->SetOut(Location::ConstantLocation(constant));
4335}
4336
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004337void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004338 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004339}
4340
Calin Juravle175dc732015-08-25 15:42:32 +01004341void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4342 // The trampoline uses the same calling convention as dex calling conventions,
4343 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4344 // the method_idx.
4345 HandleInvoke(invoke);
4346}
4347
4348void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4349 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4350}
4351
Alexandre Rames5319def2014-10-23 10:03:10 +01004352void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004353 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004354 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004355}
4356
Alexandre Rames67555f72014-11-18 10:55:16 +00004357void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4358 HandleInvoke(invoke);
4359}
4360
4361void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4362 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004363 LocationSummary* locations = invoke->GetLocations();
4364 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004365 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004366 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004367 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004368
4369 // The register ip1 is required to be used for the hidden argument in
4370 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004371 MacroAssembler* masm = GetVIXLAssembler();
4372 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004373 scratch_scope.Exclude(ip1);
4374 __ Mov(ip1, invoke->GetDexMethodIndex());
4375
Artem Serov914d7a82017-02-07 14:33:49 +00004376 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004377 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004378 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004379 {
4380 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4381 // /* HeapReference<Class> */ temp = temp->klass_
4382 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4383 codegen_->MaybeRecordImplicitNullCheck(invoke);
4384 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004385 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004386 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004387 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004388 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004389 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004390 }
Artem Serov914d7a82017-02-07 14:33:49 +00004391
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004392 // Instead of simply (possibly) unpoisoning `temp` here, we should
4393 // emit a read barrier for the previous class reference load.
4394 // However this is not required in practice, as this is an
4395 // intermediate/temporary reference and because the current
4396 // concurrent copying collector keeps the from-space memory
4397 // intact/accessible until the end of the marking phase (the
4398 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004399 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004400 __ Ldr(temp,
4401 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4402 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004403 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004404 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004405 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004406 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004407 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004408
4409 {
4410 // Ensure the pc position is recorded immediately after the `blr` instruction.
4411 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4412
4413 // lr();
4414 __ blr(lr);
4415 DCHECK(!codegen_->IsLeafMethod());
4416 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4417 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004418}
4419
4420void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004421 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004422 if (intrinsic.TryDispatch(invoke)) {
4423 return;
4424 }
4425
Alexandre Rames67555f72014-11-18 10:55:16 +00004426 HandleInvoke(invoke);
4427}
4428
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004429void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004430 // Explicit clinit checks triggered by static invokes must have been pruned by
4431 // art::PrepareForRegisterAllocation.
4432 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004433
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004434 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004435 if (intrinsic.TryDispatch(invoke)) {
4436 return;
4437 }
4438
Alexandre Rames67555f72014-11-18 10:55:16 +00004439 HandleInvoke(invoke);
4440}
4441
Andreas Gampe878d58c2015-01-15 23:24:00 -08004442static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4443 if (invoke->GetLocations()->Intrinsified()) {
4444 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4445 intrinsic.Dispatch(invoke);
4446 return true;
4447 }
4448 return false;
4449}
4450
Vladimir Markodc151b22015-10-15 18:02:30 +01004451HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4452 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004453 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004454 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004455 return desired_dispatch_info;
4456}
4457
TatWai Chongd8c052a2016-11-02 16:12:48 +08004458Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4459 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004460 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004461 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4462 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004463 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4464 uint32_t offset =
4465 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004466 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004467 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004468 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004469 }
Vladimir Marko58155012015-08-19 12:49:41 +00004470 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004471 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004472 break;
4473 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4474 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004475 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004476 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004477 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4478 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004479 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004480 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004481 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004482 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004483 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004484 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004485 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004486 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004487 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004488 }
Vladimir Marko58155012015-08-19 12:49:41 +00004489 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004490 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004491 Register reg = XRegisterFrom(temp);
4492 Register method_reg;
4493 if (current_method.IsRegister()) {
4494 method_reg = XRegisterFrom(current_method);
4495 } else {
4496 DCHECK(invoke->GetLocations()->Intrinsified());
4497 DCHECK(!current_method.IsValid());
4498 method_reg = reg;
4499 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4500 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004501
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004502 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004503 __ Ldr(reg.X(),
4504 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004505 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004506 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004507 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4508 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004509 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4510 break;
4511 }
4512 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004513 return callee_method;
4514}
4515
4516void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4517 // All registers are assumed to be correctly set up.
4518 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004519
4520 switch (invoke->GetCodePtrLocation()) {
4521 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4522 __ Bl(&frame_entry_label_);
4523 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004524 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4525 // LR = callee_method->entry_point_from_quick_compiled_code_;
4526 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004527 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004528 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004529 {
4530 // To ensure that the pc position is recorded immediately after the `blr` instruction
4531 // BLR must be the last instruction emitted in this function.
4532 // Recording the pc will occur right after returning from this function.
4533 ExactAssemblyScope eas(GetVIXLAssembler(),
4534 kInstructionSize,
4535 CodeBufferCheckScope::kExactSize);
4536 // lr()
4537 __ blr(lr);
4538 }
Vladimir Marko58155012015-08-19 12:49:41 +00004539 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004540 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004541
Andreas Gampe878d58c2015-01-15 23:24:00 -08004542 DCHECK(!IsLeafMethod());
4543}
4544
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004545void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004546 // Use the calling convention instead of the location of the receiver, as
4547 // intrinsics may have put the receiver in a different register. In the intrinsics
4548 // slow path, the arguments have been moved to the right place, so here we are
4549 // guaranteed that the receiver is the first register of the calling convention.
4550 InvokeDexCallingConvention calling_convention;
4551 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004552 Register temp = XRegisterFrom(temp_in);
4553 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4554 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4555 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004556 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004557
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004558 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004559
4560 {
4561 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4562 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4563 // /* HeapReference<Class> */ temp = receiver->klass_
4564 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4565 MaybeRecordImplicitNullCheck(invoke);
4566 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004567 // Instead of simply (possibly) unpoisoning `temp` here, we should
4568 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004569 // intermediate/temporary reference and because the current
4570 // concurrent copying collector keeps the from-space memory
4571 // intact/accessible until the end of the marking phase (the
4572 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004573 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4574 // temp = temp->GetMethodAt(method_offset);
4575 __ Ldr(temp, MemOperand(temp, method_offset));
4576 // lr = temp->GetEntryPoint();
4577 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004578 {
4579 // To ensure that the pc position is recorded immediately after the `blr` instruction
4580 // BLR should be the last instruction emitted in this function.
4581 // Recording the pc will occur right after returning from this function.
4582 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4583 // lr();
4584 __ blr(lr);
4585 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004586}
4587
Orion Hodsonac141392017-01-13 11:53:47 +00004588void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4589 HandleInvoke(invoke);
4590}
4591
4592void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4593 codegen_->GenerateInvokePolymorphicCall(invoke);
4594}
4595
Scott Wakeling97c72b72016-06-24 16:19:36 +01004596vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4597 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004598 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004599 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004600 return
4601 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004602}
4603
Scott Wakeling97c72b72016-06-24 16:19:36 +01004604vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4605 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004606 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004607 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004608 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004609}
4610
Vladimir Marko1998cd02017-01-13 13:02:58 +00004611vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4612 const DexFile& dex_file,
4613 dex::TypeIndex type_index,
4614 vixl::aarch64::Label* adrp_label) {
4615 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4616}
4617
Scott Wakeling97c72b72016-06-24 16:19:36 +01004618vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4619 const DexFile& dex_file,
4620 uint32_t element_offset,
4621 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004622 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4623}
4624
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004625vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4626 baker_read_barrier_patches_.emplace_back(custom_data);
4627 return &baker_read_barrier_patches_.back().label;
4628}
4629
Scott Wakeling97c72b72016-06-24 16:19:36 +01004630vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4631 const DexFile& dex_file,
4632 uint32_t offset_or_index,
4633 vixl::aarch64::Label* adrp_label,
4634 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004635 // Add a patch entry and return the label.
4636 patches->emplace_back(dex_file, offset_or_index);
4637 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004638 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004639 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4640 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4641 return label;
4642}
4643
Scott Wakeling97c72b72016-06-24 16:19:36 +01004644vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004645 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004646 return boot_image_string_patches_.GetOrCreate(
4647 StringReference(&dex_file, string_index),
4648 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4649}
4650
Scott Wakeling97c72b72016-06-24 16:19:36 +01004651vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004652 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004653 return boot_image_type_patches_.GetOrCreate(
4654 TypeReference(&dex_file, type_index),
4655 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4656}
4657
Scott Wakeling97c72b72016-06-24 16:19:36 +01004658vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4659 uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00004660 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004661}
4662
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004663vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004664 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4665 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4666 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004667 return jit_string_patches_.GetOrCreate(
4668 StringReference(&dex_file, string_index),
4669 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4670}
4671
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004672vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004673 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4674 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4675 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004676 return jit_class_patches_.GetOrCreate(
4677 TypeReference(&dex_file, type_index),
4678 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4679}
4680
Vladimir Markoaad75c62016-10-03 08:46:48 +00004681void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4682 vixl::aarch64::Register reg) {
4683 DCHECK(reg.IsX());
4684 SingleEmissionCheckScope guard(GetVIXLAssembler());
4685 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004686 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004687}
4688
4689void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4690 vixl::aarch64::Register out,
4691 vixl::aarch64::Register base) {
4692 DCHECK(out.IsX());
4693 DCHECK(base.IsX());
4694 SingleEmissionCheckScope guard(GetVIXLAssembler());
4695 __ Bind(fixup_label);
4696 __ add(out, base, Operand(/* offset placeholder */ 0));
4697}
4698
4699void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4700 vixl::aarch64::Register out,
4701 vixl::aarch64::Register base) {
4702 DCHECK(base.IsX());
4703 SingleEmissionCheckScope guard(GetVIXLAssembler());
4704 __ Bind(fixup_label);
4705 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4706}
4707
4708template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4709inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4710 const ArenaDeque<PcRelativePatchInfo>& infos,
4711 ArenaVector<LinkerPatch>* linker_patches) {
4712 for (const PcRelativePatchInfo& info : infos) {
4713 linker_patches->push_back(Factory(info.label.GetLocation(),
4714 &info.target_dex_file,
4715 info.pc_insn_label->GetLocation(),
4716 info.offset_or_index));
4717 }
4718}
4719
Vladimir Marko58155012015-08-19 12:49:41 +00004720void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4721 DCHECK(linker_patches->empty());
4722 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004723 pc_relative_dex_cache_patches_.size() +
4724 boot_image_string_patches_.size() +
4725 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004726 boot_image_type_patches_.size() +
4727 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004728 type_bss_entry_patches_.size() +
4729 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004730 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004731 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004732 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004733 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004734 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004735 info.offset_or_index));
4736 }
4737 for (const auto& entry : boot_image_string_patches_) {
4738 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004739 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4740 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004741 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004742 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004743 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004744 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004745 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004746 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4747 linker_patches);
4748 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004749 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4750 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004751 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4752 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004753 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004754 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4755 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004756 for (const auto& entry : boot_image_type_patches_) {
4757 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004758 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4759 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004760 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004761 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004762 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004763 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
4764 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
4765 info.custom_data));
4766 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004767 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004768}
4769
Scott Wakeling97c72b72016-06-24 16:19:36 +01004770vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004771 Uint32ToLiteralMap* map) {
4772 return map->GetOrCreate(
4773 value,
4774 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4775}
4776
Scott Wakeling97c72b72016-06-24 16:19:36 +01004777vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004778 return uint64_literals_.GetOrCreate(
4779 value,
4780 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004781}
4782
Scott Wakeling97c72b72016-06-24 16:19:36 +01004783vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004784 MethodReference target_method,
4785 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004786 return map->GetOrCreate(
4787 target_method,
4788 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004789}
4790
Andreas Gampe878d58c2015-01-15 23:24:00 -08004791void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004792 // Explicit clinit checks triggered by static invokes must have been pruned by
4793 // art::PrepareForRegisterAllocation.
4794 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004795
Andreas Gampe878d58c2015-01-15 23:24:00 -08004796 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4797 return;
4798 }
4799
Artem Serov914d7a82017-02-07 14:33:49 +00004800 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4801 // are no pools emitted.
4802 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004803 LocationSummary* locations = invoke->GetLocations();
4804 codegen_->GenerateStaticOrDirectCall(
4805 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004806 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004807}
4808
4809void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004810 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4811 return;
4812 }
4813
Artem Serov914d7a82017-02-07 14:33:49 +00004814 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4815 // are no pools emitted.
4816 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004817 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004818 DCHECK(!codegen_->IsLeafMethod());
4819 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4820}
4821
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004822HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4823 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004824 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004825 case HLoadClass::LoadKind::kInvalid:
4826 LOG(FATAL) << "UNREACHABLE";
4827 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004828 case HLoadClass::LoadKind::kReferrersClass:
4829 break;
4830 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4831 DCHECK(!GetCompilerOptions().GetCompilePic());
4832 break;
4833 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4834 DCHECK(GetCompilerOptions().GetCompilePic());
4835 break;
4836 case HLoadClass::LoadKind::kBootImageAddress:
4837 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004838 case HLoadClass::LoadKind::kBssEntry:
4839 DCHECK(!Runtime::Current()->UseJitCompilation());
4840 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004841 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004842 DCHECK(Runtime::Current()->UseJitCompilation());
4843 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004844 case HLoadClass::LoadKind::kDexCacheViaMethod:
4845 break;
4846 }
4847 return desired_class_load_kind;
4848}
4849
Alexandre Rames67555f72014-11-18 10:55:16 +00004850void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004851 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4852 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004853 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004854 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004855 cls,
4856 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004857 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004858 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004859 return;
4860 }
Vladimir Marko41559982017-01-06 14:04:23 +00004861 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004862
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004863 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4864 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004865 ? LocationSummary::kCallOnSlowPath
4866 : LocationSummary::kNoCall;
4867 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004868 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004869 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004870 }
4871
Vladimir Marko41559982017-01-06 14:04:23 +00004872 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004873 locations->SetInAt(0, Location::RequiresRegister());
4874 }
4875 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004876 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4877 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4878 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004879 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004880 RegisterSet caller_saves = RegisterSet::Empty();
4881 InvokeRuntimeCallingConvention calling_convention;
4882 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4883 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4884 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4885 Primitive::kPrimNot).GetCode());
4886 locations->SetCustomSlowPathCallerSaves(caller_saves);
4887 } else {
4888 // For non-Baker read barrier we have a temp-clobbering call.
4889 }
4890 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004891}
4892
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004893// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4894// move.
4895void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004896 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4897 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4898 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004899 return;
4900 }
Vladimir Marko41559982017-01-06 14:04:23 +00004901 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004902
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004903 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004904 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004905 Register bss_entry_temp;
4906 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004907
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004908 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4909 ? kWithoutReadBarrier
4910 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004911 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004912 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004913 case HLoadClass::LoadKind::kReferrersClass: {
4914 DCHECK(!cls->CanCallRuntime());
4915 DCHECK(!cls->MustGenerateClinitCheck());
4916 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4917 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004918 GenerateGcRootFieldLoad(cls,
4919 out_loc,
4920 current_method,
4921 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004922 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004923 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004924 break;
4925 }
4926 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004927 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004928 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4929 cls->GetTypeIndex()));
4930 break;
4931 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004932 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004933 // Add ADRP with its PC-relative type patch.
4934 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004935 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004936 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004937 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004938 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004939 vixl::aarch64::Label* add_label =
4940 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004941 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004942 break;
4943 }
4944 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004945 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004946 uint32_t address = dchecked_integral_cast<uint32_t>(
4947 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4948 DCHECK_NE(address, 0u);
4949 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004950 break;
4951 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004952 case HLoadClass::LoadKind::kBssEntry: {
4953 // Add ADRP with its PC-relative Class .bss entry patch.
4954 const DexFile& dex_file = cls->GetDexFile();
4955 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004956 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004957 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4958 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004959 // Add LDR with its PC-relative Class patch.
4960 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004961 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004962 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4963 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004964 out_loc,
4965 bss_entry_temp,
4966 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004967 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004968 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004969 generate_null_check = true;
4970 break;
4971 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004972 case HLoadClass::LoadKind::kJitTableAddress: {
4973 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4974 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004975 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004976 GenerateGcRootFieldLoad(cls,
4977 out_loc,
4978 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004979 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004980 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004981 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004982 break;
4983 }
Vladimir Marko41559982017-01-06 14:04:23 +00004984 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004985 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004986 LOG(FATAL) << "UNREACHABLE";
4987 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004988 }
4989
Vladimir Markoea4c1262017-02-06 19:59:33 +00004990 bool do_clinit = cls->MustGenerateClinitCheck();
4991 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004992 DCHECK(cls->CanCallRuntime());
4993 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00004994 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004995 codegen_->AddSlowPath(slow_path);
4996 if (generate_null_check) {
4997 __ Cbz(out, slow_path->GetEntryLabel());
4998 }
4999 if (cls->MustGenerateClinitCheck()) {
5000 GenerateClassInitializationCheck(slow_path, out);
5001 } else {
5002 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005003 }
5004 }
5005}
5006
David Brazdilcb1c0552015-08-04 16:22:25 +01005007static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005008 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005009}
5010
Alexandre Rames67555f72014-11-18 10:55:16 +00005011void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5012 LocationSummary* locations =
5013 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5014 locations->SetOut(Location::RequiresRegister());
5015}
5016
5017void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005018 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5019}
5020
5021void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
5022 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5023}
5024
5025void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5026 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005027}
5028
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005029HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5030 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005031 switch (desired_string_load_kind) {
5032 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5033 DCHECK(!GetCompilerOptions().GetCompilePic());
5034 break;
5035 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5036 DCHECK(GetCompilerOptions().GetCompilePic());
5037 break;
5038 case HLoadString::LoadKind::kBootImageAddress:
5039 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005040 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005041 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005042 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005043 case HLoadString::LoadKind::kJitTableAddress:
5044 DCHECK(Runtime::Current()->UseJitCompilation());
5045 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005046 case HLoadString::LoadKind::kDexCacheViaMethod:
5047 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005048 }
5049 return desired_string_load_kind;
5050}
5051
Alexandre Rames67555f72014-11-18 10:55:16 +00005052void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005053 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005054 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005055 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005056 InvokeRuntimeCallingConvention calling_convention;
5057 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5058 } else {
5059 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005060 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5061 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005062 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005063 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005064 RegisterSet caller_saves = RegisterSet::Empty();
5065 InvokeRuntimeCallingConvention calling_convention;
5066 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5067 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
5068 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
5069 Primitive::kPrimNot).GetCode());
5070 locations->SetCustomSlowPathCallerSaves(caller_saves);
5071 } else {
5072 // For non-Baker read barrier we have a temp-clobbering call.
5073 }
5074 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005075 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005076}
5077
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005078// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5079// move.
5080void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005081 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005082 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005083
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005084 switch (load->GetLoadKind()) {
5085 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005086 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5087 load->GetStringIndex()));
5088 return; // No dex cache slow path.
5089 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005090 // Add ADRP with its PC-relative String patch.
5091 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005092 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005093 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01005094 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005095 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005096 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005097 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005098 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005099 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005100 return; // No dex cache slow path.
5101 }
5102 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005103 uint32_t address = dchecked_integral_cast<uint32_t>(
5104 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5105 DCHECK_NE(address, 0u);
5106 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005107 return; // No dex cache slow path.
5108 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005109 case HLoadString::LoadKind::kBssEntry: {
5110 // Add ADRP with its PC-relative String .bss entry patch.
5111 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005112 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005113 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005114 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005115 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005116 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005117 // Add LDR with its PC-relative String patch.
5118 vixl::aarch64::Label* ldr_label =
5119 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005120 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005121 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005122 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005123 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005124 /* offset placeholder */ 0u,
5125 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005126 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005127 SlowPathCodeARM64* slow_path =
5128 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005129 codegen_->AddSlowPath(slow_path);
5130 __ Cbz(out.X(), slow_path->GetEntryLabel());
5131 __ Bind(slow_path->GetExitLabel());
5132 return;
5133 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005134 case HLoadString::LoadKind::kJitTableAddress: {
5135 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005136 load->GetStringIndex(),
5137 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005138 GenerateGcRootFieldLoad(load,
5139 out_loc,
5140 out.X(),
5141 /* offset */ 0,
5142 /* fixup_label */ nullptr,
5143 kCompilerReadBarrierOption);
5144 return;
5145 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005146 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005147 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005148 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005149
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005150 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005151 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005152 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005153 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005154 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5155 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005156}
5157
Alexandre Rames5319def2014-10-23 10:03:10 +01005158void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5159 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5160 locations->SetOut(Location::ConstantLocation(constant));
5161}
5162
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005163void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005164 // Will be generated at use site.
5165}
5166
Alexandre Rames67555f72014-11-18 10:55:16 +00005167void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5168 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005169 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005170 InvokeRuntimeCallingConvention calling_convention;
5171 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5172}
5173
5174void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005175 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005176 instruction,
5177 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005178 if (instruction->IsEnter()) {
5179 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5180 } else {
5181 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5182 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005183}
5184
Alexandre Rames42d641b2014-10-27 14:00:51 +00005185void LocationsBuilderARM64::VisitMul(HMul* mul) {
5186 LocationSummary* locations =
5187 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5188 switch (mul->GetResultType()) {
5189 case Primitive::kPrimInt:
5190 case Primitive::kPrimLong:
5191 locations->SetInAt(0, Location::RequiresRegister());
5192 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005193 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005194 break;
5195
5196 case Primitive::kPrimFloat:
5197 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005198 locations->SetInAt(0, Location::RequiresFpuRegister());
5199 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005200 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005201 break;
5202
5203 default:
5204 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5205 }
5206}
5207
5208void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5209 switch (mul->GetResultType()) {
5210 case Primitive::kPrimInt:
5211 case Primitive::kPrimLong:
5212 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5213 break;
5214
5215 case Primitive::kPrimFloat:
5216 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005217 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005218 break;
5219
5220 default:
5221 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5222 }
5223}
5224
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005225void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5226 LocationSummary* locations =
5227 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5228 switch (neg->GetResultType()) {
5229 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005230 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005231 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005232 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005233 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005234
5235 case Primitive::kPrimFloat:
5236 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005237 locations->SetInAt(0, Location::RequiresFpuRegister());
5238 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005239 break;
5240
5241 default:
5242 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5243 }
5244}
5245
5246void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5247 switch (neg->GetResultType()) {
5248 case Primitive::kPrimInt:
5249 case Primitive::kPrimLong:
5250 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5251 break;
5252
5253 case Primitive::kPrimFloat:
5254 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005255 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005256 break;
5257
5258 default:
5259 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5260 }
5261}
5262
5263void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5264 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005265 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005266 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005267 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005268 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5269 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005270}
5271
5272void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005273 // Note: if heap poisoning is enabled, the entry point takes cares
5274 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005275 QuickEntrypointEnum entrypoint =
5276 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5277 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005278 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005279}
5280
Alexandre Rames5319def2014-10-23 10:03:10 +01005281void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5282 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005283 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005284 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005285 if (instruction->IsStringAlloc()) {
5286 locations->AddTemp(LocationFrom(kArtMethodRegister));
5287 } else {
5288 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005289 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005290 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5291}
5292
5293void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005294 // Note: if heap poisoning is enabled, the entry point takes cares
5295 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005296 if (instruction->IsStringAlloc()) {
5297 // String is allocated through StringFactory. Call NewEmptyString entry point.
5298 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005299 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005300 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5301 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005302
5303 {
5304 // Ensure the pc position is recorded immediately after the `blr` instruction.
5305 ExactAssemblyScope eas(GetVIXLAssembler(),
5306 kInstructionSize,
5307 CodeBufferCheckScope::kExactSize);
5308 __ blr(lr);
5309 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5310 }
David Brazdil6de19382016-01-08 17:37:10 +00005311 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005312 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005313 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005314 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005315}
5316
5317void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5318 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005319 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005320 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005321}
5322
5323void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005324 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005325 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005326 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005327 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005328 break;
5329
5330 default:
5331 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5332 }
5333}
5334
David Brazdil66d126e2015-04-03 16:02:44 +01005335void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5336 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5337 locations->SetInAt(0, Location::RequiresRegister());
5338 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5339}
5340
5341void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005342 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005343}
5344
Alexandre Rames5319def2014-10-23 10:03:10 +01005345void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005346 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5347 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005348}
5349
Calin Juravle2ae48182016-03-16 14:05:09 +00005350void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5351 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005352 return;
5353 }
Artem Serov914d7a82017-02-07 14:33:49 +00005354 {
5355 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5356 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5357 Location obj = instruction->GetLocations()->InAt(0);
5358 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5359 RecordPcInfo(instruction, instruction->GetDexPc());
5360 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005361}
5362
Calin Juravle2ae48182016-03-16 14:05:09 +00005363void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005364 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005365 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005366
5367 LocationSummary* locations = instruction->GetLocations();
5368 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005369
5370 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005371}
5372
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005373void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005374 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005375}
5376
Alexandre Rames67555f72014-11-18 10:55:16 +00005377void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5378 HandleBinaryOp(instruction);
5379}
5380
5381void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5382 HandleBinaryOp(instruction);
5383}
5384
Alexandre Rames3e69f162014-12-10 10:36:50 +00005385void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5386 LOG(FATAL) << "Unreachable";
5387}
5388
5389void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5390 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5391}
5392
Alexandre Rames5319def2014-10-23 10:03:10 +01005393void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5394 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5395 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5396 if (location.IsStackSlot()) {
5397 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5398 } else if (location.IsDoubleStackSlot()) {
5399 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5400 }
5401 locations->SetOut(location);
5402}
5403
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005404void InstructionCodeGeneratorARM64::VisitParameterValue(
5405 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005406 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005407}
5408
5409void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5410 LocationSummary* locations =
5411 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005412 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005413}
5414
5415void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5416 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5417 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005418}
5419
5420void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5421 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005422 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005423 locations->SetInAt(i, Location::Any());
5424 }
5425 locations->SetOut(Location::Any());
5426}
5427
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005428void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005429 LOG(FATAL) << "Unreachable";
5430}
5431
Serban Constantinescu02164b32014-11-13 14:05:07 +00005432void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005433 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005434 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005435 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5436 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005437 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5438
5439 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005440 case Primitive::kPrimInt:
5441 case Primitive::kPrimLong:
5442 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005443 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005444 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5445 break;
5446
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005447 case Primitive::kPrimFloat:
5448 case Primitive::kPrimDouble: {
5449 InvokeRuntimeCallingConvention calling_convention;
5450 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5451 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5452 locations->SetOut(calling_convention.GetReturnLocation(type));
5453
5454 break;
5455 }
5456
Serban Constantinescu02164b32014-11-13 14:05:07 +00005457 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005458 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005459 }
5460}
5461
5462void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5463 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005464
Serban Constantinescu02164b32014-11-13 14:05:07 +00005465 switch (type) {
5466 case Primitive::kPrimInt:
5467 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005468 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005469 break;
5470 }
5471
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005472 case Primitive::kPrimFloat:
5473 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005474 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5475 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005476 if (type == Primitive::kPrimFloat) {
5477 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5478 } else {
5479 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5480 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005481 break;
5482 }
5483
Serban Constantinescu02164b32014-11-13 14:05:07 +00005484 default:
5485 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005486 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005487 }
5488}
5489
Igor Murashkind01745e2017-04-05 16:40:31 -07005490void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5491 constructor_fence->SetLocations(nullptr);
5492}
5493
5494void InstructionCodeGeneratorARM64::VisitConstructorFence(
5495 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5496 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5497}
5498
Calin Juravle27df7582015-04-17 19:12:31 +01005499void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5500 memory_barrier->SetLocations(nullptr);
5501}
5502
5503void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005504 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005505}
5506
Alexandre Rames5319def2014-10-23 10:03:10 +01005507void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5508 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5509 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005510 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005511}
5512
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005513void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005514 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005515}
5516
5517void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5518 instruction->SetLocations(nullptr);
5519}
5520
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005521void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005522 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005523}
5524
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005525void LocationsBuilderARM64::VisitRor(HRor* ror) {
5526 HandleBinaryOp(ror);
5527}
5528
5529void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5530 HandleBinaryOp(ror);
5531}
5532
Serban Constantinescu02164b32014-11-13 14:05:07 +00005533void LocationsBuilderARM64::VisitShl(HShl* shl) {
5534 HandleShift(shl);
5535}
5536
5537void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5538 HandleShift(shl);
5539}
5540
5541void LocationsBuilderARM64::VisitShr(HShr* shr) {
5542 HandleShift(shr);
5543}
5544
5545void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5546 HandleShift(shr);
5547}
5548
Alexandre Rames5319def2014-10-23 10:03:10 +01005549void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005550 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005551}
5552
5553void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005554 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005555}
5556
Alexandre Rames67555f72014-11-18 10:55:16 +00005557void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005558 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005559}
5560
5561void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005562 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005563}
5564
5565void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005566 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005567}
5568
Alexandre Rames67555f72014-11-18 10:55:16 +00005569void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005570 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005571}
5572
Calin Juravlee460d1d2015-09-29 04:52:17 +01005573void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5574 HUnresolvedInstanceFieldGet* instruction) {
5575 FieldAccessCallingConventionARM64 calling_convention;
5576 codegen_->CreateUnresolvedFieldLocationSummary(
5577 instruction, instruction->GetFieldType(), calling_convention);
5578}
5579
5580void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5581 HUnresolvedInstanceFieldGet* instruction) {
5582 FieldAccessCallingConventionARM64 calling_convention;
5583 codegen_->GenerateUnresolvedFieldAccess(instruction,
5584 instruction->GetFieldType(),
5585 instruction->GetFieldIndex(),
5586 instruction->GetDexPc(),
5587 calling_convention);
5588}
5589
5590void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5591 HUnresolvedInstanceFieldSet* instruction) {
5592 FieldAccessCallingConventionARM64 calling_convention;
5593 codegen_->CreateUnresolvedFieldLocationSummary(
5594 instruction, instruction->GetFieldType(), calling_convention);
5595}
5596
5597void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5598 HUnresolvedInstanceFieldSet* instruction) {
5599 FieldAccessCallingConventionARM64 calling_convention;
5600 codegen_->GenerateUnresolvedFieldAccess(instruction,
5601 instruction->GetFieldType(),
5602 instruction->GetFieldIndex(),
5603 instruction->GetDexPc(),
5604 calling_convention);
5605}
5606
5607void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5608 HUnresolvedStaticFieldGet* instruction) {
5609 FieldAccessCallingConventionARM64 calling_convention;
5610 codegen_->CreateUnresolvedFieldLocationSummary(
5611 instruction, instruction->GetFieldType(), calling_convention);
5612}
5613
5614void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5615 HUnresolvedStaticFieldGet* instruction) {
5616 FieldAccessCallingConventionARM64 calling_convention;
5617 codegen_->GenerateUnresolvedFieldAccess(instruction,
5618 instruction->GetFieldType(),
5619 instruction->GetFieldIndex(),
5620 instruction->GetDexPc(),
5621 calling_convention);
5622}
5623
5624void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5625 HUnresolvedStaticFieldSet* instruction) {
5626 FieldAccessCallingConventionARM64 calling_convention;
5627 codegen_->CreateUnresolvedFieldLocationSummary(
5628 instruction, instruction->GetFieldType(), calling_convention);
5629}
5630
5631void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5632 HUnresolvedStaticFieldSet* instruction) {
5633 FieldAccessCallingConventionARM64 calling_convention;
5634 codegen_->GenerateUnresolvedFieldAccess(instruction,
5635 instruction->GetFieldType(),
5636 instruction->GetFieldIndex(),
5637 instruction->GetDexPc(),
5638 calling_convention);
5639}
5640
Alexandre Rames5319def2014-10-23 10:03:10 +01005641void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005642 LocationSummary* locations =
5643 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005644 // In suspend check slow path, usually there are no caller-save registers at all.
5645 // If SIMD instructions are present, however, we force spilling all live SIMD
5646 // registers in full width (since the runtime only saves/restores lower part).
5647 locations->SetCustomSlowPathCallerSaves(
5648 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005649}
5650
5651void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005652 HBasicBlock* block = instruction->GetBlock();
5653 if (block->GetLoopInformation() != nullptr) {
5654 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5655 // The back edge will generate the suspend check.
5656 return;
5657 }
5658 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5659 // The goto will generate the suspend check.
5660 return;
5661 }
5662 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005663}
5664
Alexandre Rames67555f72014-11-18 10:55:16 +00005665void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5666 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005667 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005668 InvokeRuntimeCallingConvention calling_convention;
5669 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5670}
5671
5672void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005673 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005674 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005675}
5676
5677void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5678 LocationSummary* locations =
5679 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5680 Primitive::Type input_type = conversion->GetInputType();
5681 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005682 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005683 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5684 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5685 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5686 }
5687
Alexandre Rames542361f2015-01-29 16:57:31 +00005688 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005689 locations->SetInAt(0, Location::RequiresFpuRegister());
5690 } else {
5691 locations->SetInAt(0, Location::RequiresRegister());
5692 }
5693
Alexandre Rames542361f2015-01-29 16:57:31 +00005694 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005695 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5696 } else {
5697 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5698 }
5699}
5700
5701void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5702 Primitive::Type result_type = conversion->GetResultType();
5703 Primitive::Type input_type = conversion->GetInputType();
5704
5705 DCHECK_NE(input_type, result_type);
5706
Alexandre Rames542361f2015-01-29 16:57:31 +00005707 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005708 int result_size = Primitive::ComponentSize(result_type);
5709 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005710 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005711 Register output = OutputRegister(conversion);
5712 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005713 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005714 // 'int' values are used directly as W registers, discarding the top
5715 // bits, so we don't need to sign-extend and can just perform a move.
5716 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5717 // top 32 bits of the target register. We theoretically could leave those
5718 // bits unchanged, but we would have to make sure that no code uses a
5719 // 32bit input value as a 64bit value assuming that the top 32 bits are
5720 // zero.
5721 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005722 } else if (result_type == Primitive::kPrimChar ||
5723 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5724 __ Ubfx(output,
5725 output.IsX() ? source.X() : source.W(),
5726 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005727 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005728 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005729 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005730 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005731 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005732 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005733 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5734 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005735 } else if (Primitive::IsFloatingPointType(result_type) &&
5736 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005737 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5738 } else {
5739 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5740 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005741 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005742}
Alexandre Rames67555f72014-11-18 10:55:16 +00005743
Serban Constantinescu02164b32014-11-13 14:05:07 +00005744void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5745 HandleShift(ushr);
5746}
5747
5748void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5749 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005750}
5751
5752void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5753 HandleBinaryOp(instruction);
5754}
5755
5756void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5757 HandleBinaryOp(instruction);
5758}
5759
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005760void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005761 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005762 LOG(FATAL) << "Unreachable";
5763}
5764
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005765void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005766 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005767 LOG(FATAL) << "Unreachable";
5768}
5769
Mark Mendellfe57faa2015-09-18 09:26:15 -04005770// Simple implementation of packed switch - generate cascaded compare/jumps.
5771void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5772 LocationSummary* locations =
5773 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5774 locations->SetInAt(0, Location::RequiresRegister());
5775}
5776
5777void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5778 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005779 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005780 Register value_reg = InputRegisterAt(switch_instr, 0);
5781 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5782
Zheng Xu3927c8b2015-11-18 17:46:25 +08005783 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005784 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005785 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5786 // make sure we don't emit it if the target may run out of range.
5787 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5788 // ranges and emit the tables only as required.
5789 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005790
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005791 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005792 // Current instruction id is an upper bound of the number of HIRs in the graph.
5793 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5794 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005795 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5796 Register temp = temps.AcquireW();
5797 __ Subs(temp, value_reg, Operand(lower_bound));
5798
Zheng Xu3927c8b2015-11-18 17:46:25 +08005799 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005800 // Jump to successors[0] if value == lower_bound.
5801 __ B(eq, codegen_->GetLabelOf(successors[0]));
5802 int32_t last_index = 0;
5803 for (; num_entries - last_index > 2; last_index += 2) {
5804 __ Subs(temp, temp, Operand(2));
5805 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5806 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5807 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5808 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5809 }
5810 if (num_entries - last_index == 2) {
5811 // The last missing case_value.
5812 __ Cmp(temp, Operand(1));
5813 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005814 }
5815
5816 // And the default for any other value.
5817 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5818 __ B(codegen_->GetLabelOf(default_block));
5819 }
5820 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005821 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005822
5823 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5824
5825 // Below instructions should use at most one blocked register. Since there are two blocked
5826 // registers, we are free to block one.
5827 Register temp_w = temps.AcquireW();
5828 Register index;
5829 // Remove the bias.
5830 if (lower_bound != 0) {
5831 index = temp_w;
5832 __ Sub(index, value_reg, Operand(lower_bound));
5833 } else {
5834 index = value_reg;
5835 }
5836
5837 // Jump to default block if index is out of the range.
5838 __ Cmp(index, Operand(num_entries));
5839 __ B(hs, codegen_->GetLabelOf(default_block));
5840
5841 // In current VIXL implementation, it won't require any blocked registers to encode the
5842 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5843 // register pressure.
5844 Register table_base = temps.AcquireX();
5845 // Load jump offset from the table.
5846 __ Adr(table_base, jump_table->GetTableStartLabel());
5847 Register jump_offset = temp_w;
5848 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5849
5850 // Jump to target block by branching to table_base(pc related) + offset.
5851 Register target_address = table_base;
5852 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5853 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005854 }
5855}
5856
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005857void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5858 HInstruction* instruction,
5859 Location out,
5860 uint32_t offset,
5861 Location maybe_temp,
5862 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005863 Primitive::Type type = Primitive::kPrimNot;
5864 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005865 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005866 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005867 if (kUseBakerReadBarrier) {
5868 // Load with fast path based Baker's read barrier.
5869 // /* HeapReference<Object> */ out = *(out + offset)
5870 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5871 out,
5872 out_reg,
5873 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005874 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005875 /* needs_null_check */ false,
5876 /* use_load_acquire */ false);
5877 } else {
5878 // Load with slow path based read barrier.
5879 // Save the value of `out` into `maybe_temp` before overwriting it
5880 // in the following move operation, as we will need it for the
5881 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005882 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005883 __ Mov(temp_reg, out_reg);
5884 // /* HeapReference<Object> */ out = *(out + offset)
5885 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5886 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5887 }
5888 } else {
5889 // Plain load with no read barrier.
5890 // /* HeapReference<Object> */ out = *(out + offset)
5891 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5892 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5893 }
5894}
5895
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005896void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5897 HInstruction* instruction,
5898 Location out,
5899 Location obj,
5900 uint32_t offset,
5901 Location maybe_temp,
5902 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005903 Primitive::Type type = Primitive::kPrimNot;
5904 Register out_reg = RegisterFrom(out, type);
5905 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005906 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005907 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005908 if (kUseBakerReadBarrier) {
5909 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005910 // /* HeapReference<Object> */ out = *(obj + offset)
5911 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5912 out,
5913 obj_reg,
5914 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005915 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005916 /* needs_null_check */ false,
5917 /* use_load_acquire */ false);
5918 } else {
5919 // Load with slow path based read barrier.
5920 // /* HeapReference<Object> */ out = *(obj + offset)
5921 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5922 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5923 }
5924 } else {
5925 // Plain load with no read barrier.
5926 // /* HeapReference<Object> */ out = *(obj + offset)
5927 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5928 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5929 }
5930}
5931
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005932void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5933 HInstruction* instruction,
5934 Location root,
5935 Register obj,
5936 uint32_t offset,
5937 vixl::aarch64::Label* fixup_label,
5938 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005939 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005940 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005941 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005942 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005943 if (kUseBakerReadBarrier) {
5944 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005945 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005946 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5947 !Runtime::Current()->UseJitCompilation()) {
5948 // Note that we do not actually check the value of `GetIsGcMarking()`
5949 // to decide whether to mark the loaded GC root or not. Instead, we
Vladimir Marko66d691d2017-04-07 17:53:39 +01005950 // load into `temp` (actually IP1) the read barrier mark introspection
5951 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
5952 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005953 //
5954 // We use link-time generated thunks for the slow path. That thunk
5955 // checks the reference and jumps to the entrypoint if needed.
5956 //
5957 // temp = Thread::Current()->pReadBarrierMarkIntrospection
5958 // lr = &return_address;
5959 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5960 // if (temp != nullptr) {
5961 // goto gc_root_thunk<root_reg>(lr)
5962 // }
5963 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00005964
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005965 UseScratchRegisterScope temps(GetVIXLAssembler());
5966 DCHECK(temps.IsAvailable(ip0));
5967 DCHECK(temps.IsAvailable(ip1));
5968 temps.Exclude(ip0, ip1);
5969 uint32_t custom_data =
5970 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
5971 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00005972
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005973 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
5974 DCHECK_EQ(ip0.GetCode(), 16u);
5975 const int32_t entry_point_offset =
5976 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
5977 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
5978 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5979 vixl::aarch64::Label return_address;
5980 __ adr(lr, &return_address);
5981 if (fixup_label != nullptr) {
5982 __ Bind(fixup_label);
5983 }
5984 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
5985 "GC root LDR must be 2 instruction (8B) before the return address label.");
5986 __ ldr(root_reg, MemOperand(obj.X(), offset));
5987 __ Bind(cbnz_label);
5988 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
5989 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005990 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005991 // Note that we do not actually check the value of
5992 // `GetIsGcMarking()` to decide whether to mark the loaded GC
5993 // root or not. Instead, we load into `temp` the read barrier
5994 // mark entry point corresponding to register `root`. If `temp`
5995 // is null, it means that `GetIsGcMarking()` is false, and vice
5996 // versa.
5997 //
5998 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5999 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6000 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6001 // // Slow path.
6002 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
6003 // }
Roland Levillain44015862016-01-22 11:47:17 +00006004
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006005 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
6006 Register temp = lr;
6007 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
6008 instruction, root, /* entrypoint */ LocationFrom(temp));
6009 codegen_->AddSlowPath(slow_path);
6010
6011 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6012 const int32_t entry_point_offset =
6013 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
6014 // Loading the entrypoint does not require a load acquire since it is only changed when
6015 // threads are suspended or running a checkpoint.
6016 __ Ldr(temp, MemOperand(tr, entry_point_offset));
6017
6018 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6019 if (fixup_label == nullptr) {
6020 __ Ldr(root_reg, MemOperand(obj, offset));
6021 } else {
6022 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
6023 }
6024 static_assert(
6025 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6026 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6027 "have different sizes.");
6028 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6029 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6030 "have different sizes.");
6031
6032 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6033 // checking GetIsGcMarking.
6034 __ Cbnz(temp, slow_path->GetEntryLabel());
6035 __ Bind(slow_path->GetExitLabel());
6036 }
Roland Levillain44015862016-01-22 11:47:17 +00006037 } else {
6038 // GC root loaded through a slow path for read barriers other
6039 // than Baker's.
6040 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006041 if (fixup_label == nullptr) {
6042 __ Add(root_reg.X(), obj.X(), offset);
6043 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006044 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006045 }
Roland Levillain44015862016-01-22 11:47:17 +00006046 // /* mirror::Object* */ root = root->Read()
6047 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6048 }
6049 } else {
6050 // Plain GC root load with no read barrier.
6051 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006052 if (fixup_label == nullptr) {
6053 __ Ldr(root_reg, MemOperand(obj, offset));
6054 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006055 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006056 }
Roland Levillain44015862016-01-22 11:47:17 +00006057 // Note that GC roots are not affected by heap poisoning, thus we
6058 // do not have to unpoison `root_reg` here.
6059 }
6060}
6061
6062void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6063 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006064 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006065 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006066 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006067 bool needs_null_check,
6068 bool use_load_acquire) {
6069 DCHECK(kEmitCompilerReadBarrier);
6070 DCHECK(kUseBakerReadBarrier);
6071
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006072 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6073 !use_load_acquire &&
6074 !Runtime::Current()->UseJitCompilation()) {
6075 // Note that we do not actually check the value of `GetIsGcMarking()`
Vladimir Marko66d691d2017-04-07 17:53:39 +01006076 // to decide whether to mark the loaded reference or not. Instead, we
6077 // load into `temp` (actually IP1) the read barrier mark introspection
6078 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6079 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006080 //
6081 // We use link-time generated thunks for the slow path. That thunk checks
6082 // the holder and jumps to the entrypoint if needed. If the holder is not
6083 // gray, it creates a fake dependency and returns to the LDR instruction.
6084 //
6085 // temp = Thread::Current()->pReadBarrierMarkIntrospection
Vladimir Marko66d691d2017-04-07 17:53:39 +01006086 // lr = &gray_return_address;
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006087 // if (temp != nullptr) {
6088 // goto field_thunk<holder_reg, base_reg>(lr)
6089 // }
6090 // not_gray_return_address:
6091 // // Original reference load. If the offset is too large to fit
6092 // // into LDR, we use an adjusted base register here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006093 // GcRoot<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006094 // gray_return_address:
6095
6096 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6097 Register base = obj;
6098 if (offset >= kReferenceLoadMinFarOffset) {
6099 DCHECK(maybe_temp.IsRegister());
6100 base = WRegisterFrom(maybe_temp);
6101 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6102 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6103 offset &= (kReferenceLoadMinFarOffset - 1u);
6104 }
6105 UseScratchRegisterScope temps(GetVIXLAssembler());
6106 DCHECK(temps.IsAvailable(ip0));
6107 DCHECK(temps.IsAvailable(ip1));
6108 temps.Exclude(ip0, ip1);
6109 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6110 base.GetCode(),
6111 obj.GetCode());
6112 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6113
6114 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6115 DCHECK_EQ(ip0.GetCode(), 16u);
6116 const int32_t entry_point_offset =
6117 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6118 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
Vladimir Markod1ef8732017-04-18 13:55:13 +01006119 EmissionCheckScope guard(GetVIXLAssembler(),
6120 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006121 vixl::aarch64::Label return_address;
6122 __ adr(lr, &return_address);
6123 __ Bind(cbnz_label);
6124 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markod1ef8732017-04-18 13:55:13 +01006125 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6126 "Field LDR must be 1 instruction (4B) before the return address label; "
6127 " 2 instructions (8B) for heap poisoning.");
6128 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6129 __ ldr(ref_reg, MemOperand(base.X(), offset));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006130 if (needs_null_check) {
6131 MaybeRecordImplicitNullCheck(instruction);
6132 }
Vladimir Markod1ef8732017-04-18 13:55:13 +01006133 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006134 __ Bind(&return_address);
6135 return;
6136 }
6137
Roland Levillain44015862016-01-22 11:47:17 +00006138 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006139 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006140 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006141 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006142 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6143 ref,
6144 obj,
6145 offset,
6146 no_index,
6147 no_scale_factor,
6148 temp,
6149 needs_null_check,
6150 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006151}
6152
6153void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6154 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006155 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006156 uint32_t data_offset,
6157 Location index,
6158 Register temp,
6159 bool needs_null_check) {
6160 DCHECK(kEmitCompilerReadBarrier);
6161 DCHECK(kUseBakerReadBarrier);
6162
Vladimir Marko66d691d2017-04-07 17:53:39 +01006163 static_assert(
6164 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6165 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6166 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
6167
6168 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6169 !Runtime::Current()->UseJitCompilation()) {
6170 // Note that we do not actually check the value of `GetIsGcMarking()`
6171 // to decide whether to mark the loaded reference or not. Instead, we
6172 // load into `temp` (actually IP1) the read barrier mark introspection
6173 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6174 // false, and vice versa.
6175 //
6176 // We use link-time generated thunks for the slow path. That thunk checks
6177 // the holder and jumps to the entrypoint if needed. If the holder is not
6178 // gray, it creates a fake dependency and returns to the LDR instruction.
6179 //
6180 // temp = Thread::Current()->pReadBarrierMarkIntrospection
6181 // lr = &gray_return_address;
6182 // if (temp != nullptr) {
6183 // goto field_thunk<holder_reg, base_reg>(lr)
6184 // }
6185 // not_gray_return_address:
6186 // // Original reference load. If the offset is too large to fit
6187 // // into LDR, we use an adjusted base register here.
6188 // GcRoot<mirror::Object> reference = data[index];
6189 // gray_return_address:
6190
6191 DCHECK(index.IsValid());
6192 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
6193 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6194
6195 UseScratchRegisterScope temps(GetVIXLAssembler());
6196 DCHECK(temps.IsAvailable(ip0));
6197 DCHECK(temps.IsAvailable(ip1));
6198 temps.Exclude(ip0, ip1);
6199 uint32_t custom_data =
6200 linker::Arm64RelativePatcher::EncodeBakerReadBarrierArrayData(temp.GetCode());
6201 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6202
6203 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6204 DCHECK_EQ(ip0.GetCode(), 16u);
6205 const int32_t entry_point_offset =
6206 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6207 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
6208 __ Add(temp.X(), obj.X(), Operand(data_offset));
6209 EmissionCheckScope guard(GetVIXLAssembler(),
6210 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6211 vixl::aarch64::Label return_address;
6212 __ adr(lr, &return_address);
6213 __ Bind(cbnz_label);
6214 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6215 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6216 "Array LDR must be 1 instruction (4B) before the return address label; "
6217 " 2 instructions (8B) for heap poisoning.");
6218 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6219 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6220 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6221 __ Bind(&return_address);
6222 return;
6223 }
6224
Roland Levillain44015862016-01-22 11:47:17 +00006225 // Array cells are never volatile variables, therefore array loads
6226 // never use Load-Acquire instructions on ARM64.
6227 const bool use_load_acquire = false;
6228
6229 // /* HeapReference<Object> */ ref =
6230 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006231 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6232 ref,
6233 obj,
6234 data_offset,
6235 index,
6236 scale_factor,
6237 temp,
6238 needs_null_check,
6239 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006240}
6241
6242void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6243 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006244 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006245 uint32_t offset,
6246 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006247 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006248 Register temp,
6249 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006250 bool use_load_acquire,
6251 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00006252 DCHECK(kEmitCompilerReadBarrier);
6253 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006254 // If we are emitting an array load, we should not be using a
6255 // Load Acquire instruction. In other words:
6256 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6257 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006258
Roland Levillain54f869e2017-03-06 13:54:11 +00006259 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6260 // whether we need to enter the slow path to mark the reference.
6261 // Then, in the slow path, check the gray bit in the lock word of
6262 // the reference's holder (`obj`) to decide whether to mark `ref` or
6263 // not.
Roland Levillain44015862016-01-22 11:47:17 +00006264 //
Roland Levillainba650a42017-03-06 13:52:32 +00006265 // Note that we do not actually check the value of `GetIsGcMarking()`;
6266 // instead, we load into `temp2` the read barrier mark entry point
6267 // corresponding to register `ref`. If `temp2` is null, it means
6268 // that `GetIsGcMarking()` is false, and vice versa.
6269 //
6270 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00006271 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6272 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006273 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6274 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6275 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6276 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6277 // if (is_gray) {
6278 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6279 // }
6280 // } else {
6281 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006282 // }
Roland Levillain44015862016-01-22 11:47:17 +00006283
Roland Levillainba650a42017-03-06 13:52:32 +00006284 // Slow path marking the object `ref` when the GC is marking. The
6285 // entrypoint will already be loaded in `temp2`.
6286 Register temp2 = lr;
6287 Location temp2_loc = LocationFrom(temp2);
6288 SlowPathCodeARM64* slow_path;
6289 if (always_update_field) {
Roland Levillain54f869e2017-03-06 13:54:11 +00006290 // LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
6291 // only supports address of the form `obj + field_offset`, where
6292 // `obj` is a register and `field_offset` is a register. Thus
6293 // `offset` and `scale_factor` above are expected to be null in
6294 // this code path.
Roland Levillainba650a42017-03-06 13:52:32 +00006295 DCHECK_EQ(offset, 0u);
6296 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
Roland Levillain54f869e2017-03-06 13:54:11 +00006297 Location field_offset = index;
6298 slow_path =
6299 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6300 instruction,
6301 ref,
6302 obj,
6303 offset,
6304 /* index */ field_offset,
6305 scale_factor,
6306 needs_null_check,
6307 use_load_acquire,
6308 temp,
6309 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006310 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +00006311 slow_path = new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6312 instruction,
6313 ref,
6314 obj,
6315 offset,
6316 index,
6317 scale_factor,
6318 needs_null_check,
6319 use_load_acquire,
6320 temp,
6321 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006322 }
6323 AddSlowPath(slow_path);
6324
6325 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6326 const int32_t entry_point_offset =
6327 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6328 // Loading the entrypoint does not require a load acquire since it is only changed when
6329 // threads are suspended or running a checkpoint.
6330 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006331 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6332 // checking GetIsGcMarking.
6333 __ Cbnz(temp2, slow_path->GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +00006334 // Fast path: just load the reference.
6335 GenerateRawReferenceLoad(
6336 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006337 __ Bind(slow_path->GetExitLabel());
6338}
6339
6340void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6341 Location ref,
6342 Register obj,
6343 uint32_t offset,
6344 Location index,
6345 size_t scale_factor,
6346 bool needs_null_check,
6347 bool use_load_acquire) {
6348 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006349 Primitive::Type type = Primitive::kPrimNot;
6350 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006351
Roland Levillainba650a42017-03-06 13:52:32 +00006352 // If needed, vixl::EmissionCheckScope guards are used to ensure
6353 // that no pools are emitted between the load (macro) instruction
6354 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006355
Roland Levillain44015862016-01-22 11:47:17 +00006356 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006357 // Load types involving an "index": ArrayGet,
6358 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6359 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006360 if (use_load_acquire) {
6361 // UnsafeGetObjectVolatile intrinsic case.
6362 // Register `index` is not an index in an object array, but an
6363 // offset to an object reference field within object `obj`.
6364 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6365 DCHECK(instruction->GetLocations()->Intrinsified());
6366 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6367 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006368 DCHECK_EQ(offset, 0u);
6369 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006370 DCHECK_EQ(needs_null_check, false);
6371 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006372 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6373 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006374 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006375 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6376 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006377 if (index.IsConstant()) {
6378 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006379 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006380 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006381 if (needs_null_check) {
6382 MaybeRecordImplicitNullCheck(instruction);
6383 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006384 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006385 UseScratchRegisterScope temps(GetVIXLAssembler());
6386 Register temp = temps.AcquireW();
6387 __ Add(temp, obj, offset);
6388 {
6389 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6390 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6391 if (needs_null_check) {
6392 MaybeRecordImplicitNullCheck(instruction);
6393 }
6394 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006395 }
Roland Levillain44015862016-01-22 11:47:17 +00006396 }
Roland Levillain44015862016-01-22 11:47:17 +00006397 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006398 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006399 MemOperand field = HeapOperand(obj, offset);
6400 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006401 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6402 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006403 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006404 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006405 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006406 if (needs_null_check) {
6407 MaybeRecordImplicitNullCheck(instruction);
6408 }
Roland Levillain44015862016-01-22 11:47:17 +00006409 }
6410 }
6411
6412 // Object* ref = ref_addr->AsMirrorPtr()
6413 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006414}
6415
6416void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6417 Location out,
6418 Location ref,
6419 Location obj,
6420 uint32_t offset,
6421 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006422 DCHECK(kEmitCompilerReadBarrier);
6423
Roland Levillain44015862016-01-22 11:47:17 +00006424 // Insert a slow path based read barrier *after* the reference load.
6425 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006426 // If heap poisoning is enabled, the unpoisoning of the loaded
6427 // reference will be carried out by the runtime within the slow
6428 // path.
6429 //
6430 // Note that `ref` currently does not get unpoisoned (when heap
6431 // poisoning is enabled), which is alright as the `ref` argument is
6432 // not used by the artReadBarrierSlow entry point.
6433 //
6434 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6435 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6436 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6437 AddSlowPath(slow_path);
6438
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006439 __ B(slow_path->GetEntryLabel());
6440 __ Bind(slow_path->GetExitLabel());
6441}
6442
Roland Levillain44015862016-01-22 11:47:17 +00006443void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6444 Location out,
6445 Location ref,
6446 Location obj,
6447 uint32_t offset,
6448 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006449 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006450 // Baker's read barriers shall be handled by the fast path
6451 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6452 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006453 // If heap poisoning is enabled, unpoisoning will be taken care of
6454 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006455 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006456 } else if (kPoisonHeapReferences) {
6457 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6458 }
6459}
6460
Roland Levillain44015862016-01-22 11:47:17 +00006461void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6462 Location out,
6463 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006464 DCHECK(kEmitCompilerReadBarrier);
6465
Roland Levillain44015862016-01-22 11:47:17 +00006466 // Insert a slow path based read barrier *after* the GC root load.
6467 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006468 // Note that GC roots are not affected by heap poisoning, so we do
6469 // not need to do anything special for this here.
6470 SlowPathCodeARM64* slow_path =
6471 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6472 AddSlowPath(slow_path);
6473
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006474 __ B(slow_path->GetEntryLabel());
6475 __ Bind(slow_path->GetExitLabel());
6476}
6477
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006478void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6479 LocationSummary* locations =
6480 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6481 locations->SetInAt(0, Location::RequiresRegister());
6482 locations->SetOut(Location::RequiresRegister());
6483}
6484
6485void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6486 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006487 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006488 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006489 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006490 __ Ldr(XRegisterFrom(locations->Out()),
6491 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006492 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006493 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006494 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006495 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6496 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006497 __ Ldr(XRegisterFrom(locations->Out()),
6498 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006499 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006500}
6501
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006502static void PatchJitRootUse(uint8_t* code,
6503 const uint8_t* roots_data,
6504 vixl::aarch64::Literal<uint32_t>* literal,
6505 uint64_t index_in_table) {
6506 uint32_t literal_offset = literal->GetOffset();
6507 uintptr_t address =
6508 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6509 uint8_t* data = code + literal_offset;
6510 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6511}
6512
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006513void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6514 for (const auto& entry : jit_string_patches_) {
6515 const auto& it = jit_string_roots_.find(entry.first);
6516 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006517 PatchJitRootUse(code, roots_data, entry.second, it->second);
6518 }
6519 for (const auto& entry : jit_class_patches_) {
6520 const auto& it = jit_class_roots_.find(entry.first);
6521 DCHECK(it != jit_class_roots_.end());
6522 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006523 }
6524}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006525
Alexandre Rames67555f72014-11-18 10:55:16 +00006526#undef __
6527#undef QUICK_ENTRY_POINT
6528
Alexandre Rames5319def2014-10-23 10:03:10 +01006529} // namespace arm64
6530} // namespace art