blob: a78b3da455116e2f2470b1929495b635b8df3c50 [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;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800291 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex().index_);
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.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000575 vixl::CodeBufferCheckScope scope(codegen->GetVIXLAssembler(),
576 num_entries * sizeof(int32_t),
577 vixl::CodeBufferCheckScope::kReserveBufferSpace,
578 vixl::CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800579
580 __ Bind(&table_start_);
581 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
582 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100583 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800584 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100585 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800586 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
587 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
588 Literal<int32_t> literal(jump_offset);
589 __ place(&literal);
590 }
591}
592
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100593// Slow path marking an object reference `ref` during a read
594// barrier. The field `obj.field` in the object `obj` holding this
595// reference does not get updated by this slow path after marking (see
596// ReadBarrierMarkAndUpdateFieldSlowPathARM64 below for that).
597//
598// This means that after the execution of this slow path, `ref` will
599// always be up-to-date, but `obj.field` may not; i.e., after the
600// flip, `ref` will be a to-space reference, but `obj.field` will
601// probably still be a from-space reference (unless it gets updated by
602// another thread, or if another thread installed another object
603// reference (different from `ref`) in `obj.field`).
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800604// If entrypoint is a valid location it is assumed to already be holding the entrypoint. The case
605// where the entrypoint is passed in is for the GcRoot read barrier.
Roland Levillain44015862016-01-22 11:47:17 +0000606class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
607 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800608 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
609 Location ref,
610 Location entrypoint = Location::NoLocation())
611 : SlowPathCodeARM64(instruction),
612 ref_(ref),
613 entrypoint_(entrypoint) {
Roland Levillain44015862016-01-22 11:47:17 +0000614 DCHECK(kEmitCompilerReadBarrier);
615 }
616
617 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
618
619 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
620 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000621 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100622 DCHECK(ref_.IsRegister()) << ref_;
623 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain44015862016-01-22 11:47:17 +0000624 DCHECK(instruction_->IsInstanceFieldGet() ||
625 instruction_->IsStaticFieldGet() ||
626 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100627 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000628 instruction_->IsLoadClass() ||
629 instruction_->IsLoadString() ||
630 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100631 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100632 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
633 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000634 << "Unexpected instruction in read barrier marking slow path: "
635 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000636 // The read barrier instrumentation of object ArrayGet
637 // instructions does not support the HIntermediateAddress
638 // instruction.
639 DCHECK(!(instruction_->IsArrayGet() &&
640 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain44015862016-01-22 11:47:17 +0000641
642 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100643 // No need to save live registers; it's taken care of by the
644 // entrypoint. Also, there is no need to update the stack mask,
645 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000646 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100647 DCHECK_NE(ref_.reg(), LR);
648 DCHECK_NE(ref_.reg(), WSP);
649 DCHECK_NE(ref_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100650 // IP0 is used internally by the ReadBarrierMarkRegX entry point
651 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100652 DCHECK_NE(ref_.reg(), IP0);
653 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
Roland Levillain02b75802016-07-13 11:54:35 +0100654 // "Compact" slow path, saving two moves.
655 //
656 // Instead of using the standard runtime calling convention (input
657 // and output in W0):
658 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100659 // W0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100660 // W0 <- ReadBarrierMark(W0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100661 // ref <- W0
Roland Levillain02b75802016-07-13 11:54:35 +0100662 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100663 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100664 // of a dedicated entrypoint:
665 //
666 // rX <- ReadBarrierMarkRegX(rX)
667 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800668 if (entrypoint_.IsValid()) {
669 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
670 __ Blr(XRegisterFrom(entrypoint_));
671 } else {
672 // Entrypoint is not already loaded, load from the thread.
673 int32_t entry_point_offset =
674 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
675 // This runtime call does not require a stack map.
676 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
677 }
Roland Levillain44015862016-01-22 11:47:17 +0000678 __ B(GetExitLabel());
679 }
680
681 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100682 // The location (register) of the marked object reference.
683 const Location ref_;
Roland Levillain44015862016-01-22 11:47:17 +0000684
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800685 // The location of the entrypoint if it is already loaded.
686 const Location entrypoint_;
687
Roland Levillain44015862016-01-22 11:47:17 +0000688 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
689};
690
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100691// Slow path marking an object reference `ref` during a read barrier,
692// and if needed, atomically updating the field `obj.field` in the
693// object `obj` holding this reference after marking (contrary to
694// ReadBarrierMarkSlowPathARM64 above, which never tries to update
695// `obj.field`).
696//
697// This means that after the execution of this slow path, both `ref`
698// and `obj.field` will be up-to-date; i.e., after the flip, both will
699// hold the same to-space reference (unless another thread installed
700// another object reference (different from `ref`) in `obj.field`).
701class ReadBarrierMarkAndUpdateFieldSlowPathARM64 : public SlowPathCodeARM64 {
702 public:
703 ReadBarrierMarkAndUpdateFieldSlowPathARM64(HInstruction* instruction,
704 Location ref,
705 Register obj,
706 Location field_offset,
707 Register temp)
708 : SlowPathCodeARM64(instruction),
709 ref_(ref),
710 obj_(obj),
711 field_offset_(field_offset),
712 temp_(temp) {
713 DCHECK(kEmitCompilerReadBarrier);
714 }
715
716 const char* GetDescription() const OVERRIDE {
717 return "ReadBarrierMarkAndUpdateFieldSlowPathARM64";
718 }
719
720 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
721 LocationSummary* locations = instruction_->GetLocations();
722 Register ref_reg = WRegisterFrom(ref_);
723 DCHECK(locations->CanCall());
724 DCHECK(ref_.IsRegister()) << ref_;
725 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
726 // This slow path is only used by the UnsafeCASObject intrinsic.
727 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
728 << "Unexpected instruction in read barrier marking and field updating slow path: "
729 << instruction_->DebugName();
730 DCHECK(instruction_->GetLocations()->Intrinsified());
731 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
732 DCHECK(field_offset_.IsRegister()) << field_offset_;
733
734 __ Bind(GetEntryLabel());
735
736 // Save the old reference.
737 // Note that we cannot use IP to save the old reference, as IP is
738 // used internally by the ReadBarrierMarkRegX entry point, and we
739 // need the old reference after the call to that entry point.
740 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
741 __ Mov(temp_.W(), ref_reg);
742
743 // No need to save live registers; it's taken care of by the
744 // entrypoint. Also, there is no need to update the stack mask,
745 // as this runtime call will not trigger a garbage collection.
746 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
747 DCHECK_NE(ref_.reg(), LR);
748 DCHECK_NE(ref_.reg(), WSP);
749 DCHECK_NE(ref_.reg(), WZR);
750 // IP0 is used internally by the ReadBarrierMarkRegX entry point
751 // as a temporary, it cannot be the entry point's input/output.
752 DCHECK_NE(ref_.reg(), IP0);
753 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
754 // "Compact" slow path, saving two moves.
755 //
756 // Instead of using the standard runtime calling convention (input
757 // and output in W0):
758 //
759 // W0 <- ref
760 // W0 <- ReadBarrierMark(W0)
761 // ref <- W0
762 //
763 // we just use rX (the register containing `ref`) as input and output
764 // of a dedicated entrypoint:
765 //
766 // rX <- ReadBarrierMarkRegX(rX)
767 //
768 int32_t entry_point_offset =
769 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
770 // This runtime call does not require a stack map.
771 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
772
773 // If the new reference is different from the old reference,
774 // update the field in the holder (`*(obj_ + field_offset_)`).
775 //
776 // Note that this field could also hold a different object, if
777 // another thread had concurrently changed it. In that case, the
778 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
779 // (CAS) operation below would abort the CAS, leaving the field
780 // as-is.
781 vixl::aarch64::Label done;
782 __ Cmp(temp_.W(), ref_reg);
783 __ B(eq, &done);
784
785 // Update the the holder's field atomically. This may fail if
786 // mutator updates before us, but it's OK. This is achieved
787 // using a strong compare-and-set (CAS) operation with relaxed
788 // memory synchronization ordering, where the expected value is
789 // the old reference and the desired value is the new reference.
790
791 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
792 UseScratchRegisterScope temps(masm);
793
794 // Convenience aliases.
795 Register base = obj_.W();
796 Register offset = XRegisterFrom(field_offset_);
797 Register expected = temp_.W();
798 Register value = ref_reg;
799 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
800 Register tmp_value = temps.AcquireW(); // Value in memory.
801
802 __ Add(tmp_ptr, base.X(), Operand(offset));
803
804 if (kPoisonHeapReferences) {
805 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
806 if (value.Is(expected)) {
807 // Do not poison `value`, as it is the same register as
808 // `expected`, which has just been poisoned.
809 } else {
810 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
811 }
812 }
813
814 // do {
815 // tmp_value = [tmp_ptr] - expected;
816 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
817
Roland Levillain24a4d112016-10-26 13:10:46 +0100818 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100819 __ Bind(&loop_head);
820 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
821 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +0100822 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100823 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
824 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +0100825 __ B(&exit_loop);
826 __ Bind(&comparison_failed);
827 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100828 __ Bind(&exit_loop);
829
830 if (kPoisonHeapReferences) {
831 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
832 if (value.Is(expected)) {
833 // Do not unpoison `value`, as it is the same register as
834 // `expected`, which has just been unpoisoned.
835 } else {
836 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
837 }
838 }
839
840 __ Bind(&done);
841 __ B(GetExitLabel());
842 }
843
844 private:
845 // The location (register) of the marked object reference.
846 const Location ref_;
847 // The register containing the object holding the marked object reference field.
848 const Register obj_;
849 // The location of the offset of the marked reference field within `obj_`.
850 Location field_offset_;
851
852 const Register temp_;
853
854 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM64);
855};
856
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000857// Slow path generating a read barrier for a heap reference.
858class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
859 public:
860 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
861 Location out,
862 Location ref,
863 Location obj,
864 uint32_t offset,
865 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000866 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000867 out_(out),
868 ref_(ref),
869 obj_(obj),
870 offset_(offset),
871 index_(index) {
872 DCHECK(kEmitCompilerReadBarrier);
873 // If `obj` is equal to `out` or `ref`, it means the initial object
874 // has been overwritten by (or after) the heap object reference load
875 // to be instrumented, e.g.:
876 //
877 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000878 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000879 //
880 // In that case, we have lost the information about the original
881 // object, and the emitted read barrier cannot work properly.
882 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
883 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
884 }
885
886 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
887 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
888 LocationSummary* locations = instruction_->GetLocations();
889 Primitive::Type type = Primitive::kPrimNot;
890 DCHECK(locations->CanCall());
891 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100892 DCHECK(instruction_->IsInstanceFieldGet() ||
893 instruction_->IsStaticFieldGet() ||
894 instruction_->IsArrayGet() ||
895 instruction_->IsInstanceOf() ||
896 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100897 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000898 << "Unexpected instruction in read barrier for heap reference slow path: "
899 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000900 // The read barrier instrumentation of object ArrayGet
901 // instructions does not support the HIntermediateAddress
902 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000903 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100904 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000905
906 __ Bind(GetEntryLabel());
907
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000908 SaveLiveRegisters(codegen, locations);
909
910 // We may have to change the index's value, but as `index_` is a
911 // constant member (like other "inputs" of this slow path),
912 // introduce a copy of it, `index`.
913 Location index = index_;
914 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100915 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000916 if (instruction_->IsArrayGet()) {
917 // Compute the actual memory offset and store it in `index`.
918 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
919 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
920 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
921 // We are about to change the value of `index_reg` (see the
922 // calls to vixl::MacroAssembler::Lsl and
923 // vixl::MacroAssembler::Mov below), but it has
924 // not been saved by the previous call to
925 // art::SlowPathCode::SaveLiveRegisters, as it is a
926 // callee-save register --
927 // art::SlowPathCode::SaveLiveRegisters does not consider
928 // callee-save registers, as it has been designed with the
929 // assumption that callee-save registers are supposed to be
930 // handled by the called function. So, as a callee-save
931 // register, `index_reg` _would_ eventually be saved onto
932 // the stack, but it would be too late: we would have
933 // changed its value earlier. Therefore, we manually save
934 // it here into another freely available register,
935 // `free_reg`, chosen of course among the caller-save
936 // registers (as a callee-save `free_reg` register would
937 // exhibit the same problem).
938 //
939 // Note we could have requested a temporary register from
940 // the register allocator instead; but we prefer not to, as
941 // this is a slow path, and we know we can find a
942 // caller-save register that is available.
943 Register free_reg = FindAvailableCallerSaveRegister(codegen);
944 __ Mov(free_reg.W(), index_reg);
945 index_reg = free_reg;
946 index = LocationFrom(index_reg);
947 } else {
948 // The initial register stored in `index_` has already been
949 // saved in the call to art::SlowPathCode::SaveLiveRegisters
950 // (as it is not a callee-save register), so we can freely
951 // use it.
952 }
953 // Shifting the index value contained in `index_reg` by the scale
954 // factor (2) cannot overflow in practice, as the runtime is
955 // unable to allocate object arrays with a size larger than
956 // 2^26 - 1 (that is, 2^28 - 4 bytes).
957 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
958 static_assert(
959 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
960 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
961 __ Add(index_reg, index_reg, Operand(offset_));
962 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100963 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
964 // intrinsics, `index_` is not shifted by a scale factor of 2
965 // (as in the case of ArrayGet), as it is actually an offset
966 // to an object field within an object.
967 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000968 DCHECK(instruction_->GetLocations()->Intrinsified());
969 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
970 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
971 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100972 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100973 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000974 }
975 }
976
977 // We're moving two or three locations to locations that could
978 // overlap, so we need a parallel move resolver.
979 InvokeRuntimeCallingConvention calling_convention;
980 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
981 parallel_move.AddMove(ref_,
982 LocationFrom(calling_convention.GetRegisterAt(0)),
983 type,
984 nullptr);
985 parallel_move.AddMove(obj_,
986 LocationFrom(calling_convention.GetRegisterAt(1)),
987 type,
988 nullptr);
989 if (index.IsValid()) {
990 parallel_move.AddMove(index,
991 LocationFrom(calling_convention.GetRegisterAt(2)),
992 Primitive::kPrimInt,
993 nullptr);
994 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
995 } else {
996 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
997 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
998 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000999 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001000 instruction_,
1001 instruction_->GetDexPc(),
1002 this);
1003 CheckEntrypointTypes<
1004 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1005 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1006
1007 RestoreLiveRegisters(codegen, locations);
1008
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001009 __ B(GetExitLabel());
1010 }
1011
1012 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1013
1014 private:
1015 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001016 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1017 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001018 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1019 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1020 return Register(VIXLRegCodeFromART(i), kXRegSize);
1021 }
1022 }
1023 // We shall never fail to find a free caller-save register, as
1024 // there are more than two core caller-save registers on ARM64
1025 // (meaning it is possible to find one which is different from
1026 // `ref` and `obj`).
1027 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1028 LOG(FATAL) << "Could not find a free register";
1029 UNREACHABLE();
1030 }
1031
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001032 const Location out_;
1033 const Location ref_;
1034 const Location obj_;
1035 const uint32_t offset_;
1036 // An additional location containing an index to an array.
1037 // Only used for HArrayGet and the UnsafeGetObject &
1038 // UnsafeGetObjectVolatile intrinsics.
1039 const Location index_;
1040
1041 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1042};
1043
1044// Slow path generating a read barrier for a GC root.
1045class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1046 public:
1047 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001048 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001049 DCHECK(kEmitCompilerReadBarrier);
1050 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001051
1052 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1053 LocationSummary* locations = instruction_->GetLocations();
1054 Primitive::Type type = Primitive::kPrimNot;
1055 DCHECK(locations->CanCall());
1056 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001057 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1058 << "Unexpected instruction in read barrier for GC root slow path: "
1059 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001060
1061 __ Bind(GetEntryLabel());
1062 SaveLiveRegisters(codegen, locations);
1063
1064 InvokeRuntimeCallingConvention calling_convention;
1065 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1066 // The argument of the ReadBarrierForRootSlow is not a managed
1067 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1068 // thus we need a 64-bit move here, and we cannot use
1069 //
1070 // arm64_codegen->MoveLocation(
1071 // LocationFrom(calling_convention.GetRegisterAt(0)),
1072 // root_,
1073 // type);
1074 //
1075 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1076 // reference type (`Primitive::kPrimNot`).
1077 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001078 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001079 instruction_,
1080 instruction_->GetDexPc(),
1081 this);
1082 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1083 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1084
1085 RestoreLiveRegisters(codegen, locations);
1086 __ B(GetExitLabel());
1087 }
1088
1089 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1090
1091 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001092 const Location out_;
1093 const Location root_;
1094
1095 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1096};
1097
Alexandre Rames5319def2014-10-23 10:03:10 +01001098#undef __
1099
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001100Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001101 Location next_location;
1102 if (type == Primitive::kPrimVoid) {
1103 LOG(FATAL) << "Unreachable type " << type;
1104 }
1105
Alexandre Rames542361f2015-01-29 16:57:31 +00001106 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001107 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
1108 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +00001109 } else if (!Primitive::IsFloatingPointType(type) &&
1110 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001111 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1112 } else {
1113 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001114 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1115 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001116 }
1117
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001118 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001119 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001120 return next_location;
1121}
1122
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001123Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001124 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001125}
1126
Serban Constantinescu579885a2015-02-22 20:51:33 +00001127CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1128 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001129 const CompilerOptions& compiler_options,
1130 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001131 : CodeGenerator(graph,
1132 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001133 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001134 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001135 callee_saved_core_registers.GetList(),
1136 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001137 compiler_options,
1138 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001139 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001140 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001141 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001142 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001143 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001144 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001145 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001146 uint32_literals_(std::less<uint32_t>(),
1147 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001148 uint64_literals_(std::less<uint64_t>(),
1149 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1150 method_patches_(MethodReferenceComparator(),
1151 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1152 call_patches_(MethodReferenceComparator(),
1153 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1154 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001155 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1156 boot_image_string_patches_(StringReferenceValueComparator(),
1157 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1158 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001159 boot_image_type_patches_(TypeReferenceValueComparator(),
1160 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1161 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001162 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001163 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1164 jit_string_patches_(StringReferenceValueComparator(),
1165 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001166 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001167 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001168}
Alexandre Rames5319def2014-10-23 10:03:10 +01001169
Alexandre Rames67555f72014-11-18 10:55:16 +00001170#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001171
Zheng Xu3927c8b2015-11-18 17:46:25 +08001172void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001173 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001174 jump_table->EmitTable(this);
1175 }
1176}
1177
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001178void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001179 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001180 // Ensure we emit the literal pool.
1181 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001182
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001183 CodeGenerator::Finalize(allocator);
1184}
1185
Zheng Xuad4450e2015-04-17 18:48:56 +08001186void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1187 // Note: There are 6 kinds of moves:
1188 // 1. constant -> GPR/FPR (non-cycle)
1189 // 2. constant -> stack (non-cycle)
1190 // 3. GPR/FPR -> GPR/FPR
1191 // 4. GPR/FPR -> stack
1192 // 5. stack -> GPR/FPR
1193 // 6. stack -> stack (non-cycle)
1194 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1195 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1196 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1197 // dependency.
1198 vixl_temps_.Open(GetVIXLAssembler());
1199}
1200
1201void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1202 vixl_temps_.Close();
1203}
1204
1205Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1206 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1207 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1208 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1209 Location scratch = GetScratchLocation(kind);
1210 if (!scratch.Equals(Location::NoLocation())) {
1211 return scratch;
1212 }
1213 // Allocate from VIXL temp registers.
1214 if (kind == Location::kRegister) {
1215 scratch = LocationFrom(vixl_temps_.AcquireX());
1216 } else {
1217 DCHECK(kind == Location::kFpuRegister);
1218 scratch = LocationFrom(vixl_temps_.AcquireD());
1219 }
1220 AddScratchLocation(scratch);
1221 return scratch;
1222}
1223
1224void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1225 if (loc.IsRegister()) {
1226 vixl_temps_.Release(XRegisterFrom(loc));
1227 } else {
1228 DCHECK(loc.IsFpuRegister());
1229 vixl_temps_.Release(DRegisterFrom(loc));
1230 }
1231 RemoveScratchLocation(loc);
1232}
1233
Alexandre Rames3e69f162014-12-10 10:36:50 +00001234void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001235 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001236 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001237}
1238
Alexandre Rames5319def2014-10-23 10:03:10 +01001239void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001240 MacroAssembler* masm = GetVIXLAssembler();
1241 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001242 __ Bind(&frame_entry_label_);
1243
Serban Constantinescu02164b32014-11-13 14:05:07 +00001244 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1245 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001246 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001247 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001248 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001249 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001250 __ Ldr(wzr, MemOperand(temp, 0));
1251 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001252 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001253
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001254 if (!HasEmptyFrame()) {
1255 int frame_size = GetFrameSize();
1256 // Stack layout:
1257 // sp[frame_size - 8] : lr.
1258 // ... : other preserved core registers.
1259 // ... : other preserved fp registers.
1260 // ... : reserved frame space.
1261 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001262
1263 // Save the current method if we need it. Note that we do not
1264 // do this in HCurrentMethod, as the instruction might have been removed
1265 // in the SSA graph.
1266 if (RequiresCurrentMethod()) {
1267 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001268 } else {
1269 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001270 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001271 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001272 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1273 frame_size - GetCoreSpillSize());
1274 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1275 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001276 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001277}
1278
1279void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001280 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001281 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001282 if (!HasEmptyFrame()) {
1283 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001284 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1285 frame_size - FrameEntrySpillSize());
1286 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1287 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001288 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001289 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001290 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001291 __ Ret();
1292 GetAssembler()->cfi().RestoreState();
1293 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001294}
1295
Scott Wakeling97c72b72016-06-24 16:19:36 +01001296CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001297 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001298 return CPURegList(CPURegister::kRegister, kXRegSize,
1299 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001300}
1301
Scott Wakeling97c72b72016-06-24 16:19:36 +01001302CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001303 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1304 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001305 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1306 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001307}
1308
Alexandre Rames5319def2014-10-23 10:03:10 +01001309void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1310 __ Bind(GetLabelOf(block));
1311}
1312
Calin Juravle175dc732015-08-25 15:42:32 +01001313void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1314 DCHECK(location.IsRegister());
1315 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1316}
1317
Calin Juravlee460d1d2015-09-29 04:52:17 +01001318void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1319 if (location.IsRegister()) {
1320 locations->AddTemp(location);
1321 } else {
1322 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1323 }
1324}
1325
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001326void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001327 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001328 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001329 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001330 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001331 if (value_can_be_null) {
1332 __ Cbz(value, &done);
1333 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001334 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001335 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001336 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001337 if (value_can_be_null) {
1338 __ Bind(&done);
1339 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001340}
1341
David Brazdil58282f42016-01-14 12:45:10 +00001342void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001343 // Blocked core registers:
1344 // lr : Runtime reserved.
1345 // tr : Runtime reserved.
1346 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1347 // ip1 : VIXL core temp.
1348 // ip0 : VIXL core temp.
1349 //
1350 // Blocked fp registers:
1351 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001352 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1353 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001354 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001355 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001356 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001357
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001358 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001359 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001360 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001361 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001362
David Brazdil58282f42016-01-14 12:45:10 +00001363 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001364 // Stubs do not save callee-save floating point registers. If the graph
1365 // is debuggable, we need to deal with these registers differently. For
1366 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001367 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1368 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001369 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001370 }
1371 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001372}
1373
Alexandre Rames3e69f162014-12-10 10:36:50 +00001374size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1375 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1376 __ Str(reg, MemOperand(sp, stack_index));
1377 return kArm64WordSize;
1378}
1379
1380size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1381 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1382 __ Ldr(reg, MemOperand(sp, stack_index));
1383 return kArm64WordSize;
1384}
1385
1386size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1387 FPRegister reg = FPRegister(reg_id, kDRegSize);
1388 __ Str(reg, MemOperand(sp, stack_index));
1389 return kArm64WordSize;
1390}
1391
1392size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1393 FPRegister reg = FPRegister(reg_id, kDRegSize);
1394 __ Ldr(reg, MemOperand(sp, stack_index));
1395 return kArm64WordSize;
1396}
1397
Alexandre Rames5319def2014-10-23 10:03:10 +01001398void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001399 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001400}
1401
1402void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001403 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001404}
1405
Alexandre Rames67555f72014-11-18 10:55:16 +00001406void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001407 if (constant->IsIntConstant()) {
1408 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1409 } else if (constant->IsLongConstant()) {
1410 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1411 } else if (constant->IsNullConstant()) {
1412 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001413 } else if (constant->IsFloatConstant()) {
1414 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1415 } else {
1416 DCHECK(constant->IsDoubleConstant());
1417 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1418 }
1419}
1420
Alexandre Rames3e69f162014-12-10 10:36:50 +00001421
1422static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1423 DCHECK(constant.IsConstant());
1424 HConstant* cst = constant.GetConstant();
1425 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001426 // Null is mapped to a core W register, which we associate with kPrimInt.
1427 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001428 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1429 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1430 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1431}
1432
Calin Juravlee460d1d2015-09-29 04:52:17 +01001433void CodeGeneratorARM64::MoveLocation(Location destination,
1434 Location source,
1435 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001436 if (source.Equals(destination)) {
1437 return;
1438 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001439
1440 // A valid move can always be inferred from the destination and source
1441 // locations. When moving from and to a register, the argument type can be
1442 // used to generate 32bit instead of 64bit moves. In debug mode we also
1443 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001444 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001445
1446 if (destination.IsRegister() || destination.IsFpuRegister()) {
1447 if (unspecified_type) {
1448 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1449 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001450 (src_cst != nullptr && (src_cst->IsIntConstant()
1451 || src_cst->IsFloatConstant()
1452 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001453 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001454 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001455 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001456 // If the source is a double stack slot or a 64bit constant, a 64bit
1457 // type is appropriate. Else the source is a register, and since the
1458 // type has not been specified, we chose a 64bit type to force a 64bit
1459 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001460 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001461 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001462 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001463 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1464 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1465 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001466 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1467 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1468 __ Ldr(dst, StackOperandFrom(source));
1469 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001470 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001471 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001472 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001473 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001474 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001475 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001476 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001477 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1478 ? Primitive::kPrimLong
1479 : Primitive::kPrimInt;
1480 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1481 }
1482 } else {
1483 DCHECK(source.IsFpuRegister());
1484 if (destination.IsRegister()) {
1485 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1486 ? Primitive::kPrimDouble
1487 : Primitive::kPrimFloat;
1488 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1489 } else {
1490 DCHECK(destination.IsFpuRegister());
1491 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001492 }
1493 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001494 } else { // The destination is not a register. It must be a stack slot.
1495 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1496 if (source.IsRegister() || source.IsFpuRegister()) {
1497 if (unspecified_type) {
1498 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001499 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001500 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001501 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001502 }
1503 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001504 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1505 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1506 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001507 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001508 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1509 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001510 UseScratchRegisterScope temps(GetVIXLAssembler());
1511 HConstant* src_cst = source.GetConstant();
1512 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001513 if (src_cst->IsZeroBitPattern()) {
1514 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001515 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001516 if (src_cst->IsIntConstant()) {
1517 temp = temps.AcquireW();
1518 } else if (src_cst->IsLongConstant()) {
1519 temp = temps.AcquireX();
1520 } else if (src_cst->IsFloatConstant()) {
1521 temp = temps.AcquireS();
1522 } else {
1523 DCHECK(src_cst->IsDoubleConstant());
1524 temp = temps.AcquireD();
1525 }
1526 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001527 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001528 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001529 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001530 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001531 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001532 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001533 // There is generally less pressure on FP registers.
1534 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001535 __ Ldr(temp, StackOperandFrom(source));
1536 __ Str(temp, StackOperandFrom(destination));
1537 }
1538 }
1539}
1540
1541void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001542 CPURegister dst,
1543 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001544 switch (type) {
1545 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001546 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001547 break;
1548 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001549 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001550 break;
1551 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001552 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001553 break;
1554 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001555 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001556 break;
1557 case Primitive::kPrimInt:
1558 case Primitive::kPrimNot:
1559 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001560 case Primitive::kPrimFloat:
1561 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001562 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001563 __ Ldr(dst, src);
1564 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001565 case Primitive::kPrimVoid:
1566 LOG(FATAL) << "Unreachable type " << type;
1567 }
1568}
1569
Calin Juravle77520bc2015-01-12 18:45:46 +00001570void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001571 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001572 const MemOperand& src,
1573 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001574 MacroAssembler* masm = GetVIXLAssembler();
1575 BlockPoolsScope block_pools(masm);
1576 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001577 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001578 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001579
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001580 DCHECK(!src.IsPreIndex());
1581 DCHECK(!src.IsPostIndex());
1582
1583 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001584 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001585 MemOperand base = MemOperand(temp_base);
1586 switch (type) {
1587 case Primitive::kPrimBoolean:
1588 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001589 if (needs_null_check) {
1590 MaybeRecordImplicitNullCheck(instruction);
1591 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001592 break;
1593 case Primitive::kPrimByte:
1594 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001595 if (needs_null_check) {
1596 MaybeRecordImplicitNullCheck(instruction);
1597 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001598 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1599 break;
1600 case Primitive::kPrimChar:
1601 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001602 if (needs_null_check) {
1603 MaybeRecordImplicitNullCheck(instruction);
1604 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001605 break;
1606 case Primitive::kPrimShort:
1607 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001608 if (needs_null_check) {
1609 MaybeRecordImplicitNullCheck(instruction);
1610 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001611 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1612 break;
1613 case Primitive::kPrimInt:
1614 case Primitive::kPrimNot:
1615 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001616 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001617 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001618 if (needs_null_check) {
1619 MaybeRecordImplicitNullCheck(instruction);
1620 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001621 break;
1622 case Primitive::kPrimFloat:
1623 case Primitive::kPrimDouble: {
1624 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001625 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001626
1627 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1628 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001629 if (needs_null_check) {
1630 MaybeRecordImplicitNullCheck(instruction);
1631 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001632 __ Fmov(FPRegister(dst), temp);
1633 break;
1634 }
1635 case Primitive::kPrimVoid:
1636 LOG(FATAL) << "Unreachable type " << type;
1637 }
1638}
1639
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001640void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001641 CPURegister src,
1642 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001643 switch (type) {
1644 case Primitive::kPrimBoolean:
1645 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001646 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001647 break;
1648 case Primitive::kPrimChar:
1649 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001650 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001651 break;
1652 case Primitive::kPrimInt:
1653 case Primitive::kPrimNot:
1654 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001655 case Primitive::kPrimFloat:
1656 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001657 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001658 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001659 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001660 case Primitive::kPrimVoid:
1661 LOG(FATAL) << "Unreachable type " << type;
1662 }
1663}
1664
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001665void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1666 CPURegister src,
1667 const MemOperand& dst) {
1668 UseScratchRegisterScope temps(GetVIXLAssembler());
1669 Register temp_base = temps.AcquireX();
1670
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001671 DCHECK(!dst.IsPreIndex());
1672 DCHECK(!dst.IsPostIndex());
1673
1674 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001675 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001676 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001677 MemOperand base = MemOperand(temp_base);
1678 switch (type) {
1679 case Primitive::kPrimBoolean:
1680 case Primitive::kPrimByte:
1681 __ Stlrb(Register(src), base);
1682 break;
1683 case Primitive::kPrimChar:
1684 case Primitive::kPrimShort:
1685 __ Stlrh(Register(src), base);
1686 break;
1687 case Primitive::kPrimInt:
1688 case Primitive::kPrimNot:
1689 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001690 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001691 __ Stlr(Register(src), base);
1692 break;
1693 case Primitive::kPrimFloat:
1694 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001695 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001696 Register temp_src;
1697 if (src.IsZero()) {
1698 // The zero register is used to avoid synthesizing zero constants.
1699 temp_src = Register(src);
1700 } else {
1701 DCHECK(src.IsFPRegister());
1702 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1703 __ Fmov(temp_src, FPRegister(src));
1704 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001705
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001706 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001707 break;
1708 }
1709 case Primitive::kPrimVoid:
1710 LOG(FATAL) << "Unreachable type " << type;
1711 }
1712}
1713
Calin Juravle175dc732015-08-25 15:42:32 +01001714void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1715 HInstruction* instruction,
1716 uint32_t dex_pc,
1717 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001718 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001719 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001720 if (EntrypointRequiresStackMap(entrypoint)) {
1721 RecordPcInfo(instruction, dex_pc, slow_path);
1722 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001723}
1724
Roland Levillaindec8f632016-07-22 17:10:06 +01001725void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1726 HInstruction* instruction,
1727 SlowPathCode* slow_path) {
1728 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001729 GenerateInvokeRuntime(entry_point_offset);
1730}
1731
1732void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001733 BlockPoolsScope block_pools(GetVIXLAssembler());
1734 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1735 __ Blr(lr);
1736}
1737
Alexandre Rames67555f72014-11-18 10:55:16 +00001738void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001739 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001740 UseScratchRegisterScope temps(GetVIXLAssembler());
1741 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001742 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1743
Serban Constantinescu02164b32014-11-13 14:05:07 +00001744 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001745 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1746 __ Add(temp, class_reg, status_offset);
1747 __ Ldar(temp, HeapOperand(temp));
1748 __ Cmp(temp, mirror::Class::kStatusInitialized);
1749 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001750 __ Bind(slow_path->GetExitLabel());
1751}
Alexandre Rames5319def2014-10-23 10:03:10 +01001752
Roland Levillain44015862016-01-22 11:47:17 +00001753void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001754 BarrierType type = BarrierAll;
1755
1756 switch (kind) {
1757 case MemBarrierKind::kAnyAny:
1758 case MemBarrierKind::kAnyStore: {
1759 type = BarrierAll;
1760 break;
1761 }
1762 case MemBarrierKind::kLoadAny: {
1763 type = BarrierReads;
1764 break;
1765 }
1766 case MemBarrierKind::kStoreStore: {
1767 type = BarrierWrites;
1768 break;
1769 }
1770 default:
1771 LOG(FATAL) << "Unexpected memory barrier " << kind;
1772 }
1773 __ Dmb(InnerShareable, type);
1774}
1775
Serban Constantinescu02164b32014-11-13 14:05:07 +00001776void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1777 HBasicBlock* successor) {
1778 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001779 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1780 if (slow_path == nullptr) {
1781 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1782 instruction->SetSlowPath(slow_path);
1783 codegen_->AddSlowPath(slow_path);
1784 if (successor != nullptr) {
1785 DCHECK(successor->IsLoopHeader());
1786 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1787 }
1788 } else {
1789 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1790 }
1791
Serban Constantinescu02164b32014-11-13 14:05:07 +00001792 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1793 Register temp = temps.AcquireW();
1794
Andreas Gampe542451c2016-07-26 09:02:02 -07001795 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001796 if (successor == nullptr) {
1797 __ Cbnz(temp, slow_path->GetEntryLabel());
1798 __ Bind(slow_path->GetReturnLabel());
1799 } else {
1800 __ Cbz(temp, codegen_->GetLabelOf(successor));
1801 __ B(slow_path->GetEntryLabel());
1802 // slow_path will return to GetLabelOf(successor).
1803 }
1804}
1805
Alexandre Rames5319def2014-10-23 10:03:10 +01001806InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1807 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001808 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001809 assembler_(codegen->GetAssembler()),
1810 codegen_(codegen) {}
1811
1812#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001813 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001814
1815#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1816
1817enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001818 // Using a base helps identify when we hit such breakpoints.
1819 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001820#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1821 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1822#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1823};
1824
1825#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001826 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001827 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1828 } \
1829 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1830 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1831 locations->SetOut(Location::Any()); \
1832 }
1833 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1834#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1835
1836#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001837#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001838
Alexandre Rames67555f72014-11-18 10:55:16 +00001839void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001840 DCHECK_EQ(instr->InputCount(), 2U);
1841 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1842 Primitive::Type type = instr->GetResultType();
1843 switch (type) {
1844 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001845 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001846 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001847 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001848 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001849 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001850
1851 case Primitive::kPrimFloat:
1852 case Primitive::kPrimDouble:
1853 locations->SetInAt(0, Location::RequiresFpuRegister());
1854 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001855 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001856 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001857
Alexandre Rames5319def2014-10-23 10:03:10 +01001858 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001859 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001860 }
1861}
1862
Alexandre Rames09a99962015-04-15 11:47:56 +01001863void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001864 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1865
1866 bool object_field_get_with_read_barrier =
1867 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001868 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001869 new (GetGraph()->GetArena()) LocationSummary(instruction,
1870 object_field_get_with_read_barrier ?
1871 LocationSummary::kCallOnSlowPath :
1872 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001873 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001874 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001875 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001876 locations->SetInAt(0, Location::RequiresRegister());
1877 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1878 locations->SetOut(Location::RequiresFpuRegister());
1879 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001880 // The output overlaps for an object field get when read barriers
1881 // are enabled: we do not want the load to overwrite the object's
1882 // location, as we need it to emit the read barrier.
1883 locations->SetOut(
1884 Location::RequiresRegister(),
1885 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001886 }
1887}
1888
1889void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1890 const FieldInfo& field_info) {
1891 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001892 LocationSummary* locations = instruction->GetLocations();
1893 Location base_loc = locations->InAt(0);
1894 Location out = locations->Out();
1895 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001896 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001897 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001898 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001899
Roland Levillain44015862016-01-22 11:47:17 +00001900 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1901 // Object FieldGet with Baker's read barrier case.
1902 MacroAssembler* masm = GetVIXLAssembler();
1903 UseScratchRegisterScope temps(masm);
1904 // /* HeapReference<Object> */ out = *(base + offset)
1905 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1906 Register temp = temps.AcquireW();
1907 // Note that potential implicit null checks are handled in this
1908 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1909 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1910 instruction,
1911 out,
1912 base,
1913 offset,
1914 temp,
1915 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001916 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001917 } else {
1918 // General case.
1919 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001920 // Note that a potential implicit null check is handled in this
1921 // CodeGeneratorARM64::LoadAcquire call.
1922 // NB: LoadAcquire will record the pc info if needed.
1923 codegen_->LoadAcquire(
1924 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001925 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001926 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001927 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001928 }
Roland Levillain44015862016-01-22 11:47:17 +00001929 if (field_type == Primitive::kPrimNot) {
1930 // If read barriers are enabled, emit read barriers other than
1931 // Baker's using a slow path (and also unpoison the loaded
1932 // reference, if heap poisoning is enabled).
1933 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1934 }
Roland Levillain4d027112015-07-01 15:41:14 +01001935 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001936}
1937
1938void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1939 LocationSummary* locations =
1940 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1941 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001942 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1943 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1944 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001945 locations->SetInAt(1, Location::RequiresFpuRegister());
1946 } else {
1947 locations->SetInAt(1, Location::RequiresRegister());
1948 }
1949}
1950
1951void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001952 const FieldInfo& field_info,
1953 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001954 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001955 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001956
1957 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001958 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001959 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001960 Offset offset = field_info.GetFieldOffset();
1961 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001962
Roland Levillain4d027112015-07-01 15:41:14 +01001963 {
1964 // We use a block to end the scratch scope before the write barrier, thus
1965 // freeing the temporary registers so they can be used in `MarkGCCard`.
1966 UseScratchRegisterScope temps(GetVIXLAssembler());
1967
1968 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1969 DCHECK(value.IsW());
1970 Register temp = temps.AcquireW();
1971 __ Mov(temp, value.W());
1972 GetAssembler()->PoisonHeapReference(temp.W());
1973 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001974 }
Roland Levillain4d027112015-07-01 15:41:14 +01001975
1976 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001977 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1978 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001979 } else {
1980 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1981 codegen_->MaybeRecordImplicitNullCheck(instruction);
1982 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001983 }
1984
1985 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001986 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001987 }
1988}
1989
Alexandre Rames67555f72014-11-18 10:55:16 +00001990void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001991 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001992
1993 switch (type) {
1994 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001995 case Primitive::kPrimLong: {
1996 Register dst = OutputRegister(instr);
1997 Register lhs = InputRegisterAt(instr, 0);
1998 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001999 if (instr->IsAdd()) {
2000 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002001 } else if (instr->IsAnd()) {
2002 __ And(dst, lhs, rhs);
2003 } else if (instr->IsOr()) {
2004 __ Orr(dst, lhs, rhs);
2005 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002006 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002007 } else if (instr->IsRor()) {
2008 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002009 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002010 __ Ror(dst, lhs, shift);
2011 } else {
2012 // Ensure shift distance is in the same size register as the result. If
2013 // we are rotating a long and the shift comes in a w register originally,
2014 // we don't need to sxtw for use as an x since the shift distances are
2015 // all & reg_bits - 1.
2016 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2017 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002018 } else {
2019 DCHECK(instr->IsXor());
2020 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002021 }
2022 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002023 }
2024 case Primitive::kPrimFloat:
2025 case Primitive::kPrimDouble: {
2026 FPRegister dst = OutputFPRegister(instr);
2027 FPRegister lhs = InputFPRegisterAt(instr, 0);
2028 FPRegister rhs = InputFPRegisterAt(instr, 1);
2029 if (instr->IsAdd()) {
2030 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002031 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002032 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002033 } else {
2034 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002035 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002036 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002037 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002038 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002039 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002040 }
2041}
2042
Serban Constantinescu02164b32014-11-13 14:05:07 +00002043void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2044 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2045
2046 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2047 Primitive::Type type = instr->GetResultType();
2048 switch (type) {
2049 case Primitive::kPrimInt:
2050 case Primitive::kPrimLong: {
2051 locations->SetInAt(0, Location::RequiresRegister());
2052 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2053 locations->SetOut(Location::RequiresRegister());
2054 break;
2055 }
2056 default:
2057 LOG(FATAL) << "Unexpected shift type " << type;
2058 }
2059}
2060
2061void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2062 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2063
2064 Primitive::Type type = instr->GetType();
2065 switch (type) {
2066 case Primitive::kPrimInt:
2067 case Primitive::kPrimLong: {
2068 Register dst = OutputRegister(instr);
2069 Register lhs = InputRegisterAt(instr, 0);
2070 Operand rhs = InputOperandAt(instr, 1);
2071 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002072 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002073 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002074 if (instr->IsShl()) {
2075 __ Lsl(dst, lhs, shift_value);
2076 } else if (instr->IsShr()) {
2077 __ Asr(dst, lhs, shift_value);
2078 } else {
2079 __ Lsr(dst, lhs, shift_value);
2080 }
2081 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002082 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002083
2084 if (instr->IsShl()) {
2085 __ Lsl(dst, lhs, rhs_reg);
2086 } else if (instr->IsShr()) {
2087 __ Asr(dst, lhs, rhs_reg);
2088 } else {
2089 __ Lsr(dst, lhs, rhs_reg);
2090 }
2091 }
2092 break;
2093 }
2094 default:
2095 LOG(FATAL) << "Unexpected shift operation type " << type;
2096 }
2097}
2098
Alexandre Rames5319def2014-10-23 10:03:10 +01002099void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002100 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002101}
2102
2103void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002104 HandleBinaryOp(instruction);
2105}
2106
2107void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2108 HandleBinaryOp(instruction);
2109}
2110
2111void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2112 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002113}
2114
Artem Serov7fc63502016-02-09 17:15:29 +00002115void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002116 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2117 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2118 locations->SetInAt(0, Location::RequiresRegister());
2119 // There is no immediate variant of negated bitwise instructions in AArch64.
2120 locations->SetInAt(1, Location::RequiresRegister());
2121 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2122}
2123
Artem Serov7fc63502016-02-09 17:15:29 +00002124void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002125 Register dst = OutputRegister(instr);
2126 Register lhs = InputRegisterAt(instr, 0);
2127 Register rhs = InputRegisterAt(instr, 1);
2128
2129 switch (instr->GetOpKind()) {
2130 case HInstruction::kAnd:
2131 __ Bic(dst, lhs, rhs);
2132 break;
2133 case HInstruction::kOr:
2134 __ Orn(dst, lhs, rhs);
2135 break;
2136 case HInstruction::kXor:
2137 __ Eon(dst, lhs, rhs);
2138 break;
2139 default:
2140 LOG(FATAL) << "Unreachable";
2141 }
2142}
2143
Alexandre Rames8626b742015-11-25 16:28:08 +00002144void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
2145 HArm64DataProcWithShifterOp* instruction) {
2146 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2147 instruction->GetType() == Primitive::kPrimLong);
2148 LocationSummary* locations =
2149 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2150 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2151 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2152 } else {
2153 locations->SetInAt(0, Location::RequiresRegister());
2154 }
2155 locations->SetInAt(1, Location::RequiresRegister());
2156 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2157}
2158
2159void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
2160 HArm64DataProcWithShifterOp* instruction) {
2161 Primitive::Type type = instruction->GetType();
2162 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2163 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2164 Register out = OutputRegister(instruction);
2165 Register left;
2166 if (kind != HInstruction::kNeg) {
2167 left = InputRegisterAt(instruction, 0);
2168 }
2169 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
2170 // shifter operand operation, the IR generating `right_reg` (input to the type
2171 // conversion) can have a different type from the current instruction's type,
2172 // so we manually indicate the type.
2173 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00002174 int64_t shift_amount = instruction->GetShiftAmount() &
2175 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00002176
2177 Operand right_operand(0);
2178
2179 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2180 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
2181 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2182 } else {
2183 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
2184 }
2185
2186 // Logical binary operations do not support extension operations in the
2187 // operand. Note that VIXL would still manage if it was passed by generating
2188 // the extension as a separate instruction.
2189 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2190 DCHECK(!right_operand.IsExtendedRegister() ||
2191 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2192 kind != HInstruction::kNeg));
2193 switch (kind) {
2194 case HInstruction::kAdd:
2195 __ Add(out, left, right_operand);
2196 break;
2197 case HInstruction::kAnd:
2198 __ And(out, left, right_operand);
2199 break;
2200 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002201 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002202 __ Neg(out, right_operand);
2203 break;
2204 case HInstruction::kOr:
2205 __ Orr(out, left, right_operand);
2206 break;
2207 case HInstruction::kSub:
2208 __ Sub(out, left, right_operand);
2209 break;
2210 case HInstruction::kXor:
2211 __ Eor(out, left, right_operand);
2212 break;
2213 default:
2214 LOG(FATAL) << "Unexpected operation kind: " << kind;
2215 UNREACHABLE();
2216 }
2217}
2218
Artem Serov328429f2016-07-06 16:23:04 +01002219void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002220 LocationSummary* locations =
2221 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2222 locations->SetInAt(0, Location::RequiresRegister());
2223 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2224 locations->SetOut(Location::RequiresRegister());
2225}
2226
Roland Levillain19c54192016-11-04 13:44:09 +00002227void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002228 __ Add(OutputRegister(instruction),
2229 InputRegisterAt(instruction, 0),
2230 Operand(InputOperandAt(instruction, 1)));
2231}
2232
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002233void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002234 LocationSummary* locations =
2235 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002236 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2237 if (instr->GetOpKind() == HInstruction::kSub &&
2238 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002239 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002240 // Don't allocate register for Mneg instruction.
2241 } else {
2242 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2243 Location::RequiresRegister());
2244 }
2245 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2246 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002247 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2248}
2249
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002250void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002251 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002252 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2253 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002254
2255 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2256 // This fixup should be carried out for all multiply-accumulate instructions:
2257 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2258 if (instr->GetType() == Primitive::kPrimLong &&
2259 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2260 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002261 vixl::aarch64::Instruction* prev =
2262 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002263 if (prev->IsLoadOrStore()) {
2264 // Make sure we emit only exactly one nop.
Scott Wakelingb77051e2016-11-21 19:46:00 +00002265 vixl::CodeBufferCheckScope scope(masm,
2266 kInstructionSize,
2267 vixl::CodeBufferCheckScope::kReserveBufferSpace,
2268 vixl::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002269 __ nop();
2270 }
2271 }
2272
2273 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002274 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002275 __ Madd(res, mul_left, mul_right, accumulator);
2276 } else {
2277 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002278 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002279 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002280 __ Mneg(res, mul_left, mul_right);
2281 } else {
2282 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2283 __ Msub(res, mul_left, mul_right, accumulator);
2284 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002285 }
2286}
2287
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002288void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002289 bool object_array_get_with_read_barrier =
2290 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002291 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002292 new (GetGraph()->GetArena()) LocationSummary(instruction,
2293 object_array_get_with_read_barrier ?
2294 LocationSummary::kCallOnSlowPath :
2295 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002296 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002297 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002298 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002299 locations->SetInAt(0, Location::RequiresRegister());
2300 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002301 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2302 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2303 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002304 // The output overlaps in the case of an object array get with
2305 // read barriers enabled: we do not want the move to overwrite the
2306 // array's location, as we need it to emit the read barrier.
2307 locations->SetOut(
2308 Location::RequiresRegister(),
2309 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002310 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002311}
2312
2313void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002314 Primitive::Type type = instruction->GetType();
2315 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002316 LocationSummary* locations = instruction->GetLocations();
2317 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002318 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002319 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002320 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2321 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002322 MacroAssembler* masm = GetVIXLAssembler();
2323 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002324 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002325 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002326
Roland Levillain19c54192016-11-04 13:44:09 +00002327 // The read barrier instrumentation of object ArrayGet instructions
2328 // does not support the HIntermediateAddress instruction.
2329 DCHECK(!((type == Primitive::kPrimNot) &&
2330 instruction->GetArray()->IsIntermediateAddress() &&
2331 kEmitCompilerReadBarrier));
2332
Roland Levillain44015862016-01-22 11:47:17 +00002333 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2334 // Object ArrayGet with Baker's read barrier case.
2335 Register temp = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00002336 // Note that a potential implicit null check is handled in the
2337 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2338 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2339 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002340 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002341 // General case.
2342 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002343 Register length;
2344 if (maybe_compressed_char_at) {
2345 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2346 length = temps.AcquireW();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002347 if (instruction->GetArray()->IsIntermediateAddress()) {
2348 DCHECK_LT(count_offset, offset);
2349 int64_t adjusted_offset = static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2350 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2351 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2352 } else {
2353 __ Ldr(length, HeapOperand(obj, count_offset));
2354 }
jessicahandojo05765752016-09-09 19:01:32 -07002355 codegen_->MaybeRecordImplicitNullCheck(instruction);
2356 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002357 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002358 if (maybe_compressed_char_at) {
2359 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002360 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2361 "Expecting 0=compressed, 1=uncompressed");
2362 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002363 __ Ldrb(Register(OutputCPURegister(instruction)),
2364 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2365 __ B(&done);
2366 __ Bind(&uncompressed_load);
2367 __ Ldrh(Register(OutputCPURegister(instruction)),
2368 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2369 __ Bind(&done);
2370 } else {
2371 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2372 source = HeapOperand(obj, offset);
2373 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002374 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002375 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002376 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002377 // We do not need to compute the intermediate address from the array: the
2378 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002379 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002380 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002381 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002382 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2383 }
2384 temp = obj;
2385 } else {
2386 __ Add(temp, obj, offset);
2387 }
jessicahandojo05765752016-09-09 19:01:32 -07002388 if (maybe_compressed_char_at) {
2389 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002390 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2391 "Expecting 0=compressed, 1=uncompressed");
2392 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002393 __ Ldrb(Register(OutputCPURegister(instruction)),
2394 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2395 __ B(&done);
2396 __ Bind(&uncompressed_load);
2397 __ Ldrh(Register(OutputCPURegister(instruction)),
2398 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2399 __ Bind(&done);
2400 } else {
2401 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2402 }
Roland Levillain44015862016-01-22 11:47:17 +00002403 }
jessicahandojo05765752016-09-09 19:01:32 -07002404 if (!maybe_compressed_char_at) {
2405 codegen_->Load(type, OutputCPURegister(instruction), source);
2406 codegen_->MaybeRecordImplicitNullCheck(instruction);
2407 }
Roland Levillain44015862016-01-22 11:47:17 +00002408
2409 if (type == Primitive::kPrimNot) {
2410 static_assert(
2411 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2412 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2413 Location obj_loc = locations->InAt(0);
2414 if (index.IsConstant()) {
2415 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2416 } else {
2417 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2418 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002419 }
Roland Levillain4d027112015-07-01 15:41:14 +01002420 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002421}
2422
Alexandre Rames5319def2014-10-23 10:03:10 +01002423void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2424 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2425 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002426 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002427}
2428
2429void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002430 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002431 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002432 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002433 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002434 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002435 // Mask out compression flag from String's array length.
2436 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002437 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002438 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002439}
2440
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002441void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002442 Primitive::Type value_type = instruction->GetComponentType();
2443
2444 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002445 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2446 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002447 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002448 LocationSummary::kCallOnSlowPath :
2449 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002450 locations->SetInAt(0, Location::RequiresRegister());
2451 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002452 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2453 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2454 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002455 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002456 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002457 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002458 }
2459}
2460
2461void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2462 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002463 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002464 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002465 bool needs_write_barrier =
2466 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002467
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002468 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002469 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002470 CPURegister source = value;
2471 Location index = locations->InAt(1);
2472 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2473 MemOperand destination = HeapOperand(array);
2474 MacroAssembler* masm = GetVIXLAssembler();
2475 BlockPoolsScope block_pools(masm);
2476
2477 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002478 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002479 if (index.IsConstant()) {
2480 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2481 destination = HeapOperand(array, offset);
2482 } else {
2483 UseScratchRegisterScope temps(masm);
2484 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002485 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002486 // We do not need to compute the intermediate address from the array: the
2487 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002488 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002489 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002490 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002491 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2492 }
2493 temp = array;
2494 } else {
2495 __ Add(temp, array, offset);
2496 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002497 destination = HeapOperand(temp,
2498 XRegisterFrom(index),
2499 LSL,
2500 Primitive::ComponentSizeShift(value_type));
2501 }
2502 codegen_->Store(value_type, value, destination);
2503 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002504 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002505 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002506 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002507 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002508 {
2509 // We use a block to end the scratch scope before the write barrier, thus
2510 // freeing the temporary registers so they can be used in `MarkGCCard`.
2511 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002512 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002513 if (index.IsConstant()) {
2514 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002515 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002516 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002517 destination = HeapOperand(temp,
2518 XRegisterFrom(index),
2519 LSL,
2520 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002521 }
2522
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002523 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2524 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2525 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2526
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002527 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002528 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2529 codegen_->AddSlowPath(slow_path);
2530 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002531 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002532 __ Cbnz(Register(value), &non_zero);
2533 if (!index.IsConstant()) {
2534 __ Add(temp, array, offset);
2535 }
2536 __ Str(wzr, destination);
2537 codegen_->MaybeRecordImplicitNullCheck(instruction);
2538 __ B(&done);
2539 __ Bind(&non_zero);
2540 }
2541
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002542 // Note that when Baker read barriers are enabled, the type
2543 // checks are performed without read barriers. This is fine,
2544 // even in the case where a class object is in the from-space
2545 // after the flip, as a comparison involving such a type would
2546 // not produce a false positive; it may of course produce a
2547 // false negative, in which case we would take the ArraySet
2548 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002549
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002550 Register temp2 = temps.AcquireSameSizeAs(array);
2551 // /* HeapReference<Class> */ temp = array->klass_
2552 __ Ldr(temp, HeapOperand(array, class_offset));
2553 codegen_->MaybeRecordImplicitNullCheck(instruction);
2554 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002555
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002556 // /* HeapReference<Class> */ temp = temp->component_type_
2557 __ Ldr(temp, HeapOperand(temp, component_offset));
2558 // /* HeapReference<Class> */ temp2 = value->klass_
2559 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2560 // If heap poisoning is enabled, no need to unpoison `temp`
2561 // nor `temp2`, as we are comparing two poisoned references.
2562 __ Cmp(temp, temp2);
2563 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002564
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002565 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2566 vixl::aarch64::Label do_put;
2567 __ B(eq, &do_put);
2568 // If heap poisoning is enabled, the `temp` reference has
2569 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002570 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2571
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002572 // /* HeapReference<Class> */ temp = temp->super_class_
2573 __ Ldr(temp, HeapOperand(temp, super_offset));
2574 // If heap poisoning is enabled, no need to unpoison
2575 // `temp`, as we are comparing against null below.
2576 __ Cbnz(temp, slow_path->GetEntryLabel());
2577 __ Bind(&do_put);
2578 } else {
2579 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002580 }
2581 }
2582
2583 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002584 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002585 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002586 __ Mov(temp2, value.W());
2587 GetAssembler()->PoisonHeapReference(temp2);
2588 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002589 }
2590
2591 if (!index.IsConstant()) {
2592 __ Add(temp, array, offset);
2593 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002594 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002595
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002596 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002597 codegen_->MaybeRecordImplicitNullCheck(instruction);
2598 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002599 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002600
2601 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2602
2603 if (done.IsLinked()) {
2604 __ Bind(&done);
2605 }
2606
2607 if (slow_path != nullptr) {
2608 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002609 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002610 }
2611}
2612
Alexandre Rames67555f72014-11-18 10:55:16 +00002613void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002614 RegisterSet caller_saves = RegisterSet::Empty();
2615 InvokeRuntimeCallingConvention calling_convention;
2616 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2617 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2618 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002619 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002620 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002621}
2622
2623void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002624 BoundsCheckSlowPathARM64* slow_path =
2625 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002626 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002627 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2628 __ B(slow_path->GetEntryLabel(), hs);
2629}
2630
Alexandre Rames67555f72014-11-18 10:55:16 +00002631void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2632 LocationSummary* locations =
2633 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2634 locations->SetInAt(0, Location::RequiresRegister());
2635 if (check->HasUses()) {
2636 locations->SetOut(Location::SameAsFirstInput());
2637 }
2638}
2639
2640void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2641 // We assume the class is not null.
2642 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2643 check->GetLoadClass(), check, check->GetDexPc(), true);
2644 codegen_->AddSlowPath(slow_path);
2645 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2646}
2647
Roland Levillain1a653882016-03-18 18:05:57 +00002648static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2649 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2650 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2651}
2652
2653void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2654 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2655 Location rhs_loc = instruction->GetLocations()->InAt(1);
2656 if (rhs_loc.IsConstant()) {
2657 // 0.0 is the only immediate that can be encoded directly in
2658 // an FCMP instruction.
2659 //
2660 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2661 // specify that in a floating-point comparison, positive zero
2662 // and negative zero are considered equal, so we can use the
2663 // literal 0.0 for both cases here.
2664 //
2665 // Note however that some methods (Float.equal, Float.compare,
2666 // Float.compareTo, Double.equal, Double.compare,
2667 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2668 // StrictMath.min) consider 0.0 to be (strictly) greater than
2669 // -0.0. So if we ever translate calls to these methods into a
2670 // HCompare instruction, we must handle the -0.0 case with
2671 // care here.
2672 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2673 __ Fcmp(lhs_reg, 0.0);
2674 } else {
2675 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2676 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002677}
2678
Serban Constantinescu02164b32014-11-13 14:05:07 +00002679void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002680 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002681 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2682 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002683 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002684 case Primitive::kPrimBoolean:
2685 case Primitive::kPrimByte:
2686 case Primitive::kPrimShort:
2687 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002688 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002689 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002690 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002691 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002692 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2693 break;
2694 }
2695 case Primitive::kPrimFloat:
2696 case Primitive::kPrimDouble: {
2697 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002698 locations->SetInAt(1,
2699 IsFloatingPointZeroConstant(compare->InputAt(1))
2700 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2701 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002702 locations->SetOut(Location::RequiresRegister());
2703 break;
2704 }
2705 default:
2706 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2707 }
2708}
2709
2710void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2711 Primitive::Type in_type = compare->InputAt(0)->GetType();
2712
2713 // 0 if: left == right
2714 // 1 if: left > right
2715 // -1 if: left < right
2716 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002717 case Primitive::kPrimBoolean:
2718 case Primitive::kPrimByte:
2719 case Primitive::kPrimShort:
2720 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002721 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002722 case Primitive::kPrimLong: {
2723 Register result = OutputRegister(compare);
2724 Register left = InputRegisterAt(compare, 0);
2725 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002726 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002727 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2728 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002729 break;
2730 }
2731 case Primitive::kPrimFloat:
2732 case Primitive::kPrimDouble: {
2733 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002734 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002735 __ Cset(result, ne);
2736 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002737 break;
2738 }
2739 default:
2740 LOG(FATAL) << "Unimplemented compare type " << in_type;
2741 }
2742}
2743
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002744void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002745 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002746
2747 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2748 locations->SetInAt(0, Location::RequiresFpuRegister());
2749 locations->SetInAt(1,
2750 IsFloatingPointZeroConstant(instruction->InputAt(1))
2751 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2752 : Location::RequiresFpuRegister());
2753 } else {
2754 // Integer cases.
2755 locations->SetInAt(0, Location::RequiresRegister());
2756 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2757 }
2758
David Brazdilb3e773e2016-01-26 11:28:37 +00002759 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002760 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002761 }
2762}
2763
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002764void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002765 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002766 return;
2767 }
2768
2769 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002770 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002771 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002772
Roland Levillain7f63c522015-07-13 15:54:55 +00002773 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002774 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002775 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002776 } else {
2777 // Integer cases.
2778 Register lhs = InputRegisterAt(instruction, 0);
2779 Operand rhs = InputOperandAt(instruction, 1);
2780 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002781 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002782 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002783}
2784
2785#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2786 M(Equal) \
2787 M(NotEqual) \
2788 M(LessThan) \
2789 M(LessThanOrEqual) \
2790 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002791 M(GreaterThanOrEqual) \
2792 M(Below) \
2793 M(BelowOrEqual) \
2794 M(Above) \
2795 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002796#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002797void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2798void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002799FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002800#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002801#undef FOR_EACH_CONDITION_INSTRUCTION
2802
Zheng Xuc6667102015-05-15 16:08:45 +08002803void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2804 DCHECK(instruction->IsDiv() || instruction->IsRem());
2805
2806 LocationSummary* locations = instruction->GetLocations();
2807 Location second = locations->InAt(1);
2808 DCHECK(second.IsConstant());
2809
2810 Register out = OutputRegister(instruction);
2811 Register dividend = InputRegisterAt(instruction, 0);
2812 int64_t imm = Int64FromConstant(second.GetConstant());
2813 DCHECK(imm == 1 || imm == -1);
2814
2815 if (instruction->IsRem()) {
2816 __ Mov(out, 0);
2817 } else {
2818 if (imm == 1) {
2819 __ Mov(out, dividend);
2820 } else {
2821 __ Neg(out, dividend);
2822 }
2823 }
2824}
2825
2826void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2827 DCHECK(instruction->IsDiv() || instruction->IsRem());
2828
2829 LocationSummary* locations = instruction->GetLocations();
2830 Location second = locations->InAt(1);
2831 DCHECK(second.IsConstant());
2832
2833 Register out = OutputRegister(instruction);
2834 Register dividend = InputRegisterAt(instruction, 0);
2835 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002836 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002837 int ctz_imm = CTZ(abs_imm);
2838
2839 UseScratchRegisterScope temps(GetVIXLAssembler());
2840 Register temp = temps.AcquireSameSizeAs(out);
2841
2842 if (instruction->IsDiv()) {
2843 __ Add(temp, dividend, abs_imm - 1);
2844 __ Cmp(dividend, 0);
2845 __ Csel(out, temp, dividend, lt);
2846 if (imm > 0) {
2847 __ Asr(out, out, ctz_imm);
2848 } else {
2849 __ Neg(out, Operand(out, ASR, ctz_imm));
2850 }
2851 } else {
2852 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2853 __ Asr(temp, dividend, bits - 1);
2854 __ Lsr(temp, temp, bits - ctz_imm);
2855 __ Add(out, dividend, temp);
2856 __ And(out, out, abs_imm - 1);
2857 __ Sub(out, out, temp);
2858 }
2859}
2860
2861void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2862 DCHECK(instruction->IsDiv() || instruction->IsRem());
2863
2864 LocationSummary* locations = instruction->GetLocations();
2865 Location second = locations->InAt(1);
2866 DCHECK(second.IsConstant());
2867
2868 Register out = OutputRegister(instruction);
2869 Register dividend = InputRegisterAt(instruction, 0);
2870 int64_t imm = Int64FromConstant(second.GetConstant());
2871
2872 Primitive::Type type = instruction->GetResultType();
2873 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2874
2875 int64_t magic;
2876 int shift;
2877 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2878
2879 UseScratchRegisterScope temps(GetVIXLAssembler());
2880 Register temp = temps.AcquireSameSizeAs(out);
2881
2882 // temp = get_high(dividend * magic)
2883 __ Mov(temp, magic);
2884 if (type == Primitive::kPrimLong) {
2885 __ Smulh(temp, dividend, temp);
2886 } else {
2887 __ Smull(temp.X(), dividend, temp);
2888 __ Lsr(temp.X(), temp.X(), 32);
2889 }
2890
2891 if (imm > 0 && magic < 0) {
2892 __ Add(temp, temp, dividend);
2893 } else if (imm < 0 && magic > 0) {
2894 __ Sub(temp, temp, dividend);
2895 }
2896
2897 if (shift != 0) {
2898 __ Asr(temp, temp, shift);
2899 }
2900
2901 if (instruction->IsDiv()) {
2902 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2903 } else {
2904 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2905 // TODO: Strength reduction for msub.
2906 Register temp_imm = temps.AcquireSameSizeAs(out);
2907 __ Mov(temp_imm, imm);
2908 __ Msub(out, temp, temp_imm, dividend);
2909 }
2910}
2911
2912void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2913 DCHECK(instruction->IsDiv() || instruction->IsRem());
2914 Primitive::Type type = instruction->GetResultType();
2915 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2916
2917 LocationSummary* locations = instruction->GetLocations();
2918 Register out = OutputRegister(instruction);
2919 Location second = locations->InAt(1);
2920
2921 if (second.IsConstant()) {
2922 int64_t imm = Int64FromConstant(second.GetConstant());
2923
2924 if (imm == 0) {
2925 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2926 } else if (imm == 1 || imm == -1) {
2927 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002928 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002929 DivRemByPowerOfTwo(instruction);
2930 } else {
2931 DCHECK(imm <= -2 || imm >= 2);
2932 GenerateDivRemWithAnyConstant(instruction);
2933 }
2934 } else {
2935 Register dividend = InputRegisterAt(instruction, 0);
2936 Register divisor = InputRegisterAt(instruction, 1);
2937 if (instruction->IsDiv()) {
2938 __ Sdiv(out, dividend, divisor);
2939 } else {
2940 UseScratchRegisterScope temps(GetVIXLAssembler());
2941 Register temp = temps.AcquireSameSizeAs(out);
2942 __ Sdiv(temp, dividend, divisor);
2943 __ Msub(out, temp, divisor, dividend);
2944 }
2945 }
2946}
2947
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002948void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2949 LocationSummary* locations =
2950 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2951 switch (div->GetResultType()) {
2952 case Primitive::kPrimInt:
2953 case Primitive::kPrimLong:
2954 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002955 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002956 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2957 break;
2958
2959 case Primitive::kPrimFloat:
2960 case Primitive::kPrimDouble:
2961 locations->SetInAt(0, Location::RequiresFpuRegister());
2962 locations->SetInAt(1, Location::RequiresFpuRegister());
2963 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2964 break;
2965
2966 default:
2967 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2968 }
2969}
2970
2971void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2972 Primitive::Type type = div->GetResultType();
2973 switch (type) {
2974 case Primitive::kPrimInt:
2975 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002976 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002977 break;
2978
2979 case Primitive::kPrimFloat:
2980 case Primitive::kPrimDouble:
2981 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2982 break;
2983
2984 default:
2985 LOG(FATAL) << "Unexpected div type " << type;
2986 }
2987}
2988
Alexandre Rames67555f72014-11-18 10:55:16 +00002989void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002990 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002991 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00002992}
2993
2994void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2995 SlowPathCodeARM64* slow_path =
2996 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2997 codegen_->AddSlowPath(slow_path);
2998 Location value = instruction->GetLocations()->InAt(0);
2999
Alexandre Rames3e69f162014-12-10 10:36:50 +00003000 Primitive::Type type = instruction->GetType();
3001
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003002 if (!Primitive::IsIntegralType(type)) {
3003 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003004 return;
3005 }
3006
Alexandre Rames67555f72014-11-18 10:55:16 +00003007 if (value.IsConstant()) {
3008 int64_t divisor = Int64ConstantFrom(value);
3009 if (divisor == 0) {
3010 __ B(slow_path->GetEntryLabel());
3011 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003012 // A division by a non-null constant is valid. We don't need to perform
3013 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003014 }
3015 } else {
3016 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3017 }
3018}
3019
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003020void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3021 LocationSummary* locations =
3022 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3023 locations->SetOut(Location::ConstantLocation(constant));
3024}
3025
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003026void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3027 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003028 // Will be generated at use site.
3029}
3030
Alexandre Rames5319def2014-10-23 10:03:10 +01003031void LocationsBuilderARM64::VisitExit(HExit* exit) {
3032 exit->SetLocations(nullptr);
3033}
3034
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003035void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003036}
3037
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003038void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3039 LocationSummary* locations =
3040 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3041 locations->SetOut(Location::ConstantLocation(constant));
3042}
3043
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003044void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003045 // Will be generated at use site.
3046}
3047
David Brazdilfc6a86a2015-06-26 10:33:45 +00003048void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003049 DCHECK(!successor->IsExitBlock());
3050 HBasicBlock* block = got->GetBlock();
3051 HInstruction* previous = got->GetPrevious();
3052 HLoopInformation* info = block->GetLoopInformation();
3053
David Brazdil46e2a392015-03-16 17:31:52 +00003054 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003055 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3056 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3057 return;
3058 }
3059 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3060 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3061 }
3062 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003063 __ B(codegen_->GetLabelOf(successor));
3064 }
3065}
3066
David Brazdilfc6a86a2015-06-26 10:33:45 +00003067void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3068 got->SetLocations(nullptr);
3069}
3070
3071void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3072 HandleGoto(got, got->GetSuccessor());
3073}
3074
3075void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3076 try_boundary->SetLocations(nullptr);
3077}
3078
3079void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3080 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3081 if (!successor->IsExitBlock()) {
3082 HandleGoto(try_boundary, successor);
3083 }
3084}
3085
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003086void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003087 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003088 vixl::aarch64::Label* true_target,
3089 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003090 // FP branching requires both targets to be explicit. If either of the targets
3091 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003092 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003093 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003094
David Brazdil0debae72015-11-12 18:37:00 +00003095 if (true_target == nullptr && false_target == nullptr) {
3096 // Nothing to do. The code always falls through.
3097 return;
3098 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003099 // Constant condition, statically compared against "true" (integer value 1).
3100 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003101 if (true_target != nullptr) {
3102 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003103 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003104 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003105 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003106 if (false_target != nullptr) {
3107 __ B(false_target);
3108 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003109 }
David Brazdil0debae72015-11-12 18:37:00 +00003110 return;
3111 }
3112
3113 // The following code generates these patterns:
3114 // (1) true_target == nullptr && false_target != nullptr
3115 // - opposite condition true => branch to false_target
3116 // (2) true_target != nullptr && false_target == nullptr
3117 // - condition true => branch to true_target
3118 // (3) true_target != nullptr && false_target != nullptr
3119 // - condition true => branch to true_target
3120 // - branch to false_target
3121 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003122 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003123 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003124 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003125 if (true_target == nullptr) {
3126 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3127 } else {
3128 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3129 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003130 } else {
3131 // The condition instruction has not been materialized, use its inputs as
3132 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003133 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003134
David Brazdil0debae72015-11-12 18:37:00 +00003135 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003136 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003137 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003138 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003139 IfCondition opposite_condition = condition->GetOppositeCondition();
3140 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003141 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003142 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003143 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003144 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003145 // Integer cases.
3146 Register lhs = InputRegisterAt(condition, 0);
3147 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003148
3149 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003150 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003151 if (true_target == nullptr) {
3152 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3153 non_fallthrough_target = false_target;
3154 } else {
3155 arm64_cond = ARM64Condition(condition->GetCondition());
3156 non_fallthrough_target = true_target;
3157 }
3158
Aart Bik086d27e2016-01-20 17:02:00 -08003159 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003160 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003161 switch (arm64_cond) {
3162 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003163 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003164 break;
3165 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003166 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003167 break;
3168 case lt:
3169 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003170 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003171 break;
3172 case ge:
3173 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003174 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003175 break;
3176 default:
3177 // Without the `static_cast` the compiler throws an error for
3178 // `-Werror=sign-promo`.
3179 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3180 }
3181 } else {
3182 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003183 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003184 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003185 }
3186 }
David Brazdil0debae72015-11-12 18:37:00 +00003187
3188 // If neither branch falls through (case 3), the conditional branch to `true_target`
3189 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3190 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003191 __ B(false_target);
3192 }
David Brazdil0debae72015-11-12 18:37:00 +00003193
3194 if (fallthrough_target.IsLinked()) {
3195 __ Bind(&fallthrough_target);
3196 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003197}
3198
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003199void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3200 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003201 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003202 locations->SetInAt(0, Location::RequiresRegister());
3203 }
3204}
3205
3206void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003207 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3208 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003209 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3210 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3211 true_target = nullptr;
3212 }
3213 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3214 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3215 false_target = nullptr;
3216 }
David Brazdil0debae72015-11-12 18:37:00 +00003217 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003218}
3219
3220void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3221 LocationSummary* locations = new (GetGraph()->GetArena())
3222 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003223 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003224 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003225 locations->SetInAt(0, Location::RequiresRegister());
3226 }
3227}
3228
3229void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003230 SlowPathCodeARM64* slow_path =
3231 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003232 GenerateTestAndBranch(deoptimize,
3233 /* condition_input_index */ 0,
3234 slow_path->GetEntryLabel(),
3235 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003236}
3237
David Brazdilc0b601b2016-02-08 14:20:45 +00003238static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3239 return condition->IsCondition() &&
3240 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3241}
3242
Alexandre Rames880f1192016-06-13 16:04:50 +01003243static inline Condition GetConditionForSelect(HCondition* condition) {
3244 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003245 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3246 : ARM64Condition(cond);
3247}
3248
David Brazdil74eb1b22015-12-14 11:44:01 +00003249void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3250 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003251 if (Primitive::IsFloatingPointType(select->GetType())) {
3252 locations->SetInAt(0, Location::RequiresFpuRegister());
3253 locations->SetInAt(1, Location::RequiresFpuRegister());
3254 locations->SetOut(Location::RequiresFpuRegister());
3255 } else {
3256 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3257 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3258 bool is_true_value_constant = cst_true_value != nullptr;
3259 bool is_false_value_constant = cst_false_value != nullptr;
3260 // Ask VIXL whether we should synthesize constants in registers.
3261 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3262 Operand true_op = is_true_value_constant ?
3263 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3264 Operand false_op = is_false_value_constant ?
3265 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3266 bool true_value_in_register = false;
3267 bool false_value_in_register = false;
3268 MacroAssembler::GetCselSynthesisInformation(
3269 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3270 true_value_in_register |= !is_true_value_constant;
3271 false_value_in_register |= !is_false_value_constant;
3272
3273 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3274 : Location::ConstantLocation(cst_true_value));
3275 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3276 : Location::ConstantLocation(cst_false_value));
3277 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003278 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003279
David Brazdil74eb1b22015-12-14 11:44:01 +00003280 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3281 locations->SetInAt(2, Location::RequiresRegister());
3282 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003283}
3284
3285void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003286 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003287 Condition csel_cond;
3288
3289 if (IsBooleanValueOrMaterializedCondition(cond)) {
3290 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003291 // Use the condition flags set by the previous instruction.
3292 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003293 } else {
3294 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003295 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003296 }
3297 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003298 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003299 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003300 } else {
3301 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003302 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003303 }
3304
Alexandre Rames880f1192016-06-13 16:04:50 +01003305 if (Primitive::IsFloatingPointType(select->GetType())) {
3306 __ Fcsel(OutputFPRegister(select),
3307 InputFPRegisterAt(select, 1),
3308 InputFPRegisterAt(select, 0),
3309 csel_cond);
3310 } else {
3311 __ Csel(OutputRegister(select),
3312 InputOperandAt(select, 1),
3313 InputOperandAt(select, 0),
3314 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003315 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003316}
3317
David Srbecky0cf44932015-12-09 14:09:59 +00003318void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3319 new (GetGraph()->GetArena()) LocationSummary(info);
3320}
3321
David Srbeckyd28f4a02016-03-14 17:14:24 +00003322void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3323 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003324}
3325
3326void CodeGeneratorARM64::GenerateNop() {
3327 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003328}
3329
Alexandre Rames5319def2014-10-23 10:03:10 +01003330void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003331 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003332}
3333
3334void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003335 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003336}
3337
3338void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003339 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003340}
3341
3342void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003343 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003344}
3345
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003346// Temp is used for read barrier.
3347static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3348 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003349 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003350 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3351 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3352 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3353 return 1;
3354 }
3355 return 0;
3356}
3357
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003358// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003359// interface pointer, one for loading the current interface.
3360// The other checks have one temp for loading the object's class.
3361static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3362 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3363 return 3;
3364 }
3365 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003366}
3367
Alexandre Rames67555f72014-11-18 10:55:16 +00003368void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003369 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003370 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003371 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003372 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003373 case TypeCheckKind::kExactCheck:
3374 case TypeCheckKind::kAbstractClassCheck:
3375 case TypeCheckKind::kClassHierarchyCheck:
3376 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003377 call_kind =
3378 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003379 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003380 break;
3381 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003382 case TypeCheckKind::kUnresolvedCheck:
3383 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003384 call_kind = LocationSummary::kCallOnSlowPath;
3385 break;
3386 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003387
Alexandre Rames67555f72014-11-18 10:55:16 +00003388 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003389 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003390 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003391 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003392 locations->SetInAt(0, Location::RequiresRegister());
3393 locations->SetInAt(1, Location::RequiresRegister());
3394 // The "out" register is used as a temporary, so it overlaps with the inputs.
3395 // Note that TypeCheckSlowPathARM64 uses this register too.
3396 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003397 // Add temps if necessary for read barriers.
3398 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003399}
3400
3401void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003402 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003403 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003404 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003405 Register obj = InputRegisterAt(instruction, 0);
3406 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003407 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003408 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003409 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3410 DCHECK_LE(num_temps, 1u);
3411 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003412 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3413 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3414 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3415 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003416
Scott Wakeling97c72b72016-06-24 16:19:36 +01003417 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003418 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003419
3420 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003421 // Avoid null check if we know `obj` is not null.
3422 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003423 __ Cbz(obj, &zero);
3424 }
3425
Roland Levillain44015862016-01-22 11:47:17 +00003426 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003427 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003428 // /* HeapReference<Class> */ out = obj->klass_
3429 GenerateReferenceLoadTwoRegisters(instruction,
3430 out_loc,
3431 obj_loc,
3432 class_offset,
3433 maybe_temp_loc,
3434 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003435 __ Cmp(out, cls);
3436 __ Cset(out, eq);
3437 if (zero.IsLinked()) {
3438 __ B(&done);
3439 }
3440 break;
3441 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003442
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003443 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003444 // /* HeapReference<Class> */ out = obj->klass_
3445 GenerateReferenceLoadTwoRegisters(instruction,
3446 out_loc,
3447 obj_loc,
3448 class_offset,
3449 maybe_temp_loc,
3450 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003451 // If the class is abstract, we eagerly fetch the super class of the
3452 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003453 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003454 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003455 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003456 GenerateReferenceLoadOneRegister(instruction,
3457 out_loc,
3458 super_offset,
3459 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003460 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003461 // If `out` is null, we use it for the result, and jump to `done`.
3462 __ Cbz(out, &done);
3463 __ Cmp(out, cls);
3464 __ B(ne, &loop);
3465 __ Mov(out, 1);
3466 if (zero.IsLinked()) {
3467 __ B(&done);
3468 }
3469 break;
3470 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003471
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003472 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003473 // /* HeapReference<Class> */ out = obj->klass_
3474 GenerateReferenceLoadTwoRegisters(instruction,
3475 out_loc,
3476 obj_loc,
3477 class_offset,
3478 maybe_temp_loc,
3479 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003480 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003481 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003482 __ Bind(&loop);
3483 __ Cmp(out, cls);
3484 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003485 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003486 GenerateReferenceLoadOneRegister(instruction,
3487 out_loc,
3488 super_offset,
3489 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003490 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003491 __ Cbnz(out, &loop);
3492 // If `out` is null, we use it for the result, and jump to `done`.
3493 __ B(&done);
3494 __ Bind(&success);
3495 __ Mov(out, 1);
3496 if (zero.IsLinked()) {
3497 __ B(&done);
3498 }
3499 break;
3500 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003501
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003502 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003503 // /* HeapReference<Class> */ out = obj->klass_
3504 GenerateReferenceLoadTwoRegisters(instruction,
3505 out_loc,
3506 obj_loc,
3507 class_offset,
3508 maybe_temp_loc,
3509 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003510 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003511 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003512 __ Cmp(out, cls);
3513 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003514 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003515 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003516 GenerateReferenceLoadOneRegister(instruction,
3517 out_loc,
3518 component_offset,
3519 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003520 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003521 // If `out` is null, we use it for the result, and jump to `done`.
3522 __ Cbz(out, &done);
3523 __ Ldrh(out, HeapOperand(out, primitive_offset));
3524 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3525 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003526 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003527 __ Mov(out, 1);
3528 __ B(&done);
3529 break;
3530 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003531
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003532 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003533 // No read barrier since the slow path will retry upon failure.
3534 // /* HeapReference<Class> */ out = obj->klass_
3535 GenerateReferenceLoadTwoRegisters(instruction,
3536 out_loc,
3537 obj_loc,
3538 class_offset,
3539 maybe_temp_loc,
3540 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003541 __ Cmp(out, cls);
3542 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003543 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3544 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003545 codegen_->AddSlowPath(slow_path);
3546 __ B(ne, slow_path->GetEntryLabel());
3547 __ Mov(out, 1);
3548 if (zero.IsLinked()) {
3549 __ B(&done);
3550 }
3551 break;
3552 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003553
Calin Juravle98893e12015-10-02 21:05:03 +01003554 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003555 case TypeCheckKind::kInterfaceCheck: {
3556 // Note that we indeed only call on slow path, but we always go
3557 // into the slow path for the unresolved and interface check
3558 // cases.
3559 //
3560 // We cannot directly call the InstanceofNonTrivial runtime
3561 // entry point without resorting to a type checking slow path
3562 // here (i.e. by calling InvokeRuntime directly), as it would
3563 // require to assign fixed registers for the inputs of this
3564 // HInstanceOf instruction (following the runtime calling
3565 // convention), which might be cluttered by the potential first
3566 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003567 //
3568 // TODO: Introduce a new runtime entry point taking the object
3569 // to test (instead of its class) as argument, and let it deal
3570 // with the read barrier issues. This will let us refactor this
3571 // case of the `switch` code as it was previously (with a direct
3572 // call to the runtime not using a type checking slow path).
3573 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003574 DCHECK(locations->OnlyCallsOnSlowPath());
3575 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3576 /* is_fatal */ false);
3577 codegen_->AddSlowPath(slow_path);
3578 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003579 if (zero.IsLinked()) {
3580 __ B(&done);
3581 }
3582 break;
3583 }
3584 }
3585
3586 if (zero.IsLinked()) {
3587 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003588 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003589 }
3590
3591 if (done.IsLinked()) {
3592 __ Bind(&done);
3593 }
3594
3595 if (slow_path != nullptr) {
3596 __ Bind(slow_path->GetExitLabel());
3597 }
3598}
3599
3600void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3601 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3602 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3603
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003604 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3605 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003606 case TypeCheckKind::kExactCheck:
3607 case TypeCheckKind::kAbstractClassCheck:
3608 case TypeCheckKind::kClassHierarchyCheck:
3609 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003610 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3611 LocationSummary::kCallOnSlowPath :
3612 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003613 break;
3614 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003615 case TypeCheckKind::kUnresolvedCheck:
3616 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003617 call_kind = LocationSummary::kCallOnSlowPath;
3618 break;
3619 }
3620
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003621 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3622 locations->SetInAt(0, Location::RequiresRegister());
3623 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003624 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3625 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003626}
3627
3628void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003629 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003630 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003631 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003632 Register obj = InputRegisterAt(instruction, 0);
3633 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003634 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3635 DCHECK_GE(num_temps, 1u);
3636 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003637 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003638 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3639 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003640 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003641 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3642 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3643 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3644 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3645 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3646 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3647 const uint32_t object_array_data_offset =
3648 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003649
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003650 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003651 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3652 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3653 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003654 if (!kEmitCompilerReadBarrier) {
3655 is_type_check_slow_path_fatal =
3656 (type_check_kind == TypeCheckKind::kExactCheck ||
3657 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3658 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3659 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3660 !instruction->CanThrowIntoCatchBlock();
3661 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003662 SlowPathCodeARM64* type_check_slow_path =
3663 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3664 is_type_check_slow_path_fatal);
3665 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003666
Scott Wakeling97c72b72016-06-24 16:19:36 +01003667 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003668 // Avoid null check if we know obj is not null.
3669 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003670 __ Cbz(obj, &done);
3671 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003672
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003673 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003674 case TypeCheckKind::kExactCheck:
3675 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003676 // /* HeapReference<Class> */ temp = obj->klass_
3677 GenerateReferenceLoadTwoRegisters(instruction,
3678 temp_loc,
3679 obj_loc,
3680 class_offset,
3681 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003682 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003683
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003684 __ Cmp(temp, cls);
3685 // Jump to slow path for throwing the exception or doing a
3686 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003687 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003688 break;
3689 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003690
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003691 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003692 // /* HeapReference<Class> */ temp = obj->klass_
3693 GenerateReferenceLoadTwoRegisters(instruction,
3694 temp_loc,
3695 obj_loc,
3696 class_offset,
3697 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003698 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003699
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003700 // If the class is abstract, we eagerly fetch the super class of the
3701 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003702 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003703 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003704 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003705 GenerateReferenceLoadOneRegister(instruction,
3706 temp_loc,
3707 super_offset,
3708 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003709 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003710
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003711 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3712 // exception.
3713 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3714 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003715 __ Cmp(temp, cls);
3716 __ B(ne, &loop);
3717 break;
3718 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003719
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003720 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003721 // /* HeapReference<Class> */ temp = obj->klass_
3722 GenerateReferenceLoadTwoRegisters(instruction,
3723 temp_loc,
3724 obj_loc,
3725 class_offset,
3726 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003727 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003728
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003729 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003730 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003731 __ Bind(&loop);
3732 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003733 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003734
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003735 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003736 GenerateReferenceLoadOneRegister(instruction,
3737 temp_loc,
3738 super_offset,
3739 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003740 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003741
3742 // If the class reference currently in `temp` is not null, jump
3743 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003744 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003745 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003746 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003747 break;
3748 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003749
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003750 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003751 // /* HeapReference<Class> */ temp = obj->klass_
3752 GenerateReferenceLoadTwoRegisters(instruction,
3753 temp_loc,
3754 obj_loc,
3755 class_offset,
3756 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003757 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003758
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003759 // Do an exact check.
3760 __ Cmp(temp, cls);
3761 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003762
3763 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003764 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003765 GenerateReferenceLoadOneRegister(instruction,
3766 temp_loc,
3767 component_offset,
3768 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003769 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003770
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003771 // If the component type is null, jump to the slow path to throw the exception.
3772 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3773 // Otherwise, the object is indeed an array. Further check that this component type is not a
3774 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003775 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3776 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003777 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003778 break;
3779 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003780
Calin Juravle98893e12015-10-02 21:05:03 +01003781 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003782 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003783 //
3784 // We cannot directly call the CheckCast runtime entry point
3785 // without resorting to a type checking slow path here (i.e. by
3786 // calling InvokeRuntime directly), as it would require to
3787 // assign fixed registers for the inputs of this HInstanceOf
3788 // instruction (following the runtime calling convention), which
3789 // might be cluttered by the potential first read barrier
3790 // emission at the beginning of this method.
3791 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003792 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003793 case TypeCheckKind::kInterfaceCheck: {
3794 // /* HeapReference<Class> */ temp = obj->klass_
3795 GenerateReferenceLoadTwoRegisters(instruction,
3796 temp_loc,
3797 obj_loc,
3798 class_offset,
3799 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003800 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003801
3802 // /* HeapReference<Class> */ temp = temp->iftable_
3803 GenerateReferenceLoadTwoRegisters(instruction,
3804 temp_loc,
3805 temp_loc,
3806 iftable_offset,
3807 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003808 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003809 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003810 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08003811 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003812 vixl::aarch64::Label start_loop;
3813 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003814 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003815 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3816 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003817 // Go to next interface.
3818 __ Add(temp, temp, 2 * kHeapReferenceSize);
3819 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003820 // Compare the classes and continue the loop if they do not match.
3821 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
3822 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003823 break;
3824 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003825 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003826 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003827
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003828 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003829}
3830
Alexandre Rames5319def2014-10-23 10:03:10 +01003831void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3832 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3833 locations->SetOut(Location::ConstantLocation(constant));
3834}
3835
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003836void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003837 // Will be generated at use site.
3838}
3839
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003840void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3841 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3842 locations->SetOut(Location::ConstantLocation(constant));
3843}
3844
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003845void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003846 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003847}
3848
Calin Juravle175dc732015-08-25 15:42:32 +01003849void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3850 // The trampoline uses the same calling convention as dex calling conventions,
3851 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3852 // the method_idx.
3853 HandleInvoke(invoke);
3854}
3855
3856void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3857 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3858}
3859
Alexandre Rames5319def2014-10-23 10:03:10 +01003860void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003861 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003862 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003863}
3864
Alexandre Rames67555f72014-11-18 10:55:16 +00003865void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3866 HandleInvoke(invoke);
3867}
3868
3869void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3870 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003871 LocationSummary* locations = invoke->GetLocations();
3872 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003873 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003874 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003875 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003876
3877 // The register ip1 is required to be used for the hidden argument in
3878 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003879 MacroAssembler* masm = GetVIXLAssembler();
3880 UseScratchRegisterScope scratch_scope(masm);
3881 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003882 scratch_scope.Exclude(ip1);
3883 __ Mov(ip1, invoke->GetDexMethodIndex());
3884
Alexandre Rames67555f72014-11-18 10:55:16 +00003885 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003886 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003887 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003888 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003889 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003890 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003891 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003892 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003893 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003894 // Instead of simply (possibly) unpoisoning `temp` here, we should
3895 // emit a read barrier for the previous class reference load.
3896 // However this is not required in practice, as this is an
3897 // intermediate/temporary reference and because the current
3898 // concurrent copying collector keeps the from-space memory
3899 // intact/accessible until the end of the marking phase (the
3900 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003901 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003902 __ Ldr(temp,
3903 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3904 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003905 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003906 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003907 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003908 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003909 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003910 // lr();
3911 __ Blr(lr);
3912 DCHECK(!codegen_->IsLeafMethod());
3913 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3914}
3915
3916void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003917 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3918 if (intrinsic.TryDispatch(invoke)) {
3919 return;
3920 }
3921
Alexandre Rames67555f72014-11-18 10:55:16 +00003922 HandleInvoke(invoke);
3923}
3924
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003925void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003926 // Explicit clinit checks triggered by static invokes must have been pruned by
3927 // art::PrepareForRegisterAllocation.
3928 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003929
Andreas Gampe878d58c2015-01-15 23:24:00 -08003930 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3931 if (intrinsic.TryDispatch(invoke)) {
3932 return;
3933 }
3934
Alexandre Rames67555f72014-11-18 10:55:16 +00003935 HandleInvoke(invoke);
3936}
3937
Andreas Gampe878d58c2015-01-15 23:24:00 -08003938static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3939 if (invoke->GetLocations()->Intrinsified()) {
3940 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3941 intrinsic.Dispatch(invoke);
3942 return true;
3943 }
3944 return false;
3945}
3946
Vladimir Markodc151b22015-10-15 18:02:30 +01003947HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3948 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003949 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003950 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003951 return desired_dispatch_info;
3952}
3953
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003954void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003955 // For better instruction scheduling we load the direct code pointer before the method pointer.
3956 bool direct_code_loaded = false;
3957 switch (invoke->GetCodePtrLocation()) {
3958 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3959 // LR = code address from literal pool with link-time patch.
3960 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3961 direct_code_loaded = true;
3962 break;
3963 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3964 // LR = invoke->GetDirectCodePtr();
3965 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3966 direct_code_loaded = true;
3967 break;
3968 default:
3969 break;
3970 }
3971
Andreas Gampe878d58c2015-01-15 23:24:00 -08003972 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003973 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3974 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003975 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
3976 uint32_t offset =
3977 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003978 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003979 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00003980 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003981 }
Vladimir Marko58155012015-08-19 12:49:41 +00003982 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003983 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003984 break;
3985 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3986 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003987 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003988 break;
3989 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3990 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003991 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003992 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3993 break;
3994 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3995 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003996 const DexFile& dex_file = invoke->GetDexFile();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003997 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003998 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003999 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004000 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004001 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004002 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004003 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004004 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004005 }
Vladimir Marko58155012015-08-19 12:49:41 +00004006 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004007 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004008 Register reg = XRegisterFrom(temp);
4009 Register method_reg;
4010 if (current_method.IsRegister()) {
4011 method_reg = XRegisterFrom(current_method);
4012 } else {
4013 DCHECK(invoke->GetLocations()->Intrinsified());
4014 DCHECK(!current_method.IsValid());
4015 method_reg = reg;
4016 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4017 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004018
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004019 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004020 __ Ldr(reg.X(),
4021 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004022 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004023 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004024 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4025 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004026 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4027 break;
4028 }
4029 }
4030
4031 switch (invoke->GetCodePtrLocation()) {
4032 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4033 __ Bl(&frame_entry_label_);
4034 break;
4035 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004036 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4037 invoke->GetTargetMethod().dex_method_index);
Scott Wakeling97c72b72016-06-24 16:19:36 +01004038 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
4039 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00004040 __ Bind(label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004041 // Branch and link to itself. This will be overriden at link time.
4042 __ bl(static_cast<int64_t>(0));
Vladimir Marko58155012015-08-19 12:49:41 +00004043 break;
4044 }
4045 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4046 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4047 // LR prepared above for better instruction scheduling.
4048 DCHECK(direct_code_loaded);
4049 // lr()
4050 __ Blr(lr);
4051 break;
4052 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4053 // LR = callee_method->entry_point_from_quick_compiled_code_;
4054 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004055 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004056 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004057 // lr()
4058 __ Blr(lr);
4059 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004060 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004061
Andreas Gampe878d58c2015-01-15 23:24:00 -08004062 DCHECK(!IsLeafMethod());
4063}
4064
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004065void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004066 // Use the calling convention instead of the location of the receiver, as
4067 // intrinsics may have put the receiver in a different register. In the intrinsics
4068 // slow path, the arguments have been moved to the right place, so here we are
4069 // guaranteed that the receiver is the first register of the calling convention.
4070 InvokeDexCallingConvention calling_convention;
4071 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004072 Register temp = XRegisterFrom(temp_in);
4073 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4074 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4075 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004076 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004077
4078 BlockPoolsScope block_pools(GetVIXLAssembler());
4079
4080 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004081 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004082 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004083 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004084 // Instead of simply (possibly) unpoisoning `temp` here, we should
4085 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004086 // intermediate/temporary reference and because the current
4087 // concurrent copying collector keeps the from-space memory
4088 // intact/accessible until the end of the marking phase (the
4089 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004090 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4091 // temp = temp->GetMethodAt(method_offset);
4092 __ Ldr(temp, MemOperand(temp, method_offset));
4093 // lr = temp->GetEntryPoint();
4094 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
4095 // lr();
4096 __ Blr(lr);
4097}
4098
Scott Wakeling97c72b72016-06-24 16:19:36 +01004099vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4100 const DexFile& dex_file,
4101 uint32_t string_index,
4102 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004103 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
4104}
4105
Scott Wakeling97c72b72016-06-24 16:19:36 +01004106vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4107 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004108 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004109 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004110 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004111}
4112
Scott Wakeling97c72b72016-06-24 16:19:36 +01004113vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4114 const DexFile& dex_file,
4115 uint32_t element_offset,
4116 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004117 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4118}
4119
Scott Wakeling97c72b72016-06-24 16:19:36 +01004120vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4121 const DexFile& dex_file,
4122 uint32_t offset_or_index,
4123 vixl::aarch64::Label* adrp_label,
4124 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004125 // Add a patch entry and return the label.
4126 patches->emplace_back(dex_file, offset_or_index);
4127 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004128 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004129 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4130 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4131 return label;
4132}
4133
Scott Wakeling97c72b72016-06-24 16:19:36 +01004134vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004135 const DexFile& dex_file, uint32_t string_index) {
4136 return boot_image_string_patches_.GetOrCreate(
4137 StringReference(&dex_file, string_index),
4138 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4139}
4140
Scott Wakeling97c72b72016-06-24 16:19:36 +01004141vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004142 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004143 return boot_image_type_patches_.GetOrCreate(
4144 TypeReference(&dex_file, type_index),
4145 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4146}
4147
Scott Wakeling97c72b72016-06-24 16:19:36 +01004148vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4149 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004150 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4151 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4152 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4153}
4154
Scott Wakeling97c72b72016-06-24 16:19:36 +01004155vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
4156 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004157 return DeduplicateUint64Literal(address);
4158}
4159
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004160vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
4161 const DexFile& dex_file, uint32_t string_index) {
4162 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index), /* placeholder */ 0u);
4163 return jit_string_patches_.GetOrCreate(
4164 StringReference(&dex_file, string_index),
4165 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4166}
4167
Vladimir Markoaad75c62016-10-03 08:46:48 +00004168void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4169 vixl::aarch64::Register reg) {
4170 DCHECK(reg.IsX());
4171 SingleEmissionCheckScope guard(GetVIXLAssembler());
4172 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004173 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004174}
4175
4176void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4177 vixl::aarch64::Register out,
4178 vixl::aarch64::Register base) {
4179 DCHECK(out.IsX());
4180 DCHECK(base.IsX());
4181 SingleEmissionCheckScope guard(GetVIXLAssembler());
4182 __ Bind(fixup_label);
4183 __ add(out, base, Operand(/* offset placeholder */ 0));
4184}
4185
4186void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4187 vixl::aarch64::Register out,
4188 vixl::aarch64::Register base) {
4189 DCHECK(base.IsX());
4190 SingleEmissionCheckScope guard(GetVIXLAssembler());
4191 __ Bind(fixup_label);
4192 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4193}
4194
4195template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4196inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4197 const ArenaDeque<PcRelativePatchInfo>& infos,
4198 ArenaVector<LinkerPatch>* linker_patches) {
4199 for (const PcRelativePatchInfo& info : infos) {
4200 linker_patches->push_back(Factory(info.label.GetLocation(),
4201 &info.target_dex_file,
4202 info.pc_insn_label->GetLocation(),
4203 info.offset_or_index));
4204 }
4205}
4206
Vladimir Marko58155012015-08-19 12:49:41 +00004207void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4208 DCHECK(linker_patches->empty());
4209 size_t size =
4210 method_patches_.size() +
4211 call_patches_.size() +
4212 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004213 pc_relative_dex_cache_patches_.size() +
4214 boot_image_string_patches_.size() +
4215 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004216 boot_image_type_patches_.size() +
4217 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004218 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004219 linker_patches->reserve(size);
4220 for (const auto& entry : method_patches_) {
4221 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004222 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
4223 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00004224 target_method.dex_file,
4225 target_method.dex_method_index));
4226 }
4227 for (const auto& entry : call_patches_) {
4228 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004229 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
4230 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00004231 target_method.dex_file,
4232 target_method.dex_method_index));
4233 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004234 for (const PatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
4235 linker_patches->push_back(
4236 LinkerPatch::RelativeCodePatch(info.label.GetLocation(), &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004237 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004238 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004239 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004240 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004241 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004242 info.offset_or_index));
4243 }
4244 for (const auto& entry : boot_image_string_patches_) {
4245 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004246 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4247 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004248 target_string.dex_file,
4249 target_string.string_index));
4250 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004251 if (!GetCompilerOptions().IsBootImage()) {
4252 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4253 linker_patches);
4254 } else {
4255 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4256 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004257 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004258 for (const auto& entry : boot_image_type_patches_) {
4259 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004260 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4261 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004262 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004263 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004264 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004265 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4266 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004267 for (const auto& entry : boot_image_address_patches_) {
4268 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004269 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4270 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004271 }
4272}
4273
Scott Wakeling97c72b72016-06-24 16:19:36 +01004274vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004275 Uint32ToLiteralMap* map) {
4276 return map->GetOrCreate(
4277 value,
4278 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4279}
4280
Scott Wakeling97c72b72016-06-24 16:19:36 +01004281vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004282 return uint64_literals_.GetOrCreate(
4283 value,
4284 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004285}
4286
Scott Wakeling97c72b72016-06-24 16:19:36 +01004287vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004288 MethodReference target_method,
4289 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004290 return map->GetOrCreate(
4291 target_method,
4292 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004293}
4294
Scott Wakeling97c72b72016-06-24 16:19:36 +01004295vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004296 MethodReference target_method) {
4297 return DeduplicateMethodLiteral(target_method, &method_patches_);
4298}
4299
Scott Wakeling97c72b72016-06-24 16:19:36 +01004300vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004301 MethodReference target_method) {
4302 return DeduplicateMethodLiteral(target_method, &call_patches_);
4303}
4304
4305
Andreas Gampe878d58c2015-01-15 23:24:00 -08004306void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004307 // Explicit clinit checks triggered by static invokes must have been pruned by
4308 // art::PrepareForRegisterAllocation.
4309 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004310
Andreas Gampe878d58c2015-01-15 23:24:00 -08004311 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4312 return;
4313 }
4314
Alexandre Ramesd921d642015-04-16 15:07:16 +01004315 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004316 LocationSummary* locations = invoke->GetLocations();
4317 codegen_->GenerateStaticOrDirectCall(
4318 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004319 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004320}
4321
4322void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004323 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4324 return;
4325 }
4326
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004327 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004328 DCHECK(!codegen_->IsLeafMethod());
4329 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4330}
4331
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004332HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4333 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004334 switch (desired_class_load_kind) {
4335 case HLoadClass::LoadKind::kReferrersClass:
4336 break;
4337 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4338 DCHECK(!GetCompilerOptions().GetCompilePic());
4339 break;
4340 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4341 DCHECK(GetCompilerOptions().GetCompilePic());
4342 break;
4343 case HLoadClass::LoadKind::kBootImageAddress:
4344 break;
4345 case HLoadClass::LoadKind::kDexCacheAddress:
4346 DCHECK(Runtime::Current()->UseJitCompilation());
4347 break;
4348 case HLoadClass::LoadKind::kDexCachePcRelative:
4349 DCHECK(!Runtime::Current()->UseJitCompilation());
4350 break;
4351 case HLoadClass::LoadKind::kDexCacheViaMethod:
4352 break;
4353 }
4354 return desired_class_load_kind;
4355}
4356
Alexandre Rames67555f72014-11-18 10:55:16 +00004357void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004358 if (cls->NeedsAccessCheck()) {
4359 InvokeRuntimeCallingConvention calling_convention;
4360 CodeGenerator::CreateLoadClassLocationSummary(
4361 cls,
4362 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01004363 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004364 /* code_generator_supports_read_barrier */ true);
4365 return;
4366 }
4367
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004368 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4369 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004370 ? LocationSummary::kCallOnSlowPath
4371 : LocationSummary::kNoCall;
4372 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004373 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004374 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004375 }
4376
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004377 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4378 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4379 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4380 locations->SetInAt(0, Location::RequiresRegister());
4381 }
4382 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004383}
4384
4385void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004386 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004387 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004388 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004389 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004390 return;
4391 }
4392
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004393 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004394 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004395
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004396 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4397 ? kWithoutReadBarrier
4398 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004399 bool generate_null_check = false;
4400 switch (cls->GetLoadKind()) {
4401 case HLoadClass::LoadKind::kReferrersClass: {
4402 DCHECK(!cls->CanCallRuntime());
4403 DCHECK(!cls->MustGenerateClinitCheck());
4404 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4405 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004406 GenerateGcRootFieldLoad(cls,
4407 out_loc,
4408 current_method,
4409 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004410 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004411 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004412 break;
4413 }
4414 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004415 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004416 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4417 cls->GetTypeIndex()));
4418 break;
4419 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004420 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004421 // Add ADRP with its PC-relative type patch.
4422 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004423 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004424 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004425 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004426 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004427 vixl::aarch64::Label* add_label =
4428 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004429 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004430 break;
4431 }
4432 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004433 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004434 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4435 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4436 break;
4437 }
4438 case HLoadClass::LoadKind::kDexCacheAddress: {
4439 DCHECK_NE(cls->GetAddress(), 0u);
4440 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4441 // that gives a 16KiB range. To try and reduce the number of literals if we load
4442 // multiple types, simply split the dex cache address to a 16KiB aligned base
4443 // loaded from a literal and the remaining offset embedded in the load.
4444 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4445 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4446 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4447 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4448 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4449 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4450 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004451 GenerateGcRootFieldLoad(cls,
4452 out_loc,
4453 out.X(),
4454 offset,
Roland Levillain00468f32016-10-27 18:02:48 +01004455 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004456 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004457 generate_null_check = !cls->IsInDexCache();
4458 break;
4459 }
4460 case HLoadClass::LoadKind::kDexCachePcRelative: {
4461 // Add ADRP with its PC-relative DexCache access patch.
4462 const DexFile& dex_file = cls->GetDexFile();
4463 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004464 vixl::aarch64::Label* adrp_label =
4465 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004466 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004467 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004468 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004469 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4470 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004471 GenerateGcRootFieldLoad(cls,
4472 out_loc,
4473 out.X(),
4474 /* offset placeholder */ 0,
4475 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004476 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004477 generate_null_check = !cls->IsInDexCache();
4478 break;
4479 }
4480 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4481 MemberOffset resolved_types_offset =
4482 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4483 // /* GcRoot<mirror::Class>[] */ out =
4484 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4485 Register current_method = InputRegisterAt(cls, 0);
4486 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4487 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004488 GenerateGcRootFieldLoad(cls,
4489 out_loc,
4490 out.X(),
Andreas Gampea5b09a62016-11-17 15:21:22 -08004491 CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_),
Roland Levillain00468f32016-10-27 18:02:48 +01004492 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004493 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004494 generate_null_check = !cls->IsInDexCache();
4495 break;
4496 }
4497 }
4498
4499 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4500 DCHECK(cls->CanCallRuntime());
4501 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4502 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4503 codegen_->AddSlowPath(slow_path);
4504 if (generate_null_check) {
4505 __ Cbz(out, slow_path->GetEntryLabel());
4506 }
4507 if (cls->MustGenerateClinitCheck()) {
4508 GenerateClassInitializationCheck(slow_path, out);
4509 } else {
4510 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004511 }
4512 }
4513}
4514
David Brazdilcb1c0552015-08-04 16:22:25 +01004515static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004516 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004517}
4518
Alexandre Rames67555f72014-11-18 10:55:16 +00004519void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4520 LocationSummary* locations =
4521 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4522 locations->SetOut(Location::RequiresRegister());
4523}
4524
4525void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004526 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4527}
4528
4529void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4530 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4531}
4532
4533void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4534 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004535}
4536
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004537HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4538 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004539 switch (desired_string_load_kind) {
4540 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4541 DCHECK(!GetCompilerOptions().GetCompilePic());
4542 break;
4543 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4544 DCHECK(GetCompilerOptions().GetCompilePic());
4545 break;
4546 case HLoadString::LoadKind::kBootImageAddress:
4547 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004548 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004549 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004550 break;
4551 case HLoadString::LoadKind::kDexCacheViaMethod:
4552 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004553 case HLoadString::LoadKind::kJitTableAddress:
4554 DCHECK(Runtime::Current()->UseJitCompilation());
4555 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004556 }
4557 return desired_string_load_kind;
4558}
4559
Alexandre Rames67555f72014-11-18 10:55:16 +00004560void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004561 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004562 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004563 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004564 InvokeRuntimeCallingConvention calling_convention;
4565 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4566 } else {
4567 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004568 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4569 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4570 // Rely on the pResolveString and/or marking to save everything, including temps.
4571 RegisterSet caller_saves = RegisterSet::Empty();
4572 InvokeRuntimeCallingConvention calling_convention;
4573 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4574 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4575 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4576 Primitive::kPrimNot).GetCode());
4577 locations->SetCustomSlowPathCallerSaves(caller_saves);
4578 } else {
4579 // For non-Baker read barrier we have a temp-clobbering call.
4580 }
4581 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004582 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004583}
4584
4585void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004586 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004587 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004588
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004589 switch (load->GetLoadKind()) {
4590 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004591 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4592 load->GetStringIndex()));
4593 return; // No dex cache slow path.
4594 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004595 // Add ADRP with its PC-relative String patch.
4596 const DexFile& dex_file = load->GetDexFile();
4597 uint32_t string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004598 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004599 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004600 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004601 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004602 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004603 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004604 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004605 return; // No dex cache slow path.
4606 }
4607 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004608 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4609 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4610 return; // No dex cache slow path.
4611 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004612 case HLoadString::LoadKind::kBssEntry: {
4613 // Add ADRP with its PC-relative String .bss entry patch.
4614 const DexFile& dex_file = load->GetDexFile();
4615 uint32_t string_index = load->GetStringIndex();
4616 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004617 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4618 Register temp = temps.AcquireX();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004619 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004620 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004621 // Add LDR with its PC-relative String patch.
4622 vixl::aarch64::Label* ldr_label =
4623 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004624 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00004625 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004626 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004627 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01004628 /* offset placeholder */ 0u,
4629 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004630 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004631 SlowPathCodeARM64* slow_path =
4632 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004633 codegen_->AddSlowPath(slow_path);
4634 __ Cbz(out.X(), slow_path->GetEntryLabel());
4635 __ Bind(slow_path->GetExitLabel());
4636 return;
4637 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004638 case HLoadString::LoadKind::kJitTableAddress: {
4639 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
4640 load->GetStringIndex()));
4641 GenerateGcRootFieldLoad(load,
4642 out_loc,
4643 out.X(),
4644 /* offset */ 0,
4645 /* fixup_label */ nullptr,
4646 kCompilerReadBarrierOption);
4647 return;
4648 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004649 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004650 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004651 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004652
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004653 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004654 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004655 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004656 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex());
4657 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4658 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004659}
4660
Alexandre Rames5319def2014-10-23 10:03:10 +01004661void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4662 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4663 locations->SetOut(Location::ConstantLocation(constant));
4664}
4665
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004666void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004667 // Will be generated at use site.
4668}
4669
Alexandre Rames67555f72014-11-18 10:55:16 +00004670void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4671 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004672 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004673 InvokeRuntimeCallingConvention calling_convention;
4674 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4675}
4676
4677void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004678 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4679 instruction,
4680 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004681 if (instruction->IsEnter()) {
4682 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4683 } else {
4684 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4685 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004686}
4687
Alexandre Rames42d641b2014-10-27 14:00:51 +00004688void LocationsBuilderARM64::VisitMul(HMul* mul) {
4689 LocationSummary* locations =
4690 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4691 switch (mul->GetResultType()) {
4692 case Primitive::kPrimInt:
4693 case Primitive::kPrimLong:
4694 locations->SetInAt(0, Location::RequiresRegister());
4695 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004696 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004697 break;
4698
4699 case Primitive::kPrimFloat:
4700 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004701 locations->SetInAt(0, Location::RequiresFpuRegister());
4702 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004703 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004704 break;
4705
4706 default:
4707 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4708 }
4709}
4710
4711void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4712 switch (mul->GetResultType()) {
4713 case Primitive::kPrimInt:
4714 case Primitive::kPrimLong:
4715 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4716 break;
4717
4718 case Primitive::kPrimFloat:
4719 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004720 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004721 break;
4722
4723 default:
4724 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4725 }
4726}
4727
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004728void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4729 LocationSummary* locations =
4730 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4731 switch (neg->GetResultType()) {
4732 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004733 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004734 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004735 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004736 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004737
4738 case Primitive::kPrimFloat:
4739 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004740 locations->SetInAt(0, Location::RequiresFpuRegister());
4741 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004742 break;
4743
4744 default:
4745 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4746 }
4747}
4748
4749void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4750 switch (neg->GetResultType()) {
4751 case Primitive::kPrimInt:
4752 case Primitive::kPrimLong:
4753 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4754 break;
4755
4756 case Primitive::kPrimFloat:
4757 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004758 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004759 break;
4760
4761 default:
4762 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4763 }
4764}
4765
4766void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4767 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004768 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004769 InvokeRuntimeCallingConvention calling_convention;
4770 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004771 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004772 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004773 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004774}
4775
4776void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4777 LocationSummary* locations = instruction->GetLocations();
4778 InvokeRuntimeCallingConvention calling_convention;
4779 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4780 DCHECK(type_index.Is(w0));
Andreas Gampea5b09a62016-11-17 15:21:22 -08004781 __ Mov(type_index, instruction->GetTypeIndex().index_);
Roland Levillain4d027112015-07-01 15:41:14 +01004782 // Note: if heap poisoning is enabled, the entry point takes cares
4783 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004784 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004785 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004786}
4787
Alexandre Rames5319def2014-10-23 10:03:10 +01004788void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4789 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004790 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004791 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004792 if (instruction->IsStringAlloc()) {
4793 locations->AddTemp(LocationFrom(kArtMethodRegister));
4794 } else {
4795 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4796 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4797 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004798 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4799}
4800
4801void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004802 // Note: if heap poisoning is enabled, the entry point takes cares
4803 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004804 if (instruction->IsStringAlloc()) {
4805 // String is allocated through StringFactory. Call NewEmptyString entry point.
4806 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004807 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004808 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4809 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4810 __ Blr(lr);
4811 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4812 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004813 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004814 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4815 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004816}
4817
4818void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4819 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004820 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004821 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004822}
4823
4824void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004825 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004826 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004827 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004828 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004829 break;
4830
4831 default:
4832 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4833 }
4834}
4835
David Brazdil66d126e2015-04-03 16:02:44 +01004836void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4837 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4838 locations->SetInAt(0, Location::RequiresRegister());
4839 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4840}
4841
4842void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004843 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004844}
4845
Alexandre Rames5319def2014-10-23 10:03:10 +01004846void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004847 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4848 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004849}
4850
Calin Juravle2ae48182016-03-16 14:05:09 +00004851void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4852 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004853 return;
4854 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004855
Alexandre Ramesd921d642015-04-16 15:07:16 +01004856 BlockPoolsScope block_pools(GetVIXLAssembler());
4857 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004858 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004859 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004860}
4861
Calin Juravle2ae48182016-03-16 14:05:09 +00004862void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004863 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004864 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004865
4866 LocationSummary* locations = instruction->GetLocations();
4867 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004868
4869 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004870}
4871
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004872void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004873 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004874}
4875
Alexandre Rames67555f72014-11-18 10:55:16 +00004876void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4877 HandleBinaryOp(instruction);
4878}
4879
4880void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4881 HandleBinaryOp(instruction);
4882}
4883
Alexandre Rames3e69f162014-12-10 10:36:50 +00004884void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4885 LOG(FATAL) << "Unreachable";
4886}
4887
4888void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4889 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4890}
4891
Alexandre Rames5319def2014-10-23 10:03:10 +01004892void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4893 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4894 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4895 if (location.IsStackSlot()) {
4896 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4897 } else if (location.IsDoubleStackSlot()) {
4898 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4899 }
4900 locations->SetOut(location);
4901}
4902
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004903void InstructionCodeGeneratorARM64::VisitParameterValue(
4904 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004905 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004906}
4907
4908void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4909 LocationSummary* locations =
4910 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004911 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004912}
4913
4914void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4915 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4916 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004917}
4918
4919void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4920 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004921 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004922 locations->SetInAt(i, Location::Any());
4923 }
4924 locations->SetOut(Location::Any());
4925}
4926
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004927void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004928 LOG(FATAL) << "Unreachable";
4929}
4930
Serban Constantinescu02164b32014-11-13 14:05:07 +00004931void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004932 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004933 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004934 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4935 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004936 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4937
4938 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004939 case Primitive::kPrimInt:
4940 case Primitive::kPrimLong:
4941 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004942 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004943 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4944 break;
4945
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004946 case Primitive::kPrimFloat:
4947 case Primitive::kPrimDouble: {
4948 InvokeRuntimeCallingConvention calling_convention;
4949 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4950 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4951 locations->SetOut(calling_convention.GetReturnLocation(type));
4952
4953 break;
4954 }
4955
Serban Constantinescu02164b32014-11-13 14:05:07 +00004956 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004957 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004958 }
4959}
4960
4961void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4962 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004963
Serban Constantinescu02164b32014-11-13 14:05:07 +00004964 switch (type) {
4965 case Primitive::kPrimInt:
4966 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004967 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004968 break;
4969 }
4970
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004971 case Primitive::kPrimFloat:
4972 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004973 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4974 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004975 if (type == Primitive::kPrimFloat) {
4976 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4977 } else {
4978 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4979 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004980 break;
4981 }
4982
Serban Constantinescu02164b32014-11-13 14:05:07 +00004983 default:
4984 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004985 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004986 }
4987}
4988
Calin Juravle27df7582015-04-17 19:12:31 +01004989void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4990 memory_barrier->SetLocations(nullptr);
4991}
4992
4993void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004994 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004995}
4996
Alexandre Rames5319def2014-10-23 10:03:10 +01004997void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4998 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4999 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005000 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005001}
5002
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005003void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005004 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005005}
5006
5007void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5008 instruction->SetLocations(nullptr);
5009}
5010
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005011void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005012 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005013}
5014
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005015void LocationsBuilderARM64::VisitRor(HRor* ror) {
5016 HandleBinaryOp(ror);
5017}
5018
5019void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5020 HandleBinaryOp(ror);
5021}
5022
Serban Constantinescu02164b32014-11-13 14:05:07 +00005023void LocationsBuilderARM64::VisitShl(HShl* shl) {
5024 HandleShift(shl);
5025}
5026
5027void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5028 HandleShift(shl);
5029}
5030
5031void LocationsBuilderARM64::VisitShr(HShr* shr) {
5032 HandleShift(shr);
5033}
5034
5035void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5036 HandleShift(shr);
5037}
5038
Alexandre Rames5319def2014-10-23 10:03:10 +01005039void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005040 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005041}
5042
5043void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005044 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005045}
5046
Alexandre Rames67555f72014-11-18 10:55:16 +00005047void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005048 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005049}
5050
5051void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005052 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005053}
5054
5055void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005056 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005057}
5058
Alexandre Rames67555f72014-11-18 10:55:16 +00005059void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005060 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005061}
5062
Calin Juravlee460d1d2015-09-29 04:52:17 +01005063void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5064 HUnresolvedInstanceFieldGet* instruction) {
5065 FieldAccessCallingConventionARM64 calling_convention;
5066 codegen_->CreateUnresolvedFieldLocationSummary(
5067 instruction, instruction->GetFieldType(), calling_convention);
5068}
5069
5070void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5071 HUnresolvedInstanceFieldGet* instruction) {
5072 FieldAccessCallingConventionARM64 calling_convention;
5073 codegen_->GenerateUnresolvedFieldAccess(instruction,
5074 instruction->GetFieldType(),
5075 instruction->GetFieldIndex(),
5076 instruction->GetDexPc(),
5077 calling_convention);
5078}
5079
5080void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5081 HUnresolvedInstanceFieldSet* instruction) {
5082 FieldAccessCallingConventionARM64 calling_convention;
5083 codegen_->CreateUnresolvedFieldLocationSummary(
5084 instruction, instruction->GetFieldType(), calling_convention);
5085}
5086
5087void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5088 HUnresolvedInstanceFieldSet* instruction) {
5089 FieldAccessCallingConventionARM64 calling_convention;
5090 codegen_->GenerateUnresolvedFieldAccess(instruction,
5091 instruction->GetFieldType(),
5092 instruction->GetFieldIndex(),
5093 instruction->GetDexPc(),
5094 calling_convention);
5095}
5096
5097void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5098 HUnresolvedStaticFieldGet* instruction) {
5099 FieldAccessCallingConventionARM64 calling_convention;
5100 codegen_->CreateUnresolvedFieldLocationSummary(
5101 instruction, instruction->GetFieldType(), calling_convention);
5102}
5103
5104void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5105 HUnresolvedStaticFieldGet* instruction) {
5106 FieldAccessCallingConventionARM64 calling_convention;
5107 codegen_->GenerateUnresolvedFieldAccess(instruction,
5108 instruction->GetFieldType(),
5109 instruction->GetFieldIndex(),
5110 instruction->GetDexPc(),
5111 calling_convention);
5112}
5113
5114void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5115 HUnresolvedStaticFieldSet* instruction) {
5116 FieldAccessCallingConventionARM64 calling_convention;
5117 codegen_->CreateUnresolvedFieldLocationSummary(
5118 instruction, instruction->GetFieldType(), calling_convention);
5119}
5120
5121void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5122 HUnresolvedStaticFieldSet* instruction) {
5123 FieldAccessCallingConventionARM64 calling_convention;
5124 codegen_->GenerateUnresolvedFieldAccess(instruction,
5125 instruction->GetFieldType(),
5126 instruction->GetFieldIndex(),
5127 instruction->GetDexPc(),
5128 calling_convention);
5129}
5130
Alexandre Rames5319def2014-10-23 10:03:10 +01005131void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005132 LocationSummary* locations =
5133 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005134 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005135}
5136
5137void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005138 HBasicBlock* block = instruction->GetBlock();
5139 if (block->GetLoopInformation() != nullptr) {
5140 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5141 // The back edge will generate the suspend check.
5142 return;
5143 }
5144 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5145 // The goto will generate the suspend check.
5146 return;
5147 }
5148 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005149}
5150
Alexandre Rames67555f72014-11-18 10:55:16 +00005151void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5152 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005153 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005154 InvokeRuntimeCallingConvention calling_convention;
5155 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5156}
5157
5158void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005159 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005160 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005161}
5162
5163void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5164 LocationSummary* locations =
5165 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5166 Primitive::Type input_type = conversion->GetInputType();
5167 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005168 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005169 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5170 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5171 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5172 }
5173
Alexandre Rames542361f2015-01-29 16:57:31 +00005174 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005175 locations->SetInAt(0, Location::RequiresFpuRegister());
5176 } else {
5177 locations->SetInAt(0, Location::RequiresRegister());
5178 }
5179
Alexandre Rames542361f2015-01-29 16:57:31 +00005180 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005181 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5182 } else {
5183 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5184 }
5185}
5186
5187void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5188 Primitive::Type result_type = conversion->GetResultType();
5189 Primitive::Type input_type = conversion->GetInputType();
5190
5191 DCHECK_NE(input_type, result_type);
5192
Alexandre Rames542361f2015-01-29 16:57:31 +00005193 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005194 int result_size = Primitive::ComponentSize(result_type);
5195 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005196 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005197 Register output = OutputRegister(conversion);
5198 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005199 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005200 // 'int' values are used directly as W registers, discarding the top
5201 // bits, so we don't need to sign-extend and can just perform a move.
5202 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5203 // top 32 bits of the target register. We theoretically could leave those
5204 // bits unchanged, but we would have to make sure that no code uses a
5205 // 32bit input value as a 64bit value assuming that the top 32 bits are
5206 // zero.
5207 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005208 } else if (result_type == Primitive::kPrimChar ||
5209 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5210 __ Ubfx(output,
5211 output.IsX() ? source.X() : source.W(),
5212 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005213 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005214 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005215 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005216 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005217 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005218 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005219 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5220 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005221 } else if (Primitive::IsFloatingPointType(result_type) &&
5222 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005223 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5224 } else {
5225 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5226 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005227 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005228}
Alexandre Rames67555f72014-11-18 10:55:16 +00005229
Serban Constantinescu02164b32014-11-13 14:05:07 +00005230void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5231 HandleShift(ushr);
5232}
5233
5234void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5235 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005236}
5237
5238void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5239 HandleBinaryOp(instruction);
5240}
5241
5242void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5243 HandleBinaryOp(instruction);
5244}
5245
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005246void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005247 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005248 LOG(FATAL) << "Unreachable";
5249}
5250
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005251void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005252 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005253 LOG(FATAL) << "Unreachable";
5254}
5255
Mark Mendellfe57faa2015-09-18 09:26:15 -04005256// Simple implementation of packed switch - generate cascaded compare/jumps.
5257void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5258 LocationSummary* locations =
5259 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5260 locations->SetInAt(0, Location::RequiresRegister());
5261}
5262
5263void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5264 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005265 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005266 Register value_reg = InputRegisterAt(switch_instr, 0);
5267 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5268
Zheng Xu3927c8b2015-11-18 17:46:25 +08005269 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005270 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005271 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5272 // make sure we don't emit it if the target may run out of range.
5273 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5274 // ranges and emit the tables only as required.
5275 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005276
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005277 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005278 // Current instruction id is an upper bound of the number of HIRs in the graph.
5279 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5280 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005281 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5282 Register temp = temps.AcquireW();
5283 __ Subs(temp, value_reg, Operand(lower_bound));
5284
Zheng Xu3927c8b2015-11-18 17:46:25 +08005285 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005286 // Jump to successors[0] if value == lower_bound.
5287 __ B(eq, codegen_->GetLabelOf(successors[0]));
5288 int32_t last_index = 0;
5289 for (; num_entries - last_index > 2; last_index += 2) {
5290 __ Subs(temp, temp, Operand(2));
5291 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5292 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5293 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5294 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5295 }
5296 if (num_entries - last_index == 2) {
5297 // The last missing case_value.
5298 __ Cmp(temp, Operand(1));
5299 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005300 }
5301
5302 // And the default for any other value.
5303 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5304 __ B(codegen_->GetLabelOf(default_block));
5305 }
5306 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005307 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005308
5309 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5310
5311 // Below instructions should use at most one blocked register. Since there are two blocked
5312 // registers, we are free to block one.
5313 Register temp_w = temps.AcquireW();
5314 Register index;
5315 // Remove the bias.
5316 if (lower_bound != 0) {
5317 index = temp_w;
5318 __ Sub(index, value_reg, Operand(lower_bound));
5319 } else {
5320 index = value_reg;
5321 }
5322
5323 // Jump to default block if index is out of the range.
5324 __ Cmp(index, Operand(num_entries));
5325 __ B(hs, codegen_->GetLabelOf(default_block));
5326
5327 // In current VIXL implementation, it won't require any blocked registers to encode the
5328 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5329 // register pressure.
5330 Register table_base = temps.AcquireX();
5331 // Load jump offset from the table.
5332 __ Adr(table_base, jump_table->GetTableStartLabel());
5333 Register jump_offset = temp_w;
5334 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5335
5336 // Jump to target block by branching to table_base(pc related) + offset.
5337 Register target_address = table_base;
5338 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5339 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005340 }
5341}
5342
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005343void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5344 HInstruction* instruction,
5345 Location out,
5346 uint32_t offset,
5347 Location maybe_temp,
5348 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005349 Primitive::Type type = Primitive::kPrimNot;
5350 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005351 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005352 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005353 Register temp_reg = RegisterFrom(maybe_temp, type);
5354 if (kUseBakerReadBarrier) {
5355 // Load with fast path based Baker's read barrier.
5356 // /* HeapReference<Object> */ out = *(out + offset)
5357 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5358 out,
5359 out_reg,
5360 offset,
5361 temp_reg,
5362 /* needs_null_check */ false,
5363 /* use_load_acquire */ false);
5364 } else {
5365 // Load with slow path based read barrier.
5366 // Save the value of `out` into `maybe_temp` before overwriting it
5367 // in the following move operation, as we will need it for the
5368 // read barrier below.
5369 __ Mov(temp_reg, out_reg);
5370 // /* HeapReference<Object> */ out = *(out + offset)
5371 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5372 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5373 }
5374 } else {
5375 // Plain load with no read barrier.
5376 // /* HeapReference<Object> */ out = *(out + offset)
5377 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5378 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5379 }
5380}
5381
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005382void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5383 HInstruction* instruction,
5384 Location out,
5385 Location obj,
5386 uint32_t offset,
5387 Location maybe_temp,
5388 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005389 Primitive::Type type = Primitive::kPrimNot;
5390 Register out_reg = RegisterFrom(out, type);
5391 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005392 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005393 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005394 if (kUseBakerReadBarrier) {
5395 // Load with fast path based Baker's read barrier.
5396 Register temp_reg = RegisterFrom(maybe_temp, type);
5397 // /* HeapReference<Object> */ out = *(obj + offset)
5398 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5399 out,
5400 obj_reg,
5401 offset,
5402 temp_reg,
5403 /* needs_null_check */ false,
5404 /* use_load_acquire */ false);
5405 } else {
5406 // Load with slow path based read barrier.
5407 // /* HeapReference<Object> */ out = *(obj + offset)
5408 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5409 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5410 }
5411 } else {
5412 // Plain load with no read barrier.
5413 // /* HeapReference<Object> */ out = *(obj + offset)
5414 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5415 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5416 }
5417}
5418
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005419void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5420 HInstruction* instruction,
5421 Location root,
5422 Register obj,
5423 uint32_t offset,
5424 vixl::aarch64::Label* fixup_label,
5425 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005426 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005427 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005428 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005429 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005430 if (kUseBakerReadBarrier) {
5431 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5432 // Baker's read barrier are used:
5433 //
5434 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005435 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5436 // if (temp != null) {
5437 // root = temp(root)
Roland Levillain44015862016-01-22 11:47:17 +00005438 // }
5439
5440 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005441 if (fixup_label == nullptr) {
5442 __ Ldr(root_reg, MemOperand(obj, offset));
5443 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005444 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005445 }
Roland Levillain44015862016-01-22 11:47:17 +00005446 static_assert(
5447 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5448 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5449 "have different sizes.");
5450 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5451 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5452 "have different sizes.");
5453
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005454 Register temp = lr;
Roland Levillain44015862016-01-22 11:47:17 +00005455
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005456 // Slow path marking the GC root `root`. The entrypoint will alrady be loaded in temp.
5457 SlowPathCodeARM64* slow_path =
5458 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction,
5459 root,
5460 LocationFrom(temp));
5461 codegen_->AddSlowPath(slow_path);
5462 const int32_t entry_point_offset =
5463 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5464 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5465 // Loading the entrypoint does not require a load acquire since it is only changed when
5466 // threads are suspended or running a checkpoint.
5467 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5468 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5469 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005470 __ Cbnz(temp, slow_path->GetEntryLabel());
5471 __ Bind(slow_path->GetExitLabel());
5472 } else {
5473 // GC root loaded through a slow path for read barriers other
5474 // than Baker's.
5475 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005476 if (fixup_label == nullptr) {
5477 __ Add(root_reg.X(), obj.X(), offset);
5478 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005479 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005480 }
Roland Levillain44015862016-01-22 11:47:17 +00005481 // /* mirror::Object* */ root = root->Read()
5482 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5483 }
5484 } else {
5485 // Plain GC root load with no read barrier.
5486 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005487 if (fixup_label == nullptr) {
5488 __ Ldr(root_reg, MemOperand(obj, offset));
5489 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005490 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005491 }
Roland Levillain44015862016-01-22 11:47:17 +00005492 // Note that GC roots are not affected by heap poisoning, thus we
5493 // do not have to unpoison `root_reg` here.
5494 }
5495}
5496
5497void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(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 offset,
5501 Register temp,
5502 bool needs_null_check,
5503 bool use_load_acquire) {
5504 DCHECK(kEmitCompilerReadBarrier);
5505 DCHECK(kUseBakerReadBarrier);
5506
5507 // /* HeapReference<Object> */ ref = *(obj + offset)
5508 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005509 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005510 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5511 ref,
5512 obj,
5513 offset,
5514 no_index,
5515 no_scale_factor,
5516 temp,
5517 needs_null_check,
5518 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005519}
5520
5521void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5522 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005523 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005524 uint32_t data_offset,
5525 Location index,
5526 Register temp,
5527 bool needs_null_check) {
5528 DCHECK(kEmitCompilerReadBarrier);
5529 DCHECK(kUseBakerReadBarrier);
5530
5531 // Array cells are never volatile variables, therefore array loads
5532 // never use Load-Acquire instructions on ARM64.
5533 const bool use_load_acquire = false;
5534
Roland Levillainbfea3352016-06-23 13:48:47 +01005535 static_assert(
5536 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5537 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005538 // /* HeapReference<Object> */ ref =
5539 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005540 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5541 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5542 ref,
5543 obj,
5544 data_offset,
5545 index,
5546 scale_factor,
5547 temp,
5548 needs_null_check,
5549 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005550}
5551
5552void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5553 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005554 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005555 uint32_t offset,
5556 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005557 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005558 Register temp,
5559 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005560 bool use_load_acquire,
5561 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005562 DCHECK(kEmitCompilerReadBarrier);
5563 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005564 // If we are emitting an array load, we should not be using a
5565 // Load Acquire instruction. In other words:
5566 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5567 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005568
5569 MacroAssembler* masm = GetVIXLAssembler();
5570 UseScratchRegisterScope temps(masm);
5571
5572 // In slow path based read barriers, the read barrier call is
5573 // inserted after the original load. However, in fast path based
5574 // Baker's read barriers, we need to perform the load of
5575 // mirror::Object::monitor_ *before* the original reference load.
5576 // This load-load ordering is required by the read barrier.
5577 // The fast path/slow path (for Baker's algorithm) should look like:
5578 //
5579 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5580 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5581 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005582 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain44015862016-01-22 11:47:17 +00005583 // if (is_gray) {
5584 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5585 // }
5586 //
5587 // Note: the original implementation in ReadBarrier::Barrier is
5588 // slightly more complex as it performs additional checks that we do
5589 // not do here for performance reasons.
5590
5591 Primitive::Type type = Primitive::kPrimNot;
5592 Register ref_reg = RegisterFrom(ref, type);
5593 DCHECK(obj.IsW());
5594 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5595
5596 // /* int32_t */ monitor = obj->monitor_
5597 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5598 if (needs_null_check) {
5599 MaybeRecordImplicitNullCheck(instruction);
5600 }
5601 // /* LockWord */ lock_word = LockWord(monitor)
5602 static_assert(sizeof(LockWord) == sizeof(int32_t),
5603 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005604
Vladimir Marko877a0332016-07-11 19:30:56 +01005605 // Introduce a dependency on the lock_word including rb_state,
5606 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005607 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005608 // `obj` is unchanged by this operation, but its value now depends
5609 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005610 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005611
5612 // The actual reference load.
5613 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005614 // Load types involving an "index": ArrayGet,
5615 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5616 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01005617 if (use_load_acquire) {
5618 // UnsafeGetObjectVolatile intrinsic case.
5619 // Register `index` is not an index in an object array, but an
5620 // offset to an object reference field within object `obj`.
5621 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5622 DCHECK(instruction->GetLocations()->Intrinsified());
5623 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5624 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005625 DCHECK_EQ(offset, 0u);
5626 DCHECK_EQ(scale_factor, 0u);
5627 DCHECK_EQ(needs_null_check, 0u);
Roland Levillainbfea3352016-06-23 13:48:47 +01005628 // /* HeapReference<Object> */ ref = *(obj + index)
5629 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5630 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005631 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005632 // ArrayGet and UnsafeGetObject intrinsics cases.
5633 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5634 if (index.IsConstant()) {
5635 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5636 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5637 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005638 Register temp3 = temps.AcquireW();
5639 __ Add(temp3, obj, offset);
5640 Load(type, ref_reg, HeapOperand(temp3, XRegisterFrom(index), LSL, scale_factor));
5641 temps.Release(temp3);
Roland Levillainbfea3352016-06-23 13:48:47 +01005642 }
Roland Levillain44015862016-01-22 11:47:17 +00005643 }
Roland Levillain44015862016-01-22 11:47:17 +00005644 } else {
5645 // /* HeapReference<Object> */ ref = *(obj + offset)
5646 MemOperand field = HeapOperand(obj, offset);
5647 if (use_load_acquire) {
5648 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5649 } else {
5650 Load(type, ref_reg, field);
5651 }
5652 }
5653
5654 // Object* ref = ref_addr->AsMirrorPtr()
5655 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5656
Vladimir Marko953437b2016-08-24 08:30:46 +00005657 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005658 SlowPathCodeARM64* slow_path;
5659 if (always_update_field) {
5660 // ReadBarrierMarkAndUpdateFieldSlowPathARM64 only supports
5661 // address of the form `obj + field_offset`, where `obj` is a
5662 // register and `field_offset` is a register. Thus `offset` and
5663 // `scale_factor` above are expected to be null in this code path.
5664 DCHECK_EQ(offset, 0u);
5665 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
5666 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM64(
5667 instruction, ref, obj, /* field_offset */ index, temp);
5668 } else {
5669 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
5670 }
Roland Levillain44015862016-01-22 11:47:17 +00005671 AddSlowPath(slow_path);
5672
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005673 // if (rb_state == ReadBarrier::GrayState())
Roland Levillain44015862016-01-22 11:47:17 +00005674 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005675 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005676 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5677 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko877a0332016-07-11 19:30:56 +01005678 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005679 __ Bind(slow_path->GetExitLabel());
5680}
5681
5682void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5683 Location out,
5684 Location ref,
5685 Location obj,
5686 uint32_t offset,
5687 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005688 DCHECK(kEmitCompilerReadBarrier);
5689
Roland Levillain44015862016-01-22 11:47:17 +00005690 // Insert a slow path based read barrier *after* the reference load.
5691 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005692 // If heap poisoning is enabled, the unpoisoning of the loaded
5693 // reference will be carried out by the runtime within the slow
5694 // path.
5695 //
5696 // Note that `ref` currently does not get unpoisoned (when heap
5697 // poisoning is enabled), which is alright as the `ref` argument is
5698 // not used by the artReadBarrierSlow entry point.
5699 //
5700 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5701 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5702 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5703 AddSlowPath(slow_path);
5704
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005705 __ B(slow_path->GetEntryLabel());
5706 __ Bind(slow_path->GetExitLabel());
5707}
5708
Roland Levillain44015862016-01-22 11:47:17 +00005709void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5710 Location out,
5711 Location ref,
5712 Location obj,
5713 uint32_t offset,
5714 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005715 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005716 // Baker's read barriers shall be handled by the fast path
5717 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5718 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005719 // If heap poisoning is enabled, unpoisoning will be taken care of
5720 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005721 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005722 } else if (kPoisonHeapReferences) {
5723 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5724 }
5725}
5726
Roland Levillain44015862016-01-22 11:47:17 +00005727void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5728 Location out,
5729 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005730 DCHECK(kEmitCompilerReadBarrier);
5731
Roland Levillain44015862016-01-22 11:47:17 +00005732 // Insert a slow path based read barrier *after* the GC root load.
5733 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005734 // Note that GC roots are not affected by heap poisoning, so we do
5735 // not need to do anything special for this here.
5736 SlowPathCodeARM64* slow_path =
5737 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5738 AddSlowPath(slow_path);
5739
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005740 __ B(slow_path->GetEntryLabel());
5741 __ Bind(slow_path->GetExitLabel());
5742}
5743
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005744void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5745 LocationSummary* locations =
5746 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5747 locations->SetInAt(0, Location::RequiresRegister());
5748 locations->SetOut(Location::RequiresRegister());
5749}
5750
5751void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5752 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005753 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005754 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005755 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005756 __ Ldr(XRegisterFrom(locations->Out()),
5757 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005758 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005759 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005760 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005761 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5762 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005763 __ Ldr(XRegisterFrom(locations->Out()),
5764 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005765 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005766}
5767
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005768void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
5769 for (const auto& entry : jit_string_patches_) {
5770 const auto& it = jit_string_roots_.find(entry.first);
5771 DCHECK(it != jit_string_roots_.end());
5772 size_t index_in_table = it->second;
5773 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
5774 uint32_t literal_offset = literal->GetOffset();
5775 uintptr_t address =
5776 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
5777 uint8_t* data = code + literal_offset;
5778 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
5779 }
5780}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005781
Alexandre Rames67555f72014-11-18 10:55:16 +00005782#undef __
5783#undef QUICK_ENTRY_POINT
5784
Alexandre Rames5319def2014-10-23 10:03:10 +01005785} // namespace arm64
5786} // namespace art