blob: 6ec9c910ec356f4c4c8dd864ed99373a2c0b01f3 [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
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
Scott Wakeling97c72b72016-06-24 16:19:36 +010036using namespace vixl::aarch64; // NOLINT(build/namespaces)
Alexandre Rames5319def2014-10-23 10:03:10 +010037
38#ifdef __
39#error "ARM64 Codegen VIXL macro-assembler macro already defined."
40#endif
41
Alexandre Rames5319def2014-10-23 10:03:10 +010042namespace art {
43
Roland Levillain22ccc3a2015-11-24 13:10:05 +000044template<class MirrorType>
45class GcRoot;
46
Alexandre Rames5319def2014-10-23 10:03:10 +010047namespace arm64 {
48
Alexandre Ramesbe919d92016-08-23 18:33:36 +010049using helpers::ARM64EncodableConstantOrRegister;
50using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080051using helpers::CPURegisterFrom;
52using helpers::DRegisterFrom;
53using helpers::FPRegisterFrom;
54using helpers::HeapOperand;
55using helpers::HeapOperandFrom;
56using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010057using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080058using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080059using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080061using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010062using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080063using helpers::LocationFrom;
64using helpers::OperandFromMemOperand;
65using helpers::OutputCPURegister;
66using helpers::OutputFPRegister;
67using helpers::OutputRegister;
68using helpers::RegisterFrom;
69using helpers::StackOperandFrom;
70using helpers::VIXLRegCodeFromART;
71using helpers::WRegisterFrom;
72using helpers::XRegisterFrom;
73
Alexandre Rames5319def2014-10-23 10:03:10 +010074static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000075// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080076// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
77// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000078static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010079
Alexandre Rames5319def2014-10-23 10:03:10 +010080inline Condition ARM64Condition(IfCondition cond) {
81 switch (cond) {
82 case kCondEQ: return eq;
83 case kCondNE: return ne;
84 case kCondLT: return lt;
85 case kCondLE: return le;
86 case kCondGT: return gt;
87 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070088 case kCondB: return lo;
89 case kCondBE: return ls;
90 case kCondA: return hi;
91 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010092 }
Roland Levillain7f63c522015-07-13 15:54:55 +000093 LOG(FATAL) << "Unreachable";
94 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010095}
96
Vladimir Markod6e069b2016-01-18 11:11:01 +000097inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
98 // The ARM64 condition codes can express all the necessary branches, see the
99 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
100 // There is no dex instruction or HIR that would need the missing conditions
101 // "equal or unordered" or "not equal".
102 switch (cond) {
103 case kCondEQ: return eq;
104 case kCondNE: return ne /* unordered */;
105 case kCondLT: return gt_bias ? cc : lt /* unordered */;
106 case kCondLE: return gt_bias ? ls : le /* unordered */;
107 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
108 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
109 default:
110 LOG(FATAL) << "UNREACHABLE";
111 UNREACHABLE();
112 }
113}
114
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000115Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000116 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
117 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
118 // but we use the exact registers for clarity.
119 if (return_type == Primitive::kPrimFloat) {
120 return LocationFrom(s0);
121 } else if (return_type == Primitive::kPrimDouble) {
122 return LocationFrom(d0);
123 } else if (return_type == Primitive::kPrimLong) {
124 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100125 } else if (return_type == Primitive::kPrimVoid) {
126 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000127 } else {
128 return LocationFrom(w0);
129 }
130}
131
Alexandre Rames5319def2014-10-23 10:03:10 +0100132Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000133 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100134}
135
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100136// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
137#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700138#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100139
Zheng Xuda403092015-04-24 17:35:39 +0800140// Calculate memory accessing operand for save/restore live registers.
141static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100142 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800143 int64_t spill_offset,
144 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100145 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
146 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
147 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800148 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100149 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800150 codegen->GetNumberOfFloatingPointRegisters()));
151
Vladimir Marko804b03f2016-09-14 16:26:36 +0100152 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
153 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800154
155 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
156 UseScratchRegisterScope temps(masm);
157
158 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100159 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
160 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800161 int64_t reg_size = kXRegSizeInBytes;
162 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
163 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100164 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800165 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
166 // If the offset does not fit in the instruction's immediate field, use an alternate register
167 // to compute the base address(float point registers spill base address).
168 Register new_base = temps.AcquireSameSizeAs(base);
169 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
170 base = new_base;
171 spill_offset = -core_spill_size;
172 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
173 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
174 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
175 }
176
177 if (is_save) {
178 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
179 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
180 } else {
181 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
182 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
183 }
184}
185
186void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800187 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100188 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
189 for (uint32_t i : LowToHighBits(core_spills)) {
190 // If the register holds an object, update the stack mask.
191 if (locations->RegisterContainsObject(i)) {
192 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800193 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100194 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
195 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
196 saved_core_stack_offsets_[i] = stack_offset;
197 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800198 }
199
Vladimir Marko804b03f2016-09-14 16:26:36 +0100200 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
201 for (uint32_t i : LowToHighBits(fp_spills)) {
202 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
203 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
204 saved_fpu_stack_offsets_[i] = stack_offset;
205 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800206 }
207
Vladimir Marko804b03f2016-09-14 16:26:36 +0100208 SaveRestoreLiveRegistersHelper(codegen,
209 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800210 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
211}
212
213void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100214 SaveRestoreLiveRegistersHelper(codegen,
215 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800216 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
217}
218
Alexandre Rames5319def2014-10-23 10:03:10 +0100219class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
220 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000221 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100222
Alexandre Rames67555f72014-11-18 10:55:16 +0000223 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100224 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000225 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100226
Alexandre Rames5319def2014-10-23 10:03:10 +0100227 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000228 if (instruction_->CanThrowIntoCatchBlock()) {
229 // Live registers will be restored in the catch block if caught.
230 SaveLiveRegisters(codegen, instruction_->GetLocations());
231 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000232 // We're moving two locations to locations that could overlap, so we need a parallel
233 // move resolver.
234 InvokeRuntimeCallingConvention calling_convention;
235 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100236 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
237 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000238 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
239 ? kQuickThrowStringBounds
240 : kQuickThrowArrayBounds;
241 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100242 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800243 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100244 }
245
Alexandre Rames8158f282015-08-07 10:26:17 +0100246 bool IsFatal() const OVERRIDE { return true; }
247
Alexandre Rames9931f312015-06-19 14:47:01 +0100248 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
249
Alexandre Rames5319def2014-10-23 10:03:10 +0100250 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100251 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
252};
253
Alexandre Rames67555f72014-11-18 10:55:16 +0000254class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
255 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000256 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000257
258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
259 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
260 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000261 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800262 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000263 }
264
Alexandre Rames8158f282015-08-07 10:26:17 +0100265 bool IsFatal() const OVERRIDE { return true; }
266
Alexandre Rames9931f312015-06-19 14:47:01 +0100267 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
268
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000270 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
271};
272
273class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
274 public:
275 LoadClassSlowPathARM64(HLoadClass* cls,
276 HInstruction* at,
277 uint32_t dex_pc,
278 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000279 : SlowPathCodeARM64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000280 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
281 }
282
283 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
284 LocationSummary* locations = at_->GetLocations();
285 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
286
287 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000288 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000289
290 InvokeRuntimeCallingConvention calling_convention;
291 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000292 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
293 : kQuickInitializeType;
294 arm64_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800295 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100296 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800297 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100298 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800299 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000300
301 // Move the class to the desired location.
302 Location out = locations->Out();
303 if (out.IsValid()) {
304 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
305 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000306 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000307 }
308
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000309 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000310 __ B(GetExitLabel());
311 }
312
Alexandre Rames9931f312015-06-19 14:47:01 +0100313 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
314
Alexandre Rames67555f72014-11-18 10:55:16 +0000315 private:
316 // The class this slow path will load.
317 HLoadClass* const cls_;
318
319 // The instruction where this slow path is happening.
320 // (Might be the load class or an initialization check).
321 HInstruction* const at_;
322
323 // The dex PC of `at_`.
324 const uint32_t dex_pc_;
325
326 // Whether to initialize the class.
327 const bool do_clinit_;
328
329 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
330};
331
Vladimir Markoaad75c62016-10-03 08:46:48 +0000332class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
333 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100334 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
335 : SlowPathCodeARM64(instruction),
336 temp_(temp),
337 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000338
339 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
340 LocationSummary* locations = instruction_->GetLocations();
341 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
342 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
343
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100344 // temp_ is a scratch register. Make sure it's not used for saving/restoring registers.
345 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
346 temps.Exclude(temp_);
347
Vladimir Markoaad75c62016-10-03 08:46:48 +0000348 __ Bind(GetEntryLabel());
349 SaveLiveRegisters(codegen, locations);
350
351 InvokeRuntimeCallingConvention calling_convention;
352 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
353 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
354 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
355 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
356 Primitive::Type type = instruction_->GetType();
357 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
358
359 RestoreLiveRegisters(codegen, locations);
360
361 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000362 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100363 if (!kUseReadBarrier || kUseBakerReadBarrier) {
364 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
365 } else {
366 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
367 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
368 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
369 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000370 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100371 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000372 {
373 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
374 __ Bind(strp_label);
375 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100376 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000377 }
378
379 __ B(GetExitLabel());
380 }
381
382 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
383
384 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100385 const Register temp_;
386 vixl::aarch64::Label* adrp_label_;
387
Vladimir Markoaad75c62016-10-03 08:46:48 +0000388 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
389};
390
Alexandre Rames5319def2014-10-23 10:03:10 +0100391class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
392 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000393 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100394
Alexandre Rames67555f72014-11-18 10:55:16 +0000395 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
396 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100397 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000398 if (instruction_->CanThrowIntoCatchBlock()) {
399 // Live registers will be restored in the catch block if caught.
400 SaveLiveRegisters(codegen, instruction_->GetLocations());
401 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000402 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
403 instruction_,
404 instruction_->GetDexPc(),
405 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800406 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100407 }
408
Alexandre Rames8158f282015-08-07 10:26:17 +0100409 bool IsFatal() const OVERRIDE { return true; }
410
Alexandre Rames9931f312015-06-19 14:47:01 +0100411 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
412
Alexandre Rames5319def2014-10-23 10:03:10 +0100413 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100414 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
415};
416
417class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
418 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100419 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000420 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100421
Alexandre Rames67555f72014-11-18 10:55:16 +0000422 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
423 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100424 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000425 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800426 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000427 if (successor_ == nullptr) {
428 __ B(GetReturnLabel());
429 } else {
430 __ B(arm64_codegen->GetLabelOf(successor_));
431 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100432 }
433
Scott Wakeling97c72b72016-06-24 16:19:36 +0100434 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100435 DCHECK(successor_ == nullptr);
436 return &return_label_;
437 }
438
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100439 HBasicBlock* GetSuccessor() const {
440 return successor_;
441 }
442
Alexandre Rames9931f312015-06-19 14:47:01 +0100443 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
444
Alexandre Rames5319def2014-10-23 10:03:10 +0100445 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100446 // If not null, the block to branch to after the suspend check.
447 HBasicBlock* const successor_;
448
449 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100450 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100451
452 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
453};
454
Alexandre Rames67555f72014-11-18 10:55:16 +0000455class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
456 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000457 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000458 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000459
460 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000461 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800462
Alexandre Rames3e69f162014-12-10 10:36:50 +0000463 DCHECK(instruction_->IsCheckCast()
464 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
465 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100466 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000467
Alexandre Rames67555f72014-11-18 10:55:16 +0000468 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000469
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000470 if (!is_fatal_) {
471 SaveLiveRegisters(codegen, locations);
472 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000473
474 // We're moving two locations to locations that could overlap, so we need a parallel
475 // move resolver.
476 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800477 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800478 LocationFrom(calling_convention.GetRegisterAt(0)),
479 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800480 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800481 LocationFrom(calling_convention.GetRegisterAt(1)),
482 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000483 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000484 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800485 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000486 Primitive::Type ret_type = instruction_->GetType();
487 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
488 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
489 } else {
490 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800491 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
492 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000493 }
494
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000495 if (!is_fatal_) {
496 RestoreLiveRegisters(codegen, locations);
497 __ B(GetExitLabel());
498 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000499 }
500
Alexandre Rames9931f312015-06-19 14:47:01 +0100501 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100502 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100503
Alexandre Rames67555f72014-11-18 10:55:16 +0000504 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000505 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000506
Alexandre Rames67555f72014-11-18 10:55:16 +0000507 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
508};
509
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700510class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
511 public:
Aart Bik42249c32016-01-07 15:33:50 -0800512 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000513 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700514
515 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800516 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700517 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000518 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000519 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700520 }
521
Alexandre Rames9931f312015-06-19 14:47:01 +0100522 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
523
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700524 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700525 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
526};
527
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100528class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
529 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000530 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100531
532 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
533 LocationSummary* locations = instruction_->GetLocations();
534 __ Bind(GetEntryLabel());
535 SaveLiveRegisters(codegen, locations);
536
537 InvokeRuntimeCallingConvention calling_convention;
538 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
539 parallel_move.AddMove(
540 locations->InAt(0),
541 LocationFrom(calling_convention.GetRegisterAt(0)),
542 Primitive::kPrimNot,
543 nullptr);
544 parallel_move.AddMove(
545 locations->InAt(1),
546 LocationFrom(calling_convention.GetRegisterAt(1)),
547 Primitive::kPrimInt,
548 nullptr);
549 parallel_move.AddMove(
550 locations->InAt(2),
551 LocationFrom(calling_convention.GetRegisterAt(2)),
552 Primitive::kPrimNot,
553 nullptr);
554 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
555
556 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000557 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100558 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
559 RestoreLiveRegisters(codegen, locations);
560 __ B(GetExitLabel());
561 }
562
563 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
564
565 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100566 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
567};
568
Zheng Xu3927c8b2015-11-18 17:46:25 +0800569void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
570 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000571 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800572
573 // We are about to use the assembler to place literals directly. Make sure we have enough
574 // underlying code buffer and we have generated the jump table with right size.
575 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
576 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
577
578 __ Bind(&table_start_);
579 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
580 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100581 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800582 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100583 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800584 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
585 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
586 Literal<int32_t> literal(jump_offset);
587 __ place(&literal);
588 }
589}
590
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100591// Slow path marking an object reference `ref` during a read
592// barrier. The field `obj.field` in the object `obj` holding this
593// reference does not get updated by this slow path after marking (see
594// ReadBarrierMarkAndUpdateFieldSlowPathARM64 below for that).
595//
596// This means that after the execution of this slow path, `ref` will
597// always be up-to-date, but `obj.field` may not; i.e., after the
598// flip, `ref` will be a to-space reference, but `obj.field` will
599// probably still be a from-space reference (unless it gets updated by
600// another thread, or if another thread installed another object
601// reference (different from `ref`) in `obj.field`).
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800602// If entrypoint is a valid location it is assumed to already be holding the entrypoint. The case
603// where the entrypoint is passed in is for the GcRoot read barrier.
Roland Levillain44015862016-01-22 11:47:17 +0000604class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
605 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800606 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
607 Location ref,
608 Location entrypoint = Location::NoLocation())
609 : SlowPathCodeARM64(instruction),
610 ref_(ref),
611 entrypoint_(entrypoint) {
Roland Levillain44015862016-01-22 11:47:17 +0000612 DCHECK(kEmitCompilerReadBarrier);
613 }
614
615 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
616
617 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
618 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000619 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100620 DCHECK(ref_.IsRegister()) << ref_;
621 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain44015862016-01-22 11:47:17 +0000622 DCHECK(instruction_->IsInstanceFieldGet() ||
623 instruction_->IsStaticFieldGet() ||
624 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100625 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000626 instruction_->IsLoadClass() ||
627 instruction_->IsLoadString() ||
628 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100629 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100630 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
631 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000632 << "Unexpected instruction in read barrier marking slow path: "
633 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000634 // The read barrier instrumentation of object ArrayGet
635 // instructions does not support the HIntermediateAddress
636 // instruction.
637 DCHECK(!(instruction_->IsArrayGet() &&
638 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain44015862016-01-22 11:47:17 +0000639
640 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100641 // No need to save live registers; it's taken care of by the
642 // entrypoint. Also, there is no need to update the stack mask,
643 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000644 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100645 DCHECK_NE(ref_.reg(), LR);
646 DCHECK_NE(ref_.reg(), WSP);
647 DCHECK_NE(ref_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100648 // IP0 is used internally by the ReadBarrierMarkRegX entry point
649 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100650 DCHECK_NE(ref_.reg(), IP0);
651 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
Roland Levillain02b75802016-07-13 11:54:35 +0100652 // "Compact" slow path, saving two moves.
653 //
654 // Instead of using the standard runtime calling convention (input
655 // and output in W0):
656 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100657 // W0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100658 // W0 <- ReadBarrierMark(W0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100659 // ref <- W0
Roland Levillain02b75802016-07-13 11:54:35 +0100660 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100661 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100662 // of a dedicated entrypoint:
663 //
664 // rX <- ReadBarrierMarkRegX(rX)
665 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800666 if (entrypoint_.IsValid()) {
667 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
668 __ Blr(XRegisterFrom(entrypoint_));
669 } else {
670 // Entrypoint is not already loaded, load from the thread.
671 int32_t entry_point_offset =
672 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
673 // This runtime call does not require a stack map.
674 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
675 }
Roland Levillain44015862016-01-22 11:47:17 +0000676 __ B(GetExitLabel());
677 }
678
679 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100680 // The location (register) of the marked object reference.
681 const Location ref_;
Roland Levillain44015862016-01-22 11:47:17 +0000682
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800683 // The location of the entrypoint if it is already loaded.
684 const Location entrypoint_;
685
Roland Levillain44015862016-01-22 11:47:17 +0000686 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
687};
688
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100689// Slow path marking an object reference `ref` during a read barrier,
690// and if needed, atomically updating the field `obj.field` in the
691// object `obj` holding this reference after marking (contrary to
692// ReadBarrierMarkSlowPathARM64 above, which never tries to update
693// `obj.field`).
694//
695// This means that after the execution of this slow path, both `ref`
696// and `obj.field` will be up-to-date; i.e., after the flip, both will
697// hold the same to-space reference (unless another thread installed
698// another object reference (different from `ref`) in `obj.field`).
699class ReadBarrierMarkAndUpdateFieldSlowPathARM64 : public SlowPathCodeARM64 {
700 public:
701 ReadBarrierMarkAndUpdateFieldSlowPathARM64(HInstruction* instruction,
702 Location ref,
703 Register obj,
704 Location field_offset,
705 Register temp)
706 : SlowPathCodeARM64(instruction),
707 ref_(ref),
708 obj_(obj),
709 field_offset_(field_offset),
710 temp_(temp) {
711 DCHECK(kEmitCompilerReadBarrier);
712 }
713
714 const char* GetDescription() const OVERRIDE {
715 return "ReadBarrierMarkAndUpdateFieldSlowPathARM64";
716 }
717
718 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
719 LocationSummary* locations = instruction_->GetLocations();
720 Register ref_reg = WRegisterFrom(ref_);
721 DCHECK(locations->CanCall());
722 DCHECK(ref_.IsRegister()) << ref_;
723 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
724 // This slow path is only used by the UnsafeCASObject intrinsic.
725 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
726 << "Unexpected instruction in read barrier marking and field updating slow path: "
727 << instruction_->DebugName();
728 DCHECK(instruction_->GetLocations()->Intrinsified());
729 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
730 DCHECK(field_offset_.IsRegister()) << field_offset_;
731
732 __ Bind(GetEntryLabel());
733
734 // Save the old reference.
735 // Note that we cannot use IP to save the old reference, as IP is
736 // used internally by the ReadBarrierMarkRegX entry point, and we
737 // need the old reference after the call to that entry point.
738 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
739 __ Mov(temp_.W(), ref_reg);
740
741 // No need to save live registers; it's taken care of by the
742 // entrypoint. Also, there is no need to update the stack mask,
743 // as this runtime call will not trigger a garbage collection.
744 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
745 DCHECK_NE(ref_.reg(), LR);
746 DCHECK_NE(ref_.reg(), WSP);
747 DCHECK_NE(ref_.reg(), WZR);
748 // IP0 is used internally by the ReadBarrierMarkRegX entry point
749 // as a temporary, it cannot be the entry point's input/output.
750 DCHECK_NE(ref_.reg(), IP0);
751 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
752 // "Compact" slow path, saving two moves.
753 //
754 // Instead of using the standard runtime calling convention (input
755 // and output in W0):
756 //
757 // W0 <- ref
758 // W0 <- ReadBarrierMark(W0)
759 // ref <- W0
760 //
761 // we just use rX (the register containing `ref`) as input and output
762 // of a dedicated entrypoint:
763 //
764 // rX <- ReadBarrierMarkRegX(rX)
765 //
766 int32_t entry_point_offset =
767 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
768 // This runtime call does not require a stack map.
769 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
770
771 // If the new reference is different from the old reference,
772 // update the field in the holder (`*(obj_ + field_offset_)`).
773 //
774 // Note that this field could also hold a different object, if
775 // another thread had concurrently changed it. In that case, the
776 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
777 // (CAS) operation below would abort the CAS, leaving the field
778 // as-is.
779 vixl::aarch64::Label done;
780 __ Cmp(temp_.W(), ref_reg);
781 __ B(eq, &done);
782
783 // Update the the holder's field atomically. This may fail if
784 // mutator updates before us, but it's OK. This is achieved
785 // using a strong compare-and-set (CAS) operation with relaxed
786 // memory synchronization ordering, where the expected value is
787 // the old reference and the desired value is the new reference.
788
789 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
790 UseScratchRegisterScope temps(masm);
791
792 // Convenience aliases.
793 Register base = obj_.W();
794 Register offset = XRegisterFrom(field_offset_);
795 Register expected = temp_.W();
796 Register value = ref_reg;
797 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
798 Register tmp_value = temps.AcquireW(); // Value in memory.
799
800 __ Add(tmp_ptr, base.X(), Operand(offset));
801
802 if (kPoisonHeapReferences) {
803 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
804 if (value.Is(expected)) {
805 // Do not poison `value`, as it is the same register as
806 // `expected`, which has just been poisoned.
807 } else {
808 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
809 }
810 }
811
812 // do {
813 // tmp_value = [tmp_ptr] - expected;
814 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
815
Roland Levillain24a4d112016-10-26 13:10:46 +0100816 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100817 __ Bind(&loop_head);
818 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
819 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +0100820 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100821 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
822 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +0100823 __ B(&exit_loop);
824 __ Bind(&comparison_failed);
825 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100826 __ Bind(&exit_loop);
827
828 if (kPoisonHeapReferences) {
829 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
830 if (value.Is(expected)) {
831 // Do not unpoison `value`, as it is the same register as
832 // `expected`, which has just been unpoisoned.
833 } else {
834 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
835 }
836 }
837
838 __ Bind(&done);
839 __ B(GetExitLabel());
840 }
841
842 private:
843 // The location (register) of the marked object reference.
844 const Location ref_;
845 // The register containing the object holding the marked object reference field.
846 const Register obj_;
847 // The location of the offset of the marked reference field within `obj_`.
848 Location field_offset_;
849
850 const Register temp_;
851
852 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM64);
853};
854
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000855// Slow path generating a read barrier for a heap reference.
856class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
857 public:
858 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
859 Location out,
860 Location ref,
861 Location obj,
862 uint32_t offset,
863 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000864 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000865 out_(out),
866 ref_(ref),
867 obj_(obj),
868 offset_(offset),
869 index_(index) {
870 DCHECK(kEmitCompilerReadBarrier);
871 // If `obj` is equal to `out` or `ref`, it means the initial object
872 // has been overwritten by (or after) the heap object reference load
873 // to be instrumented, e.g.:
874 //
875 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000876 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000877 //
878 // In that case, we have lost the information about the original
879 // object, and the emitted read barrier cannot work properly.
880 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
881 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
882 }
883
884 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
885 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
886 LocationSummary* locations = instruction_->GetLocations();
887 Primitive::Type type = Primitive::kPrimNot;
888 DCHECK(locations->CanCall());
889 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100890 DCHECK(instruction_->IsInstanceFieldGet() ||
891 instruction_->IsStaticFieldGet() ||
892 instruction_->IsArrayGet() ||
893 instruction_->IsInstanceOf() ||
894 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100895 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000896 << "Unexpected instruction in read barrier for heap reference slow path: "
897 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000898 // The read barrier instrumentation of object ArrayGet
899 // instructions does not support the HIntermediateAddress
900 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000901 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100902 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000903
904 __ Bind(GetEntryLabel());
905
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000906 SaveLiveRegisters(codegen, locations);
907
908 // We may have to change the index's value, but as `index_` is a
909 // constant member (like other "inputs" of this slow path),
910 // introduce a copy of it, `index`.
911 Location index = index_;
912 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100913 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000914 if (instruction_->IsArrayGet()) {
915 // Compute the actual memory offset and store it in `index`.
916 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
917 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
918 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
919 // We are about to change the value of `index_reg` (see the
920 // calls to vixl::MacroAssembler::Lsl and
921 // vixl::MacroAssembler::Mov below), but it has
922 // not been saved by the previous call to
923 // art::SlowPathCode::SaveLiveRegisters, as it is a
924 // callee-save register --
925 // art::SlowPathCode::SaveLiveRegisters does not consider
926 // callee-save registers, as it has been designed with the
927 // assumption that callee-save registers are supposed to be
928 // handled by the called function. So, as a callee-save
929 // register, `index_reg` _would_ eventually be saved onto
930 // the stack, but it would be too late: we would have
931 // changed its value earlier. Therefore, we manually save
932 // it here into another freely available register,
933 // `free_reg`, chosen of course among the caller-save
934 // registers (as a callee-save `free_reg` register would
935 // exhibit the same problem).
936 //
937 // Note we could have requested a temporary register from
938 // the register allocator instead; but we prefer not to, as
939 // this is a slow path, and we know we can find a
940 // caller-save register that is available.
941 Register free_reg = FindAvailableCallerSaveRegister(codegen);
942 __ Mov(free_reg.W(), index_reg);
943 index_reg = free_reg;
944 index = LocationFrom(index_reg);
945 } else {
946 // The initial register stored in `index_` has already been
947 // saved in the call to art::SlowPathCode::SaveLiveRegisters
948 // (as it is not a callee-save register), so we can freely
949 // use it.
950 }
951 // Shifting the index value contained in `index_reg` by the scale
952 // factor (2) cannot overflow in practice, as the runtime is
953 // unable to allocate object arrays with a size larger than
954 // 2^26 - 1 (that is, 2^28 - 4 bytes).
955 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
956 static_assert(
957 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
958 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
959 __ Add(index_reg, index_reg, Operand(offset_));
960 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100961 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
962 // intrinsics, `index_` is not shifted by a scale factor of 2
963 // (as in the case of ArrayGet), as it is actually an offset
964 // to an object field within an object.
965 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000966 DCHECK(instruction_->GetLocations()->Intrinsified());
967 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
968 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
969 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100970 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100971 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000972 }
973 }
974
975 // We're moving two or three locations to locations that could
976 // overlap, so we need a parallel move resolver.
977 InvokeRuntimeCallingConvention calling_convention;
978 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
979 parallel_move.AddMove(ref_,
980 LocationFrom(calling_convention.GetRegisterAt(0)),
981 type,
982 nullptr);
983 parallel_move.AddMove(obj_,
984 LocationFrom(calling_convention.GetRegisterAt(1)),
985 type,
986 nullptr);
987 if (index.IsValid()) {
988 parallel_move.AddMove(index,
989 LocationFrom(calling_convention.GetRegisterAt(2)),
990 Primitive::kPrimInt,
991 nullptr);
992 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
993 } else {
994 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
995 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
996 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000997 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000998 instruction_,
999 instruction_->GetDexPc(),
1000 this);
1001 CheckEntrypointTypes<
1002 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1003 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1004
1005 RestoreLiveRegisters(codegen, locations);
1006
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001007 __ B(GetExitLabel());
1008 }
1009
1010 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1011
1012 private:
1013 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001014 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1015 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001016 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1017 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1018 return Register(VIXLRegCodeFromART(i), kXRegSize);
1019 }
1020 }
1021 // We shall never fail to find a free caller-save register, as
1022 // there are more than two core caller-save registers on ARM64
1023 // (meaning it is possible to find one which is different from
1024 // `ref` and `obj`).
1025 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1026 LOG(FATAL) << "Could not find a free register";
1027 UNREACHABLE();
1028 }
1029
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001030 const Location out_;
1031 const Location ref_;
1032 const Location obj_;
1033 const uint32_t offset_;
1034 // An additional location containing an index to an array.
1035 // Only used for HArrayGet and the UnsafeGetObject &
1036 // UnsafeGetObjectVolatile intrinsics.
1037 const Location index_;
1038
1039 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1040};
1041
1042// Slow path generating a read barrier for a GC root.
1043class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1044 public:
1045 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001046 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001047 DCHECK(kEmitCompilerReadBarrier);
1048 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001049
1050 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1051 LocationSummary* locations = instruction_->GetLocations();
1052 Primitive::Type type = Primitive::kPrimNot;
1053 DCHECK(locations->CanCall());
1054 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001055 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1056 << "Unexpected instruction in read barrier for GC root slow path: "
1057 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001058
1059 __ Bind(GetEntryLabel());
1060 SaveLiveRegisters(codegen, locations);
1061
1062 InvokeRuntimeCallingConvention calling_convention;
1063 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1064 // The argument of the ReadBarrierForRootSlow is not a managed
1065 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1066 // thus we need a 64-bit move here, and we cannot use
1067 //
1068 // arm64_codegen->MoveLocation(
1069 // LocationFrom(calling_convention.GetRegisterAt(0)),
1070 // root_,
1071 // type);
1072 //
1073 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1074 // reference type (`Primitive::kPrimNot`).
1075 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001076 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001077 instruction_,
1078 instruction_->GetDexPc(),
1079 this);
1080 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1081 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1082
1083 RestoreLiveRegisters(codegen, locations);
1084 __ B(GetExitLabel());
1085 }
1086
1087 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1088
1089 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001090 const Location out_;
1091 const Location root_;
1092
1093 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1094};
1095
Alexandre Rames5319def2014-10-23 10:03:10 +01001096#undef __
1097
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001098Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001099 Location next_location;
1100 if (type == Primitive::kPrimVoid) {
1101 LOG(FATAL) << "Unreachable type " << type;
1102 }
1103
Alexandre Rames542361f2015-01-29 16:57:31 +00001104 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001105 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
1106 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +00001107 } else if (!Primitive::IsFloatingPointType(type) &&
1108 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001109 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1110 } else {
1111 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001112 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1113 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001114 }
1115
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001116 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001117 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001118 return next_location;
1119}
1120
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001121Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001122 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001123}
1124
Serban Constantinescu579885a2015-02-22 20:51:33 +00001125CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1126 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001127 const CompilerOptions& compiler_options,
1128 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001129 : CodeGenerator(graph,
1130 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001131 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001132 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001133 callee_saved_core_registers.GetList(),
1134 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001135 compiler_options,
1136 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001137 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001138 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001139 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001140 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001141 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001142 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001143 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001144 uint32_literals_(std::less<uint32_t>(),
1145 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001146 uint64_literals_(std::less<uint64_t>(),
1147 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1148 method_patches_(MethodReferenceComparator(),
1149 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1150 call_patches_(MethodReferenceComparator(),
1151 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1152 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001153 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1154 boot_image_string_patches_(StringReferenceValueComparator(),
1155 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1156 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001157 boot_image_type_patches_(TypeReferenceValueComparator(),
1158 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1159 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001160 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00001161 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001162 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001163 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001164}
Alexandre Rames5319def2014-10-23 10:03:10 +01001165
Alexandre Rames67555f72014-11-18 10:55:16 +00001166#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001167
Zheng Xu3927c8b2015-11-18 17:46:25 +08001168void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001169 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001170 jump_table->EmitTable(this);
1171 }
1172}
1173
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001174void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001175 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001176 // Ensure we emit the literal pool.
1177 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001178
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001179 CodeGenerator::Finalize(allocator);
1180}
1181
Zheng Xuad4450e2015-04-17 18:48:56 +08001182void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1183 // Note: There are 6 kinds of moves:
1184 // 1. constant -> GPR/FPR (non-cycle)
1185 // 2. constant -> stack (non-cycle)
1186 // 3. GPR/FPR -> GPR/FPR
1187 // 4. GPR/FPR -> stack
1188 // 5. stack -> GPR/FPR
1189 // 6. stack -> stack (non-cycle)
1190 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1191 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1192 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1193 // dependency.
1194 vixl_temps_.Open(GetVIXLAssembler());
1195}
1196
1197void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1198 vixl_temps_.Close();
1199}
1200
1201Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1202 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1203 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1204 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1205 Location scratch = GetScratchLocation(kind);
1206 if (!scratch.Equals(Location::NoLocation())) {
1207 return scratch;
1208 }
1209 // Allocate from VIXL temp registers.
1210 if (kind == Location::kRegister) {
1211 scratch = LocationFrom(vixl_temps_.AcquireX());
1212 } else {
1213 DCHECK(kind == Location::kFpuRegister);
1214 scratch = LocationFrom(vixl_temps_.AcquireD());
1215 }
1216 AddScratchLocation(scratch);
1217 return scratch;
1218}
1219
1220void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1221 if (loc.IsRegister()) {
1222 vixl_temps_.Release(XRegisterFrom(loc));
1223 } else {
1224 DCHECK(loc.IsFpuRegister());
1225 vixl_temps_.Release(DRegisterFrom(loc));
1226 }
1227 RemoveScratchLocation(loc);
1228}
1229
Alexandre Rames3e69f162014-12-10 10:36:50 +00001230void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001231 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001232 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001233}
1234
Alexandre Rames5319def2014-10-23 10:03:10 +01001235void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001236 MacroAssembler* masm = GetVIXLAssembler();
1237 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001238 __ Bind(&frame_entry_label_);
1239
Serban Constantinescu02164b32014-11-13 14:05:07 +00001240 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1241 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001242 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001243 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001244 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001245 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001246 __ Ldr(wzr, MemOperand(temp, 0));
1247 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001248 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001249
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001250 if (!HasEmptyFrame()) {
1251 int frame_size = GetFrameSize();
1252 // Stack layout:
1253 // sp[frame_size - 8] : lr.
1254 // ... : other preserved core registers.
1255 // ... : other preserved fp registers.
1256 // ... : reserved frame space.
1257 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001258
1259 // Save the current method if we need it. Note that we do not
1260 // do this in HCurrentMethod, as the instruction might have been removed
1261 // in the SSA graph.
1262 if (RequiresCurrentMethod()) {
1263 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001264 } else {
1265 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001266 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001267 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001268 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1269 frame_size - GetCoreSpillSize());
1270 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1271 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001272 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001273}
1274
1275void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001276 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001277 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001278 if (!HasEmptyFrame()) {
1279 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001280 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1281 frame_size - FrameEntrySpillSize());
1282 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1283 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001284 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001285 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001286 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001287 __ Ret();
1288 GetAssembler()->cfi().RestoreState();
1289 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001290}
1291
Scott Wakeling97c72b72016-06-24 16:19:36 +01001292CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001293 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001294 return CPURegList(CPURegister::kRegister, kXRegSize,
1295 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001296}
1297
Scott Wakeling97c72b72016-06-24 16:19:36 +01001298CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001299 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1300 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001301 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1302 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001303}
1304
Alexandre Rames5319def2014-10-23 10:03:10 +01001305void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1306 __ Bind(GetLabelOf(block));
1307}
1308
Calin Juravle175dc732015-08-25 15:42:32 +01001309void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1310 DCHECK(location.IsRegister());
1311 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1312}
1313
Calin Juravlee460d1d2015-09-29 04:52:17 +01001314void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1315 if (location.IsRegister()) {
1316 locations->AddTemp(location);
1317 } else {
1318 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1319 }
1320}
1321
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001322void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001323 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001324 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001325 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001326 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001327 if (value_can_be_null) {
1328 __ Cbz(value, &done);
1329 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001330 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001331 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001332 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001333 if (value_can_be_null) {
1334 __ Bind(&done);
1335 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001336}
1337
David Brazdil58282f42016-01-14 12:45:10 +00001338void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001339 // Blocked core registers:
1340 // lr : Runtime reserved.
1341 // tr : Runtime reserved.
1342 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1343 // ip1 : VIXL core temp.
1344 // ip0 : VIXL core temp.
1345 //
1346 // Blocked fp registers:
1347 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001348 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1349 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001350 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001351 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001352 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001353
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001354 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001355 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001356 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001357 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001358
David Brazdil58282f42016-01-14 12:45:10 +00001359 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001360 // Stubs do not save callee-save floating point registers. If the graph
1361 // is debuggable, we need to deal with these registers differently. For
1362 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001363 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1364 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001365 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001366 }
1367 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001368}
1369
Alexandre Rames3e69f162014-12-10 10:36:50 +00001370size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1371 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1372 __ Str(reg, MemOperand(sp, stack_index));
1373 return kArm64WordSize;
1374}
1375
1376size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1377 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1378 __ Ldr(reg, MemOperand(sp, stack_index));
1379 return kArm64WordSize;
1380}
1381
1382size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1383 FPRegister reg = FPRegister(reg_id, kDRegSize);
1384 __ Str(reg, MemOperand(sp, stack_index));
1385 return kArm64WordSize;
1386}
1387
1388size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1389 FPRegister reg = FPRegister(reg_id, kDRegSize);
1390 __ Ldr(reg, MemOperand(sp, stack_index));
1391 return kArm64WordSize;
1392}
1393
Alexandre Rames5319def2014-10-23 10:03:10 +01001394void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001395 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001396}
1397
1398void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001399 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001400}
1401
Alexandre Rames67555f72014-11-18 10:55:16 +00001402void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001403 if (constant->IsIntConstant()) {
1404 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1405 } else if (constant->IsLongConstant()) {
1406 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1407 } else if (constant->IsNullConstant()) {
1408 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001409 } else if (constant->IsFloatConstant()) {
1410 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1411 } else {
1412 DCHECK(constant->IsDoubleConstant());
1413 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1414 }
1415}
1416
Alexandre Rames3e69f162014-12-10 10:36:50 +00001417
1418static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1419 DCHECK(constant.IsConstant());
1420 HConstant* cst = constant.GetConstant();
1421 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001422 // Null is mapped to a core W register, which we associate with kPrimInt.
1423 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001424 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1425 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1426 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1427}
1428
Calin Juravlee460d1d2015-09-29 04:52:17 +01001429void CodeGeneratorARM64::MoveLocation(Location destination,
1430 Location source,
1431 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001432 if (source.Equals(destination)) {
1433 return;
1434 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001435
1436 // A valid move can always be inferred from the destination and source
1437 // locations. When moving from and to a register, the argument type can be
1438 // used to generate 32bit instead of 64bit moves. In debug mode we also
1439 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001440 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001441
1442 if (destination.IsRegister() || destination.IsFpuRegister()) {
1443 if (unspecified_type) {
1444 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1445 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001446 (src_cst != nullptr && (src_cst->IsIntConstant()
1447 || src_cst->IsFloatConstant()
1448 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001449 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001450 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001451 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001452 // If the source is a double stack slot or a 64bit constant, a 64bit
1453 // type is appropriate. Else the source is a register, and since the
1454 // type has not been specified, we chose a 64bit type to force a 64bit
1455 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001456 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001457 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001458 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001459 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1460 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1461 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001462 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1463 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1464 __ Ldr(dst, StackOperandFrom(source));
1465 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001466 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001467 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001468 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001469 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001470 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001471 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001472 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001473 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1474 ? Primitive::kPrimLong
1475 : Primitive::kPrimInt;
1476 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1477 }
1478 } else {
1479 DCHECK(source.IsFpuRegister());
1480 if (destination.IsRegister()) {
1481 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1482 ? Primitive::kPrimDouble
1483 : Primitive::kPrimFloat;
1484 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1485 } else {
1486 DCHECK(destination.IsFpuRegister());
1487 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001488 }
1489 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001490 } else { // The destination is not a register. It must be a stack slot.
1491 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1492 if (source.IsRegister() || source.IsFpuRegister()) {
1493 if (unspecified_type) {
1494 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001495 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001496 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001497 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001498 }
1499 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001500 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1501 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1502 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001503 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001504 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1505 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001506 UseScratchRegisterScope temps(GetVIXLAssembler());
1507 HConstant* src_cst = source.GetConstant();
1508 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001509 if (src_cst->IsZeroBitPattern()) {
1510 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001511 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001512 if (src_cst->IsIntConstant()) {
1513 temp = temps.AcquireW();
1514 } else if (src_cst->IsLongConstant()) {
1515 temp = temps.AcquireX();
1516 } else if (src_cst->IsFloatConstant()) {
1517 temp = temps.AcquireS();
1518 } else {
1519 DCHECK(src_cst->IsDoubleConstant());
1520 temp = temps.AcquireD();
1521 }
1522 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001523 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001524 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001525 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001526 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001527 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001528 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001529 // There is generally less pressure on FP registers.
1530 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001531 __ Ldr(temp, StackOperandFrom(source));
1532 __ Str(temp, StackOperandFrom(destination));
1533 }
1534 }
1535}
1536
1537void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001538 CPURegister dst,
1539 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001540 switch (type) {
1541 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001542 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001543 break;
1544 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001545 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001546 break;
1547 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001548 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001549 break;
1550 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001551 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001552 break;
1553 case Primitive::kPrimInt:
1554 case Primitive::kPrimNot:
1555 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001556 case Primitive::kPrimFloat:
1557 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001558 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001559 __ Ldr(dst, src);
1560 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001561 case Primitive::kPrimVoid:
1562 LOG(FATAL) << "Unreachable type " << type;
1563 }
1564}
1565
Calin Juravle77520bc2015-01-12 18:45:46 +00001566void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001567 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001568 const MemOperand& src,
1569 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001570 MacroAssembler* masm = GetVIXLAssembler();
1571 BlockPoolsScope block_pools(masm);
1572 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001573 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001574 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001575
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001576 DCHECK(!src.IsPreIndex());
1577 DCHECK(!src.IsPostIndex());
1578
1579 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001580 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001581 MemOperand base = MemOperand(temp_base);
1582 switch (type) {
1583 case Primitive::kPrimBoolean:
1584 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001585 if (needs_null_check) {
1586 MaybeRecordImplicitNullCheck(instruction);
1587 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001588 break;
1589 case Primitive::kPrimByte:
1590 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001591 if (needs_null_check) {
1592 MaybeRecordImplicitNullCheck(instruction);
1593 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001594 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1595 break;
1596 case Primitive::kPrimChar:
1597 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001598 if (needs_null_check) {
1599 MaybeRecordImplicitNullCheck(instruction);
1600 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001601 break;
1602 case Primitive::kPrimShort:
1603 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001604 if (needs_null_check) {
1605 MaybeRecordImplicitNullCheck(instruction);
1606 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001607 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1608 break;
1609 case Primitive::kPrimInt:
1610 case Primitive::kPrimNot:
1611 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001612 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001613 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001614 if (needs_null_check) {
1615 MaybeRecordImplicitNullCheck(instruction);
1616 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001617 break;
1618 case Primitive::kPrimFloat:
1619 case Primitive::kPrimDouble: {
1620 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001621 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001622
1623 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1624 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001625 if (needs_null_check) {
1626 MaybeRecordImplicitNullCheck(instruction);
1627 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001628 __ Fmov(FPRegister(dst), temp);
1629 break;
1630 }
1631 case Primitive::kPrimVoid:
1632 LOG(FATAL) << "Unreachable type " << type;
1633 }
1634}
1635
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001636void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001637 CPURegister src,
1638 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001639 switch (type) {
1640 case Primitive::kPrimBoolean:
1641 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001642 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001643 break;
1644 case Primitive::kPrimChar:
1645 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001646 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001647 break;
1648 case Primitive::kPrimInt:
1649 case Primitive::kPrimNot:
1650 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001651 case Primitive::kPrimFloat:
1652 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001653 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001654 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001655 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001656 case Primitive::kPrimVoid:
1657 LOG(FATAL) << "Unreachable type " << type;
1658 }
1659}
1660
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001661void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1662 CPURegister src,
1663 const MemOperand& dst) {
1664 UseScratchRegisterScope temps(GetVIXLAssembler());
1665 Register temp_base = temps.AcquireX();
1666
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001667 DCHECK(!dst.IsPreIndex());
1668 DCHECK(!dst.IsPostIndex());
1669
1670 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001671 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001672 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001673 MemOperand base = MemOperand(temp_base);
1674 switch (type) {
1675 case Primitive::kPrimBoolean:
1676 case Primitive::kPrimByte:
1677 __ Stlrb(Register(src), base);
1678 break;
1679 case Primitive::kPrimChar:
1680 case Primitive::kPrimShort:
1681 __ Stlrh(Register(src), base);
1682 break;
1683 case Primitive::kPrimInt:
1684 case Primitive::kPrimNot:
1685 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001686 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001687 __ Stlr(Register(src), base);
1688 break;
1689 case Primitive::kPrimFloat:
1690 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001691 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001692 Register temp_src;
1693 if (src.IsZero()) {
1694 // The zero register is used to avoid synthesizing zero constants.
1695 temp_src = Register(src);
1696 } else {
1697 DCHECK(src.IsFPRegister());
1698 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1699 __ Fmov(temp_src, FPRegister(src));
1700 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001701
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001702 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001703 break;
1704 }
1705 case Primitive::kPrimVoid:
1706 LOG(FATAL) << "Unreachable type " << type;
1707 }
1708}
1709
Calin Juravle175dc732015-08-25 15:42:32 +01001710void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1711 HInstruction* instruction,
1712 uint32_t dex_pc,
1713 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001714 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001715 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001716 if (EntrypointRequiresStackMap(entrypoint)) {
1717 RecordPcInfo(instruction, dex_pc, slow_path);
1718 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001719}
1720
Roland Levillaindec8f632016-07-22 17:10:06 +01001721void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1722 HInstruction* instruction,
1723 SlowPathCode* slow_path) {
1724 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001725 GenerateInvokeRuntime(entry_point_offset);
1726}
1727
1728void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001729 BlockPoolsScope block_pools(GetVIXLAssembler());
1730 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1731 __ Blr(lr);
1732}
1733
Alexandre Rames67555f72014-11-18 10:55:16 +00001734void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001735 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001736 UseScratchRegisterScope temps(GetVIXLAssembler());
1737 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001738 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1739
Serban Constantinescu02164b32014-11-13 14:05:07 +00001740 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001741 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1742 __ Add(temp, class_reg, status_offset);
1743 __ Ldar(temp, HeapOperand(temp));
1744 __ Cmp(temp, mirror::Class::kStatusInitialized);
1745 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001746 __ Bind(slow_path->GetExitLabel());
1747}
Alexandre Rames5319def2014-10-23 10:03:10 +01001748
Roland Levillain44015862016-01-22 11:47:17 +00001749void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001750 BarrierType type = BarrierAll;
1751
1752 switch (kind) {
1753 case MemBarrierKind::kAnyAny:
1754 case MemBarrierKind::kAnyStore: {
1755 type = BarrierAll;
1756 break;
1757 }
1758 case MemBarrierKind::kLoadAny: {
1759 type = BarrierReads;
1760 break;
1761 }
1762 case MemBarrierKind::kStoreStore: {
1763 type = BarrierWrites;
1764 break;
1765 }
1766 default:
1767 LOG(FATAL) << "Unexpected memory barrier " << kind;
1768 }
1769 __ Dmb(InnerShareable, type);
1770}
1771
Serban Constantinescu02164b32014-11-13 14:05:07 +00001772void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1773 HBasicBlock* successor) {
1774 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001775 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1776 if (slow_path == nullptr) {
1777 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1778 instruction->SetSlowPath(slow_path);
1779 codegen_->AddSlowPath(slow_path);
1780 if (successor != nullptr) {
1781 DCHECK(successor->IsLoopHeader());
1782 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1783 }
1784 } else {
1785 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1786 }
1787
Serban Constantinescu02164b32014-11-13 14:05:07 +00001788 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1789 Register temp = temps.AcquireW();
1790
Andreas Gampe542451c2016-07-26 09:02:02 -07001791 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001792 if (successor == nullptr) {
1793 __ Cbnz(temp, slow_path->GetEntryLabel());
1794 __ Bind(slow_path->GetReturnLabel());
1795 } else {
1796 __ Cbz(temp, codegen_->GetLabelOf(successor));
1797 __ B(slow_path->GetEntryLabel());
1798 // slow_path will return to GetLabelOf(successor).
1799 }
1800}
1801
Alexandre Rames5319def2014-10-23 10:03:10 +01001802InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1803 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001804 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001805 assembler_(codegen->GetAssembler()),
1806 codegen_(codegen) {}
1807
1808#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001809 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001810
1811#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1812
1813enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001814 // Using a base helps identify when we hit such breakpoints.
1815 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001816#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1817 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1818#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1819};
1820
1821#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001822 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001823 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1824 } \
1825 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1826 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1827 locations->SetOut(Location::Any()); \
1828 }
1829 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1830#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1831
1832#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001833#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001834
Alexandre Rames67555f72014-11-18 10:55:16 +00001835void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001836 DCHECK_EQ(instr->InputCount(), 2U);
1837 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1838 Primitive::Type type = instr->GetResultType();
1839 switch (type) {
1840 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001841 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001842 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001843 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001844 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001845 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001846
1847 case Primitive::kPrimFloat:
1848 case Primitive::kPrimDouble:
1849 locations->SetInAt(0, Location::RequiresFpuRegister());
1850 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001851 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001852 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001853
Alexandre Rames5319def2014-10-23 10:03:10 +01001854 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001855 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001856 }
1857}
1858
Alexandre Rames09a99962015-04-15 11:47:56 +01001859void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001860 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1861
1862 bool object_field_get_with_read_barrier =
1863 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001864 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001865 new (GetGraph()->GetArena()) LocationSummary(instruction,
1866 object_field_get_with_read_barrier ?
1867 LocationSummary::kCallOnSlowPath :
1868 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001869 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001870 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001871 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001872 locations->SetInAt(0, Location::RequiresRegister());
1873 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1874 locations->SetOut(Location::RequiresFpuRegister());
1875 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001876 // The output overlaps for an object field get when read barriers
1877 // are enabled: we do not want the load to overwrite the object's
1878 // location, as we need it to emit the read barrier.
1879 locations->SetOut(
1880 Location::RequiresRegister(),
1881 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001882 }
1883}
1884
1885void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1886 const FieldInfo& field_info) {
1887 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001888 LocationSummary* locations = instruction->GetLocations();
1889 Location base_loc = locations->InAt(0);
1890 Location out = locations->Out();
1891 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001892 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001893 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001894 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001895
Roland Levillain44015862016-01-22 11:47:17 +00001896 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1897 // Object FieldGet with Baker's read barrier case.
1898 MacroAssembler* masm = GetVIXLAssembler();
1899 UseScratchRegisterScope temps(masm);
1900 // /* HeapReference<Object> */ out = *(base + offset)
1901 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1902 Register temp = temps.AcquireW();
1903 // Note that potential implicit null checks are handled in this
1904 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1905 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1906 instruction,
1907 out,
1908 base,
1909 offset,
1910 temp,
1911 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001912 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001913 } else {
1914 // General case.
1915 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001916 // Note that a potential implicit null check is handled in this
1917 // CodeGeneratorARM64::LoadAcquire call.
1918 // NB: LoadAcquire will record the pc info if needed.
1919 codegen_->LoadAcquire(
1920 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001921 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001922 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001923 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001924 }
Roland Levillain44015862016-01-22 11:47:17 +00001925 if (field_type == Primitive::kPrimNot) {
1926 // If read barriers are enabled, emit read barriers other than
1927 // Baker's using a slow path (and also unpoison the loaded
1928 // reference, if heap poisoning is enabled).
1929 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1930 }
Roland Levillain4d027112015-07-01 15:41:14 +01001931 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001932}
1933
1934void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1935 LocationSummary* locations =
1936 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1937 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001938 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1939 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1940 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001941 locations->SetInAt(1, Location::RequiresFpuRegister());
1942 } else {
1943 locations->SetInAt(1, Location::RequiresRegister());
1944 }
1945}
1946
1947void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001948 const FieldInfo& field_info,
1949 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001950 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001951 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001952
1953 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001954 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001955 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001956 Offset offset = field_info.GetFieldOffset();
1957 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001958
Roland Levillain4d027112015-07-01 15:41:14 +01001959 {
1960 // We use a block to end the scratch scope before the write barrier, thus
1961 // freeing the temporary registers so they can be used in `MarkGCCard`.
1962 UseScratchRegisterScope temps(GetVIXLAssembler());
1963
1964 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1965 DCHECK(value.IsW());
1966 Register temp = temps.AcquireW();
1967 __ Mov(temp, value.W());
1968 GetAssembler()->PoisonHeapReference(temp.W());
1969 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001970 }
Roland Levillain4d027112015-07-01 15:41:14 +01001971
1972 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001973 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1974 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001975 } else {
1976 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1977 codegen_->MaybeRecordImplicitNullCheck(instruction);
1978 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001979 }
1980
1981 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001982 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001983 }
1984}
1985
Alexandre Rames67555f72014-11-18 10:55:16 +00001986void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001987 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001988
1989 switch (type) {
1990 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001991 case Primitive::kPrimLong: {
1992 Register dst = OutputRegister(instr);
1993 Register lhs = InputRegisterAt(instr, 0);
1994 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001995 if (instr->IsAdd()) {
1996 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001997 } else if (instr->IsAnd()) {
1998 __ And(dst, lhs, rhs);
1999 } else if (instr->IsOr()) {
2000 __ Orr(dst, lhs, rhs);
2001 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002002 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002003 } else if (instr->IsRor()) {
2004 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002005 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002006 __ Ror(dst, lhs, shift);
2007 } else {
2008 // Ensure shift distance is in the same size register as the result. If
2009 // we are rotating a long and the shift comes in a w register originally,
2010 // we don't need to sxtw for use as an x since the shift distances are
2011 // all & reg_bits - 1.
2012 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2013 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002014 } else {
2015 DCHECK(instr->IsXor());
2016 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002017 }
2018 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002019 }
2020 case Primitive::kPrimFloat:
2021 case Primitive::kPrimDouble: {
2022 FPRegister dst = OutputFPRegister(instr);
2023 FPRegister lhs = InputFPRegisterAt(instr, 0);
2024 FPRegister rhs = InputFPRegisterAt(instr, 1);
2025 if (instr->IsAdd()) {
2026 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002027 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002028 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002029 } else {
2030 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002031 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002032 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002033 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002034 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002035 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002036 }
2037}
2038
Serban Constantinescu02164b32014-11-13 14:05:07 +00002039void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2040 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2041
2042 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2043 Primitive::Type type = instr->GetResultType();
2044 switch (type) {
2045 case Primitive::kPrimInt:
2046 case Primitive::kPrimLong: {
2047 locations->SetInAt(0, Location::RequiresRegister());
2048 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2049 locations->SetOut(Location::RequiresRegister());
2050 break;
2051 }
2052 default:
2053 LOG(FATAL) << "Unexpected shift type " << type;
2054 }
2055}
2056
2057void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2058 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2059
2060 Primitive::Type type = instr->GetType();
2061 switch (type) {
2062 case Primitive::kPrimInt:
2063 case Primitive::kPrimLong: {
2064 Register dst = OutputRegister(instr);
2065 Register lhs = InputRegisterAt(instr, 0);
2066 Operand rhs = InputOperandAt(instr, 1);
2067 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002068 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002069 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002070 if (instr->IsShl()) {
2071 __ Lsl(dst, lhs, shift_value);
2072 } else if (instr->IsShr()) {
2073 __ Asr(dst, lhs, shift_value);
2074 } else {
2075 __ Lsr(dst, lhs, shift_value);
2076 }
2077 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002078 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002079
2080 if (instr->IsShl()) {
2081 __ Lsl(dst, lhs, rhs_reg);
2082 } else if (instr->IsShr()) {
2083 __ Asr(dst, lhs, rhs_reg);
2084 } else {
2085 __ Lsr(dst, lhs, rhs_reg);
2086 }
2087 }
2088 break;
2089 }
2090 default:
2091 LOG(FATAL) << "Unexpected shift operation type " << type;
2092 }
2093}
2094
Alexandre Rames5319def2014-10-23 10:03:10 +01002095void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002096 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002097}
2098
2099void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002100 HandleBinaryOp(instruction);
2101}
2102
2103void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2104 HandleBinaryOp(instruction);
2105}
2106
2107void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2108 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002109}
2110
Artem Serov7fc63502016-02-09 17:15:29 +00002111void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002112 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2113 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2114 locations->SetInAt(0, Location::RequiresRegister());
2115 // There is no immediate variant of negated bitwise instructions in AArch64.
2116 locations->SetInAt(1, Location::RequiresRegister());
2117 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2118}
2119
Artem Serov7fc63502016-02-09 17:15:29 +00002120void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002121 Register dst = OutputRegister(instr);
2122 Register lhs = InputRegisterAt(instr, 0);
2123 Register rhs = InputRegisterAt(instr, 1);
2124
2125 switch (instr->GetOpKind()) {
2126 case HInstruction::kAnd:
2127 __ Bic(dst, lhs, rhs);
2128 break;
2129 case HInstruction::kOr:
2130 __ Orn(dst, lhs, rhs);
2131 break;
2132 case HInstruction::kXor:
2133 __ Eon(dst, lhs, rhs);
2134 break;
2135 default:
2136 LOG(FATAL) << "Unreachable";
2137 }
2138}
2139
Alexandre Rames8626b742015-11-25 16:28:08 +00002140void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
2141 HArm64DataProcWithShifterOp* instruction) {
2142 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2143 instruction->GetType() == Primitive::kPrimLong);
2144 LocationSummary* locations =
2145 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2146 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2147 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2148 } else {
2149 locations->SetInAt(0, Location::RequiresRegister());
2150 }
2151 locations->SetInAt(1, Location::RequiresRegister());
2152 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2153}
2154
2155void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
2156 HArm64DataProcWithShifterOp* instruction) {
2157 Primitive::Type type = instruction->GetType();
2158 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2159 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2160 Register out = OutputRegister(instruction);
2161 Register left;
2162 if (kind != HInstruction::kNeg) {
2163 left = InputRegisterAt(instruction, 0);
2164 }
2165 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
2166 // shifter operand operation, the IR generating `right_reg` (input to the type
2167 // conversion) can have a different type from the current instruction's type,
2168 // so we manually indicate the type.
2169 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00002170 int64_t shift_amount = instruction->GetShiftAmount() &
2171 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00002172
2173 Operand right_operand(0);
2174
2175 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2176 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
2177 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2178 } else {
2179 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
2180 }
2181
2182 // Logical binary operations do not support extension operations in the
2183 // operand. Note that VIXL would still manage if it was passed by generating
2184 // the extension as a separate instruction.
2185 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2186 DCHECK(!right_operand.IsExtendedRegister() ||
2187 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2188 kind != HInstruction::kNeg));
2189 switch (kind) {
2190 case HInstruction::kAdd:
2191 __ Add(out, left, right_operand);
2192 break;
2193 case HInstruction::kAnd:
2194 __ And(out, left, right_operand);
2195 break;
2196 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002197 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002198 __ Neg(out, right_operand);
2199 break;
2200 case HInstruction::kOr:
2201 __ Orr(out, left, right_operand);
2202 break;
2203 case HInstruction::kSub:
2204 __ Sub(out, left, right_operand);
2205 break;
2206 case HInstruction::kXor:
2207 __ Eor(out, left, right_operand);
2208 break;
2209 default:
2210 LOG(FATAL) << "Unexpected operation kind: " << kind;
2211 UNREACHABLE();
2212 }
2213}
2214
Artem Serov328429f2016-07-06 16:23:04 +01002215void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002216 LocationSummary* locations =
2217 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2218 locations->SetInAt(0, Location::RequiresRegister());
2219 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2220 locations->SetOut(Location::RequiresRegister());
2221}
2222
Roland Levillain19c54192016-11-04 13:44:09 +00002223void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002224 __ Add(OutputRegister(instruction),
2225 InputRegisterAt(instruction, 0),
2226 Operand(InputOperandAt(instruction, 1)));
2227}
2228
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002229void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002230 LocationSummary* locations =
2231 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002232 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2233 if (instr->GetOpKind() == HInstruction::kSub &&
2234 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002235 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002236 // Don't allocate register for Mneg instruction.
2237 } else {
2238 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2239 Location::RequiresRegister());
2240 }
2241 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2242 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002243 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2244}
2245
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002246void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002247 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002248 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2249 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002250
2251 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2252 // This fixup should be carried out for all multiply-accumulate instructions:
2253 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2254 if (instr->GetType() == Primitive::kPrimLong &&
2255 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2256 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002257 vixl::aarch64::Instruction* prev =
2258 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002259 if (prev->IsLoadOrStore()) {
2260 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002261 vixl::aarch64::CodeBufferCheckScope scope(masm,
2262 kInstructionSize,
2263 vixl::aarch64::CodeBufferCheckScope::kCheck,
2264 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002265 __ nop();
2266 }
2267 }
2268
2269 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002270 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002271 __ Madd(res, mul_left, mul_right, accumulator);
2272 } else {
2273 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002274 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002275 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002276 __ Mneg(res, mul_left, mul_right);
2277 } else {
2278 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2279 __ Msub(res, mul_left, mul_right, accumulator);
2280 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002281 }
2282}
2283
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002284void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002285 bool object_array_get_with_read_barrier =
2286 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002287 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002288 new (GetGraph()->GetArena()) LocationSummary(instruction,
2289 object_array_get_with_read_barrier ?
2290 LocationSummary::kCallOnSlowPath :
2291 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002292 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002293 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002294 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002295 locations->SetInAt(0, Location::RequiresRegister());
2296 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002297 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2298 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2299 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002300 // The output overlaps in the case of an object array get with
2301 // read barriers enabled: we do not want the move to overwrite the
2302 // array's location, as we need it to emit the read barrier.
2303 locations->SetOut(
2304 Location::RequiresRegister(),
2305 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002306 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002307}
2308
2309void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002310 Primitive::Type type = instruction->GetType();
2311 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002312 LocationSummary* locations = instruction->GetLocations();
2313 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002314 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002315 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002316 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2317 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002318 MacroAssembler* masm = GetVIXLAssembler();
2319 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002320 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002321 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002322
Roland Levillain19c54192016-11-04 13:44:09 +00002323 // The read barrier instrumentation of object ArrayGet instructions
2324 // does not support the HIntermediateAddress instruction.
2325 DCHECK(!((type == Primitive::kPrimNot) &&
2326 instruction->GetArray()->IsIntermediateAddress() &&
2327 kEmitCompilerReadBarrier));
2328
Roland Levillain44015862016-01-22 11:47:17 +00002329 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2330 // Object ArrayGet with Baker's read barrier case.
2331 Register temp = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00002332 // Note that a potential implicit null check is handled in the
2333 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2334 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2335 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002336 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002337 // General case.
2338 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002339 Register length;
2340 if (maybe_compressed_char_at) {
2341 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2342 length = temps.AcquireW();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002343 if (instruction->GetArray()->IsIntermediateAddress()) {
2344 DCHECK_LT(count_offset, offset);
2345 int64_t adjusted_offset = static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2346 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2347 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2348 } else {
2349 __ Ldr(length, HeapOperand(obj, count_offset));
2350 }
jessicahandojo05765752016-09-09 19:01:32 -07002351 codegen_->MaybeRecordImplicitNullCheck(instruction);
2352 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002353 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002354 if (maybe_compressed_char_at) {
2355 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002356 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2357 "Expecting 0=compressed, 1=uncompressed");
2358 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002359 __ Ldrb(Register(OutputCPURegister(instruction)),
2360 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2361 __ B(&done);
2362 __ Bind(&uncompressed_load);
2363 __ Ldrh(Register(OutputCPURegister(instruction)),
2364 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2365 __ Bind(&done);
2366 } else {
2367 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2368 source = HeapOperand(obj, offset);
2369 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002370 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002371 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002372 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002373 // We do not need to compute the intermediate address from the array: the
2374 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002375 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002376 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002377 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002378 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2379 }
2380 temp = obj;
2381 } else {
2382 __ Add(temp, obj, offset);
2383 }
jessicahandojo05765752016-09-09 19:01:32 -07002384 if (maybe_compressed_char_at) {
2385 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002386 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2387 "Expecting 0=compressed, 1=uncompressed");
2388 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002389 __ Ldrb(Register(OutputCPURegister(instruction)),
2390 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2391 __ B(&done);
2392 __ Bind(&uncompressed_load);
2393 __ Ldrh(Register(OutputCPURegister(instruction)),
2394 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2395 __ Bind(&done);
2396 } else {
2397 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2398 }
Roland Levillain44015862016-01-22 11:47:17 +00002399 }
jessicahandojo05765752016-09-09 19:01:32 -07002400 if (!maybe_compressed_char_at) {
2401 codegen_->Load(type, OutputCPURegister(instruction), source);
2402 codegen_->MaybeRecordImplicitNullCheck(instruction);
2403 }
Roland Levillain44015862016-01-22 11:47:17 +00002404
2405 if (type == Primitive::kPrimNot) {
2406 static_assert(
2407 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2408 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2409 Location obj_loc = locations->InAt(0);
2410 if (index.IsConstant()) {
2411 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2412 } else {
2413 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2414 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002415 }
Roland Levillain4d027112015-07-01 15:41:14 +01002416 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002417}
2418
Alexandre Rames5319def2014-10-23 10:03:10 +01002419void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2420 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2421 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002422 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002423}
2424
2425void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002426 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002427 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002428 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002429 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002430 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002431 // Mask out compression flag from String's array length.
2432 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002433 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002434 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002435}
2436
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002437void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002438 Primitive::Type value_type = instruction->GetComponentType();
2439
2440 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002441 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2442 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002443 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002444 LocationSummary::kCallOnSlowPath :
2445 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002446 locations->SetInAt(0, Location::RequiresRegister());
2447 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002448 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2449 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2450 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002451 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002452 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002453 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002454 }
2455}
2456
2457void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2458 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002459 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002460 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002461 bool needs_write_barrier =
2462 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002463
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002464 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002465 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002466 CPURegister source = value;
2467 Location index = locations->InAt(1);
2468 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2469 MemOperand destination = HeapOperand(array);
2470 MacroAssembler* masm = GetVIXLAssembler();
2471 BlockPoolsScope block_pools(masm);
2472
2473 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002474 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002475 if (index.IsConstant()) {
2476 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2477 destination = HeapOperand(array, offset);
2478 } else {
2479 UseScratchRegisterScope temps(masm);
2480 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002481 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002482 // We do not need to compute the intermediate address from the array: the
2483 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002484 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002485 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002486 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002487 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2488 }
2489 temp = array;
2490 } else {
2491 __ Add(temp, array, offset);
2492 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002493 destination = HeapOperand(temp,
2494 XRegisterFrom(index),
2495 LSL,
2496 Primitive::ComponentSizeShift(value_type));
2497 }
2498 codegen_->Store(value_type, value, destination);
2499 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002500 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002501 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002502 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002503 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002504 {
2505 // We use a block to end the scratch scope before the write barrier, thus
2506 // freeing the temporary registers so they can be used in `MarkGCCard`.
2507 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002508 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002509 if (index.IsConstant()) {
2510 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002511 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002512 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002513 destination = HeapOperand(temp,
2514 XRegisterFrom(index),
2515 LSL,
2516 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002517 }
2518
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002519 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2520 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2521 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2522
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002523 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002524 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2525 codegen_->AddSlowPath(slow_path);
2526 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002527 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002528 __ Cbnz(Register(value), &non_zero);
2529 if (!index.IsConstant()) {
2530 __ Add(temp, array, offset);
2531 }
2532 __ Str(wzr, destination);
2533 codegen_->MaybeRecordImplicitNullCheck(instruction);
2534 __ B(&done);
2535 __ Bind(&non_zero);
2536 }
2537
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002538 // Note that when Baker read barriers are enabled, the type
2539 // checks are performed without read barriers. This is fine,
2540 // even in the case where a class object is in the from-space
2541 // after the flip, as a comparison involving such a type would
2542 // not produce a false positive; it may of course produce a
2543 // false negative, in which case we would take the ArraySet
2544 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002545
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002546 Register temp2 = temps.AcquireSameSizeAs(array);
2547 // /* HeapReference<Class> */ temp = array->klass_
2548 __ Ldr(temp, HeapOperand(array, class_offset));
2549 codegen_->MaybeRecordImplicitNullCheck(instruction);
2550 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002551
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002552 // /* HeapReference<Class> */ temp = temp->component_type_
2553 __ Ldr(temp, HeapOperand(temp, component_offset));
2554 // /* HeapReference<Class> */ temp2 = value->klass_
2555 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2556 // If heap poisoning is enabled, no need to unpoison `temp`
2557 // nor `temp2`, as we are comparing two poisoned references.
2558 __ Cmp(temp, temp2);
2559 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002560
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002561 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2562 vixl::aarch64::Label do_put;
2563 __ B(eq, &do_put);
2564 // If heap poisoning is enabled, the `temp` reference has
2565 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002566 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2567
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002568 // /* HeapReference<Class> */ temp = temp->super_class_
2569 __ Ldr(temp, HeapOperand(temp, super_offset));
2570 // If heap poisoning is enabled, no need to unpoison
2571 // `temp`, as we are comparing against null below.
2572 __ Cbnz(temp, slow_path->GetEntryLabel());
2573 __ Bind(&do_put);
2574 } else {
2575 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002576 }
2577 }
2578
2579 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002580 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002581 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002582 __ Mov(temp2, value.W());
2583 GetAssembler()->PoisonHeapReference(temp2);
2584 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002585 }
2586
2587 if (!index.IsConstant()) {
2588 __ Add(temp, array, offset);
2589 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002590 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002591
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002592 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002593 codegen_->MaybeRecordImplicitNullCheck(instruction);
2594 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002595 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002596
2597 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2598
2599 if (done.IsLinked()) {
2600 __ Bind(&done);
2601 }
2602
2603 if (slow_path != nullptr) {
2604 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002605 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002606 }
2607}
2608
Alexandre Rames67555f72014-11-18 10:55:16 +00002609void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002610 RegisterSet caller_saves = RegisterSet::Empty();
2611 InvokeRuntimeCallingConvention calling_convention;
2612 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2613 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2614 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002615 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002616 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002617}
2618
2619void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002620 BoundsCheckSlowPathARM64* slow_path =
2621 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002622 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002623 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2624 __ B(slow_path->GetEntryLabel(), hs);
2625}
2626
Alexandre Rames67555f72014-11-18 10:55:16 +00002627void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2628 LocationSummary* locations =
2629 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2630 locations->SetInAt(0, Location::RequiresRegister());
2631 if (check->HasUses()) {
2632 locations->SetOut(Location::SameAsFirstInput());
2633 }
2634}
2635
2636void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2637 // We assume the class is not null.
2638 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2639 check->GetLoadClass(), check, check->GetDexPc(), true);
2640 codegen_->AddSlowPath(slow_path);
2641 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2642}
2643
Roland Levillain1a653882016-03-18 18:05:57 +00002644static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2645 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2646 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2647}
2648
2649void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2650 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2651 Location rhs_loc = instruction->GetLocations()->InAt(1);
2652 if (rhs_loc.IsConstant()) {
2653 // 0.0 is the only immediate that can be encoded directly in
2654 // an FCMP instruction.
2655 //
2656 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2657 // specify that in a floating-point comparison, positive zero
2658 // and negative zero are considered equal, so we can use the
2659 // literal 0.0 for both cases here.
2660 //
2661 // Note however that some methods (Float.equal, Float.compare,
2662 // Float.compareTo, Double.equal, Double.compare,
2663 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2664 // StrictMath.min) consider 0.0 to be (strictly) greater than
2665 // -0.0. So if we ever translate calls to these methods into a
2666 // HCompare instruction, we must handle the -0.0 case with
2667 // care here.
2668 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2669 __ Fcmp(lhs_reg, 0.0);
2670 } else {
2671 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2672 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002673}
2674
Serban Constantinescu02164b32014-11-13 14:05:07 +00002675void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002676 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002677 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2678 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002679 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002680 case Primitive::kPrimBoolean:
2681 case Primitive::kPrimByte:
2682 case Primitive::kPrimShort:
2683 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002684 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002685 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002686 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002687 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002688 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2689 break;
2690 }
2691 case Primitive::kPrimFloat:
2692 case Primitive::kPrimDouble: {
2693 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002694 locations->SetInAt(1,
2695 IsFloatingPointZeroConstant(compare->InputAt(1))
2696 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2697 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002698 locations->SetOut(Location::RequiresRegister());
2699 break;
2700 }
2701 default:
2702 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2703 }
2704}
2705
2706void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2707 Primitive::Type in_type = compare->InputAt(0)->GetType();
2708
2709 // 0 if: left == right
2710 // 1 if: left > right
2711 // -1 if: left < right
2712 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002713 case Primitive::kPrimBoolean:
2714 case Primitive::kPrimByte:
2715 case Primitive::kPrimShort:
2716 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002717 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002718 case Primitive::kPrimLong: {
2719 Register result = OutputRegister(compare);
2720 Register left = InputRegisterAt(compare, 0);
2721 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002722 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002723 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2724 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002725 break;
2726 }
2727 case Primitive::kPrimFloat:
2728 case Primitive::kPrimDouble: {
2729 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002730 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002731 __ Cset(result, ne);
2732 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002733 break;
2734 }
2735 default:
2736 LOG(FATAL) << "Unimplemented compare type " << in_type;
2737 }
2738}
2739
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002740void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002741 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002742
2743 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2744 locations->SetInAt(0, Location::RequiresFpuRegister());
2745 locations->SetInAt(1,
2746 IsFloatingPointZeroConstant(instruction->InputAt(1))
2747 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2748 : Location::RequiresFpuRegister());
2749 } else {
2750 // Integer cases.
2751 locations->SetInAt(0, Location::RequiresRegister());
2752 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2753 }
2754
David Brazdilb3e773e2016-01-26 11:28:37 +00002755 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002756 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002757 }
2758}
2759
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002760void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002761 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002762 return;
2763 }
2764
2765 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002766 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002767 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002768
Roland Levillain7f63c522015-07-13 15:54:55 +00002769 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002770 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002771 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002772 } else {
2773 // Integer cases.
2774 Register lhs = InputRegisterAt(instruction, 0);
2775 Operand rhs = InputOperandAt(instruction, 1);
2776 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002777 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002778 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002779}
2780
2781#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2782 M(Equal) \
2783 M(NotEqual) \
2784 M(LessThan) \
2785 M(LessThanOrEqual) \
2786 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002787 M(GreaterThanOrEqual) \
2788 M(Below) \
2789 M(BelowOrEqual) \
2790 M(Above) \
2791 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002792#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002793void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2794void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002795FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002796#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002797#undef FOR_EACH_CONDITION_INSTRUCTION
2798
Zheng Xuc6667102015-05-15 16:08:45 +08002799void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2800 DCHECK(instruction->IsDiv() || instruction->IsRem());
2801
2802 LocationSummary* locations = instruction->GetLocations();
2803 Location second = locations->InAt(1);
2804 DCHECK(second.IsConstant());
2805
2806 Register out = OutputRegister(instruction);
2807 Register dividend = InputRegisterAt(instruction, 0);
2808 int64_t imm = Int64FromConstant(second.GetConstant());
2809 DCHECK(imm == 1 || imm == -1);
2810
2811 if (instruction->IsRem()) {
2812 __ Mov(out, 0);
2813 } else {
2814 if (imm == 1) {
2815 __ Mov(out, dividend);
2816 } else {
2817 __ Neg(out, dividend);
2818 }
2819 }
2820}
2821
2822void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2823 DCHECK(instruction->IsDiv() || instruction->IsRem());
2824
2825 LocationSummary* locations = instruction->GetLocations();
2826 Location second = locations->InAt(1);
2827 DCHECK(second.IsConstant());
2828
2829 Register out = OutputRegister(instruction);
2830 Register dividend = InputRegisterAt(instruction, 0);
2831 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002832 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002833 int ctz_imm = CTZ(abs_imm);
2834
2835 UseScratchRegisterScope temps(GetVIXLAssembler());
2836 Register temp = temps.AcquireSameSizeAs(out);
2837
2838 if (instruction->IsDiv()) {
2839 __ Add(temp, dividend, abs_imm - 1);
2840 __ Cmp(dividend, 0);
2841 __ Csel(out, temp, dividend, lt);
2842 if (imm > 0) {
2843 __ Asr(out, out, ctz_imm);
2844 } else {
2845 __ Neg(out, Operand(out, ASR, ctz_imm));
2846 }
2847 } else {
2848 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2849 __ Asr(temp, dividend, bits - 1);
2850 __ Lsr(temp, temp, bits - ctz_imm);
2851 __ Add(out, dividend, temp);
2852 __ And(out, out, abs_imm - 1);
2853 __ Sub(out, out, temp);
2854 }
2855}
2856
2857void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2858 DCHECK(instruction->IsDiv() || instruction->IsRem());
2859
2860 LocationSummary* locations = instruction->GetLocations();
2861 Location second = locations->InAt(1);
2862 DCHECK(second.IsConstant());
2863
2864 Register out = OutputRegister(instruction);
2865 Register dividend = InputRegisterAt(instruction, 0);
2866 int64_t imm = Int64FromConstant(second.GetConstant());
2867
2868 Primitive::Type type = instruction->GetResultType();
2869 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2870
2871 int64_t magic;
2872 int shift;
2873 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2874
2875 UseScratchRegisterScope temps(GetVIXLAssembler());
2876 Register temp = temps.AcquireSameSizeAs(out);
2877
2878 // temp = get_high(dividend * magic)
2879 __ Mov(temp, magic);
2880 if (type == Primitive::kPrimLong) {
2881 __ Smulh(temp, dividend, temp);
2882 } else {
2883 __ Smull(temp.X(), dividend, temp);
2884 __ Lsr(temp.X(), temp.X(), 32);
2885 }
2886
2887 if (imm > 0 && magic < 0) {
2888 __ Add(temp, temp, dividend);
2889 } else if (imm < 0 && magic > 0) {
2890 __ Sub(temp, temp, dividend);
2891 }
2892
2893 if (shift != 0) {
2894 __ Asr(temp, temp, shift);
2895 }
2896
2897 if (instruction->IsDiv()) {
2898 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2899 } else {
2900 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2901 // TODO: Strength reduction for msub.
2902 Register temp_imm = temps.AcquireSameSizeAs(out);
2903 __ Mov(temp_imm, imm);
2904 __ Msub(out, temp, temp_imm, dividend);
2905 }
2906}
2907
2908void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2909 DCHECK(instruction->IsDiv() || instruction->IsRem());
2910 Primitive::Type type = instruction->GetResultType();
2911 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2912
2913 LocationSummary* locations = instruction->GetLocations();
2914 Register out = OutputRegister(instruction);
2915 Location second = locations->InAt(1);
2916
2917 if (second.IsConstant()) {
2918 int64_t imm = Int64FromConstant(second.GetConstant());
2919
2920 if (imm == 0) {
2921 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2922 } else if (imm == 1 || imm == -1) {
2923 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002924 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002925 DivRemByPowerOfTwo(instruction);
2926 } else {
2927 DCHECK(imm <= -2 || imm >= 2);
2928 GenerateDivRemWithAnyConstant(instruction);
2929 }
2930 } else {
2931 Register dividend = InputRegisterAt(instruction, 0);
2932 Register divisor = InputRegisterAt(instruction, 1);
2933 if (instruction->IsDiv()) {
2934 __ Sdiv(out, dividend, divisor);
2935 } else {
2936 UseScratchRegisterScope temps(GetVIXLAssembler());
2937 Register temp = temps.AcquireSameSizeAs(out);
2938 __ Sdiv(temp, dividend, divisor);
2939 __ Msub(out, temp, divisor, dividend);
2940 }
2941 }
2942}
2943
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002944void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2945 LocationSummary* locations =
2946 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2947 switch (div->GetResultType()) {
2948 case Primitive::kPrimInt:
2949 case Primitive::kPrimLong:
2950 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002951 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002952 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2953 break;
2954
2955 case Primitive::kPrimFloat:
2956 case Primitive::kPrimDouble:
2957 locations->SetInAt(0, Location::RequiresFpuRegister());
2958 locations->SetInAt(1, Location::RequiresFpuRegister());
2959 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2960 break;
2961
2962 default:
2963 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2964 }
2965}
2966
2967void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2968 Primitive::Type type = div->GetResultType();
2969 switch (type) {
2970 case Primitive::kPrimInt:
2971 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002972 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002973 break;
2974
2975 case Primitive::kPrimFloat:
2976 case Primitive::kPrimDouble:
2977 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2978 break;
2979
2980 default:
2981 LOG(FATAL) << "Unexpected div type " << type;
2982 }
2983}
2984
Alexandre Rames67555f72014-11-18 10:55:16 +00002985void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002986 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002987 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00002988}
2989
2990void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2991 SlowPathCodeARM64* slow_path =
2992 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2993 codegen_->AddSlowPath(slow_path);
2994 Location value = instruction->GetLocations()->InAt(0);
2995
Alexandre Rames3e69f162014-12-10 10:36:50 +00002996 Primitive::Type type = instruction->GetType();
2997
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002998 if (!Primitive::IsIntegralType(type)) {
2999 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003000 return;
3001 }
3002
Alexandre Rames67555f72014-11-18 10:55:16 +00003003 if (value.IsConstant()) {
3004 int64_t divisor = Int64ConstantFrom(value);
3005 if (divisor == 0) {
3006 __ B(slow_path->GetEntryLabel());
3007 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003008 // A division by a non-null constant is valid. We don't need to perform
3009 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003010 }
3011 } else {
3012 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3013 }
3014}
3015
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003016void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3017 LocationSummary* locations =
3018 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3019 locations->SetOut(Location::ConstantLocation(constant));
3020}
3021
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003022void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3023 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003024 // Will be generated at use site.
3025}
3026
Alexandre Rames5319def2014-10-23 10:03:10 +01003027void LocationsBuilderARM64::VisitExit(HExit* exit) {
3028 exit->SetLocations(nullptr);
3029}
3030
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003031void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003032}
3033
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003034void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3035 LocationSummary* locations =
3036 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3037 locations->SetOut(Location::ConstantLocation(constant));
3038}
3039
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003040void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003041 // Will be generated at use site.
3042}
3043
David Brazdilfc6a86a2015-06-26 10:33:45 +00003044void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003045 DCHECK(!successor->IsExitBlock());
3046 HBasicBlock* block = got->GetBlock();
3047 HInstruction* previous = got->GetPrevious();
3048 HLoopInformation* info = block->GetLoopInformation();
3049
David Brazdil46e2a392015-03-16 17:31:52 +00003050 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003051 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3052 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3053 return;
3054 }
3055 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3056 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3057 }
3058 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003059 __ B(codegen_->GetLabelOf(successor));
3060 }
3061}
3062
David Brazdilfc6a86a2015-06-26 10:33:45 +00003063void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3064 got->SetLocations(nullptr);
3065}
3066
3067void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3068 HandleGoto(got, got->GetSuccessor());
3069}
3070
3071void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3072 try_boundary->SetLocations(nullptr);
3073}
3074
3075void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3076 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3077 if (!successor->IsExitBlock()) {
3078 HandleGoto(try_boundary, successor);
3079 }
3080}
3081
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003082void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003083 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003084 vixl::aarch64::Label* true_target,
3085 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003086 // FP branching requires both targets to be explicit. If either of the targets
3087 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003088 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003089 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003090
David Brazdil0debae72015-11-12 18:37:00 +00003091 if (true_target == nullptr && false_target == nullptr) {
3092 // Nothing to do. The code always falls through.
3093 return;
3094 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003095 // Constant condition, statically compared against "true" (integer value 1).
3096 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003097 if (true_target != nullptr) {
3098 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003099 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003100 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003101 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003102 if (false_target != nullptr) {
3103 __ B(false_target);
3104 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003105 }
David Brazdil0debae72015-11-12 18:37:00 +00003106 return;
3107 }
3108
3109 // The following code generates these patterns:
3110 // (1) true_target == nullptr && false_target != nullptr
3111 // - opposite condition true => branch to false_target
3112 // (2) true_target != nullptr && false_target == nullptr
3113 // - condition true => branch to true_target
3114 // (3) true_target != nullptr && false_target != nullptr
3115 // - condition true => branch to true_target
3116 // - branch to false_target
3117 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003118 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003119 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003120 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003121 if (true_target == nullptr) {
3122 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3123 } else {
3124 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3125 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003126 } else {
3127 // The condition instruction has not been materialized, use its inputs as
3128 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003129 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003130
David Brazdil0debae72015-11-12 18:37:00 +00003131 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003132 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003133 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003134 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003135 IfCondition opposite_condition = condition->GetOppositeCondition();
3136 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003137 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003138 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003139 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003140 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003141 // Integer cases.
3142 Register lhs = InputRegisterAt(condition, 0);
3143 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003144
3145 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003146 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003147 if (true_target == nullptr) {
3148 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3149 non_fallthrough_target = false_target;
3150 } else {
3151 arm64_cond = ARM64Condition(condition->GetCondition());
3152 non_fallthrough_target = true_target;
3153 }
3154
Aart Bik086d27e2016-01-20 17:02:00 -08003155 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003156 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003157 switch (arm64_cond) {
3158 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003159 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003160 break;
3161 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003162 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003163 break;
3164 case lt:
3165 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003166 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003167 break;
3168 case ge:
3169 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003170 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003171 break;
3172 default:
3173 // Without the `static_cast` the compiler throws an error for
3174 // `-Werror=sign-promo`.
3175 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3176 }
3177 } else {
3178 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003179 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003180 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003181 }
3182 }
David Brazdil0debae72015-11-12 18:37:00 +00003183
3184 // If neither branch falls through (case 3), the conditional branch to `true_target`
3185 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3186 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003187 __ B(false_target);
3188 }
David Brazdil0debae72015-11-12 18:37:00 +00003189
3190 if (fallthrough_target.IsLinked()) {
3191 __ Bind(&fallthrough_target);
3192 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003193}
3194
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003195void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3196 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003197 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003198 locations->SetInAt(0, Location::RequiresRegister());
3199 }
3200}
3201
3202void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003203 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3204 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003205 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3206 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3207 true_target = nullptr;
3208 }
3209 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3210 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3211 false_target = nullptr;
3212 }
David Brazdil0debae72015-11-12 18:37:00 +00003213 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003214}
3215
3216void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3217 LocationSummary* locations = new (GetGraph()->GetArena())
3218 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003219 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003220 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003221 locations->SetInAt(0, Location::RequiresRegister());
3222 }
3223}
3224
3225void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003226 SlowPathCodeARM64* slow_path =
3227 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003228 GenerateTestAndBranch(deoptimize,
3229 /* condition_input_index */ 0,
3230 slow_path->GetEntryLabel(),
3231 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003232}
3233
David Brazdilc0b601b2016-02-08 14:20:45 +00003234static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3235 return condition->IsCondition() &&
3236 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3237}
3238
Alexandre Rames880f1192016-06-13 16:04:50 +01003239static inline Condition GetConditionForSelect(HCondition* condition) {
3240 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003241 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3242 : ARM64Condition(cond);
3243}
3244
David Brazdil74eb1b22015-12-14 11:44:01 +00003245void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3246 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003247 if (Primitive::IsFloatingPointType(select->GetType())) {
3248 locations->SetInAt(0, Location::RequiresFpuRegister());
3249 locations->SetInAt(1, Location::RequiresFpuRegister());
3250 locations->SetOut(Location::RequiresFpuRegister());
3251 } else {
3252 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3253 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3254 bool is_true_value_constant = cst_true_value != nullptr;
3255 bool is_false_value_constant = cst_false_value != nullptr;
3256 // Ask VIXL whether we should synthesize constants in registers.
3257 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3258 Operand true_op = is_true_value_constant ?
3259 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3260 Operand false_op = is_false_value_constant ?
3261 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3262 bool true_value_in_register = false;
3263 bool false_value_in_register = false;
3264 MacroAssembler::GetCselSynthesisInformation(
3265 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3266 true_value_in_register |= !is_true_value_constant;
3267 false_value_in_register |= !is_false_value_constant;
3268
3269 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3270 : Location::ConstantLocation(cst_true_value));
3271 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3272 : Location::ConstantLocation(cst_false_value));
3273 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003274 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003275
David Brazdil74eb1b22015-12-14 11:44:01 +00003276 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3277 locations->SetInAt(2, Location::RequiresRegister());
3278 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003279}
3280
3281void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003282 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003283 Condition csel_cond;
3284
3285 if (IsBooleanValueOrMaterializedCondition(cond)) {
3286 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003287 // Use the condition flags set by the previous instruction.
3288 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003289 } else {
3290 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003291 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003292 }
3293 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003294 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003295 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003296 } else {
3297 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003298 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003299 }
3300
Alexandre Rames880f1192016-06-13 16:04:50 +01003301 if (Primitive::IsFloatingPointType(select->GetType())) {
3302 __ Fcsel(OutputFPRegister(select),
3303 InputFPRegisterAt(select, 1),
3304 InputFPRegisterAt(select, 0),
3305 csel_cond);
3306 } else {
3307 __ Csel(OutputRegister(select),
3308 InputOperandAt(select, 1),
3309 InputOperandAt(select, 0),
3310 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003311 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003312}
3313
David Srbecky0cf44932015-12-09 14:09:59 +00003314void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3315 new (GetGraph()->GetArena()) LocationSummary(info);
3316}
3317
David Srbeckyd28f4a02016-03-14 17:14:24 +00003318void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3319 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003320}
3321
3322void CodeGeneratorARM64::GenerateNop() {
3323 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003324}
3325
Alexandre Rames5319def2014-10-23 10:03:10 +01003326void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003327 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003328}
3329
3330void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003331 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003332}
3333
3334void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003335 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003336}
3337
3338void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003339 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003340}
3341
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003342// Temp is used for read barrier.
3343static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3344 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003345 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003346 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3347 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3348 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3349 return 1;
3350 }
3351 return 0;
3352}
3353
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003354// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003355// interface pointer, one for loading the current interface.
3356// The other checks have one temp for loading the object's class.
3357static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3358 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3359 return 3;
3360 }
3361 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003362}
3363
Alexandre Rames67555f72014-11-18 10:55:16 +00003364void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003365 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003366 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003367 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003368 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003369 case TypeCheckKind::kExactCheck:
3370 case TypeCheckKind::kAbstractClassCheck:
3371 case TypeCheckKind::kClassHierarchyCheck:
3372 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003373 call_kind =
3374 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003375 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003376 break;
3377 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003378 case TypeCheckKind::kUnresolvedCheck:
3379 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003380 call_kind = LocationSummary::kCallOnSlowPath;
3381 break;
3382 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003383
Alexandre Rames67555f72014-11-18 10:55:16 +00003384 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003385 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003386 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003387 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003388 locations->SetInAt(0, Location::RequiresRegister());
3389 locations->SetInAt(1, Location::RequiresRegister());
3390 // The "out" register is used as a temporary, so it overlaps with the inputs.
3391 // Note that TypeCheckSlowPathARM64 uses this register too.
3392 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003393 // Add temps if necessary for read barriers.
3394 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003395}
3396
3397void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003398 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003399 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003400 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003401 Register obj = InputRegisterAt(instruction, 0);
3402 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003403 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003404 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003405 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3406 DCHECK_LE(num_temps, 1u);
3407 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003408 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3409 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3410 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3411 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003412
Scott Wakeling97c72b72016-06-24 16:19:36 +01003413 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003414 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003415
3416 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003417 // Avoid null check if we know `obj` is not null.
3418 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003419 __ Cbz(obj, &zero);
3420 }
3421
Roland Levillain44015862016-01-22 11:47:17 +00003422 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003423 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003424 // /* HeapReference<Class> */ out = obj->klass_
3425 GenerateReferenceLoadTwoRegisters(instruction,
3426 out_loc,
3427 obj_loc,
3428 class_offset,
3429 maybe_temp_loc,
3430 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003431 __ Cmp(out, cls);
3432 __ Cset(out, eq);
3433 if (zero.IsLinked()) {
3434 __ B(&done);
3435 }
3436 break;
3437 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003438
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003439 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003440 // /* HeapReference<Class> */ out = obj->klass_
3441 GenerateReferenceLoadTwoRegisters(instruction,
3442 out_loc,
3443 obj_loc,
3444 class_offset,
3445 maybe_temp_loc,
3446 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003447 // If the class is abstract, we eagerly fetch the super class of the
3448 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003449 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003450 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003451 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003452 GenerateReferenceLoadOneRegister(instruction,
3453 out_loc,
3454 super_offset,
3455 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003456 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003457 // If `out` is null, we use it for the result, and jump to `done`.
3458 __ Cbz(out, &done);
3459 __ Cmp(out, cls);
3460 __ B(ne, &loop);
3461 __ Mov(out, 1);
3462 if (zero.IsLinked()) {
3463 __ B(&done);
3464 }
3465 break;
3466 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003467
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003468 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003469 // /* HeapReference<Class> */ out = obj->klass_
3470 GenerateReferenceLoadTwoRegisters(instruction,
3471 out_loc,
3472 obj_loc,
3473 class_offset,
3474 maybe_temp_loc,
3475 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003476 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003477 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003478 __ Bind(&loop);
3479 __ Cmp(out, cls);
3480 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003481 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003482 GenerateReferenceLoadOneRegister(instruction,
3483 out_loc,
3484 super_offset,
3485 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003486 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003487 __ Cbnz(out, &loop);
3488 // If `out` is null, we use it for the result, and jump to `done`.
3489 __ B(&done);
3490 __ Bind(&success);
3491 __ Mov(out, 1);
3492 if (zero.IsLinked()) {
3493 __ B(&done);
3494 }
3495 break;
3496 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003497
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003498 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003499 // /* HeapReference<Class> */ out = obj->klass_
3500 GenerateReferenceLoadTwoRegisters(instruction,
3501 out_loc,
3502 obj_loc,
3503 class_offset,
3504 maybe_temp_loc,
3505 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003506 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003507 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003508 __ Cmp(out, cls);
3509 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003510 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003511 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003512 GenerateReferenceLoadOneRegister(instruction,
3513 out_loc,
3514 component_offset,
3515 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003516 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003517 // If `out` is null, we use it for the result, and jump to `done`.
3518 __ Cbz(out, &done);
3519 __ Ldrh(out, HeapOperand(out, primitive_offset));
3520 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3521 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003522 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003523 __ Mov(out, 1);
3524 __ B(&done);
3525 break;
3526 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003527
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003528 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003529 // No read barrier since the slow path will retry upon failure.
3530 // /* HeapReference<Class> */ out = obj->klass_
3531 GenerateReferenceLoadTwoRegisters(instruction,
3532 out_loc,
3533 obj_loc,
3534 class_offset,
3535 maybe_temp_loc,
3536 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003537 __ Cmp(out, cls);
3538 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003539 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3540 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003541 codegen_->AddSlowPath(slow_path);
3542 __ B(ne, slow_path->GetEntryLabel());
3543 __ Mov(out, 1);
3544 if (zero.IsLinked()) {
3545 __ B(&done);
3546 }
3547 break;
3548 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003549
Calin Juravle98893e12015-10-02 21:05:03 +01003550 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003551 case TypeCheckKind::kInterfaceCheck: {
3552 // Note that we indeed only call on slow path, but we always go
3553 // into the slow path for the unresolved and interface check
3554 // cases.
3555 //
3556 // We cannot directly call the InstanceofNonTrivial runtime
3557 // entry point without resorting to a type checking slow path
3558 // here (i.e. by calling InvokeRuntime directly), as it would
3559 // require to assign fixed registers for the inputs of this
3560 // HInstanceOf instruction (following the runtime calling
3561 // convention), which might be cluttered by the potential first
3562 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003563 //
3564 // TODO: Introduce a new runtime entry point taking the object
3565 // to test (instead of its class) as argument, and let it deal
3566 // with the read barrier issues. This will let us refactor this
3567 // case of the `switch` code as it was previously (with a direct
3568 // call to the runtime not using a type checking slow path).
3569 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003570 DCHECK(locations->OnlyCallsOnSlowPath());
3571 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3572 /* is_fatal */ false);
3573 codegen_->AddSlowPath(slow_path);
3574 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003575 if (zero.IsLinked()) {
3576 __ B(&done);
3577 }
3578 break;
3579 }
3580 }
3581
3582 if (zero.IsLinked()) {
3583 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003584 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003585 }
3586
3587 if (done.IsLinked()) {
3588 __ Bind(&done);
3589 }
3590
3591 if (slow_path != nullptr) {
3592 __ Bind(slow_path->GetExitLabel());
3593 }
3594}
3595
3596void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3597 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3598 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3599
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003600 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3601 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003602 case TypeCheckKind::kExactCheck:
3603 case TypeCheckKind::kAbstractClassCheck:
3604 case TypeCheckKind::kClassHierarchyCheck:
3605 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003606 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3607 LocationSummary::kCallOnSlowPath :
3608 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003609 break;
3610 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003611 case TypeCheckKind::kUnresolvedCheck:
3612 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003613 call_kind = LocationSummary::kCallOnSlowPath;
3614 break;
3615 }
3616
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003617 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3618 locations->SetInAt(0, Location::RequiresRegister());
3619 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003620 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3621 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003622}
3623
3624void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003625 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003626 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003627 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003628 Register obj = InputRegisterAt(instruction, 0);
3629 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003630 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3631 DCHECK_GE(num_temps, 1u);
3632 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003633 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003634 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3635 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003636 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003637 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3638 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3639 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3640 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3641 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3642 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3643 const uint32_t object_array_data_offset =
3644 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003645
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003646 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003647 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3648 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3649 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003650 if (!kEmitCompilerReadBarrier) {
3651 is_type_check_slow_path_fatal =
3652 (type_check_kind == TypeCheckKind::kExactCheck ||
3653 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3654 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3655 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3656 !instruction->CanThrowIntoCatchBlock();
3657 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003658 SlowPathCodeARM64* type_check_slow_path =
3659 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3660 is_type_check_slow_path_fatal);
3661 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003662
Scott Wakeling97c72b72016-06-24 16:19:36 +01003663 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003664 // Avoid null check if we know obj is not null.
3665 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003666 __ Cbz(obj, &done);
3667 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003668
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003669 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003670 case TypeCheckKind::kExactCheck:
3671 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003672 // /* HeapReference<Class> */ temp = obj->klass_
3673 GenerateReferenceLoadTwoRegisters(instruction,
3674 temp_loc,
3675 obj_loc,
3676 class_offset,
3677 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003678 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003679
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003680 __ Cmp(temp, cls);
3681 // Jump to slow path for throwing the exception or doing a
3682 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003683 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003684 break;
3685 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003686
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003687 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003688 // /* HeapReference<Class> */ temp = obj->klass_
3689 GenerateReferenceLoadTwoRegisters(instruction,
3690 temp_loc,
3691 obj_loc,
3692 class_offset,
3693 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003694 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003695
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003696 // If the class is abstract, we eagerly fetch the super class of the
3697 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003698 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003699 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003700 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003701 GenerateReferenceLoadOneRegister(instruction,
3702 temp_loc,
3703 super_offset,
3704 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003705 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003706
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003707 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3708 // exception.
3709 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3710 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003711 __ Cmp(temp, cls);
3712 __ B(ne, &loop);
3713 break;
3714 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003715
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003716 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003717 // /* HeapReference<Class> */ temp = obj->klass_
3718 GenerateReferenceLoadTwoRegisters(instruction,
3719 temp_loc,
3720 obj_loc,
3721 class_offset,
3722 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003723 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003724
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003725 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003726 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003727 __ Bind(&loop);
3728 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003729 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003730
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003731 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003732 GenerateReferenceLoadOneRegister(instruction,
3733 temp_loc,
3734 super_offset,
3735 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003736 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003737
3738 // If the class reference currently in `temp` is not null, jump
3739 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003740 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003741 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003742 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003743 break;
3744 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003745
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003746 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003747 // /* HeapReference<Class> */ temp = obj->klass_
3748 GenerateReferenceLoadTwoRegisters(instruction,
3749 temp_loc,
3750 obj_loc,
3751 class_offset,
3752 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003753 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003754
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003755 // Do an exact check.
3756 __ Cmp(temp, cls);
3757 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003758
3759 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003760 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003761 GenerateReferenceLoadOneRegister(instruction,
3762 temp_loc,
3763 component_offset,
3764 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003765 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003766
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003767 // If the component type is null, jump to the slow path to throw the exception.
3768 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3769 // Otherwise, the object is indeed an array. Further check that this component type is not a
3770 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003771 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3772 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003773 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003774 break;
3775 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003776
Calin Juravle98893e12015-10-02 21:05:03 +01003777 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003778 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003779 //
3780 // We cannot directly call the CheckCast runtime entry point
3781 // without resorting to a type checking slow path here (i.e. by
3782 // calling InvokeRuntime directly), as it would require to
3783 // assign fixed registers for the inputs of this HInstanceOf
3784 // instruction (following the runtime calling convention), which
3785 // might be cluttered by the potential first read barrier
3786 // emission at the beginning of this method.
3787 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003788 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003789 case TypeCheckKind::kInterfaceCheck: {
3790 // /* HeapReference<Class> */ temp = obj->klass_
3791 GenerateReferenceLoadTwoRegisters(instruction,
3792 temp_loc,
3793 obj_loc,
3794 class_offset,
3795 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003796 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003797
3798 // /* HeapReference<Class> */ temp = temp->iftable_
3799 GenerateReferenceLoadTwoRegisters(instruction,
3800 temp_loc,
3801 temp_loc,
3802 iftable_offset,
3803 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003804 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003805 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003806 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08003807 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003808 vixl::aarch64::Label start_loop;
3809 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003810 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003811 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3812 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003813 // Go to next interface.
3814 __ Add(temp, temp, 2 * kHeapReferenceSize);
3815 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003816 // Compare the classes and continue the loop if they do not match.
3817 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
3818 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003819 break;
3820 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003821 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003822 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003823
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003824 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003825}
3826
Alexandre Rames5319def2014-10-23 10:03:10 +01003827void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3828 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3829 locations->SetOut(Location::ConstantLocation(constant));
3830}
3831
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003832void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003833 // Will be generated at use site.
3834}
3835
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003836void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3837 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3838 locations->SetOut(Location::ConstantLocation(constant));
3839}
3840
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003841void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003842 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003843}
3844
Calin Juravle175dc732015-08-25 15:42:32 +01003845void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3846 // The trampoline uses the same calling convention as dex calling conventions,
3847 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3848 // the method_idx.
3849 HandleInvoke(invoke);
3850}
3851
3852void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3853 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3854}
3855
Alexandre Rames5319def2014-10-23 10:03:10 +01003856void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003857 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003858 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003859}
3860
Alexandre Rames67555f72014-11-18 10:55:16 +00003861void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3862 HandleInvoke(invoke);
3863}
3864
3865void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3866 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003867 LocationSummary* locations = invoke->GetLocations();
3868 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003869 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003870 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003871 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003872
3873 // The register ip1 is required to be used for the hidden argument in
3874 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003875 MacroAssembler* masm = GetVIXLAssembler();
3876 UseScratchRegisterScope scratch_scope(masm);
3877 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003878 scratch_scope.Exclude(ip1);
3879 __ Mov(ip1, invoke->GetDexMethodIndex());
3880
Alexandre Rames67555f72014-11-18 10:55:16 +00003881 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003882 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003883 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003884 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003885 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003886 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003887 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003888 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003889 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003890 // Instead of simply (possibly) unpoisoning `temp` here, we should
3891 // emit a read barrier for the previous class reference load.
3892 // However this is not required in practice, as this is an
3893 // intermediate/temporary reference and because the current
3894 // concurrent copying collector keeps the from-space memory
3895 // intact/accessible until the end of the marking phase (the
3896 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003897 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003898 __ Ldr(temp,
3899 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3900 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003901 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003902 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003903 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003904 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003905 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003906 // lr();
3907 __ Blr(lr);
3908 DCHECK(!codegen_->IsLeafMethod());
3909 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3910}
3911
3912void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003913 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3914 if (intrinsic.TryDispatch(invoke)) {
3915 return;
3916 }
3917
Alexandre Rames67555f72014-11-18 10:55:16 +00003918 HandleInvoke(invoke);
3919}
3920
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003921void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003922 // Explicit clinit checks triggered by static invokes must have been pruned by
3923 // art::PrepareForRegisterAllocation.
3924 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003925
Andreas Gampe878d58c2015-01-15 23:24:00 -08003926 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3927 if (intrinsic.TryDispatch(invoke)) {
3928 return;
3929 }
3930
Alexandre Rames67555f72014-11-18 10:55:16 +00003931 HandleInvoke(invoke);
3932}
3933
Andreas Gampe878d58c2015-01-15 23:24:00 -08003934static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3935 if (invoke->GetLocations()->Intrinsified()) {
3936 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3937 intrinsic.Dispatch(invoke);
3938 return true;
3939 }
3940 return false;
3941}
3942
Vladimir Markodc151b22015-10-15 18:02:30 +01003943HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3944 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003945 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003946 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003947 return desired_dispatch_info;
3948}
3949
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003950void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003951 // For better instruction scheduling we load the direct code pointer before the method pointer.
3952 bool direct_code_loaded = false;
3953 switch (invoke->GetCodePtrLocation()) {
3954 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3955 // LR = code address from literal pool with link-time patch.
3956 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3957 direct_code_loaded = true;
3958 break;
3959 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3960 // LR = invoke->GetDirectCodePtr();
3961 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3962 direct_code_loaded = true;
3963 break;
3964 default:
3965 break;
3966 }
3967
Andreas Gampe878d58c2015-01-15 23:24:00 -08003968 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003969 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3970 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003971 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
3972 uint32_t offset =
3973 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003974 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003975 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00003976 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003977 }
Vladimir Marko58155012015-08-19 12:49:41 +00003978 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003979 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003980 break;
3981 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3982 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003983 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003984 break;
3985 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3986 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003987 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003988 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3989 break;
3990 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3991 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003992 const DexFile& dex_file = invoke->GetDexFile();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003993 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003994 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003995 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003996 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003997 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003998 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003999 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004000 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004001 }
Vladimir Marko58155012015-08-19 12:49:41 +00004002 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004003 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004004 Register reg = XRegisterFrom(temp);
4005 Register method_reg;
4006 if (current_method.IsRegister()) {
4007 method_reg = XRegisterFrom(current_method);
4008 } else {
4009 DCHECK(invoke->GetLocations()->Intrinsified());
4010 DCHECK(!current_method.IsValid());
4011 method_reg = reg;
4012 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4013 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004014
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004015 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004016 __ Ldr(reg.X(),
4017 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004018 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004019 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004020 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4021 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004022 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4023 break;
4024 }
4025 }
4026
4027 switch (invoke->GetCodePtrLocation()) {
4028 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4029 __ Bl(&frame_entry_label_);
4030 break;
4031 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004032 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4033 invoke->GetTargetMethod().dex_method_index);
Scott Wakeling97c72b72016-06-24 16:19:36 +01004034 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
4035 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00004036 __ Bind(label);
4037 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00004038 break;
4039 }
4040 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4041 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4042 // LR prepared above for better instruction scheduling.
4043 DCHECK(direct_code_loaded);
4044 // lr()
4045 __ Blr(lr);
4046 break;
4047 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4048 // LR = callee_method->entry_point_from_quick_compiled_code_;
4049 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004050 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004051 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004052 // lr()
4053 __ Blr(lr);
4054 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004055 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004056
Andreas Gampe878d58c2015-01-15 23:24:00 -08004057 DCHECK(!IsLeafMethod());
4058}
4059
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004060void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004061 // Use the calling convention instead of the location of the receiver, as
4062 // intrinsics may have put the receiver in a different register. In the intrinsics
4063 // slow path, the arguments have been moved to the right place, so here we are
4064 // guaranteed that the receiver is the first register of the calling convention.
4065 InvokeDexCallingConvention calling_convention;
4066 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004067 Register temp = XRegisterFrom(temp_in);
4068 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4069 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4070 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004071 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004072
4073 BlockPoolsScope block_pools(GetVIXLAssembler());
4074
4075 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004076 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004077 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004078 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004079 // Instead of simply (possibly) unpoisoning `temp` here, we should
4080 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004081 // intermediate/temporary reference and because the current
4082 // concurrent copying collector keeps the from-space memory
4083 // intact/accessible until the end of the marking phase (the
4084 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004085 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4086 // temp = temp->GetMethodAt(method_offset);
4087 __ Ldr(temp, MemOperand(temp, method_offset));
4088 // lr = temp->GetEntryPoint();
4089 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
4090 // lr();
4091 __ Blr(lr);
4092}
4093
Scott Wakeling97c72b72016-06-24 16:19:36 +01004094vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4095 const DexFile& dex_file,
4096 uint32_t string_index,
4097 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004098 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
4099}
4100
Scott Wakeling97c72b72016-06-24 16:19:36 +01004101vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4102 const DexFile& dex_file,
4103 uint32_t type_index,
4104 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004105 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
4106}
4107
Scott Wakeling97c72b72016-06-24 16:19:36 +01004108vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4109 const DexFile& dex_file,
4110 uint32_t element_offset,
4111 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004112 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4113}
4114
Scott Wakeling97c72b72016-06-24 16:19:36 +01004115vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4116 const DexFile& dex_file,
4117 uint32_t offset_or_index,
4118 vixl::aarch64::Label* adrp_label,
4119 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004120 // Add a patch entry and return the label.
4121 patches->emplace_back(dex_file, offset_or_index);
4122 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004123 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004124 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4125 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4126 return label;
4127}
4128
Scott Wakeling97c72b72016-06-24 16:19:36 +01004129vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004130 const DexFile& dex_file, uint32_t string_index) {
4131 return boot_image_string_patches_.GetOrCreate(
4132 StringReference(&dex_file, string_index),
4133 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4134}
4135
Scott Wakeling97c72b72016-06-24 16:19:36 +01004136vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004137 const DexFile& dex_file, uint32_t type_index) {
4138 return boot_image_type_patches_.GetOrCreate(
4139 TypeReference(&dex_file, type_index),
4140 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4141}
4142
Scott Wakeling97c72b72016-06-24 16:19:36 +01004143vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4144 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004145 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4146 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4147 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4148}
4149
Scott Wakeling97c72b72016-06-24 16:19:36 +01004150vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
4151 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004152 return DeduplicateUint64Literal(address);
4153}
4154
Vladimir Markoaad75c62016-10-03 08:46:48 +00004155void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4156 vixl::aarch64::Register reg) {
4157 DCHECK(reg.IsX());
4158 SingleEmissionCheckScope guard(GetVIXLAssembler());
4159 __ Bind(fixup_label);
4160 __ adrp(reg, /* offset placeholder */ 0);
4161}
4162
4163void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4164 vixl::aarch64::Register out,
4165 vixl::aarch64::Register base) {
4166 DCHECK(out.IsX());
4167 DCHECK(base.IsX());
4168 SingleEmissionCheckScope guard(GetVIXLAssembler());
4169 __ Bind(fixup_label);
4170 __ add(out, base, Operand(/* offset placeholder */ 0));
4171}
4172
4173void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4174 vixl::aarch64::Register out,
4175 vixl::aarch64::Register base) {
4176 DCHECK(base.IsX());
4177 SingleEmissionCheckScope guard(GetVIXLAssembler());
4178 __ Bind(fixup_label);
4179 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4180}
4181
4182template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4183inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4184 const ArenaDeque<PcRelativePatchInfo>& infos,
4185 ArenaVector<LinkerPatch>* linker_patches) {
4186 for (const PcRelativePatchInfo& info : infos) {
4187 linker_patches->push_back(Factory(info.label.GetLocation(),
4188 &info.target_dex_file,
4189 info.pc_insn_label->GetLocation(),
4190 info.offset_or_index));
4191 }
4192}
4193
Vladimir Marko58155012015-08-19 12:49:41 +00004194void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4195 DCHECK(linker_patches->empty());
4196 size_t size =
4197 method_patches_.size() +
4198 call_patches_.size() +
4199 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004200 pc_relative_dex_cache_patches_.size() +
4201 boot_image_string_patches_.size() +
4202 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004203 boot_image_type_patches_.size() +
4204 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004205 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004206 linker_patches->reserve(size);
4207 for (const auto& entry : method_patches_) {
4208 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004209 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
4210 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00004211 target_method.dex_file,
4212 target_method.dex_method_index));
4213 }
4214 for (const auto& entry : call_patches_) {
4215 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004216 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
4217 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00004218 target_method.dex_file,
4219 target_method.dex_method_index));
4220 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004221 for (const PatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
4222 linker_patches->push_back(
4223 LinkerPatch::RelativeCodePatch(info.label.GetLocation(), &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004224 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004225 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004226 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004227 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004228 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004229 info.offset_or_index));
4230 }
4231 for (const auto& entry : boot_image_string_patches_) {
4232 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004233 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4234 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004235 target_string.dex_file,
4236 target_string.string_index));
4237 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004238 if (!GetCompilerOptions().IsBootImage()) {
4239 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4240 linker_patches);
4241 } else {
4242 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4243 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004244 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004245 for (const auto& entry : boot_image_type_patches_) {
4246 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004247 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4248 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004249 target_type.dex_file,
4250 target_type.type_index));
4251 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004252 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4253 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004254 for (const auto& entry : boot_image_address_patches_) {
4255 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004256 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4257 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004258 }
4259}
4260
Scott Wakeling97c72b72016-06-24 16:19:36 +01004261vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004262 Uint32ToLiteralMap* map) {
4263 return map->GetOrCreate(
4264 value,
4265 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4266}
4267
Scott Wakeling97c72b72016-06-24 16:19:36 +01004268vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004269 return uint64_literals_.GetOrCreate(
4270 value,
4271 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004272}
4273
Scott Wakeling97c72b72016-06-24 16:19:36 +01004274vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004275 MethodReference target_method,
4276 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004277 return map->GetOrCreate(
4278 target_method,
4279 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004280}
4281
Scott Wakeling97c72b72016-06-24 16:19:36 +01004282vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004283 MethodReference target_method) {
4284 return DeduplicateMethodLiteral(target_method, &method_patches_);
4285}
4286
Scott Wakeling97c72b72016-06-24 16:19:36 +01004287vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004288 MethodReference target_method) {
4289 return DeduplicateMethodLiteral(target_method, &call_patches_);
4290}
4291
4292
Andreas Gampe878d58c2015-01-15 23:24:00 -08004293void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004294 // Explicit clinit checks triggered by static invokes must have been pruned by
4295 // art::PrepareForRegisterAllocation.
4296 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004297
Andreas Gampe878d58c2015-01-15 23:24:00 -08004298 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4299 return;
4300 }
4301
Alexandre Ramesd921d642015-04-16 15:07:16 +01004302 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004303 LocationSummary* locations = invoke->GetLocations();
4304 codegen_->GenerateStaticOrDirectCall(
4305 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004306 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004307}
4308
4309void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004310 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4311 return;
4312 }
4313
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004314 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004315 DCHECK(!codegen_->IsLeafMethod());
4316 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4317}
4318
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004319HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4320 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004321 switch (desired_class_load_kind) {
4322 case HLoadClass::LoadKind::kReferrersClass:
4323 break;
4324 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4325 DCHECK(!GetCompilerOptions().GetCompilePic());
4326 break;
4327 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4328 DCHECK(GetCompilerOptions().GetCompilePic());
4329 break;
4330 case HLoadClass::LoadKind::kBootImageAddress:
4331 break;
4332 case HLoadClass::LoadKind::kDexCacheAddress:
4333 DCHECK(Runtime::Current()->UseJitCompilation());
4334 break;
4335 case HLoadClass::LoadKind::kDexCachePcRelative:
4336 DCHECK(!Runtime::Current()->UseJitCompilation());
4337 break;
4338 case HLoadClass::LoadKind::kDexCacheViaMethod:
4339 break;
4340 }
4341 return desired_class_load_kind;
4342}
4343
Alexandre Rames67555f72014-11-18 10:55:16 +00004344void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004345 if (cls->NeedsAccessCheck()) {
4346 InvokeRuntimeCallingConvention calling_convention;
4347 CodeGenerator::CreateLoadClassLocationSummary(
4348 cls,
4349 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01004350 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004351 /* code_generator_supports_read_barrier */ true);
4352 return;
4353 }
4354
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004355 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4356 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004357 ? LocationSummary::kCallOnSlowPath
4358 : LocationSummary::kNoCall;
4359 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004360 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004361 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004362 }
4363
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004364 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4365 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4366 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4367 locations->SetInAt(0, Location::RequiresRegister());
4368 }
4369 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004370}
4371
4372void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004373 if (cls->NeedsAccessCheck()) {
4374 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004375 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004376 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004377 return;
4378 }
4379
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004380 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004381 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004382
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004383 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4384 ? kWithoutReadBarrier
4385 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004386 bool generate_null_check = false;
4387 switch (cls->GetLoadKind()) {
4388 case HLoadClass::LoadKind::kReferrersClass: {
4389 DCHECK(!cls->CanCallRuntime());
4390 DCHECK(!cls->MustGenerateClinitCheck());
4391 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4392 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004393 GenerateGcRootFieldLoad(cls,
4394 out_loc,
4395 current_method,
4396 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004397 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004398 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004399 break;
4400 }
4401 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004402 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004403 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4404 cls->GetTypeIndex()));
4405 break;
4406 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004407 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004408 // Add ADRP with its PC-relative type patch.
4409 const DexFile& dex_file = cls->GetDexFile();
4410 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004411 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004412 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004413 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004414 vixl::aarch64::Label* add_label =
4415 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004416 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004417 break;
4418 }
4419 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004420 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004421 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4422 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4423 break;
4424 }
4425 case HLoadClass::LoadKind::kDexCacheAddress: {
4426 DCHECK_NE(cls->GetAddress(), 0u);
4427 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4428 // that gives a 16KiB range. To try and reduce the number of literals if we load
4429 // multiple types, simply split the dex cache address to a 16KiB aligned base
4430 // loaded from a literal and the remaining offset embedded in the load.
4431 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4432 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4433 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4434 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4435 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4436 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4437 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004438 GenerateGcRootFieldLoad(cls,
4439 out_loc,
4440 out.X(),
4441 offset,
Roland Levillain00468f32016-10-27 18:02:48 +01004442 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004443 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004444 generate_null_check = !cls->IsInDexCache();
4445 break;
4446 }
4447 case HLoadClass::LoadKind::kDexCachePcRelative: {
4448 // Add ADRP with its PC-relative DexCache access patch.
4449 const DexFile& dex_file = cls->GetDexFile();
4450 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004451 vixl::aarch64::Label* adrp_label =
4452 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004453 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004454 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004455 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004456 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4457 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004458 GenerateGcRootFieldLoad(cls,
4459 out_loc,
4460 out.X(),
4461 /* offset placeholder */ 0,
4462 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004463 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004464 generate_null_check = !cls->IsInDexCache();
4465 break;
4466 }
4467 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4468 MemberOffset resolved_types_offset =
4469 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4470 // /* GcRoot<mirror::Class>[] */ out =
4471 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4472 Register current_method = InputRegisterAt(cls, 0);
4473 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4474 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004475 GenerateGcRootFieldLoad(cls,
4476 out_loc,
4477 out.X(),
4478 CodeGenerator::GetCacheOffset(cls->GetTypeIndex()),
Roland Levillain00468f32016-10-27 18:02:48 +01004479 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004480 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004481 generate_null_check = !cls->IsInDexCache();
4482 break;
4483 }
4484 }
4485
4486 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4487 DCHECK(cls->CanCallRuntime());
4488 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4489 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4490 codegen_->AddSlowPath(slow_path);
4491 if (generate_null_check) {
4492 __ Cbz(out, slow_path->GetEntryLabel());
4493 }
4494 if (cls->MustGenerateClinitCheck()) {
4495 GenerateClassInitializationCheck(slow_path, out);
4496 } else {
4497 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004498 }
4499 }
4500}
4501
David Brazdilcb1c0552015-08-04 16:22:25 +01004502static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004503 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004504}
4505
Alexandre Rames67555f72014-11-18 10:55:16 +00004506void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4507 LocationSummary* locations =
4508 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4509 locations->SetOut(Location::RequiresRegister());
4510}
4511
4512void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004513 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4514}
4515
4516void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4517 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4518}
4519
4520void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4521 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004522}
4523
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004524HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4525 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004526 switch (desired_string_load_kind) {
4527 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4528 DCHECK(!GetCompilerOptions().GetCompilePic());
4529 break;
4530 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4531 DCHECK(GetCompilerOptions().GetCompilePic());
4532 break;
4533 case HLoadString::LoadKind::kBootImageAddress:
4534 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004535 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004536 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004537 break;
4538 case HLoadString::LoadKind::kDexCacheViaMethod:
4539 break;
4540 }
4541 return desired_string_load_kind;
4542}
4543
Alexandre Rames67555f72014-11-18 10:55:16 +00004544void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00004545 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
4546 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
4547 ? LocationSummary::kCallOnMainOnly
4548 : LocationSummary::kCallOnSlowPath)
4549 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004550 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004551 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004552 InvokeRuntimeCallingConvention calling_convention;
4553 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4554 } else {
4555 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004556 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4557 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4558 // Rely on the pResolveString and/or marking to save everything, including temps.
4559 RegisterSet caller_saves = RegisterSet::Empty();
4560 InvokeRuntimeCallingConvention calling_convention;
4561 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4562 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4563 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4564 Primitive::kPrimNot).GetCode());
4565 locations->SetCustomSlowPathCallerSaves(caller_saves);
4566 } else {
4567 // For non-Baker read barrier we have a temp-clobbering call.
4568 }
4569 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004570 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004571}
4572
4573void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004574 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004575
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004576 switch (load->GetLoadKind()) {
4577 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004578 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4579 load->GetStringIndex()));
4580 return; // No dex cache slow path.
4581 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004582 // Add ADRP with its PC-relative String patch.
4583 const DexFile& dex_file = load->GetDexFile();
4584 uint32_t string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004585 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004586 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004587 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004588 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004589 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004590 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004591 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004592 return; // No dex cache slow path.
4593 }
4594 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004595 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4596 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4597 return; // No dex cache slow path.
4598 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004599 case HLoadString::LoadKind::kBssEntry: {
4600 // Add ADRP with its PC-relative String .bss entry patch.
4601 const DexFile& dex_file = load->GetDexFile();
4602 uint32_t string_index = load->GetStringIndex();
4603 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004604 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4605 Register temp = temps.AcquireX();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004606 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004607 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004608 // Add LDR with its PC-relative String patch.
4609 vixl::aarch64::Label* ldr_label =
4610 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00004611 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00004612 GenerateGcRootFieldLoad(load,
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00004613 load->GetLocations()->Out(),
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004614 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01004615 /* offset placeholder */ 0u,
4616 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004617 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004618 SlowPathCodeARM64* slow_path =
4619 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004620 codegen_->AddSlowPath(slow_path);
4621 __ Cbz(out.X(), slow_path->GetEntryLabel());
4622 __ Bind(slow_path->GetExitLabel());
4623 return;
4624 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004625 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004626 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004627 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004628
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004629 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004630 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004631 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004632 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex());
4633 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4634 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004635}
4636
Alexandre Rames5319def2014-10-23 10:03:10 +01004637void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4638 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4639 locations->SetOut(Location::ConstantLocation(constant));
4640}
4641
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004642void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004643 // Will be generated at use site.
4644}
4645
Alexandre Rames67555f72014-11-18 10:55:16 +00004646void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4647 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004648 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004649 InvokeRuntimeCallingConvention calling_convention;
4650 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4651}
4652
4653void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004654 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4655 instruction,
4656 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004657 if (instruction->IsEnter()) {
4658 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4659 } else {
4660 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4661 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004662}
4663
Alexandre Rames42d641b2014-10-27 14:00:51 +00004664void LocationsBuilderARM64::VisitMul(HMul* mul) {
4665 LocationSummary* locations =
4666 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4667 switch (mul->GetResultType()) {
4668 case Primitive::kPrimInt:
4669 case Primitive::kPrimLong:
4670 locations->SetInAt(0, Location::RequiresRegister());
4671 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004672 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004673 break;
4674
4675 case Primitive::kPrimFloat:
4676 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004677 locations->SetInAt(0, Location::RequiresFpuRegister());
4678 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004679 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004680 break;
4681
4682 default:
4683 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4684 }
4685}
4686
4687void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4688 switch (mul->GetResultType()) {
4689 case Primitive::kPrimInt:
4690 case Primitive::kPrimLong:
4691 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4692 break;
4693
4694 case Primitive::kPrimFloat:
4695 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004696 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004697 break;
4698
4699 default:
4700 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4701 }
4702}
4703
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004704void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4705 LocationSummary* locations =
4706 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4707 switch (neg->GetResultType()) {
4708 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004709 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004710 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004711 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004712 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004713
4714 case Primitive::kPrimFloat:
4715 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004716 locations->SetInAt(0, Location::RequiresFpuRegister());
4717 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004718 break;
4719
4720 default:
4721 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4722 }
4723}
4724
4725void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4726 switch (neg->GetResultType()) {
4727 case Primitive::kPrimInt:
4728 case Primitive::kPrimLong:
4729 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4730 break;
4731
4732 case Primitive::kPrimFloat:
4733 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004734 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004735 break;
4736
4737 default:
4738 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4739 }
4740}
4741
4742void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4743 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004744 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004745 InvokeRuntimeCallingConvention calling_convention;
4746 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004747 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004748 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004749 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004750}
4751
4752void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4753 LocationSummary* locations = instruction->GetLocations();
4754 InvokeRuntimeCallingConvention calling_convention;
4755 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4756 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004757 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004758 // Note: if heap poisoning is enabled, the entry point takes cares
4759 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004760 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004761 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004762}
4763
Alexandre Rames5319def2014-10-23 10:03:10 +01004764void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4765 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004766 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004767 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004768 if (instruction->IsStringAlloc()) {
4769 locations->AddTemp(LocationFrom(kArtMethodRegister));
4770 } else {
4771 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4772 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4773 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004774 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4775}
4776
4777void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004778 // Note: if heap poisoning is enabled, the entry point takes cares
4779 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004780 if (instruction->IsStringAlloc()) {
4781 // String is allocated through StringFactory. Call NewEmptyString entry point.
4782 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004783 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004784 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4785 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4786 __ Blr(lr);
4787 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4788 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004789 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004790 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4791 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004792}
4793
4794void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4795 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004796 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004797 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004798}
4799
4800void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004801 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004802 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004803 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004804 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004805 break;
4806
4807 default:
4808 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4809 }
4810}
4811
David Brazdil66d126e2015-04-03 16:02:44 +01004812void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4813 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4814 locations->SetInAt(0, Location::RequiresRegister());
4815 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4816}
4817
4818void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004819 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004820}
4821
Alexandre Rames5319def2014-10-23 10:03:10 +01004822void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004823 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4824 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004825}
4826
Calin Juravle2ae48182016-03-16 14:05:09 +00004827void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4828 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004829 return;
4830 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004831
Alexandre Ramesd921d642015-04-16 15:07:16 +01004832 BlockPoolsScope block_pools(GetVIXLAssembler());
4833 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004834 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004835 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004836}
4837
Calin Juravle2ae48182016-03-16 14:05:09 +00004838void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004839 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004840 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004841
4842 LocationSummary* locations = instruction->GetLocations();
4843 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004844
4845 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004846}
4847
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004848void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004849 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004850}
4851
Alexandre Rames67555f72014-11-18 10:55:16 +00004852void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4853 HandleBinaryOp(instruction);
4854}
4855
4856void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4857 HandleBinaryOp(instruction);
4858}
4859
Alexandre Rames3e69f162014-12-10 10:36:50 +00004860void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4861 LOG(FATAL) << "Unreachable";
4862}
4863
4864void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4865 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4866}
4867
Alexandre Rames5319def2014-10-23 10:03:10 +01004868void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4869 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4870 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4871 if (location.IsStackSlot()) {
4872 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4873 } else if (location.IsDoubleStackSlot()) {
4874 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4875 }
4876 locations->SetOut(location);
4877}
4878
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004879void InstructionCodeGeneratorARM64::VisitParameterValue(
4880 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004881 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004882}
4883
4884void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4885 LocationSummary* locations =
4886 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004887 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004888}
4889
4890void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4891 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4892 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004893}
4894
4895void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4896 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004897 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004898 locations->SetInAt(i, Location::Any());
4899 }
4900 locations->SetOut(Location::Any());
4901}
4902
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004903void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004904 LOG(FATAL) << "Unreachable";
4905}
4906
Serban Constantinescu02164b32014-11-13 14:05:07 +00004907void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004908 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004909 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004910 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4911 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004912 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4913
4914 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004915 case Primitive::kPrimInt:
4916 case Primitive::kPrimLong:
4917 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004918 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004919 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4920 break;
4921
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004922 case Primitive::kPrimFloat:
4923 case Primitive::kPrimDouble: {
4924 InvokeRuntimeCallingConvention calling_convention;
4925 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4926 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4927 locations->SetOut(calling_convention.GetReturnLocation(type));
4928
4929 break;
4930 }
4931
Serban Constantinescu02164b32014-11-13 14:05:07 +00004932 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004933 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004934 }
4935}
4936
4937void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4938 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004939
Serban Constantinescu02164b32014-11-13 14:05:07 +00004940 switch (type) {
4941 case Primitive::kPrimInt:
4942 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004943 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004944 break;
4945 }
4946
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004947 case Primitive::kPrimFloat:
4948 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004949 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4950 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004951 if (type == Primitive::kPrimFloat) {
4952 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4953 } else {
4954 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4955 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004956 break;
4957 }
4958
Serban Constantinescu02164b32014-11-13 14:05:07 +00004959 default:
4960 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004961 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004962 }
4963}
4964
Calin Juravle27df7582015-04-17 19:12:31 +01004965void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4966 memory_barrier->SetLocations(nullptr);
4967}
4968
4969void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004970 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004971}
4972
Alexandre Rames5319def2014-10-23 10:03:10 +01004973void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4974 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4975 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004976 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004977}
4978
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004979void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004980 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004981}
4982
4983void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4984 instruction->SetLocations(nullptr);
4985}
4986
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004987void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004988 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004989}
4990
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004991void LocationsBuilderARM64::VisitRor(HRor* ror) {
4992 HandleBinaryOp(ror);
4993}
4994
4995void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4996 HandleBinaryOp(ror);
4997}
4998
Serban Constantinescu02164b32014-11-13 14:05:07 +00004999void LocationsBuilderARM64::VisitShl(HShl* shl) {
5000 HandleShift(shl);
5001}
5002
5003void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5004 HandleShift(shl);
5005}
5006
5007void LocationsBuilderARM64::VisitShr(HShr* shr) {
5008 HandleShift(shr);
5009}
5010
5011void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5012 HandleShift(shr);
5013}
5014
Alexandre Rames5319def2014-10-23 10:03:10 +01005015void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005016 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005017}
5018
5019void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005020 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005021}
5022
Alexandre Rames67555f72014-11-18 10:55:16 +00005023void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005024 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005025}
5026
5027void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005028 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005029}
5030
5031void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005032 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005033}
5034
Alexandre Rames67555f72014-11-18 10:55:16 +00005035void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005036 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005037}
5038
Calin Juravlee460d1d2015-09-29 04:52:17 +01005039void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5040 HUnresolvedInstanceFieldGet* instruction) {
5041 FieldAccessCallingConventionARM64 calling_convention;
5042 codegen_->CreateUnresolvedFieldLocationSummary(
5043 instruction, instruction->GetFieldType(), calling_convention);
5044}
5045
5046void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5047 HUnresolvedInstanceFieldGet* instruction) {
5048 FieldAccessCallingConventionARM64 calling_convention;
5049 codegen_->GenerateUnresolvedFieldAccess(instruction,
5050 instruction->GetFieldType(),
5051 instruction->GetFieldIndex(),
5052 instruction->GetDexPc(),
5053 calling_convention);
5054}
5055
5056void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5057 HUnresolvedInstanceFieldSet* instruction) {
5058 FieldAccessCallingConventionARM64 calling_convention;
5059 codegen_->CreateUnresolvedFieldLocationSummary(
5060 instruction, instruction->GetFieldType(), calling_convention);
5061}
5062
5063void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5064 HUnresolvedInstanceFieldSet* instruction) {
5065 FieldAccessCallingConventionARM64 calling_convention;
5066 codegen_->GenerateUnresolvedFieldAccess(instruction,
5067 instruction->GetFieldType(),
5068 instruction->GetFieldIndex(),
5069 instruction->GetDexPc(),
5070 calling_convention);
5071}
5072
5073void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5074 HUnresolvedStaticFieldGet* instruction) {
5075 FieldAccessCallingConventionARM64 calling_convention;
5076 codegen_->CreateUnresolvedFieldLocationSummary(
5077 instruction, instruction->GetFieldType(), calling_convention);
5078}
5079
5080void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5081 HUnresolvedStaticFieldGet* instruction) {
5082 FieldAccessCallingConventionARM64 calling_convention;
5083 codegen_->GenerateUnresolvedFieldAccess(instruction,
5084 instruction->GetFieldType(),
5085 instruction->GetFieldIndex(),
5086 instruction->GetDexPc(),
5087 calling_convention);
5088}
5089
5090void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5091 HUnresolvedStaticFieldSet* instruction) {
5092 FieldAccessCallingConventionARM64 calling_convention;
5093 codegen_->CreateUnresolvedFieldLocationSummary(
5094 instruction, instruction->GetFieldType(), calling_convention);
5095}
5096
5097void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5098 HUnresolvedStaticFieldSet* instruction) {
5099 FieldAccessCallingConventionARM64 calling_convention;
5100 codegen_->GenerateUnresolvedFieldAccess(instruction,
5101 instruction->GetFieldType(),
5102 instruction->GetFieldIndex(),
5103 instruction->GetDexPc(),
5104 calling_convention);
5105}
5106
Alexandre Rames5319def2014-10-23 10:03:10 +01005107void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005108 LocationSummary* locations =
5109 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005110 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005111}
5112
5113void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005114 HBasicBlock* block = instruction->GetBlock();
5115 if (block->GetLoopInformation() != nullptr) {
5116 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5117 // The back edge will generate the suspend check.
5118 return;
5119 }
5120 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5121 // The goto will generate the suspend check.
5122 return;
5123 }
5124 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005125}
5126
Alexandre Rames67555f72014-11-18 10:55:16 +00005127void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5128 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005129 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005130 InvokeRuntimeCallingConvention calling_convention;
5131 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5132}
5133
5134void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005135 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005136 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005137}
5138
5139void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5140 LocationSummary* locations =
5141 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5142 Primitive::Type input_type = conversion->GetInputType();
5143 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005144 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005145 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5146 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5147 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5148 }
5149
Alexandre Rames542361f2015-01-29 16:57:31 +00005150 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005151 locations->SetInAt(0, Location::RequiresFpuRegister());
5152 } else {
5153 locations->SetInAt(0, Location::RequiresRegister());
5154 }
5155
Alexandre Rames542361f2015-01-29 16:57:31 +00005156 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005157 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5158 } else {
5159 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5160 }
5161}
5162
5163void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5164 Primitive::Type result_type = conversion->GetResultType();
5165 Primitive::Type input_type = conversion->GetInputType();
5166
5167 DCHECK_NE(input_type, result_type);
5168
Alexandre Rames542361f2015-01-29 16:57:31 +00005169 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005170 int result_size = Primitive::ComponentSize(result_type);
5171 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005172 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005173 Register output = OutputRegister(conversion);
5174 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005175 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005176 // 'int' values are used directly as W registers, discarding the top
5177 // bits, so we don't need to sign-extend and can just perform a move.
5178 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5179 // top 32 bits of the target register. We theoretically could leave those
5180 // bits unchanged, but we would have to make sure that no code uses a
5181 // 32bit input value as a 64bit value assuming that the top 32 bits are
5182 // zero.
5183 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005184 } else if (result_type == Primitive::kPrimChar ||
5185 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5186 __ Ubfx(output,
5187 output.IsX() ? source.X() : source.W(),
5188 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005189 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005190 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005191 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005192 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005193 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005194 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005195 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5196 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005197 } else if (Primitive::IsFloatingPointType(result_type) &&
5198 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005199 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5200 } else {
5201 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5202 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005203 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005204}
Alexandre Rames67555f72014-11-18 10:55:16 +00005205
Serban Constantinescu02164b32014-11-13 14:05:07 +00005206void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5207 HandleShift(ushr);
5208}
5209
5210void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5211 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005212}
5213
5214void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5215 HandleBinaryOp(instruction);
5216}
5217
5218void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5219 HandleBinaryOp(instruction);
5220}
5221
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005222void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005223 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005224 LOG(FATAL) << "Unreachable";
5225}
5226
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005227void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005228 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005229 LOG(FATAL) << "Unreachable";
5230}
5231
Mark Mendellfe57faa2015-09-18 09:26:15 -04005232// Simple implementation of packed switch - generate cascaded compare/jumps.
5233void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5234 LocationSummary* locations =
5235 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5236 locations->SetInAt(0, Location::RequiresRegister());
5237}
5238
5239void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5240 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005241 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005242 Register value_reg = InputRegisterAt(switch_instr, 0);
5243 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5244
Zheng Xu3927c8b2015-11-18 17:46:25 +08005245 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005246 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005247 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5248 // make sure we don't emit it if the target may run out of range.
5249 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5250 // ranges and emit the tables only as required.
5251 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005252
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005253 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005254 // Current instruction id is an upper bound of the number of HIRs in the graph.
5255 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5256 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005257 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5258 Register temp = temps.AcquireW();
5259 __ Subs(temp, value_reg, Operand(lower_bound));
5260
Zheng Xu3927c8b2015-11-18 17:46:25 +08005261 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005262 // Jump to successors[0] if value == lower_bound.
5263 __ B(eq, codegen_->GetLabelOf(successors[0]));
5264 int32_t last_index = 0;
5265 for (; num_entries - last_index > 2; last_index += 2) {
5266 __ Subs(temp, temp, Operand(2));
5267 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5268 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5269 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5270 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5271 }
5272 if (num_entries - last_index == 2) {
5273 // The last missing case_value.
5274 __ Cmp(temp, Operand(1));
5275 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005276 }
5277
5278 // And the default for any other value.
5279 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5280 __ B(codegen_->GetLabelOf(default_block));
5281 }
5282 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005283 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005284
5285 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5286
5287 // Below instructions should use at most one blocked register. Since there are two blocked
5288 // registers, we are free to block one.
5289 Register temp_w = temps.AcquireW();
5290 Register index;
5291 // Remove the bias.
5292 if (lower_bound != 0) {
5293 index = temp_w;
5294 __ Sub(index, value_reg, Operand(lower_bound));
5295 } else {
5296 index = value_reg;
5297 }
5298
5299 // Jump to default block if index is out of the range.
5300 __ Cmp(index, Operand(num_entries));
5301 __ B(hs, codegen_->GetLabelOf(default_block));
5302
5303 // In current VIXL implementation, it won't require any blocked registers to encode the
5304 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5305 // register pressure.
5306 Register table_base = temps.AcquireX();
5307 // Load jump offset from the table.
5308 __ Adr(table_base, jump_table->GetTableStartLabel());
5309 Register jump_offset = temp_w;
5310 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5311
5312 // Jump to target block by branching to table_base(pc related) + offset.
5313 Register target_address = table_base;
5314 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5315 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005316 }
5317}
5318
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005319void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5320 HInstruction* instruction,
5321 Location out,
5322 uint32_t offset,
5323 Location maybe_temp,
5324 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005325 Primitive::Type type = Primitive::kPrimNot;
5326 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005327 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005328 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005329 Register temp_reg = RegisterFrom(maybe_temp, type);
5330 if (kUseBakerReadBarrier) {
5331 // Load with fast path based Baker's read barrier.
5332 // /* HeapReference<Object> */ out = *(out + offset)
5333 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5334 out,
5335 out_reg,
5336 offset,
5337 temp_reg,
5338 /* needs_null_check */ false,
5339 /* use_load_acquire */ false);
5340 } else {
5341 // Load with slow path based read barrier.
5342 // Save the value of `out` into `maybe_temp` before overwriting it
5343 // in the following move operation, as we will need it for the
5344 // read barrier below.
5345 __ Mov(temp_reg, out_reg);
5346 // /* HeapReference<Object> */ out = *(out + offset)
5347 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5348 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5349 }
5350 } else {
5351 // Plain load with no read barrier.
5352 // /* HeapReference<Object> */ out = *(out + offset)
5353 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5354 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5355 }
5356}
5357
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005358void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5359 HInstruction* instruction,
5360 Location out,
5361 Location obj,
5362 uint32_t offset,
5363 Location maybe_temp,
5364 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005365 Primitive::Type type = Primitive::kPrimNot;
5366 Register out_reg = RegisterFrom(out, type);
5367 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005368 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005369 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005370 if (kUseBakerReadBarrier) {
5371 // Load with fast path based Baker's read barrier.
5372 Register temp_reg = RegisterFrom(maybe_temp, type);
5373 // /* HeapReference<Object> */ out = *(obj + offset)
5374 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5375 out,
5376 obj_reg,
5377 offset,
5378 temp_reg,
5379 /* needs_null_check */ false,
5380 /* use_load_acquire */ false);
5381 } else {
5382 // Load with slow path based read barrier.
5383 // /* HeapReference<Object> */ out = *(obj + offset)
5384 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5385 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5386 }
5387 } else {
5388 // Plain load with no read barrier.
5389 // /* HeapReference<Object> */ out = *(obj + offset)
5390 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5391 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5392 }
5393}
5394
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005395void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5396 HInstruction* instruction,
5397 Location root,
5398 Register obj,
5399 uint32_t offset,
5400 vixl::aarch64::Label* fixup_label,
5401 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005402 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005403 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005404 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005405 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005406 if (kUseBakerReadBarrier) {
5407 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5408 // Baker's read barrier are used:
5409 //
5410 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005411 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5412 // if (temp != null) {
5413 // root = temp(root)
Roland Levillain44015862016-01-22 11:47:17 +00005414 // }
5415
5416 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005417 if (fixup_label == nullptr) {
5418 __ Ldr(root_reg, MemOperand(obj, offset));
5419 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005420 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005421 }
Roland Levillain44015862016-01-22 11:47:17 +00005422 static_assert(
5423 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5424 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5425 "have different sizes.");
5426 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5427 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5428 "have different sizes.");
5429
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005430 Register temp = lr;
Roland Levillain44015862016-01-22 11:47:17 +00005431
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005432 // Slow path marking the GC root `root`. The entrypoint will alrady be loaded in temp.
5433 SlowPathCodeARM64* slow_path =
5434 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction,
5435 root,
5436 LocationFrom(temp));
5437 codegen_->AddSlowPath(slow_path);
5438 const int32_t entry_point_offset =
5439 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5440 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5441 // Loading the entrypoint does not require a load acquire since it is only changed when
5442 // threads are suspended or running a checkpoint.
5443 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5444 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5445 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005446 __ Cbnz(temp, slow_path->GetEntryLabel());
5447 __ Bind(slow_path->GetExitLabel());
5448 } else {
5449 // GC root loaded through a slow path for read barriers other
5450 // than Baker's.
5451 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005452 if (fixup_label == nullptr) {
5453 __ Add(root_reg.X(), obj.X(), offset);
5454 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005455 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005456 }
Roland Levillain44015862016-01-22 11:47:17 +00005457 // /* mirror::Object* */ root = root->Read()
5458 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5459 }
5460 } else {
5461 // Plain GC root load with no read barrier.
5462 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005463 if (fixup_label == nullptr) {
5464 __ Ldr(root_reg, MemOperand(obj, offset));
5465 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005466 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005467 }
Roland Levillain44015862016-01-22 11:47:17 +00005468 // Note that GC roots are not affected by heap poisoning, thus we
5469 // do not have to unpoison `root_reg` here.
5470 }
5471}
5472
5473void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5474 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005475 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005476 uint32_t offset,
5477 Register temp,
5478 bool needs_null_check,
5479 bool use_load_acquire) {
5480 DCHECK(kEmitCompilerReadBarrier);
5481 DCHECK(kUseBakerReadBarrier);
5482
5483 // /* HeapReference<Object> */ ref = *(obj + offset)
5484 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005485 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005486 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5487 ref,
5488 obj,
5489 offset,
5490 no_index,
5491 no_scale_factor,
5492 temp,
5493 needs_null_check,
5494 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005495}
5496
5497void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5498 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005499 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005500 uint32_t data_offset,
5501 Location index,
5502 Register temp,
5503 bool needs_null_check) {
5504 DCHECK(kEmitCompilerReadBarrier);
5505 DCHECK(kUseBakerReadBarrier);
5506
5507 // Array cells are never volatile variables, therefore array loads
5508 // never use Load-Acquire instructions on ARM64.
5509 const bool use_load_acquire = false;
5510
Roland Levillainbfea3352016-06-23 13:48:47 +01005511 static_assert(
5512 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5513 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005514 // /* HeapReference<Object> */ ref =
5515 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005516 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5517 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5518 ref,
5519 obj,
5520 data_offset,
5521 index,
5522 scale_factor,
5523 temp,
5524 needs_null_check,
5525 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005526}
5527
5528void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5529 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005530 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005531 uint32_t offset,
5532 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005533 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005534 Register temp,
5535 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005536 bool use_load_acquire,
5537 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005538 DCHECK(kEmitCompilerReadBarrier);
5539 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005540 // If we are emitting an array load, we should not be using a
5541 // Load Acquire instruction. In other words:
5542 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5543 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005544
5545 MacroAssembler* masm = GetVIXLAssembler();
5546 UseScratchRegisterScope temps(masm);
5547
5548 // In slow path based read barriers, the read barrier call is
5549 // inserted after the original load. However, in fast path based
5550 // Baker's read barriers, we need to perform the load of
5551 // mirror::Object::monitor_ *before* the original reference load.
5552 // This load-load ordering is required by the read barrier.
5553 // The fast path/slow path (for Baker's algorithm) should look like:
5554 //
5555 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5556 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5557 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005558 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain44015862016-01-22 11:47:17 +00005559 // if (is_gray) {
5560 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5561 // }
5562 //
5563 // Note: the original implementation in ReadBarrier::Barrier is
5564 // slightly more complex as it performs additional checks that we do
5565 // not do here for performance reasons.
5566
5567 Primitive::Type type = Primitive::kPrimNot;
5568 Register ref_reg = RegisterFrom(ref, type);
5569 DCHECK(obj.IsW());
5570 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5571
5572 // /* int32_t */ monitor = obj->monitor_
5573 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5574 if (needs_null_check) {
5575 MaybeRecordImplicitNullCheck(instruction);
5576 }
5577 // /* LockWord */ lock_word = LockWord(monitor)
5578 static_assert(sizeof(LockWord) == sizeof(int32_t),
5579 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005580
Vladimir Marko877a0332016-07-11 19:30:56 +01005581 // Introduce a dependency on the lock_word including rb_state,
5582 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005583 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005584 // `obj` is unchanged by this operation, but its value now depends
5585 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005586 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005587
5588 // The actual reference load.
5589 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005590 // Load types involving an "index": ArrayGet,
5591 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5592 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01005593 if (use_load_acquire) {
5594 // UnsafeGetObjectVolatile intrinsic case.
5595 // Register `index` is not an index in an object array, but an
5596 // offset to an object reference field within object `obj`.
5597 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5598 DCHECK(instruction->GetLocations()->Intrinsified());
5599 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5600 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005601 DCHECK_EQ(offset, 0u);
5602 DCHECK_EQ(scale_factor, 0u);
5603 DCHECK_EQ(needs_null_check, 0u);
Roland Levillainbfea3352016-06-23 13:48:47 +01005604 // /* HeapReference<Object> */ ref = *(obj + index)
5605 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5606 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005607 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005608 // ArrayGet and UnsafeGetObject intrinsics cases.
5609 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5610 if (index.IsConstant()) {
5611 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5612 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5613 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005614 Register temp3 = temps.AcquireW();
5615 __ Add(temp3, obj, offset);
5616 Load(type, ref_reg, HeapOperand(temp3, XRegisterFrom(index), LSL, scale_factor));
5617 temps.Release(temp3);
Roland Levillainbfea3352016-06-23 13:48:47 +01005618 }
Roland Levillain44015862016-01-22 11:47:17 +00005619 }
Roland Levillain44015862016-01-22 11:47:17 +00005620 } else {
5621 // /* HeapReference<Object> */ ref = *(obj + offset)
5622 MemOperand field = HeapOperand(obj, offset);
5623 if (use_load_acquire) {
5624 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5625 } else {
5626 Load(type, ref_reg, field);
5627 }
5628 }
5629
5630 // Object* ref = ref_addr->AsMirrorPtr()
5631 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5632
Vladimir Marko953437b2016-08-24 08:30:46 +00005633 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005634 SlowPathCodeARM64* slow_path;
5635 if (always_update_field) {
5636 // ReadBarrierMarkAndUpdateFieldSlowPathARM64 only supports
5637 // address of the form `obj + field_offset`, where `obj` is a
5638 // register and `field_offset` is a register. Thus `offset` and
5639 // `scale_factor` above are expected to be null in this code path.
5640 DCHECK_EQ(offset, 0u);
5641 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
5642 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM64(
5643 instruction, ref, obj, /* field_offset */ index, temp);
5644 } else {
5645 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
5646 }
Roland Levillain44015862016-01-22 11:47:17 +00005647 AddSlowPath(slow_path);
5648
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005649 // if (rb_state == ReadBarrier::GrayState())
Roland Levillain44015862016-01-22 11:47:17 +00005650 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005651 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005652 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5653 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko877a0332016-07-11 19:30:56 +01005654 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005655 __ Bind(slow_path->GetExitLabel());
5656}
5657
5658void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5659 Location out,
5660 Location ref,
5661 Location obj,
5662 uint32_t offset,
5663 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005664 DCHECK(kEmitCompilerReadBarrier);
5665
Roland Levillain44015862016-01-22 11:47:17 +00005666 // Insert a slow path based read barrier *after* the reference load.
5667 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005668 // If heap poisoning is enabled, the unpoisoning of the loaded
5669 // reference will be carried out by the runtime within the slow
5670 // path.
5671 //
5672 // Note that `ref` currently does not get unpoisoned (when heap
5673 // poisoning is enabled), which is alright as the `ref` argument is
5674 // not used by the artReadBarrierSlow entry point.
5675 //
5676 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5677 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5678 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5679 AddSlowPath(slow_path);
5680
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005681 __ B(slow_path->GetEntryLabel());
5682 __ Bind(slow_path->GetExitLabel());
5683}
5684
Roland Levillain44015862016-01-22 11:47:17 +00005685void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5686 Location out,
5687 Location ref,
5688 Location obj,
5689 uint32_t offset,
5690 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005691 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005692 // Baker's read barriers shall be handled by the fast path
5693 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5694 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005695 // If heap poisoning is enabled, unpoisoning will be taken care of
5696 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005697 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005698 } else if (kPoisonHeapReferences) {
5699 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5700 }
5701}
5702
Roland Levillain44015862016-01-22 11:47:17 +00005703void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5704 Location out,
5705 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005706 DCHECK(kEmitCompilerReadBarrier);
5707
Roland Levillain44015862016-01-22 11:47:17 +00005708 // Insert a slow path based read barrier *after* the GC root load.
5709 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005710 // Note that GC roots are not affected by heap poisoning, so we do
5711 // not need to do anything special for this here.
5712 SlowPathCodeARM64* slow_path =
5713 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5714 AddSlowPath(slow_path);
5715
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005716 __ B(slow_path->GetEntryLabel());
5717 __ Bind(slow_path->GetExitLabel());
5718}
5719
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005720void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5721 LocationSummary* locations =
5722 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5723 locations->SetInAt(0, Location::RequiresRegister());
5724 locations->SetOut(Location::RequiresRegister());
5725}
5726
5727void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5728 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005729 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005730 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005731 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005732 __ Ldr(XRegisterFrom(locations->Out()),
5733 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005734 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005735 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005736 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005737 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5738 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005739 __ Ldr(XRegisterFrom(locations->Out()),
5740 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005741 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005742}
5743
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00005744
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005745
Alexandre Rames67555f72014-11-18 10:55:16 +00005746#undef __
5747#undef QUICK_ENTRY_POINT
5748
Alexandre Rames5319def2014-10-23 10:03:10 +01005749} // namespace arm64
5750} // namespace art