blob: 578c7e76e30ee18bd857de55a2643e6bddbfe2cd [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
Andreas Gampe878d58c2015-01-15 23:24:00 -080049using helpers::CPURegisterFrom;
50using helpers::DRegisterFrom;
51using helpers::FPRegisterFrom;
52using helpers::HeapOperand;
53using helpers::HeapOperandFrom;
54using helpers::InputCPURegisterAt;
55using helpers::InputFPRegisterAt;
56using helpers::InputRegisterAt;
57using helpers::InputOperandAt;
58using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080059using helpers::LocationFrom;
60using helpers::OperandFromMemOperand;
61using helpers::OutputCPURegister;
62using helpers::OutputFPRegister;
63using helpers::OutputRegister;
64using helpers::RegisterFrom;
65using helpers::StackOperandFrom;
66using helpers::VIXLRegCodeFromART;
67using helpers::WRegisterFrom;
68using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000069using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080070using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080071
Alexandre Rames5319def2014-10-23 10:03:10 +010072static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000073// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080074// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
75// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000076static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010077
Alexandre Rames5319def2014-10-23 10:03:10 +010078inline Condition ARM64Condition(IfCondition cond) {
79 switch (cond) {
80 case kCondEQ: return eq;
81 case kCondNE: return ne;
82 case kCondLT: return lt;
83 case kCondLE: return le;
84 case kCondGT: return gt;
85 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070086 case kCondB: return lo;
87 case kCondBE: return ls;
88 case kCondA: return hi;
89 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010090 }
Roland Levillain7f63c522015-07-13 15:54:55 +000091 LOG(FATAL) << "Unreachable";
92 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010093}
94
Vladimir Markod6e069b2016-01-18 11:11:01 +000095inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
96 // The ARM64 condition codes can express all the necessary branches, see the
97 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
98 // There is no dex instruction or HIR that would need the missing conditions
99 // "equal or unordered" or "not equal".
100 switch (cond) {
101 case kCondEQ: return eq;
102 case kCondNE: return ne /* unordered */;
103 case kCondLT: return gt_bias ? cc : lt /* unordered */;
104 case kCondLE: return gt_bias ? ls : le /* unordered */;
105 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
106 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
107 default:
108 LOG(FATAL) << "UNREACHABLE";
109 UNREACHABLE();
110 }
111}
112
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000113Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000114 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
115 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
116 // but we use the exact registers for clarity.
117 if (return_type == Primitive::kPrimFloat) {
118 return LocationFrom(s0);
119 } else if (return_type == Primitive::kPrimDouble) {
120 return LocationFrom(d0);
121 } else if (return_type == Primitive::kPrimLong) {
122 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100123 } else if (return_type == Primitive::kPrimVoid) {
124 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000125 } else {
126 return LocationFrom(w0);
127 }
128}
129
Alexandre Rames5319def2014-10-23 10:03:10 +0100130Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000131 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100132}
133
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100134// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
135#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700136#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100137
Zheng Xuda403092015-04-24 17:35:39 +0800138// Calculate memory accessing operand for save/restore live registers.
139static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
140 RegisterSet* register_set,
141 int64_t spill_offset,
142 bool is_save) {
143 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
144 codegen->GetNumberOfCoreRegisters(),
145 register_set->GetFloatingPointRegisters(),
146 codegen->GetNumberOfFloatingPointRegisters()));
147
148 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100149 register_set->GetCoreRegisters() & (~callee_saved_core_registers.GetList()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000150 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100151 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.GetList()));
Zheng Xuda403092015-04-24 17:35:39 +0800152
153 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
154 UseScratchRegisterScope temps(masm);
155
156 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100157 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
158 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800159 int64_t reg_size = kXRegSizeInBytes;
160 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
161 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100162 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800163 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
164 // If the offset does not fit in the instruction's immediate field, use an alternate register
165 // to compute the base address(float point registers spill base address).
166 Register new_base = temps.AcquireSameSizeAs(base);
167 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
168 base = new_base;
169 spill_offset = -core_spill_size;
170 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
171 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
172 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
173 }
174
175 if (is_save) {
176 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
177 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
178 } else {
179 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
180 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
181 }
182}
183
184void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
185 RegisterSet* register_set = locations->GetLiveRegisters();
186 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
187 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
188 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
189 // If the register holds an object, update the stack mask.
190 if (locations->RegisterContainsObject(i)) {
191 locations->SetStackBit(stack_offset / kVRegSize);
192 }
193 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
194 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
195 saved_core_stack_offsets_[i] = stack_offset;
196 stack_offset += kXRegSizeInBytes;
197 }
198 }
199
200 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
201 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
202 register_set->ContainsFloatingPointRegister(i)) {
203 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
204 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
205 saved_fpu_stack_offsets_[i] = stack_offset;
206 stack_offset += kDRegSizeInBytes;
207 }
208 }
209
210 SaveRestoreLiveRegistersHelper(codegen, register_set,
211 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
212}
213
214void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
215 RegisterSet* register_set = locations->GetLiveRegisters();
216 SaveRestoreLiveRegistersHelper(codegen, register_set,
217 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
218}
219
Alexandre Rames5319def2014-10-23 10:03:10 +0100220class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
221 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000222 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100223
Alexandre Rames67555f72014-11-18 10:55:16 +0000224 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100225 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000226 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100227
Alexandre Rames5319def2014-10-23 10:03:10 +0100228 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000229 if (instruction_->CanThrowIntoCatchBlock()) {
230 // Live registers will be restored in the catch block if caught.
231 SaveLiveRegisters(codegen, instruction_->GetLocations());
232 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000233 // We're moving two locations to locations that could overlap, so we need a parallel
234 // move resolver.
235 InvokeRuntimeCallingConvention calling_convention;
236 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100237 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
238 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100239 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
240 ? QUICK_ENTRY_POINT(pThrowStringBounds)
241 : QUICK_ENTRY_POINT(pThrowArrayBounds);
242 arm64_codegen->InvokeRuntime(entry_point_offset, instruction_, instruction_->GetDexPc(), this);
243 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800244 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100245 }
246
Alexandre Rames8158f282015-08-07 10:26:17 +0100247 bool IsFatal() const OVERRIDE { return true; }
248
Alexandre Rames9931f312015-06-19 14:47:01 +0100249 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
250
Alexandre Rames5319def2014-10-23 10:03:10 +0100251 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100252 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
253};
254
Alexandre Rames67555f72014-11-18 10:55:16 +0000255class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
256 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000257 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000258
259 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
260 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
261 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000262 if (instruction_->CanThrowIntoCatchBlock()) {
263 // Live registers will be restored in the catch block if caught.
264 SaveLiveRegisters(codegen, instruction_->GetLocations());
265 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000266 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000267 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800268 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 }
270
Alexandre Rames8158f282015-08-07 10:26:17 +0100271 bool IsFatal() const OVERRIDE { return true; }
272
Alexandre Rames9931f312015-06-19 14:47:01 +0100273 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
274
Alexandre Rames67555f72014-11-18 10:55:16 +0000275 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000276 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
277};
278
279class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
280 public:
281 LoadClassSlowPathARM64(HLoadClass* cls,
282 HInstruction* at,
283 uint32_t dex_pc,
284 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000285 : SlowPathCodeARM64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000286 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
287 }
288
289 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
290 LocationSummary* locations = at_->GetLocations();
291 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
292
293 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000294 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000295
296 InvokeRuntimeCallingConvention calling_convention;
297 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
299 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000300 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800301 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100302 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800303 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100304 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800305 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000306
307 // Move the class to the desired location.
308 Location out = locations->Out();
309 if (out.IsValid()) {
310 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
311 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000312 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000313 }
314
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000315 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000316 __ B(GetExitLabel());
317 }
318
Alexandre Rames9931f312015-06-19 14:47:01 +0100319 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
320
Alexandre Rames67555f72014-11-18 10:55:16 +0000321 private:
322 // The class this slow path will load.
323 HLoadClass* const cls_;
324
325 // The instruction where this slow path is happening.
326 // (Might be the load class or an initialization check).
327 HInstruction* const at_;
328
329 // The dex PC of `at_`.
330 const uint32_t dex_pc_;
331
332 // Whether to initialize the class.
333 const bool do_clinit_;
334
335 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
336};
337
338class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
339 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000340 explicit LoadStringSlowPathARM64(HLoadString* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000341
342 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
343 LocationSummary* locations = instruction_->GetLocations();
344 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
345 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
346
347 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000348 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000349
350 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000351 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
352 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
Alexandre Rames67555f72014-11-18 10:55:16 +0000353 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000354 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100355 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000357 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000358
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000359 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000360 __ B(GetExitLabel());
361 }
362
Alexandre Rames9931f312015-06-19 14:47:01 +0100363 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
364
Alexandre Rames67555f72014-11-18 10:55:16 +0000365 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000366 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
367};
368
Alexandre Rames5319def2014-10-23 10:03:10 +0100369class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
370 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000371 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100372
Alexandre Rames67555f72014-11-18 10:55:16 +0000373 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
374 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100375 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000376 if (instruction_->CanThrowIntoCatchBlock()) {
377 // Live registers will be restored in the catch block if caught.
378 SaveLiveRegisters(codegen, instruction_->GetLocations());
379 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000380 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000381 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800382 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100383 }
384
Alexandre Rames8158f282015-08-07 10:26:17 +0100385 bool IsFatal() const OVERRIDE { return true; }
386
Alexandre Rames9931f312015-06-19 14:47:01 +0100387 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
388
Alexandre Rames5319def2014-10-23 10:03:10 +0100389 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100390 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
391};
392
393class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
394 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100395 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000396 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100397
Alexandre Rames67555f72014-11-18 10:55:16 +0000398 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
399 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100400 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000401 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000402 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800403 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000404 if (successor_ == nullptr) {
405 __ B(GetReturnLabel());
406 } else {
407 __ B(arm64_codegen->GetLabelOf(successor_));
408 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100409 }
410
Scott Wakeling97c72b72016-06-24 16:19:36 +0100411 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100412 DCHECK(successor_ == nullptr);
413 return &return_label_;
414 }
415
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100416 HBasicBlock* GetSuccessor() const {
417 return successor_;
418 }
419
Alexandre Rames9931f312015-06-19 14:47:01 +0100420 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
421
Alexandre Rames5319def2014-10-23 10:03:10 +0100422 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100423 // If not null, the block to branch to after the suspend check.
424 HBasicBlock* const successor_;
425
426 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100427 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100428
429 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
430};
431
Alexandre Rames67555f72014-11-18 10:55:16 +0000432class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
433 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000434 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000435 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000436
437 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000438 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100439 Location class_to_check = locations->InAt(1);
440 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
441 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000442 DCHECK(instruction_->IsCheckCast()
443 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
444 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100445 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000446
Alexandre Rames67555f72014-11-18 10:55:16 +0000447 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000448
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000449 if (!is_fatal_) {
450 SaveLiveRegisters(codegen, locations);
451 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000452
453 // We're moving two locations to locations that could overlap, so we need a parallel
454 // move resolver.
455 InvokeRuntimeCallingConvention calling_convention;
456 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100457 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
458 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000459
460 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000461 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100462 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Andreas Gampe67409972016-07-19 22:34:53 -0700463 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t,
Roland Levillain888d0672015-11-23 18:53:50 +0000464 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000465 Primitive::Type ret_type = instruction_->GetType();
466 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
467 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
468 } else {
469 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100470 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800471 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000472 }
473
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000474 if (!is_fatal_) {
475 RestoreLiveRegisters(codegen, locations);
476 __ B(GetExitLabel());
477 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000478 }
479
Alexandre Rames9931f312015-06-19 14:47:01 +0100480 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000481 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100482
Alexandre Rames67555f72014-11-18 10:55:16 +0000483 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000484 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000485
Alexandre Rames67555f72014-11-18 10:55:16 +0000486 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
487};
488
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700489class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
490 public:
Aart Bik42249c32016-01-07 15:33:50 -0800491 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000492 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700493
494 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800495 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700496 __ Bind(GetEntryLabel());
497 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800498 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
499 instruction_,
500 instruction_->GetDexPc(),
501 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000502 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700503 }
504
Alexandre Rames9931f312015-06-19 14:47:01 +0100505 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
506
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700507 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700508 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
509};
510
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100511class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
512 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000513 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100514
515 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
516 LocationSummary* locations = instruction_->GetLocations();
517 __ Bind(GetEntryLabel());
518 SaveLiveRegisters(codegen, locations);
519
520 InvokeRuntimeCallingConvention calling_convention;
521 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
522 parallel_move.AddMove(
523 locations->InAt(0),
524 LocationFrom(calling_convention.GetRegisterAt(0)),
525 Primitive::kPrimNot,
526 nullptr);
527 parallel_move.AddMove(
528 locations->InAt(1),
529 LocationFrom(calling_convention.GetRegisterAt(1)),
530 Primitive::kPrimInt,
531 nullptr);
532 parallel_move.AddMove(
533 locations->InAt(2),
534 LocationFrom(calling_convention.GetRegisterAt(2)),
535 Primitive::kPrimNot,
536 nullptr);
537 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
538
539 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
540 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
541 instruction_,
542 instruction_->GetDexPc(),
543 this);
544 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
545 RestoreLiveRegisters(codegen, locations);
546 __ B(GetExitLabel());
547 }
548
549 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
550
551 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100552 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
553};
554
Zheng Xu3927c8b2015-11-18 17:46:25 +0800555void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
556 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000557 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800558
559 // We are about to use the assembler to place literals directly. Make sure we have enough
560 // underlying code buffer and we have generated the jump table with right size.
561 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
562 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
563
564 __ Bind(&table_start_);
565 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
566 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100567 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800568 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100569 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800570 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
571 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
572 Literal<int32_t> literal(jump_offset);
573 __ place(&literal);
574 }
575}
576
Roland Levillain44015862016-01-22 11:47:17 +0000577// Slow path marking an object during a read barrier.
578class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
579 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100580 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location obj)
581 : SlowPathCodeARM64(instruction), obj_(obj) {
Roland Levillain44015862016-01-22 11:47:17 +0000582 DCHECK(kEmitCompilerReadBarrier);
583 }
584
585 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
586
587 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
588 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000589 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100590 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(obj_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000591 DCHECK(instruction_->IsInstanceFieldGet() ||
592 instruction_->IsStaticFieldGet() ||
593 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100594 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000595 instruction_->IsLoadClass() ||
596 instruction_->IsLoadString() ||
597 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100598 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100599 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
600 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000601 << "Unexpected instruction in read barrier marking slow path: "
602 << instruction_->DebugName();
603
604 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100605 // No need to save live registers; it's taken care of by the
606 // entrypoint. Also, there is no need to update the stack mask,
607 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000608 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100609 DCHECK_NE(obj_.reg(), LR);
610 DCHECK_NE(obj_.reg(), WSP);
611 DCHECK_NE(obj_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100612 // IP0 is used internally by the ReadBarrierMarkRegX entry point
613 // as a temporary, it cannot be the entry point's input/output.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700614 DCHECK_NE(obj_.reg(), IP0);
Roland Levillain02b75802016-07-13 11:54:35 +0100615 DCHECK(0 <= obj_.reg() && obj_.reg() < kNumberOfWRegisters) << obj_.reg();
616 // "Compact" slow path, saving two moves.
617 //
618 // Instead of using the standard runtime calling convention (input
619 // and output in W0):
620 //
621 // W0 <- obj
622 // W0 <- ReadBarrierMark(W0)
623 // obj <- W0
624 //
625 // we just use rX (the register holding `obj`) as input and output
626 // of a dedicated entrypoint:
627 //
628 // rX <- ReadBarrierMarkRegX(rX)
629 //
630 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700631 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(obj_.reg());
Roland Levillaindec8f632016-07-22 17:10:06 +0100632 // This runtime call does not require a stack map.
633 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain44015862016-01-22 11:47:17 +0000634 __ B(GetExitLabel());
635 }
636
637 private:
Roland Levillain44015862016-01-22 11:47:17 +0000638 const Location obj_;
639
640 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
641};
642
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000643// Slow path generating a read barrier for a heap reference.
644class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
645 public:
646 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
647 Location out,
648 Location ref,
649 Location obj,
650 uint32_t offset,
651 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000652 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000653 out_(out),
654 ref_(ref),
655 obj_(obj),
656 offset_(offset),
657 index_(index) {
658 DCHECK(kEmitCompilerReadBarrier);
659 // If `obj` is equal to `out` or `ref`, it means the initial object
660 // has been overwritten by (or after) the heap object reference load
661 // to be instrumented, e.g.:
662 //
663 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000664 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000665 //
666 // In that case, we have lost the information about the original
667 // object, and the emitted read barrier cannot work properly.
668 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
669 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
670 }
671
672 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
673 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
674 LocationSummary* locations = instruction_->GetLocations();
675 Primitive::Type type = Primitive::kPrimNot;
676 DCHECK(locations->CanCall());
677 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100678 DCHECK(instruction_->IsInstanceFieldGet() ||
679 instruction_->IsStaticFieldGet() ||
680 instruction_->IsArrayGet() ||
681 instruction_->IsInstanceOf() ||
682 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100683 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000684 << "Unexpected instruction in read barrier for heap reference slow path: "
685 << instruction_->DebugName();
Roland Levillain4a3aa572016-08-15 13:17:06 +0000686 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000687 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100688 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000689
690 __ Bind(GetEntryLabel());
691
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000692 SaveLiveRegisters(codegen, locations);
693
694 // We may have to change the index's value, but as `index_` is a
695 // constant member (like other "inputs" of this slow path),
696 // introduce a copy of it, `index`.
697 Location index = index_;
698 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100699 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000700 if (instruction_->IsArrayGet()) {
701 // Compute the actual memory offset and store it in `index`.
702 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
703 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
704 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
705 // We are about to change the value of `index_reg` (see the
706 // calls to vixl::MacroAssembler::Lsl and
707 // vixl::MacroAssembler::Mov below), but it has
708 // not been saved by the previous call to
709 // art::SlowPathCode::SaveLiveRegisters, as it is a
710 // callee-save register --
711 // art::SlowPathCode::SaveLiveRegisters does not consider
712 // callee-save registers, as it has been designed with the
713 // assumption that callee-save registers are supposed to be
714 // handled by the called function. So, as a callee-save
715 // register, `index_reg` _would_ eventually be saved onto
716 // the stack, but it would be too late: we would have
717 // changed its value earlier. Therefore, we manually save
718 // it here into another freely available register,
719 // `free_reg`, chosen of course among the caller-save
720 // registers (as a callee-save `free_reg` register would
721 // exhibit the same problem).
722 //
723 // Note we could have requested a temporary register from
724 // the register allocator instead; but we prefer not to, as
725 // this is a slow path, and we know we can find a
726 // caller-save register that is available.
727 Register free_reg = FindAvailableCallerSaveRegister(codegen);
728 __ Mov(free_reg.W(), index_reg);
729 index_reg = free_reg;
730 index = LocationFrom(index_reg);
731 } else {
732 // The initial register stored in `index_` has already been
733 // saved in the call to art::SlowPathCode::SaveLiveRegisters
734 // (as it is not a callee-save register), so we can freely
735 // use it.
736 }
737 // Shifting the index value contained in `index_reg` by the scale
738 // factor (2) cannot overflow in practice, as the runtime is
739 // unable to allocate object arrays with a size larger than
740 // 2^26 - 1 (that is, 2^28 - 4 bytes).
741 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
742 static_assert(
743 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
744 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
745 __ Add(index_reg, index_reg, Operand(offset_));
746 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100747 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
748 // intrinsics, `index_` is not shifted by a scale factor of 2
749 // (as in the case of ArrayGet), as it is actually an offset
750 // to an object field within an object.
751 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000752 DCHECK(instruction_->GetLocations()->Intrinsified());
753 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
754 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
755 << instruction_->AsInvoke()->GetIntrinsic();
756 DCHECK_EQ(offset_, 0U);
Roland Levillaina7426c62016-08-03 15:02:10 +0100757 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000758 }
759 }
760
761 // We're moving two or three locations to locations that could
762 // overlap, so we need a parallel move resolver.
763 InvokeRuntimeCallingConvention calling_convention;
764 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
765 parallel_move.AddMove(ref_,
766 LocationFrom(calling_convention.GetRegisterAt(0)),
767 type,
768 nullptr);
769 parallel_move.AddMove(obj_,
770 LocationFrom(calling_convention.GetRegisterAt(1)),
771 type,
772 nullptr);
773 if (index.IsValid()) {
774 parallel_move.AddMove(index,
775 LocationFrom(calling_convention.GetRegisterAt(2)),
776 Primitive::kPrimInt,
777 nullptr);
778 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
779 } else {
780 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
781 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
782 }
783 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
784 instruction_,
785 instruction_->GetDexPc(),
786 this);
787 CheckEntrypointTypes<
788 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
789 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
790
791 RestoreLiveRegisters(codegen, locations);
792
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000793 __ B(GetExitLabel());
794 }
795
796 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
797
798 private:
799 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100800 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
801 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000802 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
803 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
804 return Register(VIXLRegCodeFromART(i), kXRegSize);
805 }
806 }
807 // We shall never fail to find a free caller-save register, as
808 // there are more than two core caller-save registers on ARM64
809 // (meaning it is possible to find one which is different from
810 // `ref` and `obj`).
811 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
812 LOG(FATAL) << "Could not find a free register";
813 UNREACHABLE();
814 }
815
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000816 const Location out_;
817 const Location ref_;
818 const Location obj_;
819 const uint32_t offset_;
820 // An additional location containing an index to an array.
821 // Only used for HArrayGet and the UnsafeGetObject &
822 // UnsafeGetObjectVolatile intrinsics.
823 const Location index_;
824
825 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
826};
827
828// Slow path generating a read barrier for a GC root.
829class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
830 public:
831 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000832 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000833 DCHECK(kEmitCompilerReadBarrier);
834 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000835
836 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
837 LocationSummary* locations = instruction_->GetLocations();
838 Primitive::Type type = Primitive::kPrimNot;
839 DCHECK(locations->CanCall());
840 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000841 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
842 << "Unexpected instruction in read barrier for GC root slow path: "
843 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000844
845 __ Bind(GetEntryLabel());
846 SaveLiveRegisters(codegen, locations);
847
848 InvokeRuntimeCallingConvention calling_convention;
849 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
850 // The argument of the ReadBarrierForRootSlow is not a managed
851 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
852 // thus we need a 64-bit move here, and we cannot use
853 //
854 // arm64_codegen->MoveLocation(
855 // LocationFrom(calling_convention.GetRegisterAt(0)),
856 // root_,
857 // type);
858 //
859 // which would emit a 32-bit move, as `type` is a (32-bit wide)
860 // reference type (`Primitive::kPrimNot`).
861 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
862 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
863 instruction_,
864 instruction_->GetDexPc(),
865 this);
866 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
867 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
868
869 RestoreLiveRegisters(codegen, locations);
870 __ B(GetExitLabel());
871 }
872
873 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
874
875 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000876 const Location out_;
877 const Location root_;
878
879 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
880};
881
Alexandre Rames5319def2014-10-23 10:03:10 +0100882#undef __
883
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100884Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100885 Location next_location;
886 if (type == Primitive::kPrimVoid) {
887 LOG(FATAL) << "Unreachable type " << type;
888 }
889
Alexandre Rames542361f2015-01-29 16:57:31 +0000890 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100891 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
892 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000893 } else if (!Primitive::IsFloatingPointType(type) &&
894 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000895 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
896 } else {
897 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000898 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
899 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100900 }
901
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000902 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000903 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100904 return next_location;
905}
906
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100907Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100908 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100909}
910
Serban Constantinescu579885a2015-02-22 20:51:33 +0000911CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
912 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100913 const CompilerOptions& compiler_options,
914 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100915 : CodeGenerator(graph,
916 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000917 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000918 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100919 callee_saved_core_registers.GetList(),
920 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100921 compiler_options,
922 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100923 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800924 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100925 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000926 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000927 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100928 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000929 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000930 uint32_literals_(std::less<uint32_t>(),
931 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100932 uint64_literals_(std::less<uint64_t>(),
933 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
934 method_patches_(MethodReferenceComparator(),
935 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
936 call_patches_(MethodReferenceComparator(),
937 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
938 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000939 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
940 boot_image_string_patches_(StringReferenceValueComparator(),
941 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
942 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100943 boot_image_type_patches_(TypeReferenceValueComparator(),
944 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
945 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000946 boot_image_address_patches_(std::less<uint32_t>(),
947 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000948 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000949 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000950}
Alexandre Rames5319def2014-10-23 10:03:10 +0100951
Alexandre Rames67555f72014-11-18 10:55:16 +0000952#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100953
Zheng Xu3927c8b2015-11-18 17:46:25 +0800954void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100955 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800956 jump_table->EmitTable(this);
957 }
958}
959
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000960void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800961 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000962 // Ensure we emit the literal pool.
963 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000964
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000965 CodeGenerator::Finalize(allocator);
966}
967
Zheng Xuad4450e2015-04-17 18:48:56 +0800968void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
969 // Note: There are 6 kinds of moves:
970 // 1. constant -> GPR/FPR (non-cycle)
971 // 2. constant -> stack (non-cycle)
972 // 3. GPR/FPR -> GPR/FPR
973 // 4. GPR/FPR -> stack
974 // 5. stack -> GPR/FPR
975 // 6. stack -> stack (non-cycle)
976 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
977 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
978 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
979 // dependency.
980 vixl_temps_.Open(GetVIXLAssembler());
981}
982
983void ParallelMoveResolverARM64::FinishEmitNativeCode() {
984 vixl_temps_.Close();
985}
986
987Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
988 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
989 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
990 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
991 Location scratch = GetScratchLocation(kind);
992 if (!scratch.Equals(Location::NoLocation())) {
993 return scratch;
994 }
995 // Allocate from VIXL temp registers.
996 if (kind == Location::kRegister) {
997 scratch = LocationFrom(vixl_temps_.AcquireX());
998 } else {
999 DCHECK(kind == Location::kFpuRegister);
1000 scratch = LocationFrom(vixl_temps_.AcquireD());
1001 }
1002 AddScratchLocation(scratch);
1003 return scratch;
1004}
1005
1006void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1007 if (loc.IsRegister()) {
1008 vixl_temps_.Release(XRegisterFrom(loc));
1009 } else {
1010 DCHECK(loc.IsFpuRegister());
1011 vixl_temps_.Release(DRegisterFrom(loc));
1012 }
1013 RemoveScratchLocation(loc);
1014}
1015
Alexandre Rames3e69f162014-12-10 10:36:50 +00001016void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001017 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001018 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001019}
1020
Alexandre Rames5319def2014-10-23 10:03:10 +01001021void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001022 MacroAssembler* masm = GetVIXLAssembler();
1023 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001024 __ Bind(&frame_entry_label_);
1025
Serban Constantinescu02164b32014-11-13 14:05:07 +00001026 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1027 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001028 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001029 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001030 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001031 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001032 __ Ldr(wzr, MemOperand(temp, 0));
1033 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001034 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001035
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001036 if (!HasEmptyFrame()) {
1037 int frame_size = GetFrameSize();
1038 // Stack layout:
1039 // sp[frame_size - 8] : lr.
1040 // ... : other preserved core registers.
1041 // ... : other preserved fp registers.
1042 // ... : reserved frame space.
1043 // sp[0] : current method.
1044 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001045 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001046 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1047 frame_size - GetCoreSpillSize());
1048 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1049 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001050 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001051}
1052
1053void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001054 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001055 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001056 if (!HasEmptyFrame()) {
1057 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001058 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1059 frame_size - FrameEntrySpillSize());
1060 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1061 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001062 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001063 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001064 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001065 __ Ret();
1066 GetAssembler()->cfi().RestoreState();
1067 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001068}
1069
Scott Wakeling97c72b72016-06-24 16:19:36 +01001070CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001071 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001072 return CPURegList(CPURegister::kRegister, kXRegSize,
1073 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001074}
1075
Scott Wakeling97c72b72016-06-24 16:19:36 +01001076CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001077 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1078 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001079 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1080 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001081}
1082
Alexandre Rames5319def2014-10-23 10:03:10 +01001083void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1084 __ Bind(GetLabelOf(block));
1085}
1086
Calin Juravle175dc732015-08-25 15:42:32 +01001087void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1088 DCHECK(location.IsRegister());
1089 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1090}
1091
Calin Juravlee460d1d2015-09-29 04:52:17 +01001092void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1093 if (location.IsRegister()) {
1094 locations->AddTemp(location);
1095 } else {
1096 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1097 }
1098}
1099
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001100void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001101 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001102 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001103 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001104 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001105 if (value_can_be_null) {
1106 __ Cbz(value, &done);
1107 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001108 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001109 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001110 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001111 if (value_can_be_null) {
1112 __ Bind(&done);
1113 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001114}
1115
David Brazdil58282f42016-01-14 12:45:10 +00001116void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001117 // Blocked core registers:
1118 // lr : Runtime reserved.
1119 // tr : Runtime reserved.
1120 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1121 // ip1 : VIXL core temp.
1122 // ip0 : VIXL core temp.
1123 //
1124 // Blocked fp registers:
1125 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001126 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1127 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001128 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001129 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001130 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001131
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001132 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001133 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001134 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001135 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001136
David Brazdil58282f42016-01-14 12:45:10 +00001137 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001138 // Stubs do not save callee-save floating point registers. If the graph
1139 // is debuggable, we need to deal with these registers differently. For
1140 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001141 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1142 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001143 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001144 }
1145 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001146}
1147
Alexandre Rames3e69f162014-12-10 10:36:50 +00001148size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1149 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1150 __ Str(reg, MemOperand(sp, stack_index));
1151 return kArm64WordSize;
1152}
1153
1154size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1155 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1156 __ Ldr(reg, MemOperand(sp, stack_index));
1157 return kArm64WordSize;
1158}
1159
1160size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1161 FPRegister reg = FPRegister(reg_id, kDRegSize);
1162 __ Str(reg, MemOperand(sp, stack_index));
1163 return kArm64WordSize;
1164}
1165
1166size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1167 FPRegister reg = FPRegister(reg_id, kDRegSize);
1168 __ Ldr(reg, MemOperand(sp, stack_index));
1169 return kArm64WordSize;
1170}
1171
Alexandre Rames5319def2014-10-23 10:03:10 +01001172void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001173 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001174}
1175
1176void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001177 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001178}
1179
Alexandre Rames67555f72014-11-18 10:55:16 +00001180void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001181 if (constant->IsIntConstant()) {
1182 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1183 } else if (constant->IsLongConstant()) {
1184 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1185 } else if (constant->IsNullConstant()) {
1186 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001187 } else if (constant->IsFloatConstant()) {
1188 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1189 } else {
1190 DCHECK(constant->IsDoubleConstant());
1191 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1192 }
1193}
1194
Alexandre Rames3e69f162014-12-10 10:36:50 +00001195
1196static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1197 DCHECK(constant.IsConstant());
1198 HConstant* cst = constant.GetConstant();
1199 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001200 // Null is mapped to a core W register, which we associate with kPrimInt.
1201 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001202 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1203 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1204 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1205}
1206
Calin Juravlee460d1d2015-09-29 04:52:17 +01001207void CodeGeneratorARM64::MoveLocation(Location destination,
1208 Location source,
1209 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001210 if (source.Equals(destination)) {
1211 return;
1212 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001213
1214 // A valid move can always be inferred from the destination and source
1215 // locations. When moving from and to a register, the argument type can be
1216 // used to generate 32bit instead of 64bit moves. In debug mode we also
1217 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001218 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001219
1220 if (destination.IsRegister() || destination.IsFpuRegister()) {
1221 if (unspecified_type) {
1222 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1223 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001224 (src_cst != nullptr && (src_cst->IsIntConstant()
1225 || src_cst->IsFloatConstant()
1226 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001227 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001228 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001229 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001230 // If the source is a double stack slot or a 64bit constant, a 64bit
1231 // type is appropriate. Else the source is a register, and since the
1232 // type has not been specified, we chose a 64bit type to force a 64bit
1233 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001234 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001235 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001236 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001237 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1238 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1239 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001240 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1241 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1242 __ Ldr(dst, StackOperandFrom(source));
1243 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001244 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001245 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001246 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001247 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001248 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001249 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001250 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001251 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1252 ? Primitive::kPrimLong
1253 : Primitive::kPrimInt;
1254 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1255 }
1256 } else {
1257 DCHECK(source.IsFpuRegister());
1258 if (destination.IsRegister()) {
1259 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1260 ? Primitive::kPrimDouble
1261 : Primitive::kPrimFloat;
1262 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1263 } else {
1264 DCHECK(destination.IsFpuRegister());
1265 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001266 }
1267 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001268 } else { // The destination is not a register. It must be a stack slot.
1269 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1270 if (source.IsRegister() || source.IsFpuRegister()) {
1271 if (unspecified_type) {
1272 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001273 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001274 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001275 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001276 }
1277 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001278 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1279 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1280 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001281 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001282 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1283 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001284 UseScratchRegisterScope temps(GetVIXLAssembler());
1285 HConstant* src_cst = source.GetConstant();
1286 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001287 if (src_cst->IsZeroBitPattern()) {
1288 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001289 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001290 if (src_cst->IsIntConstant()) {
1291 temp = temps.AcquireW();
1292 } else if (src_cst->IsLongConstant()) {
1293 temp = temps.AcquireX();
1294 } else if (src_cst->IsFloatConstant()) {
1295 temp = temps.AcquireS();
1296 } else {
1297 DCHECK(src_cst->IsDoubleConstant());
1298 temp = temps.AcquireD();
1299 }
1300 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001301 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001302 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001303 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001304 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001305 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001306 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001307 // There is generally less pressure on FP registers.
1308 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001309 __ Ldr(temp, StackOperandFrom(source));
1310 __ Str(temp, StackOperandFrom(destination));
1311 }
1312 }
1313}
1314
1315void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001316 CPURegister dst,
1317 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001318 switch (type) {
1319 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001320 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001321 break;
1322 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001323 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001324 break;
1325 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001326 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001327 break;
1328 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001329 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001330 break;
1331 case Primitive::kPrimInt:
1332 case Primitive::kPrimNot:
1333 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001334 case Primitive::kPrimFloat:
1335 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001336 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001337 __ Ldr(dst, src);
1338 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001339 case Primitive::kPrimVoid:
1340 LOG(FATAL) << "Unreachable type " << type;
1341 }
1342}
1343
Calin Juravle77520bc2015-01-12 18:45:46 +00001344void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001345 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001346 const MemOperand& src,
1347 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001348 MacroAssembler* masm = GetVIXLAssembler();
1349 BlockPoolsScope block_pools(masm);
1350 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001351 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001352 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001353
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001354 DCHECK(!src.IsPreIndex());
1355 DCHECK(!src.IsPostIndex());
1356
1357 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001358 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001359 MemOperand base = MemOperand(temp_base);
1360 switch (type) {
1361 case Primitive::kPrimBoolean:
1362 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001363 if (needs_null_check) {
1364 MaybeRecordImplicitNullCheck(instruction);
1365 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001366 break;
1367 case Primitive::kPrimByte:
1368 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001369 if (needs_null_check) {
1370 MaybeRecordImplicitNullCheck(instruction);
1371 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001372 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1373 break;
1374 case Primitive::kPrimChar:
1375 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001376 if (needs_null_check) {
1377 MaybeRecordImplicitNullCheck(instruction);
1378 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001379 break;
1380 case Primitive::kPrimShort:
1381 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001382 if (needs_null_check) {
1383 MaybeRecordImplicitNullCheck(instruction);
1384 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001385 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1386 break;
1387 case Primitive::kPrimInt:
1388 case Primitive::kPrimNot:
1389 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001390 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001391 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001392 if (needs_null_check) {
1393 MaybeRecordImplicitNullCheck(instruction);
1394 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001395 break;
1396 case Primitive::kPrimFloat:
1397 case Primitive::kPrimDouble: {
1398 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001399 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001400
1401 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1402 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001403 if (needs_null_check) {
1404 MaybeRecordImplicitNullCheck(instruction);
1405 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001406 __ Fmov(FPRegister(dst), temp);
1407 break;
1408 }
1409 case Primitive::kPrimVoid:
1410 LOG(FATAL) << "Unreachable type " << type;
1411 }
1412}
1413
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001414void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001415 CPURegister src,
1416 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001417 switch (type) {
1418 case Primitive::kPrimBoolean:
1419 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001420 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001421 break;
1422 case Primitive::kPrimChar:
1423 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001424 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001425 break;
1426 case Primitive::kPrimInt:
1427 case Primitive::kPrimNot:
1428 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001429 case Primitive::kPrimFloat:
1430 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001431 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001432 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001433 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001434 case Primitive::kPrimVoid:
1435 LOG(FATAL) << "Unreachable type " << type;
1436 }
1437}
1438
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001439void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1440 CPURegister src,
1441 const MemOperand& dst) {
1442 UseScratchRegisterScope temps(GetVIXLAssembler());
1443 Register temp_base = temps.AcquireX();
1444
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001445 DCHECK(!dst.IsPreIndex());
1446 DCHECK(!dst.IsPostIndex());
1447
1448 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001449 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001450 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001451 MemOperand base = MemOperand(temp_base);
1452 switch (type) {
1453 case Primitive::kPrimBoolean:
1454 case Primitive::kPrimByte:
1455 __ Stlrb(Register(src), base);
1456 break;
1457 case Primitive::kPrimChar:
1458 case Primitive::kPrimShort:
1459 __ Stlrh(Register(src), base);
1460 break;
1461 case Primitive::kPrimInt:
1462 case Primitive::kPrimNot:
1463 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001464 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001465 __ Stlr(Register(src), base);
1466 break;
1467 case Primitive::kPrimFloat:
1468 case Primitive::kPrimDouble: {
1469 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001470 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001471
1472 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1473 __ Fmov(temp, FPRegister(src));
1474 __ Stlr(temp, base);
1475 break;
1476 }
1477 case Primitive::kPrimVoid:
1478 LOG(FATAL) << "Unreachable type " << type;
1479 }
1480}
1481
Calin Juravle175dc732015-08-25 15:42:32 +01001482void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1483 HInstruction* instruction,
1484 uint32_t dex_pc,
1485 SlowPathCode* slow_path) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001486 InvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +01001487 instruction,
1488 dex_pc,
1489 slow_path);
1490}
1491
Alexandre Rames67555f72014-11-18 10:55:16 +00001492void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1493 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001494 uint32_t dex_pc,
1495 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001496 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001497 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001498 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1499 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001500 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001501}
1502
Roland Levillaindec8f632016-07-22 17:10:06 +01001503void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1504 HInstruction* instruction,
1505 SlowPathCode* slow_path) {
1506 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1507 BlockPoolsScope block_pools(GetVIXLAssembler());
1508 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1509 __ Blr(lr);
1510}
1511
Alexandre Rames67555f72014-11-18 10:55:16 +00001512void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001513 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001514 UseScratchRegisterScope temps(GetVIXLAssembler());
1515 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001516 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1517
Serban Constantinescu02164b32014-11-13 14:05:07 +00001518 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001519 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1520 __ Add(temp, class_reg, status_offset);
1521 __ Ldar(temp, HeapOperand(temp));
1522 __ Cmp(temp, mirror::Class::kStatusInitialized);
1523 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001524 __ Bind(slow_path->GetExitLabel());
1525}
Alexandre Rames5319def2014-10-23 10:03:10 +01001526
Roland Levillain44015862016-01-22 11:47:17 +00001527void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001528 BarrierType type = BarrierAll;
1529
1530 switch (kind) {
1531 case MemBarrierKind::kAnyAny:
1532 case MemBarrierKind::kAnyStore: {
1533 type = BarrierAll;
1534 break;
1535 }
1536 case MemBarrierKind::kLoadAny: {
1537 type = BarrierReads;
1538 break;
1539 }
1540 case MemBarrierKind::kStoreStore: {
1541 type = BarrierWrites;
1542 break;
1543 }
1544 default:
1545 LOG(FATAL) << "Unexpected memory barrier " << kind;
1546 }
1547 __ Dmb(InnerShareable, type);
1548}
1549
Serban Constantinescu02164b32014-11-13 14:05:07 +00001550void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1551 HBasicBlock* successor) {
1552 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001553 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1554 if (slow_path == nullptr) {
1555 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1556 instruction->SetSlowPath(slow_path);
1557 codegen_->AddSlowPath(slow_path);
1558 if (successor != nullptr) {
1559 DCHECK(successor->IsLoopHeader());
1560 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1561 }
1562 } else {
1563 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1564 }
1565
Serban Constantinescu02164b32014-11-13 14:05:07 +00001566 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1567 Register temp = temps.AcquireW();
1568
Andreas Gampe542451c2016-07-26 09:02:02 -07001569 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001570 if (successor == nullptr) {
1571 __ Cbnz(temp, slow_path->GetEntryLabel());
1572 __ Bind(slow_path->GetReturnLabel());
1573 } else {
1574 __ Cbz(temp, codegen_->GetLabelOf(successor));
1575 __ B(slow_path->GetEntryLabel());
1576 // slow_path will return to GetLabelOf(successor).
1577 }
1578}
1579
Alexandre Rames5319def2014-10-23 10:03:10 +01001580InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1581 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001582 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001583 assembler_(codegen->GetAssembler()),
1584 codegen_(codegen) {}
1585
1586#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001587 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001588
1589#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1590
1591enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001592 // Using a base helps identify when we hit such breakpoints.
1593 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001594#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1595 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1596#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1597};
1598
1599#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001600 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001601 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1602 } \
1603 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1604 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1605 locations->SetOut(Location::Any()); \
1606 }
1607 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1608#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1609
1610#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001611#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001612
Alexandre Rames67555f72014-11-18 10:55:16 +00001613void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001614 DCHECK_EQ(instr->InputCount(), 2U);
1615 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1616 Primitive::Type type = instr->GetResultType();
1617 switch (type) {
1618 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001619 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001620 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001621 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001622 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001623 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001624
1625 case Primitive::kPrimFloat:
1626 case Primitive::kPrimDouble:
1627 locations->SetInAt(0, Location::RequiresFpuRegister());
1628 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001629 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001630 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001631
Alexandre Rames5319def2014-10-23 10:03:10 +01001632 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001633 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001634 }
1635}
1636
Alexandre Rames09a99962015-04-15 11:47:56 +01001637void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001638 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1639
1640 bool object_field_get_with_read_barrier =
1641 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001642 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001643 new (GetGraph()->GetArena()) LocationSummary(instruction,
1644 object_field_get_with_read_barrier ?
1645 LocationSummary::kCallOnSlowPath :
1646 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001647 locations->SetInAt(0, Location::RequiresRegister());
1648 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1649 locations->SetOut(Location::RequiresFpuRegister());
1650 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001651 // The output overlaps for an object field get when read barriers
1652 // are enabled: we do not want the load to overwrite the object's
1653 // location, as we need it to emit the read barrier.
1654 locations->SetOut(
1655 Location::RequiresRegister(),
1656 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001657 }
1658}
1659
1660void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1661 const FieldInfo& field_info) {
1662 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001663 LocationSummary* locations = instruction->GetLocations();
1664 Location base_loc = locations->InAt(0);
1665 Location out = locations->Out();
1666 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001667 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001668 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001669 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001670
Roland Levillain44015862016-01-22 11:47:17 +00001671 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1672 // Object FieldGet with Baker's read barrier case.
1673 MacroAssembler* masm = GetVIXLAssembler();
1674 UseScratchRegisterScope temps(masm);
1675 // /* HeapReference<Object> */ out = *(base + offset)
1676 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1677 Register temp = temps.AcquireW();
1678 // Note that potential implicit null checks are handled in this
1679 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1680 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1681 instruction,
1682 out,
1683 base,
1684 offset,
1685 temp,
1686 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001687 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001688 } else {
1689 // General case.
1690 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001691 // Note that a potential implicit null check is handled in this
1692 // CodeGeneratorARM64::LoadAcquire call.
1693 // NB: LoadAcquire will record the pc info if needed.
1694 codegen_->LoadAcquire(
1695 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001696 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001697 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001698 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001699 }
Roland Levillain44015862016-01-22 11:47:17 +00001700 if (field_type == Primitive::kPrimNot) {
1701 // If read barriers are enabled, emit read barriers other than
1702 // Baker's using a slow path (and also unpoison the loaded
1703 // reference, if heap poisoning is enabled).
1704 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1705 }
Roland Levillain4d027112015-07-01 15:41:14 +01001706 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001707}
1708
1709void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1710 LocationSummary* locations =
1711 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1712 locations->SetInAt(0, Location::RequiresRegister());
1713 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1714 locations->SetInAt(1, Location::RequiresFpuRegister());
1715 } else {
1716 locations->SetInAt(1, Location::RequiresRegister());
1717 }
1718}
1719
1720void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001721 const FieldInfo& field_info,
1722 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001723 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001724 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001725
1726 Register obj = InputRegisterAt(instruction, 0);
1727 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001728 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001729 Offset offset = field_info.GetFieldOffset();
1730 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001731
Roland Levillain4d027112015-07-01 15:41:14 +01001732 {
1733 // We use a block to end the scratch scope before the write barrier, thus
1734 // freeing the temporary registers so they can be used in `MarkGCCard`.
1735 UseScratchRegisterScope temps(GetVIXLAssembler());
1736
1737 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1738 DCHECK(value.IsW());
1739 Register temp = temps.AcquireW();
1740 __ Mov(temp, value.W());
1741 GetAssembler()->PoisonHeapReference(temp.W());
1742 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001743 }
Roland Levillain4d027112015-07-01 15:41:14 +01001744
1745 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001746 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1747 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001748 } else {
1749 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1750 codegen_->MaybeRecordImplicitNullCheck(instruction);
1751 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001752 }
1753
1754 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001755 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001756 }
1757}
1758
Alexandre Rames67555f72014-11-18 10:55:16 +00001759void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001760 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001761
1762 switch (type) {
1763 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001764 case Primitive::kPrimLong: {
1765 Register dst = OutputRegister(instr);
1766 Register lhs = InputRegisterAt(instr, 0);
1767 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001768 if (instr->IsAdd()) {
1769 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001770 } else if (instr->IsAnd()) {
1771 __ And(dst, lhs, rhs);
1772 } else if (instr->IsOr()) {
1773 __ Orr(dst, lhs, rhs);
1774 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001775 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001776 } else if (instr->IsRor()) {
1777 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001778 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001779 __ Ror(dst, lhs, shift);
1780 } else {
1781 // Ensure shift distance is in the same size register as the result. If
1782 // we are rotating a long and the shift comes in a w register originally,
1783 // we don't need to sxtw for use as an x since the shift distances are
1784 // all & reg_bits - 1.
1785 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1786 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001787 } else {
1788 DCHECK(instr->IsXor());
1789 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001790 }
1791 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001792 }
1793 case Primitive::kPrimFloat:
1794 case Primitive::kPrimDouble: {
1795 FPRegister dst = OutputFPRegister(instr);
1796 FPRegister lhs = InputFPRegisterAt(instr, 0);
1797 FPRegister rhs = InputFPRegisterAt(instr, 1);
1798 if (instr->IsAdd()) {
1799 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001800 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001801 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001802 } else {
1803 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001804 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001805 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001806 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001807 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001808 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001809 }
1810}
1811
Serban Constantinescu02164b32014-11-13 14:05:07 +00001812void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1813 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1814
1815 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1816 Primitive::Type type = instr->GetResultType();
1817 switch (type) {
1818 case Primitive::kPrimInt:
1819 case Primitive::kPrimLong: {
1820 locations->SetInAt(0, Location::RequiresRegister());
1821 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1822 locations->SetOut(Location::RequiresRegister());
1823 break;
1824 }
1825 default:
1826 LOG(FATAL) << "Unexpected shift type " << type;
1827 }
1828}
1829
1830void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1831 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1832
1833 Primitive::Type type = instr->GetType();
1834 switch (type) {
1835 case Primitive::kPrimInt:
1836 case Primitive::kPrimLong: {
1837 Register dst = OutputRegister(instr);
1838 Register lhs = InputRegisterAt(instr, 0);
1839 Operand rhs = InputOperandAt(instr, 1);
1840 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001841 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00001842 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001843 if (instr->IsShl()) {
1844 __ Lsl(dst, lhs, shift_value);
1845 } else if (instr->IsShr()) {
1846 __ Asr(dst, lhs, shift_value);
1847 } else {
1848 __ Lsr(dst, lhs, shift_value);
1849 }
1850 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001851 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001852
1853 if (instr->IsShl()) {
1854 __ Lsl(dst, lhs, rhs_reg);
1855 } else if (instr->IsShr()) {
1856 __ Asr(dst, lhs, rhs_reg);
1857 } else {
1858 __ Lsr(dst, lhs, rhs_reg);
1859 }
1860 }
1861 break;
1862 }
1863 default:
1864 LOG(FATAL) << "Unexpected shift operation type " << type;
1865 }
1866}
1867
Alexandre Rames5319def2014-10-23 10:03:10 +01001868void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001869 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001870}
1871
1872void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001873 HandleBinaryOp(instruction);
1874}
1875
1876void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1877 HandleBinaryOp(instruction);
1878}
1879
1880void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1881 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001882}
1883
Artem Serov7fc63502016-02-09 17:15:29 +00001884void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001885 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1886 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1887 locations->SetInAt(0, Location::RequiresRegister());
1888 // There is no immediate variant of negated bitwise instructions in AArch64.
1889 locations->SetInAt(1, Location::RequiresRegister());
1890 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1891}
1892
Artem Serov7fc63502016-02-09 17:15:29 +00001893void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001894 Register dst = OutputRegister(instr);
1895 Register lhs = InputRegisterAt(instr, 0);
1896 Register rhs = InputRegisterAt(instr, 1);
1897
1898 switch (instr->GetOpKind()) {
1899 case HInstruction::kAnd:
1900 __ Bic(dst, lhs, rhs);
1901 break;
1902 case HInstruction::kOr:
1903 __ Orn(dst, lhs, rhs);
1904 break;
1905 case HInstruction::kXor:
1906 __ Eon(dst, lhs, rhs);
1907 break;
1908 default:
1909 LOG(FATAL) << "Unreachable";
1910 }
1911}
1912
Alexandre Rames8626b742015-11-25 16:28:08 +00001913void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1914 HArm64DataProcWithShifterOp* instruction) {
1915 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1916 instruction->GetType() == Primitive::kPrimLong);
1917 LocationSummary* locations =
1918 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1919 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1920 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1921 } else {
1922 locations->SetInAt(0, Location::RequiresRegister());
1923 }
1924 locations->SetInAt(1, Location::RequiresRegister());
1925 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1926}
1927
1928void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1929 HArm64DataProcWithShifterOp* instruction) {
1930 Primitive::Type type = instruction->GetType();
1931 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1932 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1933 Register out = OutputRegister(instruction);
1934 Register left;
1935 if (kind != HInstruction::kNeg) {
1936 left = InputRegisterAt(instruction, 0);
1937 }
1938 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1939 // shifter operand operation, the IR generating `right_reg` (input to the type
1940 // conversion) can have a different type from the current instruction's type,
1941 // so we manually indicate the type.
1942 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001943 int64_t shift_amount = instruction->GetShiftAmount() &
1944 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001945
1946 Operand right_operand(0);
1947
1948 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1949 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1950 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1951 } else {
1952 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1953 }
1954
1955 // Logical binary operations do not support extension operations in the
1956 // operand. Note that VIXL would still manage if it was passed by generating
1957 // the extension as a separate instruction.
1958 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1959 DCHECK(!right_operand.IsExtendedRegister() ||
1960 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1961 kind != HInstruction::kNeg));
1962 switch (kind) {
1963 case HInstruction::kAdd:
1964 __ Add(out, left, right_operand);
1965 break;
1966 case HInstruction::kAnd:
1967 __ And(out, left, right_operand);
1968 break;
1969 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001970 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001971 __ Neg(out, right_operand);
1972 break;
1973 case HInstruction::kOr:
1974 __ Orr(out, left, right_operand);
1975 break;
1976 case HInstruction::kSub:
1977 __ Sub(out, left, right_operand);
1978 break;
1979 case HInstruction::kXor:
1980 __ Eor(out, left, right_operand);
1981 break;
1982 default:
1983 LOG(FATAL) << "Unexpected operation kind: " << kind;
1984 UNREACHABLE();
1985 }
1986}
1987
Artem Serov328429f2016-07-06 16:23:04 +01001988void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00001989 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
1990 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001991 LocationSummary* locations =
1992 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1993 locations->SetInAt(0, Location::RequiresRegister());
1994 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1995 locations->SetOut(Location::RequiresRegister());
1996}
1997
Roland Levillain4a3aa572016-08-15 13:17:06 +00001998void InstructionCodeGeneratorARM64::VisitIntermediateAddress(
1999 HIntermediateAddress* instruction) {
2000 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2001 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002002 __ Add(OutputRegister(instruction),
2003 InputRegisterAt(instruction, 0),
2004 Operand(InputOperandAt(instruction, 1)));
2005}
2006
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002007void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002008 LocationSummary* locations =
2009 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002010 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2011 if (instr->GetOpKind() == HInstruction::kSub &&
2012 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002013 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002014 // Don't allocate register for Mneg instruction.
2015 } else {
2016 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2017 Location::RequiresRegister());
2018 }
2019 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2020 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002021 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2022}
2023
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002024void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002025 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002026 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2027 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002028
2029 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2030 // This fixup should be carried out for all multiply-accumulate instructions:
2031 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2032 if (instr->GetType() == Primitive::kPrimLong &&
2033 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2034 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002035 vixl::aarch64::Instruction* prev =
2036 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002037 if (prev->IsLoadOrStore()) {
2038 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002039 vixl::aarch64::CodeBufferCheckScope scope(masm,
2040 kInstructionSize,
2041 vixl::aarch64::CodeBufferCheckScope::kCheck,
2042 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002043 __ nop();
2044 }
2045 }
2046
2047 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002048 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002049 __ Madd(res, mul_left, mul_right, accumulator);
2050 } else {
2051 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002052 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002053 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002054 __ Mneg(res, mul_left, mul_right);
2055 } else {
2056 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2057 __ Msub(res, mul_left, mul_right, accumulator);
2058 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002059 }
2060}
2061
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002062void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002063 bool object_array_get_with_read_barrier =
2064 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002065 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002066 new (GetGraph()->GetArena()) LocationSummary(instruction,
2067 object_array_get_with_read_barrier ?
2068 LocationSummary::kCallOnSlowPath :
2069 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002070 locations->SetInAt(0, Location::RequiresRegister());
2071 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002072 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2073 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2074 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002075 // The output overlaps in the case of an object array get with
2076 // read barriers enabled: we do not want the move to overwrite the
2077 // array's location, as we need it to emit the read barrier.
2078 locations->SetOut(
2079 Location::RequiresRegister(),
2080 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002081 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002082}
2083
2084void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002085 Primitive::Type type = instruction->GetType();
2086 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002087 LocationSummary* locations = instruction->GetLocations();
2088 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002089 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002090 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002091
Alexandre Ramesd921d642015-04-16 15:07:16 +01002092 MacroAssembler* masm = GetVIXLAssembler();
2093 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002094 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002095 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002096
Roland Levillain44015862016-01-22 11:47:17 +00002097 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2098 // Object ArrayGet with Baker's read barrier case.
2099 Register temp = temps.AcquireW();
Roland Levillain4a3aa572016-08-15 13:17:06 +00002100 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2101 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Roland Levillain44015862016-01-22 11:47:17 +00002102 // Note that a potential implicit null check is handled in the
2103 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2104 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2105 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002106 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002107 // General case.
2108 MemOperand source = HeapOperand(obj);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002109 if (index.IsConstant()) {
Roland Levillain44015862016-01-22 11:47:17 +00002110 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2111 source = HeapOperand(obj, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002112 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002113 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002114 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002115 // The read barrier instrumentation does not support the
2116 // HIntermediateAddress instruction yet.
2117 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00002118 // We do not need to compute the intermediate address from the array: the
2119 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002120 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002121 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002122 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002123 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2124 }
2125 temp = obj;
2126 } else {
2127 __ Add(temp, obj, offset);
2128 }
2129 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2130 }
2131
2132 codegen_->Load(type, OutputCPURegister(instruction), source);
2133 codegen_->MaybeRecordImplicitNullCheck(instruction);
2134
2135 if (type == Primitive::kPrimNot) {
2136 static_assert(
2137 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2138 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2139 Location obj_loc = locations->InAt(0);
2140 if (index.IsConstant()) {
2141 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2142 } else {
2143 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2144 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002145 }
Roland Levillain4d027112015-07-01 15:41:14 +01002146 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002147}
2148
Alexandre Rames5319def2014-10-23 10:03:10 +01002149void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2150 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2151 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002152 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002153}
2154
2155void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002156 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002157 BlockPoolsScope block_pools(GetVIXLAssembler());
Vladimir Markodce016e2016-04-28 13:10:02 +01002158 __ Ldr(OutputRegister(instruction), HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002159 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002160}
2161
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002162void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002163 Primitive::Type value_type = instruction->GetComponentType();
2164
2165 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2166 bool object_array_set_with_read_barrier =
2167 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002168 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2169 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002170 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2171 LocationSummary::kCallOnSlowPath :
2172 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002173 locations->SetInAt(0, Location::RequiresRegister());
2174 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002175 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002176 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002177 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002178 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002179 }
Roland Levillain16d9f942016-08-25 17:27:56 +01002180 if (object_array_set_with_read_barrier && kUseBakerReadBarrier) {
2181 // Additional temporary registers for a Baker read barrier.
2182 locations->AddTemp(Location::RequiresRegister());
2183 locations->AddTemp(Location::RequiresRegister());
2184 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002185}
2186
2187void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2188 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002189 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002190 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002191 bool needs_write_barrier =
2192 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002193
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002194 Register array = InputRegisterAt(instruction, 0);
2195 CPURegister value = InputCPURegisterAt(instruction, 2);
2196 CPURegister source = value;
2197 Location index = locations->InAt(1);
2198 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2199 MemOperand destination = HeapOperand(array);
2200 MacroAssembler* masm = GetVIXLAssembler();
2201 BlockPoolsScope block_pools(masm);
2202
2203 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002204 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002205 if (index.IsConstant()) {
2206 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2207 destination = HeapOperand(array, offset);
2208 } else {
2209 UseScratchRegisterScope temps(masm);
2210 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002211 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002212 // The read barrier instrumentation does not support the
2213 // HIntermediateAddress instruction yet.
2214 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002215 // We do not need to compute the intermediate address from the array: the
2216 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002217 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002218 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002219 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002220 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2221 }
2222 temp = array;
2223 } else {
2224 __ Add(temp, array, offset);
2225 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002226 destination = HeapOperand(temp,
2227 XRegisterFrom(index),
2228 LSL,
2229 Primitive::ComponentSizeShift(value_type));
2230 }
2231 codegen_->Store(value_type, value, destination);
2232 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002233 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002234 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002235 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002236 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002237 {
2238 // We use a block to end the scratch scope before the write barrier, thus
2239 // freeing the temporary registers so they can be used in `MarkGCCard`.
2240 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002241 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002242 if (index.IsConstant()) {
2243 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002244 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002245 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002246 destination = HeapOperand(temp,
2247 XRegisterFrom(index),
2248 LSL,
2249 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002250 }
2251
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002252 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2253 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2254 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2255
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002256 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002257 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2258 codegen_->AddSlowPath(slow_path);
2259 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002260 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002261 __ Cbnz(Register(value), &non_zero);
2262 if (!index.IsConstant()) {
2263 __ Add(temp, array, offset);
2264 }
2265 __ Str(wzr, destination);
2266 codegen_->MaybeRecordImplicitNullCheck(instruction);
2267 __ B(&done);
2268 __ Bind(&non_zero);
2269 }
2270
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002271 if (kEmitCompilerReadBarrier) {
Roland Levillain16d9f942016-08-25 17:27:56 +01002272 if (!kUseBakerReadBarrier) {
2273 // When (non-Baker) read barriers are enabled, the type
2274 // checking instrumentation requires two read barriers
2275 // generated by CodeGeneratorARM64::GenerateReadBarrierSlow:
2276 //
2277 // __ Mov(temp2, temp);
2278 // // /* HeapReference<Class> */ temp = temp->component_type_
2279 // __ Ldr(temp, HeapOperand(temp, component_offset));
2280 // codegen_->GenerateReadBarrierSlow(
2281 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2282 //
2283 // // /* HeapReference<Class> */ temp2 = value->klass_
2284 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2285 // codegen_->GenerateReadBarrierSlow(
2286 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2287 //
2288 // __ Cmp(temp, temp2);
2289 //
2290 // However, the second read barrier may trash `temp`, as it
2291 // is a temporary register, and as such would not be saved
2292 // along with live registers before calling the runtime (nor
2293 // restored afterwards). So in this case, we bail out and
2294 // delegate the work to the array set slow path.
2295 //
2296 // TODO: Extend the register allocator to support a new
2297 // "(locally) live temp" location so as to avoid always
2298 // going into the slow path when read barriers are enabled?
2299 //
2300 // There is no such problem with Baker read barriers (see below).
2301 __ B(slow_path->GetEntryLabel());
2302 } else {
2303 // Note that we cannot use `temps` (instance of VIXL's
2304 // UseScratchRegisterScope) to allocate `temp2` because
2305 // the Baker read barriers generated by
2306 // GenerateFieldLoadWithBakerReadBarrier below also use
2307 // that facility to allocate a temporary register, thus
2308 // making VIXL's scratch register pool empty.
2309 Location temp2_loc = locations->GetTemp(0);
2310 Register temp2 = WRegisterFrom(temp2_loc);
2311
2312 // Note: Because it is acquired from VIXL's scratch register
2313 // pool, `temp` might be IP0, and thus cannot be used as
2314 // `ref` argument of GenerateFieldLoadWithBakerReadBarrier
2315 // calls below (see ReadBarrierMarkSlowPathARM64 for more
2316 // details).
2317
2318 // /* HeapReference<Class> */ temp2 = array->klass_
2319 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2320 temp2_loc,
2321 array,
2322 class_offset,
2323 temp,
2324 /* needs_null_check */ true,
2325 /* use_load_acquire */ false);
2326
2327 // /* HeapReference<Class> */ temp2 = temp2->component_type_
2328 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2329 temp2_loc,
2330 temp2,
2331 component_offset,
2332 temp,
2333 /* needs_null_check */ false,
2334 /* use_load_acquire */ false);
2335 // For the same reason that we request `temp2` from the
2336 // register allocator above, we cannot get `temp3` from
2337 // VIXL's scratch register pool.
2338 Location temp3_loc = locations->GetTemp(1);
2339 Register temp3 = WRegisterFrom(temp3_loc);
2340 // Register `temp2` is not trashed by the read barrier
2341 // emitted by GenerateFieldLoadWithBakerReadBarrier below,
2342 // as that method produces a call to a ReadBarrierMarkRegX
2343 // entry point, which saves all potentially live registers,
2344 // including temporaries such a `temp2`.
2345 // /* HeapReference<Class> */ temp3 = register_value->klass_
2346 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2347 temp3_loc,
2348 value.W(),
2349 class_offset,
2350 temp,
2351 /* needs_null_check */ false,
2352 /* use_load_acquire */ false);
2353 // If heap poisoning is enabled, `temp2` and `temp3` have
2354 // been unpoisoned by the the previous calls to
2355 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
2356 __ Cmp(temp2, temp3);
2357
2358 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2359 vixl::aarch64::Label do_put;
2360 __ B(eq, &do_put);
2361 // We do not need to emit a read barrier for the
2362 // following heap reference load, as `temp2` is only used
2363 // in a comparison with null below, and this reference
2364 // is not kept afterwards.
2365 // /* HeapReference<Class> */ temp = temp2->super_class_
2366 __ Ldr(temp, HeapOperand(temp2, super_offset));
2367 // If heap poisoning is enabled, no need to unpoison
2368 // `temp`, as we are comparing against null below.
2369 __ Cbnz(temp, slow_path->GetEntryLabel());
2370 __ Bind(&do_put);
2371 } else {
2372 __ B(ne, slow_path->GetEntryLabel());
2373 }
2374 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002375 } else {
Roland Levillain16d9f942016-08-25 17:27:56 +01002376 // Non read barrier code.
2377
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002378 Register temp2 = temps.AcquireSameSizeAs(array);
2379 // /* HeapReference<Class> */ temp = array->klass_
2380 __ Ldr(temp, HeapOperand(array, class_offset));
2381 codegen_->MaybeRecordImplicitNullCheck(instruction);
2382 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2383
2384 // /* HeapReference<Class> */ temp = temp->component_type_
2385 __ Ldr(temp, HeapOperand(temp, component_offset));
2386 // /* HeapReference<Class> */ temp2 = value->klass_
2387 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2388 // If heap poisoning is enabled, no need to unpoison `temp`
2389 // nor `temp2`, as we are comparing two poisoned references.
2390 __ Cmp(temp, temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002391 temps.Release(temp2);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002392
2393 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002394 vixl::aarch64::Label do_put;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002395 __ B(eq, &do_put);
2396 // If heap poisoning is enabled, the `temp` reference has
2397 // not been unpoisoned yet; unpoison it now.
2398 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2399
2400 // /* HeapReference<Class> */ temp = temp->super_class_
2401 __ Ldr(temp, HeapOperand(temp, super_offset));
2402 // If heap poisoning is enabled, no need to unpoison
2403 // `temp`, as we are comparing against null below.
2404 __ Cbnz(temp, slow_path->GetEntryLabel());
2405 __ Bind(&do_put);
2406 } else {
2407 __ B(ne, slow_path->GetEntryLabel());
2408 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002409 }
2410 }
2411
2412 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002413 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002414 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002415 __ Mov(temp2, value.W());
2416 GetAssembler()->PoisonHeapReference(temp2);
2417 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002418 }
2419
2420 if (!index.IsConstant()) {
2421 __ Add(temp, array, offset);
2422 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002423 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002424
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002425 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002426 codegen_->MaybeRecordImplicitNullCheck(instruction);
2427 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002428 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002429
2430 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2431
2432 if (done.IsLinked()) {
2433 __ Bind(&done);
2434 }
2435
2436 if (slow_path != nullptr) {
2437 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002438 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002439 }
2440}
2441
Alexandre Rames67555f72014-11-18 10:55:16 +00002442void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002443 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2444 ? LocationSummary::kCallOnSlowPath
2445 : LocationSummary::kNoCall;
2446 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002447 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002448 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002449 if (instruction->HasUses()) {
2450 locations->SetOut(Location::SameAsFirstInput());
2451 }
2452}
2453
2454void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002455 BoundsCheckSlowPathARM64* slow_path =
2456 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002457 codegen_->AddSlowPath(slow_path);
2458
2459 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2460 __ B(slow_path->GetEntryLabel(), hs);
2461}
2462
Alexandre Rames67555f72014-11-18 10:55:16 +00002463void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2464 LocationSummary* locations =
2465 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2466 locations->SetInAt(0, Location::RequiresRegister());
2467 if (check->HasUses()) {
2468 locations->SetOut(Location::SameAsFirstInput());
2469 }
2470}
2471
2472void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2473 // We assume the class is not null.
2474 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2475 check->GetLoadClass(), check, check->GetDexPc(), true);
2476 codegen_->AddSlowPath(slow_path);
2477 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2478}
2479
Roland Levillain1a653882016-03-18 18:05:57 +00002480static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2481 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2482 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2483}
2484
2485void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2486 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2487 Location rhs_loc = instruction->GetLocations()->InAt(1);
2488 if (rhs_loc.IsConstant()) {
2489 // 0.0 is the only immediate that can be encoded directly in
2490 // an FCMP instruction.
2491 //
2492 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2493 // specify that in a floating-point comparison, positive zero
2494 // and negative zero are considered equal, so we can use the
2495 // literal 0.0 for both cases here.
2496 //
2497 // Note however that some methods (Float.equal, Float.compare,
2498 // Float.compareTo, Double.equal, Double.compare,
2499 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2500 // StrictMath.min) consider 0.0 to be (strictly) greater than
2501 // -0.0. So if we ever translate calls to these methods into a
2502 // HCompare instruction, we must handle the -0.0 case with
2503 // care here.
2504 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2505 __ Fcmp(lhs_reg, 0.0);
2506 } else {
2507 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2508 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002509}
2510
Serban Constantinescu02164b32014-11-13 14:05:07 +00002511void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002512 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002513 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2514 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002515 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002516 case Primitive::kPrimBoolean:
2517 case Primitive::kPrimByte:
2518 case Primitive::kPrimShort:
2519 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002520 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002521 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002522 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002523 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002524 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2525 break;
2526 }
2527 case Primitive::kPrimFloat:
2528 case Primitive::kPrimDouble: {
2529 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002530 locations->SetInAt(1,
2531 IsFloatingPointZeroConstant(compare->InputAt(1))
2532 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2533 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002534 locations->SetOut(Location::RequiresRegister());
2535 break;
2536 }
2537 default:
2538 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2539 }
2540}
2541
2542void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2543 Primitive::Type in_type = compare->InputAt(0)->GetType();
2544
2545 // 0 if: left == right
2546 // 1 if: left > right
2547 // -1 if: left < right
2548 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002549 case Primitive::kPrimBoolean:
2550 case Primitive::kPrimByte:
2551 case Primitive::kPrimShort:
2552 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002553 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002554 case Primitive::kPrimLong: {
2555 Register result = OutputRegister(compare);
2556 Register left = InputRegisterAt(compare, 0);
2557 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002558 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002559 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2560 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002561 break;
2562 }
2563 case Primitive::kPrimFloat:
2564 case Primitive::kPrimDouble: {
2565 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002566 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002567 __ Cset(result, ne);
2568 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002569 break;
2570 }
2571 default:
2572 LOG(FATAL) << "Unimplemented compare type " << in_type;
2573 }
2574}
2575
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002576void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002577 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002578
2579 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2580 locations->SetInAt(0, Location::RequiresFpuRegister());
2581 locations->SetInAt(1,
2582 IsFloatingPointZeroConstant(instruction->InputAt(1))
2583 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2584 : Location::RequiresFpuRegister());
2585 } else {
2586 // Integer cases.
2587 locations->SetInAt(0, Location::RequiresRegister());
2588 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2589 }
2590
David Brazdilb3e773e2016-01-26 11:28:37 +00002591 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002592 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002593 }
2594}
2595
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002596void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002597 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002598 return;
2599 }
2600
2601 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002602 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002603 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002604
Roland Levillain7f63c522015-07-13 15:54:55 +00002605 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002606 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002607 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002608 } else {
2609 // Integer cases.
2610 Register lhs = InputRegisterAt(instruction, 0);
2611 Operand rhs = InputOperandAt(instruction, 1);
2612 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002613 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002614 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002615}
2616
2617#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2618 M(Equal) \
2619 M(NotEqual) \
2620 M(LessThan) \
2621 M(LessThanOrEqual) \
2622 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002623 M(GreaterThanOrEqual) \
2624 M(Below) \
2625 M(BelowOrEqual) \
2626 M(Above) \
2627 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002628#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002629void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2630void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002631FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002632#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002633#undef FOR_EACH_CONDITION_INSTRUCTION
2634
Zheng Xuc6667102015-05-15 16:08:45 +08002635void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2636 DCHECK(instruction->IsDiv() || instruction->IsRem());
2637
2638 LocationSummary* locations = instruction->GetLocations();
2639 Location second = locations->InAt(1);
2640 DCHECK(second.IsConstant());
2641
2642 Register out = OutputRegister(instruction);
2643 Register dividend = InputRegisterAt(instruction, 0);
2644 int64_t imm = Int64FromConstant(second.GetConstant());
2645 DCHECK(imm == 1 || imm == -1);
2646
2647 if (instruction->IsRem()) {
2648 __ Mov(out, 0);
2649 } else {
2650 if (imm == 1) {
2651 __ Mov(out, dividend);
2652 } else {
2653 __ Neg(out, dividend);
2654 }
2655 }
2656}
2657
2658void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2659 DCHECK(instruction->IsDiv() || instruction->IsRem());
2660
2661 LocationSummary* locations = instruction->GetLocations();
2662 Location second = locations->InAt(1);
2663 DCHECK(second.IsConstant());
2664
2665 Register out = OutputRegister(instruction);
2666 Register dividend = InputRegisterAt(instruction, 0);
2667 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002668 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002669 int ctz_imm = CTZ(abs_imm);
2670
2671 UseScratchRegisterScope temps(GetVIXLAssembler());
2672 Register temp = temps.AcquireSameSizeAs(out);
2673
2674 if (instruction->IsDiv()) {
2675 __ Add(temp, dividend, abs_imm - 1);
2676 __ Cmp(dividend, 0);
2677 __ Csel(out, temp, dividend, lt);
2678 if (imm > 0) {
2679 __ Asr(out, out, ctz_imm);
2680 } else {
2681 __ Neg(out, Operand(out, ASR, ctz_imm));
2682 }
2683 } else {
2684 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2685 __ Asr(temp, dividend, bits - 1);
2686 __ Lsr(temp, temp, bits - ctz_imm);
2687 __ Add(out, dividend, temp);
2688 __ And(out, out, abs_imm - 1);
2689 __ Sub(out, out, temp);
2690 }
2691}
2692
2693void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2694 DCHECK(instruction->IsDiv() || instruction->IsRem());
2695
2696 LocationSummary* locations = instruction->GetLocations();
2697 Location second = locations->InAt(1);
2698 DCHECK(second.IsConstant());
2699
2700 Register out = OutputRegister(instruction);
2701 Register dividend = InputRegisterAt(instruction, 0);
2702 int64_t imm = Int64FromConstant(second.GetConstant());
2703
2704 Primitive::Type type = instruction->GetResultType();
2705 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2706
2707 int64_t magic;
2708 int shift;
2709 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2710
2711 UseScratchRegisterScope temps(GetVIXLAssembler());
2712 Register temp = temps.AcquireSameSizeAs(out);
2713
2714 // temp = get_high(dividend * magic)
2715 __ Mov(temp, magic);
2716 if (type == Primitive::kPrimLong) {
2717 __ Smulh(temp, dividend, temp);
2718 } else {
2719 __ Smull(temp.X(), dividend, temp);
2720 __ Lsr(temp.X(), temp.X(), 32);
2721 }
2722
2723 if (imm > 0 && magic < 0) {
2724 __ Add(temp, temp, dividend);
2725 } else if (imm < 0 && magic > 0) {
2726 __ Sub(temp, temp, dividend);
2727 }
2728
2729 if (shift != 0) {
2730 __ Asr(temp, temp, shift);
2731 }
2732
2733 if (instruction->IsDiv()) {
2734 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2735 } else {
2736 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2737 // TODO: Strength reduction for msub.
2738 Register temp_imm = temps.AcquireSameSizeAs(out);
2739 __ Mov(temp_imm, imm);
2740 __ Msub(out, temp, temp_imm, dividend);
2741 }
2742}
2743
2744void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2745 DCHECK(instruction->IsDiv() || instruction->IsRem());
2746 Primitive::Type type = instruction->GetResultType();
2747 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2748
2749 LocationSummary* locations = instruction->GetLocations();
2750 Register out = OutputRegister(instruction);
2751 Location second = locations->InAt(1);
2752
2753 if (second.IsConstant()) {
2754 int64_t imm = Int64FromConstant(second.GetConstant());
2755
2756 if (imm == 0) {
2757 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2758 } else if (imm == 1 || imm == -1) {
2759 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002760 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002761 DivRemByPowerOfTwo(instruction);
2762 } else {
2763 DCHECK(imm <= -2 || imm >= 2);
2764 GenerateDivRemWithAnyConstant(instruction);
2765 }
2766 } else {
2767 Register dividend = InputRegisterAt(instruction, 0);
2768 Register divisor = InputRegisterAt(instruction, 1);
2769 if (instruction->IsDiv()) {
2770 __ Sdiv(out, dividend, divisor);
2771 } else {
2772 UseScratchRegisterScope temps(GetVIXLAssembler());
2773 Register temp = temps.AcquireSameSizeAs(out);
2774 __ Sdiv(temp, dividend, divisor);
2775 __ Msub(out, temp, divisor, dividend);
2776 }
2777 }
2778}
2779
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002780void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2781 LocationSummary* locations =
2782 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2783 switch (div->GetResultType()) {
2784 case Primitive::kPrimInt:
2785 case Primitive::kPrimLong:
2786 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002787 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002788 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2789 break;
2790
2791 case Primitive::kPrimFloat:
2792 case Primitive::kPrimDouble:
2793 locations->SetInAt(0, Location::RequiresFpuRegister());
2794 locations->SetInAt(1, Location::RequiresFpuRegister());
2795 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2796 break;
2797
2798 default:
2799 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2800 }
2801}
2802
2803void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2804 Primitive::Type type = div->GetResultType();
2805 switch (type) {
2806 case Primitive::kPrimInt:
2807 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002808 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002809 break;
2810
2811 case Primitive::kPrimFloat:
2812 case Primitive::kPrimDouble:
2813 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2814 break;
2815
2816 default:
2817 LOG(FATAL) << "Unexpected div type " << type;
2818 }
2819}
2820
Alexandre Rames67555f72014-11-18 10:55:16 +00002821void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002822 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2823 ? LocationSummary::kCallOnSlowPath
2824 : LocationSummary::kNoCall;
2825 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002826 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2827 if (instruction->HasUses()) {
2828 locations->SetOut(Location::SameAsFirstInput());
2829 }
2830}
2831
2832void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2833 SlowPathCodeARM64* slow_path =
2834 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2835 codegen_->AddSlowPath(slow_path);
2836 Location value = instruction->GetLocations()->InAt(0);
2837
Alexandre Rames3e69f162014-12-10 10:36:50 +00002838 Primitive::Type type = instruction->GetType();
2839
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002840 if (!Primitive::IsIntegralType(type)) {
2841 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002842 return;
2843 }
2844
Alexandre Rames67555f72014-11-18 10:55:16 +00002845 if (value.IsConstant()) {
2846 int64_t divisor = Int64ConstantFrom(value);
2847 if (divisor == 0) {
2848 __ B(slow_path->GetEntryLabel());
2849 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002850 // A division by a non-null constant is valid. We don't need to perform
2851 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002852 }
2853 } else {
2854 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2855 }
2856}
2857
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002858void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2859 LocationSummary* locations =
2860 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2861 locations->SetOut(Location::ConstantLocation(constant));
2862}
2863
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002864void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2865 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002866 // Will be generated at use site.
2867}
2868
Alexandre Rames5319def2014-10-23 10:03:10 +01002869void LocationsBuilderARM64::VisitExit(HExit* exit) {
2870 exit->SetLocations(nullptr);
2871}
2872
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002873void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002874}
2875
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002876void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2877 LocationSummary* locations =
2878 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2879 locations->SetOut(Location::ConstantLocation(constant));
2880}
2881
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002882void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002883 // Will be generated at use site.
2884}
2885
David Brazdilfc6a86a2015-06-26 10:33:45 +00002886void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002887 DCHECK(!successor->IsExitBlock());
2888 HBasicBlock* block = got->GetBlock();
2889 HInstruction* previous = got->GetPrevious();
2890 HLoopInformation* info = block->GetLoopInformation();
2891
David Brazdil46e2a392015-03-16 17:31:52 +00002892 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002893 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2894 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2895 return;
2896 }
2897 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2898 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2899 }
2900 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002901 __ B(codegen_->GetLabelOf(successor));
2902 }
2903}
2904
David Brazdilfc6a86a2015-06-26 10:33:45 +00002905void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2906 got->SetLocations(nullptr);
2907}
2908
2909void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2910 HandleGoto(got, got->GetSuccessor());
2911}
2912
2913void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2914 try_boundary->SetLocations(nullptr);
2915}
2916
2917void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2918 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2919 if (!successor->IsExitBlock()) {
2920 HandleGoto(try_boundary, successor);
2921 }
2922}
2923
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002924void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002925 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002926 vixl::aarch64::Label* true_target,
2927 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002928 // FP branching requires both targets to be explicit. If either of the targets
2929 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002930 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002931 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002932
David Brazdil0debae72015-11-12 18:37:00 +00002933 if (true_target == nullptr && false_target == nullptr) {
2934 // Nothing to do. The code always falls through.
2935 return;
2936 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002937 // Constant condition, statically compared against "true" (integer value 1).
2938 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002939 if (true_target != nullptr) {
2940 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002941 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002942 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002943 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002944 if (false_target != nullptr) {
2945 __ B(false_target);
2946 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002947 }
David Brazdil0debae72015-11-12 18:37:00 +00002948 return;
2949 }
2950
2951 // The following code generates these patterns:
2952 // (1) true_target == nullptr && false_target != nullptr
2953 // - opposite condition true => branch to false_target
2954 // (2) true_target != nullptr && false_target == nullptr
2955 // - condition true => branch to true_target
2956 // (3) true_target != nullptr && false_target != nullptr
2957 // - condition true => branch to true_target
2958 // - branch to false_target
2959 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002960 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002961 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002962 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002963 if (true_target == nullptr) {
2964 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2965 } else {
2966 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2967 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002968 } else {
2969 // The condition instruction has not been materialized, use its inputs as
2970 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002971 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002972
David Brazdil0debae72015-11-12 18:37:00 +00002973 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002974 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002975 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002976 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002977 IfCondition opposite_condition = condition->GetOppositeCondition();
2978 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002979 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002980 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002981 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002982 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002983 // Integer cases.
2984 Register lhs = InputRegisterAt(condition, 0);
2985 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002986
2987 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01002988 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002989 if (true_target == nullptr) {
2990 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2991 non_fallthrough_target = false_target;
2992 } else {
2993 arm64_cond = ARM64Condition(condition->GetCondition());
2994 non_fallthrough_target = true_target;
2995 }
2996
Aart Bik086d27e2016-01-20 17:02:00 -08002997 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01002998 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002999 switch (arm64_cond) {
3000 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003001 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003002 break;
3003 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003004 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003005 break;
3006 case lt:
3007 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003008 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003009 break;
3010 case ge:
3011 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003012 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003013 break;
3014 default:
3015 // Without the `static_cast` the compiler throws an error for
3016 // `-Werror=sign-promo`.
3017 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3018 }
3019 } else {
3020 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003021 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003022 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003023 }
3024 }
David Brazdil0debae72015-11-12 18:37:00 +00003025
3026 // If neither branch falls through (case 3), the conditional branch to `true_target`
3027 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3028 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003029 __ B(false_target);
3030 }
David Brazdil0debae72015-11-12 18:37:00 +00003031
3032 if (fallthrough_target.IsLinked()) {
3033 __ Bind(&fallthrough_target);
3034 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003035}
3036
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003037void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3038 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003039 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003040 locations->SetInAt(0, Location::RequiresRegister());
3041 }
3042}
3043
3044void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003045 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3046 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003047 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3048 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3049 true_target = nullptr;
3050 }
3051 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3052 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3053 false_target = nullptr;
3054 }
David Brazdil0debae72015-11-12 18:37:00 +00003055 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003056}
3057
3058void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3059 LocationSummary* locations = new (GetGraph()->GetArena())
3060 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003061 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003062 locations->SetInAt(0, Location::RequiresRegister());
3063 }
3064}
3065
3066void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003067 SlowPathCodeARM64* slow_path =
3068 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003069 GenerateTestAndBranch(deoptimize,
3070 /* condition_input_index */ 0,
3071 slow_path->GetEntryLabel(),
3072 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003073}
3074
David Brazdilc0b601b2016-02-08 14:20:45 +00003075static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3076 return condition->IsCondition() &&
3077 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3078}
3079
Alexandre Rames880f1192016-06-13 16:04:50 +01003080static inline Condition GetConditionForSelect(HCondition* condition) {
3081 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003082 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3083 : ARM64Condition(cond);
3084}
3085
David Brazdil74eb1b22015-12-14 11:44:01 +00003086void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3087 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003088 if (Primitive::IsFloatingPointType(select->GetType())) {
3089 locations->SetInAt(0, Location::RequiresFpuRegister());
3090 locations->SetInAt(1, Location::RequiresFpuRegister());
3091 locations->SetOut(Location::RequiresFpuRegister());
3092 } else {
3093 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3094 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3095 bool is_true_value_constant = cst_true_value != nullptr;
3096 bool is_false_value_constant = cst_false_value != nullptr;
3097 // Ask VIXL whether we should synthesize constants in registers.
3098 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3099 Operand true_op = is_true_value_constant ?
3100 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3101 Operand false_op = is_false_value_constant ?
3102 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3103 bool true_value_in_register = false;
3104 bool false_value_in_register = false;
3105 MacroAssembler::GetCselSynthesisInformation(
3106 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3107 true_value_in_register |= !is_true_value_constant;
3108 false_value_in_register |= !is_false_value_constant;
3109
3110 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3111 : Location::ConstantLocation(cst_true_value));
3112 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3113 : Location::ConstantLocation(cst_false_value));
3114 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003115 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003116
David Brazdil74eb1b22015-12-14 11:44:01 +00003117 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3118 locations->SetInAt(2, Location::RequiresRegister());
3119 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003120}
3121
3122void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003123 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003124 Condition csel_cond;
3125
3126 if (IsBooleanValueOrMaterializedCondition(cond)) {
3127 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003128 // Use the condition flags set by the previous instruction.
3129 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003130 } else {
3131 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003132 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003133 }
3134 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003135 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003136 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003137 } else {
3138 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003139 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003140 }
3141
Alexandre Rames880f1192016-06-13 16:04:50 +01003142 if (Primitive::IsFloatingPointType(select->GetType())) {
3143 __ Fcsel(OutputFPRegister(select),
3144 InputFPRegisterAt(select, 1),
3145 InputFPRegisterAt(select, 0),
3146 csel_cond);
3147 } else {
3148 __ Csel(OutputRegister(select),
3149 InputOperandAt(select, 1),
3150 InputOperandAt(select, 0),
3151 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003152 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003153}
3154
David Srbecky0cf44932015-12-09 14:09:59 +00003155void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3156 new (GetGraph()->GetArena()) LocationSummary(info);
3157}
3158
David Srbeckyd28f4a02016-03-14 17:14:24 +00003159void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3160 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003161}
3162
3163void CodeGeneratorARM64::GenerateNop() {
3164 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003165}
3166
Alexandre Rames5319def2014-10-23 10:03:10 +01003167void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003168 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003169}
3170
3171void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003172 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003173}
3174
3175void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003176 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003177}
3178
3179void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003180 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003181}
3182
Roland Levillain44015862016-01-22 11:47:17 +00003183static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3184 return kEmitCompilerReadBarrier &&
3185 (kUseBakerReadBarrier ||
3186 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3187 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3188 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3189}
3190
Alexandre Rames67555f72014-11-18 10:55:16 +00003191void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003192 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003193 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3194 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003195 case TypeCheckKind::kExactCheck:
3196 case TypeCheckKind::kAbstractClassCheck:
3197 case TypeCheckKind::kClassHierarchyCheck:
3198 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003199 call_kind =
3200 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003201 break;
3202 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003203 case TypeCheckKind::kUnresolvedCheck:
3204 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003205 call_kind = LocationSummary::kCallOnSlowPath;
3206 break;
3207 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003208
Alexandre Rames67555f72014-11-18 10:55:16 +00003209 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003210 locations->SetInAt(0, Location::RequiresRegister());
3211 locations->SetInAt(1, Location::RequiresRegister());
3212 // The "out" register is used as a temporary, so it overlaps with the inputs.
3213 // Note that TypeCheckSlowPathARM64 uses this register too.
3214 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3215 // When read barriers are enabled, we need a temporary register for
3216 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003217 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003218 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003219 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003220}
3221
3222void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003223 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003224 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003225 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003226 Register obj = InputRegisterAt(instruction, 0);
3227 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003228 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003229 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003230 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3231 locations->GetTemp(0) :
3232 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003233 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3234 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3235 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3236 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003237
Scott Wakeling97c72b72016-06-24 16:19:36 +01003238 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003239 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003240
3241 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003242 // Avoid null check if we know `obj` is not null.
3243 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003244 __ Cbz(obj, &zero);
3245 }
3246
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003247 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003248 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003249
Roland Levillain44015862016-01-22 11:47:17 +00003250 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003251 case TypeCheckKind::kExactCheck: {
3252 __ Cmp(out, cls);
3253 __ Cset(out, eq);
3254 if (zero.IsLinked()) {
3255 __ B(&done);
3256 }
3257 break;
3258 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003259
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003260 case TypeCheckKind::kAbstractClassCheck: {
3261 // If the class is abstract, we eagerly fetch the super class of the
3262 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003263 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003264 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003265 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003266 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003267 // If `out` is null, we use it for the result, and jump to `done`.
3268 __ Cbz(out, &done);
3269 __ Cmp(out, cls);
3270 __ B(ne, &loop);
3271 __ Mov(out, 1);
3272 if (zero.IsLinked()) {
3273 __ B(&done);
3274 }
3275 break;
3276 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003277
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003278 case TypeCheckKind::kClassHierarchyCheck: {
3279 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003280 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003281 __ Bind(&loop);
3282 __ Cmp(out, cls);
3283 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003284 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003285 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003286 __ Cbnz(out, &loop);
3287 // If `out` is null, we use it for the result, and jump to `done`.
3288 __ B(&done);
3289 __ Bind(&success);
3290 __ Mov(out, 1);
3291 if (zero.IsLinked()) {
3292 __ B(&done);
3293 }
3294 break;
3295 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003296
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003297 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003298 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003299 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003300 __ Cmp(out, cls);
3301 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003302 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003303 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003304 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003305 // If `out` is null, we use it for the result, and jump to `done`.
3306 __ Cbz(out, &done);
3307 __ Ldrh(out, HeapOperand(out, primitive_offset));
3308 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3309 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003310 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003311 __ Mov(out, 1);
3312 __ B(&done);
3313 break;
3314 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003315
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003316 case TypeCheckKind::kArrayCheck: {
3317 __ Cmp(out, cls);
3318 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003319 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3320 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003321 codegen_->AddSlowPath(slow_path);
3322 __ B(ne, slow_path->GetEntryLabel());
3323 __ Mov(out, 1);
3324 if (zero.IsLinked()) {
3325 __ B(&done);
3326 }
3327 break;
3328 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003329
Calin Juravle98893e12015-10-02 21:05:03 +01003330 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003331 case TypeCheckKind::kInterfaceCheck: {
3332 // Note that we indeed only call on slow path, but we always go
3333 // into the slow path for the unresolved and interface check
3334 // cases.
3335 //
3336 // We cannot directly call the InstanceofNonTrivial runtime
3337 // entry point without resorting to a type checking slow path
3338 // here (i.e. by calling InvokeRuntime directly), as it would
3339 // require to assign fixed registers for the inputs of this
3340 // HInstanceOf instruction (following the runtime calling
3341 // convention), which might be cluttered by the potential first
3342 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003343 //
3344 // TODO: Introduce a new runtime entry point taking the object
3345 // to test (instead of its class) as argument, and let it deal
3346 // with the read barrier issues. This will let us refactor this
3347 // case of the `switch` code as it was previously (with a direct
3348 // call to the runtime not using a type checking slow path).
3349 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003350 DCHECK(locations->OnlyCallsOnSlowPath());
3351 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3352 /* is_fatal */ false);
3353 codegen_->AddSlowPath(slow_path);
3354 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003355 if (zero.IsLinked()) {
3356 __ B(&done);
3357 }
3358 break;
3359 }
3360 }
3361
3362 if (zero.IsLinked()) {
3363 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003364 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003365 }
3366
3367 if (done.IsLinked()) {
3368 __ Bind(&done);
3369 }
3370
3371 if (slow_path != nullptr) {
3372 __ Bind(slow_path->GetExitLabel());
3373 }
3374}
3375
3376void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3377 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3378 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3379
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003380 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3381 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003382 case TypeCheckKind::kExactCheck:
3383 case TypeCheckKind::kAbstractClassCheck:
3384 case TypeCheckKind::kClassHierarchyCheck:
3385 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003386 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3387 LocationSummary::kCallOnSlowPath :
3388 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003389 break;
3390 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003391 case TypeCheckKind::kUnresolvedCheck:
3392 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003393 call_kind = LocationSummary::kCallOnSlowPath;
3394 break;
3395 }
3396
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003397 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3398 locations->SetInAt(0, Location::RequiresRegister());
3399 locations->SetInAt(1, Location::RequiresRegister());
3400 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3401 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003402 // When read barriers are enabled, we need an additional temporary
3403 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003404 if (TypeCheckNeedsATemporary(type_check_kind)) {
3405 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003406 }
3407}
3408
3409void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003410 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003411 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003412 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003413 Register obj = InputRegisterAt(instruction, 0);
3414 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003415 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003416 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3417 locations->GetTemp(1) :
3418 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003419 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003420 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3421 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3422 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3423 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003424
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003425 bool is_type_check_slow_path_fatal =
3426 (type_check_kind == TypeCheckKind::kExactCheck ||
3427 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3428 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3429 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3430 !instruction->CanThrowIntoCatchBlock();
3431 SlowPathCodeARM64* type_check_slow_path =
3432 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3433 is_type_check_slow_path_fatal);
3434 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003435
Scott Wakeling97c72b72016-06-24 16:19:36 +01003436 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003437 // Avoid null check if we know obj is not null.
3438 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003439 __ Cbz(obj, &done);
3440 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003441
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003442 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003443 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003444
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003445 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003446 case TypeCheckKind::kExactCheck:
3447 case TypeCheckKind::kArrayCheck: {
3448 __ Cmp(temp, cls);
3449 // Jump to slow path for throwing the exception or doing a
3450 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003451 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003452 break;
3453 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003454
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003455 case TypeCheckKind::kAbstractClassCheck: {
3456 // If the class is abstract, we eagerly fetch the super class of the
3457 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003458 vixl::aarch64::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003459 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003460 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003461 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003462
3463 // If the class reference currently in `temp` is not null, jump
3464 // to the `compare_classes` label to compare it with the checked
3465 // class.
3466 __ Cbnz(temp, &compare_classes);
3467 // Otherwise, jump to the slow path to throw the exception.
3468 //
3469 // But before, move back the object's class into `temp` before
3470 // going into the slow path, as it has been overwritten in the
3471 // meantime.
3472 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003473 GenerateReferenceLoadTwoRegisters(
3474 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003475 __ B(type_check_slow_path->GetEntryLabel());
3476
3477 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003478 __ Cmp(temp, cls);
3479 __ B(ne, &loop);
3480 break;
3481 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003482
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003483 case TypeCheckKind::kClassHierarchyCheck: {
3484 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003485 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003486 __ Bind(&loop);
3487 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003488 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003489
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003490 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003491 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003492
3493 // If the class reference currently in `temp` is not null, jump
3494 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003495 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003496 // Otherwise, jump to the slow path to throw the exception.
3497 //
3498 // But before, move back the object's class into `temp` before
3499 // going into the slow path, as it has been overwritten in the
3500 // meantime.
3501 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003502 GenerateReferenceLoadTwoRegisters(
3503 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003504 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003505 break;
3506 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003507
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003508 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003509 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003510 vixl::aarch64::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003511 __ Cmp(temp, cls);
3512 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003513
3514 // 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> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003516 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003517
3518 // If the component type is not null (i.e. the object is indeed
3519 // an array), jump to label `check_non_primitive_component_type`
3520 // to further check that this component type is not a primitive
3521 // type.
3522 __ Cbnz(temp, &check_non_primitive_component_type);
3523 // Otherwise, jump to the slow path to throw the exception.
3524 //
3525 // But before, move back the object's class into `temp` before
3526 // going into the slow path, as it has been overwritten in the
3527 // meantime.
3528 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003529 GenerateReferenceLoadTwoRegisters(
3530 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003531 __ B(type_check_slow_path->GetEntryLabel());
3532
3533 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003534 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3535 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003536 __ Cbz(temp, &done);
3537 // Same comment as above regarding `temp` and the slow path.
3538 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003539 GenerateReferenceLoadTwoRegisters(
3540 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003541 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003542 break;
3543 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003544
Calin Juravle98893e12015-10-02 21:05:03 +01003545 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003546 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003547 // We always go into the type check slow path for the unresolved
3548 // and interface check cases.
3549 //
3550 // We cannot directly call the CheckCast runtime entry point
3551 // without resorting to a type checking slow path here (i.e. by
3552 // calling InvokeRuntime directly), as it would require to
3553 // assign fixed registers for the inputs of this HInstanceOf
3554 // instruction (following the runtime calling convention), which
3555 // might be cluttered by the potential first read barrier
3556 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003557 //
3558 // TODO: Introduce a new runtime entry point taking the object
3559 // to test (instead of its class) as argument, and let it deal
3560 // with the read barrier issues. This will let us refactor this
3561 // case of the `switch` code as it was previously (with a direct
3562 // call to the runtime not using a type checking slow path).
3563 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003564 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003565 break;
3566 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003567 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003568
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003569 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003570}
3571
Alexandre Rames5319def2014-10-23 10:03:10 +01003572void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3573 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3574 locations->SetOut(Location::ConstantLocation(constant));
3575}
3576
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003577void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003578 // Will be generated at use site.
3579}
3580
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003581void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3582 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3583 locations->SetOut(Location::ConstantLocation(constant));
3584}
3585
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003586void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003587 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003588}
3589
Calin Juravle175dc732015-08-25 15:42:32 +01003590void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3591 // The trampoline uses the same calling convention as dex calling conventions,
3592 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3593 // the method_idx.
3594 HandleInvoke(invoke);
3595}
3596
3597void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3598 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3599}
3600
Alexandre Rames5319def2014-10-23 10:03:10 +01003601void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003602 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003603 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003604}
3605
Alexandre Rames67555f72014-11-18 10:55:16 +00003606void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3607 HandleInvoke(invoke);
3608}
3609
3610void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3611 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003612 LocationSummary* locations = invoke->GetLocations();
3613 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003614 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003615 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003616 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003617
3618 // The register ip1 is required to be used for the hidden argument in
3619 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003620 MacroAssembler* masm = GetVIXLAssembler();
3621 UseScratchRegisterScope scratch_scope(masm);
3622 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003623 scratch_scope.Exclude(ip1);
3624 __ Mov(ip1, invoke->GetDexMethodIndex());
3625
Alexandre Rames67555f72014-11-18 10:55:16 +00003626 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003627 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003628 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003629 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003630 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003631 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003632 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003633 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003634 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003635 // Instead of simply (possibly) unpoisoning `temp` here, we should
3636 // emit a read barrier for the previous class reference load.
3637 // However this is not required in practice, as this is an
3638 // intermediate/temporary reference and because the current
3639 // concurrent copying collector keeps the from-space memory
3640 // intact/accessible until the end of the marking phase (the
3641 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003642 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003643 __ Ldr(temp,
3644 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3645 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003646 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003647 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003648 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003649 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003650 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003651 // lr();
3652 __ Blr(lr);
3653 DCHECK(!codegen_->IsLeafMethod());
3654 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3655}
3656
3657void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003658 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3659 if (intrinsic.TryDispatch(invoke)) {
3660 return;
3661 }
3662
Alexandre Rames67555f72014-11-18 10:55:16 +00003663 HandleInvoke(invoke);
3664}
3665
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003666void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003667 // Explicit clinit checks triggered by static invokes must have been pruned by
3668 // art::PrepareForRegisterAllocation.
3669 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003670
Andreas Gampe878d58c2015-01-15 23:24:00 -08003671 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3672 if (intrinsic.TryDispatch(invoke)) {
3673 return;
3674 }
3675
Alexandre Rames67555f72014-11-18 10:55:16 +00003676 HandleInvoke(invoke);
3677}
3678
Andreas Gampe878d58c2015-01-15 23:24:00 -08003679static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3680 if (invoke->GetLocations()->Intrinsified()) {
3681 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3682 intrinsic.Dispatch(invoke);
3683 return true;
3684 }
3685 return false;
3686}
3687
Vladimir Markodc151b22015-10-15 18:02:30 +01003688HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3689 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3690 MethodReference target_method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003691 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003692 return desired_dispatch_info;
3693}
3694
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003695void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003696 // For better instruction scheduling we load the direct code pointer before the method pointer.
3697 bool direct_code_loaded = false;
3698 switch (invoke->GetCodePtrLocation()) {
3699 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3700 // LR = code address from literal pool with link-time patch.
3701 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3702 direct_code_loaded = true;
3703 break;
3704 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3705 // LR = invoke->GetDirectCodePtr();
3706 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3707 direct_code_loaded = true;
3708 break;
3709 default:
3710 break;
3711 }
3712
Andreas Gampe878d58c2015-01-15 23:24:00 -08003713 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003714 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3715 switch (invoke->GetMethodLoadKind()) {
3716 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3717 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003718 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003719 break;
3720 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003721 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003722 break;
3723 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3724 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003725 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003726 break;
3727 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3728 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003729 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003730 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3731 break;
3732 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3733 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003734 const DexFile& dex_file = *invoke->GetTargetMethod().dex_file;
3735 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003736 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003737 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003738 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003739 __ Bind(adrp_label);
3740 __ adrp(XRegisterFrom(temp), /* offset placeholder */ 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003741 }
Vladimir Marko58155012015-08-19 12:49:41 +00003742 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003743 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003744 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Alexandre Rames6dc01742015-11-12 14:44:19 +00003745 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003746 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003747 __ Bind(ldr_label);
3748 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), /* offset placeholder */ 0));
Alexandre Rames6dc01742015-11-12 14:44:19 +00003749 }
Vladimir Marko58155012015-08-19 12:49:41 +00003750 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003751 }
Vladimir Marko58155012015-08-19 12:49:41 +00003752 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003753 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003754 Register reg = XRegisterFrom(temp);
3755 Register method_reg;
3756 if (current_method.IsRegister()) {
3757 method_reg = XRegisterFrom(current_method);
3758 } else {
3759 DCHECK(invoke->GetLocations()->Intrinsified());
3760 DCHECK(!current_method.IsValid());
3761 method_reg = reg;
3762 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3763 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003764
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003765 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003766 __ Ldr(reg.X(),
3767 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07003768 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003769 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003770 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3771 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003772 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3773 break;
3774 }
3775 }
3776
3777 switch (invoke->GetCodePtrLocation()) {
3778 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3779 __ Bl(&frame_entry_label_);
3780 break;
3781 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3782 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003783 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
3784 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003785 __ Bind(label);
3786 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003787 break;
3788 }
3789 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3790 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3791 // LR prepared above for better instruction scheduling.
3792 DCHECK(direct_code_loaded);
3793 // lr()
3794 __ Blr(lr);
3795 break;
3796 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3797 // LR = callee_method->entry_point_from_quick_compiled_code_;
3798 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003799 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07003800 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003801 // lr()
3802 __ Blr(lr);
3803 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003804 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003805
Andreas Gampe878d58c2015-01-15 23:24:00 -08003806 DCHECK(!IsLeafMethod());
3807}
3808
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003809void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003810 // Use the calling convention instead of the location of the receiver, as
3811 // intrinsics may have put the receiver in a different register. In the intrinsics
3812 // slow path, the arguments have been moved to the right place, so here we are
3813 // guaranteed that the receiver is the first register of the calling convention.
3814 InvokeDexCallingConvention calling_convention;
3815 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003816 Register temp = XRegisterFrom(temp_in);
3817 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3818 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3819 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003820 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003821
3822 BlockPoolsScope block_pools(GetVIXLAssembler());
3823
3824 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003825 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003826 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003827 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003828 // Instead of simply (possibly) unpoisoning `temp` here, we should
3829 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003830 // intermediate/temporary reference and because the current
3831 // concurrent copying collector keeps the from-space memory
3832 // intact/accessible until the end of the marking phase (the
3833 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003834 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3835 // temp = temp->GetMethodAt(method_offset);
3836 __ Ldr(temp, MemOperand(temp, method_offset));
3837 // lr = temp->GetEntryPoint();
3838 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3839 // lr();
3840 __ Blr(lr);
3841}
3842
Scott Wakeling97c72b72016-06-24 16:19:36 +01003843vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
3844 const DexFile& dex_file,
3845 uint32_t string_index,
3846 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003847 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3848}
3849
Scott Wakeling97c72b72016-06-24 16:19:36 +01003850vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
3851 const DexFile& dex_file,
3852 uint32_t type_index,
3853 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003854 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
3855}
3856
Scott Wakeling97c72b72016-06-24 16:19:36 +01003857vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
3858 const DexFile& dex_file,
3859 uint32_t element_offset,
3860 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003861 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3862}
3863
Scott Wakeling97c72b72016-06-24 16:19:36 +01003864vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
3865 const DexFile& dex_file,
3866 uint32_t offset_or_index,
3867 vixl::aarch64::Label* adrp_label,
3868 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003869 // Add a patch entry and return the label.
3870 patches->emplace_back(dex_file, offset_or_index);
3871 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003872 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003873 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3874 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3875 return label;
3876}
3877
Scott Wakeling97c72b72016-06-24 16:19:36 +01003878vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003879 const DexFile& dex_file, uint32_t string_index) {
3880 return boot_image_string_patches_.GetOrCreate(
3881 StringReference(&dex_file, string_index),
3882 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3883}
3884
Scott Wakeling97c72b72016-06-24 16:19:36 +01003885vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003886 const DexFile& dex_file, uint32_t type_index) {
3887 return boot_image_type_patches_.GetOrCreate(
3888 TypeReference(&dex_file, type_index),
3889 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3890}
3891
Scott Wakeling97c72b72016-06-24 16:19:36 +01003892vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
3893 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003894 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3895 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3896 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3897}
3898
Scott Wakeling97c72b72016-06-24 16:19:36 +01003899vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
3900 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003901 return DeduplicateUint64Literal(address);
3902}
3903
Vladimir Marko58155012015-08-19 12:49:41 +00003904void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3905 DCHECK(linker_patches->empty());
3906 size_t size =
3907 method_patches_.size() +
3908 call_patches_.size() +
3909 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003910 pc_relative_dex_cache_patches_.size() +
3911 boot_image_string_patches_.size() +
3912 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003913 boot_image_type_patches_.size() +
3914 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003915 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003916 linker_patches->reserve(size);
3917 for (const auto& entry : method_patches_) {
3918 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003919 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3920 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003921 target_method.dex_file,
3922 target_method.dex_method_index));
3923 }
3924 for (const auto& entry : call_patches_) {
3925 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003926 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3927 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003928 target_method.dex_file,
3929 target_method.dex_method_index));
3930 }
Scott Wakeling97c72b72016-06-24 16:19:36 +01003931 for (const MethodPatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
3932 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003933 info.target_method.dex_file,
3934 info.target_method.dex_method_index));
3935 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003936 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003937 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003938 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003939 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003940 info.offset_or_index));
3941 }
3942 for (const auto& entry : boot_image_string_patches_) {
3943 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003944 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3945 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003946 target_string.dex_file,
3947 target_string.string_index));
3948 }
3949 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003950 linker_patches->push_back(LinkerPatch::RelativeStringPatch(info.label.GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003951 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003952 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003953 info.offset_or_index));
3954 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003955 for (const auto& entry : boot_image_type_patches_) {
3956 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003957 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3958 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003959 target_type.dex_file,
3960 target_type.type_index));
3961 }
3962 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003963 linker_patches->push_back(LinkerPatch::RelativeTypePatch(info.label.GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003964 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003965 info.pc_insn_label->GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003966 info.offset_or_index));
3967 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003968 for (const auto& entry : boot_image_address_patches_) {
3969 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003970 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3971 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003972 }
3973}
3974
Scott Wakeling97c72b72016-06-24 16:19:36 +01003975vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003976 Uint32ToLiteralMap* map) {
3977 return map->GetOrCreate(
3978 value,
3979 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3980}
3981
Scott Wakeling97c72b72016-06-24 16:19:36 +01003982vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003983 return uint64_literals_.GetOrCreate(
3984 value,
3985 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003986}
3987
Scott Wakeling97c72b72016-06-24 16:19:36 +01003988vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003989 MethodReference target_method,
3990 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003991 return map->GetOrCreate(
3992 target_method,
3993 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003994}
3995
Scott Wakeling97c72b72016-06-24 16:19:36 +01003996vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003997 MethodReference target_method) {
3998 return DeduplicateMethodLiteral(target_method, &method_patches_);
3999}
4000
Scott Wakeling97c72b72016-06-24 16:19:36 +01004001vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004002 MethodReference target_method) {
4003 return DeduplicateMethodLiteral(target_method, &call_patches_);
4004}
4005
4006
Andreas Gampe878d58c2015-01-15 23:24:00 -08004007void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004008 // Explicit clinit checks triggered by static invokes must have been pruned by
4009 // art::PrepareForRegisterAllocation.
4010 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004011
Andreas Gampe878d58c2015-01-15 23:24:00 -08004012 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4013 return;
4014 }
4015
Alexandre Ramesd921d642015-04-16 15:07:16 +01004016 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004017 LocationSummary* locations = invoke->GetLocations();
4018 codegen_->GenerateStaticOrDirectCall(
4019 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004020 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004021}
4022
4023void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004024 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4025 return;
4026 }
4027
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004028 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004029 DCHECK(!codegen_->IsLeafMethod());
4030 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4031}
4032
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004033HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4034 HLoadClass::LoadKind desired_class_load_kind) {
4035 if (kEmitCompilerReadBarrier) {
4036 switch (desired_class_load_kind) {
4037 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4038 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4039 case HLoadClass::LoadKind::kBootImageAddress:
4040 // TODO: Implement for read barrier.
4041 return HLoadClass::LoadKind::kDexCacheViaMethod;
4042 default:
4043 break;
4044 }
4045 }
4046 switch (desired_class_load_kind) {
4047 case HLoadClass::LoadKind::kReferrersClass:
4048 break;
4049 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4050 DCHECK(!GetCompilerOptions().GetCompilePic());
4051 break;
4052 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4053 DCHECK(GetCompilerOptions().GetCompilePic());
4054 break;
4055 case HLoadClass::LoadKind::kBootImageAddress:
4056 break;
4057 case HLoadClass::LoadKind::kDexCacheAddress:
4058 DCHECK(Runtime::Current()->UseJitCompilation());
4059 break;
4060 case HLoadClass::LoadKind::kDexCachePcRelative:
4061 DCHECK(!Runtime::Current()->UseJitCompilation());
4062 break;
4063 case HLoadClass::LoadKind::kDexCacheViaMethod:
4064 break;
4065 }
4066 return desired_class_load_kind;
4067}
4068
Alexandre Rames67555f72014-11-18 10:55:16 +00004069void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004070 if (cls->NeedsAccessCheck()) {
4071 InvokeRuntimeCallingConvention calling_convention;
4072 CodeGenerator::CreateLoadClassLocationSummary(
4073 cls,
4074 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01004075 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004076 /* code_generator_supports_read_barrier */ true);
4077 return;
4078 }
4079
4080 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
4081 ? LocationSummary::kCallOnSlowPath
4082 : LocationSummary::kNoCall;
4083 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4084 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4085 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4086 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4087 locations->SetInAt(0, Location::RequiresRegister());
4088 }
4089 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004090}
4091
4092void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004093 if (cls->NeedsAccessCheck()) {
4094 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
4095 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4096 cls,
4097 cls->GetDexPc(),
4098 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004099 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004100 return;
4101 }
4102
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004103 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004104 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004105
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004106 bool generate_null_check = false;
4107 switch (cls->GetLoadKind()) {
4108 case HLoadClass::LoadKind::kReferrersClass: {
4109 DCHECK(!cls->CanCallRuntime());
4110 DCHECK(!cls->MustGenerateClinitCheck());
4111 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4112 Register current_method = InputRegisterAt(cls, 0);
4113 GenerateGcRootFieldLoad(
4114 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4115 break;
4116 }
4117 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4118 DCHECK(!kEmitCompilerReadBarrier);
4119 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4120 cls->GetTypeIndex()));
4121 break;
4122 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4123 DCHECK(!kEmitCompilerReadBarrier);
4124 // Add ADRP with its PC-relative type patch.
4125 const DexFile& dex_file = cls->GetDexFile();
4126 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004127 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004128 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004129 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004130 __ Bind(adrp_label);
4131 __ adrp(out.X(), /* offset placeholder */ 0);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004132 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004133 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004134 vixl::aarch64::Label* add_label =
4135 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004136 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004137 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004138 __ Bind(add_label);
4139 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004140 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004141 break;
4142 }
4143 case HLoadClass::LoadKind::kBootImageAddress: {
4144 DCHECK(!kEmitCompilerReadBarrier);
4145 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4146 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4147 break;
4148 }
4149 case HLoadClass::LoadKind::kDexCacheAddress: {
4150 DCHECK_NE(cls->GetAddress(), 0u);
4151 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4152 // that gives a 16KiB range. To try and reduce the number of literals if we load
4153 // multiple types, simply split the dex cache address to a 16KiB aligned base
4154 // loaded from a literal and the remaining offset embedded in the load.
4155 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4156 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4157 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4158 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4159 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4160 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4161 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4162 GenerateGcRootFieldLoad(cls, out_loc, out.X(), offset);
4163 generate_null_check = !cls->IsInDexCache();
4164 break;
4165 }
4166 case HLoadClass::LoadKind::kDexCachePcRelative: {
4167 // Add ADRP with its PC-relative DexCache access patch.
4168 const DexFile& dex_file = cls->GetDexFile();
4169 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004170 vixl::aarch64::Label* adrp_label =
4171 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004172 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004173 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004174 __ Bind(adrp_label);
4175 __ adrp(out.X(), /* offset placeholder */ 0);
4176 }
4177 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004178 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004179 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4180 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4181 GenerateGcRootFieldLoad(cls, out_loc, out.X(), /* offset placeholder */ 0, ldr_label);
4182 generate_null_check = !cls->IsInDexCache();
4183 break;
4184 }
4185 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4186 MemberOffset resolved_types_offset =
4187 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4188 // /* GcRoot<mirror::Class>[] */ out =
4189 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4190 Register current_method = InputRegisterAt(cls, 0);
4191 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4192 // /* GcRoot<mirror::Class> */ out = out[type_index]
4193 GenerateGcRootFieldLoad(
4194 cls, out_loc, out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
4195 generate_null_check = !cls->IsInDexCache();
4196 break;
4197 }
4198 }
4199
4200 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4201 DCHECK(cls->CanCallRuntime());
4202 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4203 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4204 codegen_->AddSlowPath(slow_path);
4205 if (generate_null_check) {
4206 __ Cbz(out, slow_path->GetEntryLabel());
4207 }
4208 if (cls->MustGenerateClinitCheck()) {
4209 GenerateClassInitializationCheck(slow_path, out);
4210 } else {
4211 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004212 }
4213 }
4214}
4215
David Brazdilcb1c0552015-08-04 16:22:25 +01004216static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004217 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004218}
4219
Alexandre Rames67555f72014-11-18 10:55:16 +00004220void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4221 LocationSummary* locations =
4222 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4223 locations->SetOut(Location::RequiresRegister());
4224}
4225
4226void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004227 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4228}
4229
4230void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4231 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4232}
4233
4234void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4235 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004236}
4237
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004238HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4239 HLoadString::LoadKind desired_string_load_kind) {
4240 if (kEmitCompilerReadBarrier) {
4241 switch (desired_string_load_kind) {
4242 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4243 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4244 case HLoadString::LoadKind::kBootImageAddress:
4245 // TODO: Implement for read barrier.
4246 return HLoadString::LoadKind::kDexCacheViaMethod;
4247 default:
4248 break;
4249 }
4250 }
4251 switch (desired_string_load_kind) {
4252 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4253 DCHECK(!GetCompilerOptions().GetCompilePic());
4254 break;
4255 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4256 DCHECK(GetCompilerOptions().GetCompilePic());
4257 break;
4258 case HLoadString::LoadKind::kBootImageAddress:
4259 break;
4260 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01004261 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004262 break;
4263 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01004264 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004265 break;
4266 case HLoadString::LoadKind::kDexCacheViaMethod:
4267 break;
4268 }
4269 return desired_string_load_kind;
4270}
4271
Alexandre Rames67555f72014-11-18 10:55:16 +00004272void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004273 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004274 ? LocationSummary::kCallOnSlowPath
4275 : LocationSummary::kNoCall;
4276 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004277 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
4278 locations->SetInAt(0, Location::RequiresRegister());
4279 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004280 locations->SetOut(Location::RequiresRegister());
4281}
4282
4283void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004284 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004285
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004286 switch (load->GetLoadKind()) {
4287 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4288 DCHECK(!kEmitCompilerReadBarrier);
4289 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4290 load->GetStringIndex()));
4291 return; // No dex cache slow path.
4292 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4293 DCHECK(!kEmitCompilerReadBarrier);
4294 // Add ADRP with its PC-relative String patch.
4295 const DexFile& dex_file = load->GetDexFile();
4296 uint32_t string_index = load->GetStringIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004297 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004298 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004299 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004300 __ Bind(adrp_label);
4301 __ adrp(out.X(), /* offset placeholder */ 0);
4302 }
4303 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004304 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004305 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4306 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004307 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004308 __ Bind(add_label);
4309 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
4310 }
4311 return; // No dex cache slow path.
4312 }
4313 case HLoadString::LoadKind::kBootImageAddress: {
4314 DCHECK(!kEmitCompilerReadBarrier);
4315 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4316 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4317 return; // No dex cache slow path.
4318 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004319 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004320 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004321 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004322
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004323 // TODO: Re-add the compiler code to do string dex cache lookup again.
4324 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
4325 codegen_->AddSlowPath(slow_path);
4326 __ B(slow_path->GetEntryLabel());
4327 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004328}
4329
Alexandre Rames5319def2014-10-23 10:03:10 +01004330void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4331 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4332 locations->SetOut(Location::ConstantLocation(constant));
4333}
4334
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004335void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004336 // Will be generated at use site.
4337}
4338
Alexandre Rames67555f72014-11-18 10:55:16 +00004339void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4340 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004341 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004342 InvokeRuntimeCallingConvention calling_convention;
4343 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4344}
4345
4346void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4347 codegen_->InvokeRuntime(instruction->IsEnter()
4348 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4349 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004350 instruction->GetDexPc(),
4351 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004352 if (instruction->IsEnter()) {
4353 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4354 } else {
4355 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4356 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004357}
4358
Alexandre Rames42d641b2014-10-27 14:00:51 +00004359void LocationsBuilderARM64::VisitMul(HMul* mul) {
4360 LocationSummary* locations =
4361 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4362 switch (mul->GetResultType()) {
4363 case Primitive::kPrimInt:
4364 case Primitive::kPrimLong:
4365 locations->SetInAt(0, Location::RequiresRegister());
4366 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004367 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004368 break;
4369
4370 case Primitive::kPrimFloat:
4371 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004372 locations->SetInAt(0, Location::RequiresFpuRegister());
4373 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004374 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004375 break;
4376
4377 default:
4378 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4379 }
4380}
4381
4382void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4383 switch (mul->GetResultType()) {
4384 case Primitive::kPrimInt:
4385 case Primitive::kPrimLong:
4386 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4387 break;
4388
4389 case Primitive::kPrimFloat:
4390 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004391 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004392 break;
4393
4394 default:
4395 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4396 }
4397}
4398
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004399void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4400 LocationSummary* locations =
4401 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4402 switch (neg->GetResultType()) {
4403 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004404 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004405 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004406 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004407 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004408
4409 case Primitive::kPrimFloat:
4410 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004411 locations->SetInAt(0, Location::RequiresFpuRegister());
4412 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004413 break;
4414
4415 default:
4416 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4417 }
4418}
4419
4420void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4421 switch (neg->GetResultType()) {
4422 case Primitive::kPrimInt:
4423 case Primitive::kPrimLong:
4424 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4425 break;
4426
4427 case Primitive::kPrimFloat:
4428 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004429 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004430 break;
4431
4432 default:
4433 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4434 }
4435}
4436
4437void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4438 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004439 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004440 InvokeRuntimeCallingConvention calling_convention;
4441 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004442 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004443 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004444 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004445}
4446
4447void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4448 LocationSummary* locations = instruction->GetLocations();
4449 InvokeRuntimeCallingConvention calling_convention;
4450 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4451 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004452 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004453 // Note: if heap poisoning is enabled, the entry point takes cares
4454 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004455 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4456 instruction,
4457 instruction->GetDexPc(),
4458 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004459 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004460}
4461
Alexandre Rames5319def2014-10-23 10:03:10 +01004462void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4463 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004464 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004465 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004466 if (instruction->IsStringAlloc()) {
4467 locations->AddTemp(LocationFrom(kArtMethodRegister));
4468 } else {
4469 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4470 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4471 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004472 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4473}
4474
4475void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004476 // Note: if heap poisoning is enabled, the entry point takes cares
4477 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004478 if (instruction->IsStringAlloc()) {
4479 // String is allocated through StringFactory. Call NewEmptyString entry point.
4480 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004481 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004482 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4483 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4484 __ Blr(lr);
4485 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4486 } else {
4487 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4488 instruction,
4489 instruction->GetDexPc(),
4490 nullptr);
4491 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4492 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004493}
4494
4495void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4496 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004497 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004498 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004499}
4500
4501void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004502 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004503 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004504 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004505 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004506 break;
4507
4508 default:
4509 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4510 }
4511}
4512
David Brazdil66d126e2015-04-03 16:02:44 +01004513void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4514 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4515 locations->SetInAt(0, Location::RequiresRegister());
4516 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4517}
4518
4519void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004520 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004521}
4522
Alexandre Rames5319def2014-10-23 10:03:10 +01004523void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004524 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4525 ? LocationSummary::kCallOnSlowPath
4526 : LocationSummary::kNoCall;
4527 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004528 locations->SetInAt(0, Location::RequiresRegister());
4529 if (instruction->HasUses()) {
4530 locations->SetOut(Location::SameAsFirstInput());
4531 }
4532}
4533
Calin Juravle2ae48182016-03-16 14:05:09 +00004534void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4535 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004536 return;
4537 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004538
Alexandre Ramesd921d642015-04-16 15:07:16 +01004539 BlockPoolsScope block_pools(GetVIXLAssembler());
4540 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004541 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004542 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004543}
4544
Calin Juravle2ae48182016-03-16 14:05:09 +00004545void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004546 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004547 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004548
4549 LocationSummary* locations = instruction->GetLocations();
4550 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004551
4552 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004553}
4554
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004555void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004556 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004557}
4558
Alexandre Rames67555f72014-11-18 10:55:16 +00004559void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4560 HandleBinaryOp(instruction);
4561}
4562
4563void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4564 HandleBinaryOp(instruction);
4565}
4566
Alexandre Rames3e69f162014-12-10 10:36:50 +00004567void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4568 LOG(FATAL) << "Unreachable";
4569}
4570
4571void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4572 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4573}
4574
Alexandre Rames5319def2014-10-23 10:03:10 +01004575void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4576 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4577 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4578 if (location.IsStackSlot()) {
4579 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4580 } else if (location.IsDoubleStackSlot()) {
4581 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4582 }
4583 locations->SetOut(location);
4584}
4585
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004586void InstructionCodeGeneratorARM64::VisitParameterValue(
4587 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004588 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004589}
4590
4591void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4592 LocationSummary* locations =
4593 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004594 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004595}
4596
4597void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4598 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4599 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004600}
4601
4602void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4603 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004604 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004605 locations->SetInAt(i, Location::Any());
4606 }
4607 locations->SetOut(Location::Any());
4608}
4609
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004610void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004611 LOG(FATAL) << "Unreachable";
4612}
4613
Serban Constantinescu02164b32014-11-13 14:05:07 +00004614void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004615 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004616 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004617 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4618 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004619 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4620
4621 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004622 case Primitive::kPrimInt:
4623 case Primitive::kPrimLong:
4624 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004625 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004626 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4627 break;
4628
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004629 case Primitive::kPrimFloat:
4630 case Primitive::kPrimDouble: {
4631 InvokeRuntimeCallingConvention calling_convention;
4632 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4633 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4634 locations->SetOut(calling_convention.GetReturnLocation(type));
4635
4636 break;
4637 }
4638
Serban Constantinescu02164b32014-11-13 14:05:07 +00004639 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004640 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004641 }
4642}
4643
4644void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4645 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004646
Serban Constantinescu02164b32014-11-13 14:05:07 +00004647 switch (type) {
4648 case Primitive::kPrimInt:
4649 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004650 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004651 break;
4652 }
4653
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004654 case Primitive::kPrimFloat:
4655 case Primitive::kPrimDouble: {
4656 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4657 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004658 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004659 if (type == Primitive::kPrimFloat) {
4660 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4661 } else {
4662 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4663 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004664 break;
4665 }
4666
Serban Constantinescu02164b32014-11-13 14:05:07 +00004667 default:
4668 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004669 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004670 }
4671}
4672
Calin Juravle27df7582015-04-17 19:12:31 +01004673void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4674 memory_barrier->SetLocations(nullptr);
4675}
4676
4677void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004678 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004679}
4680
Alexandre Rames5319def2014-10-23 10:03:10 +01004681void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4682 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4683 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004684 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004685}
4686
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004687void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004688 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004689}
4690
4691void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4692 instruction->SetLocations(nullptr);
4693}
4694
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004695void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004696 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004697}
4698
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004699void LocationsBuilderARM64::VisitRor(HRor* ror) {
4700 HandleBinaryOp(ror);
4701}
4702
4703void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4704 HandleBinaryOp(ror);
4705}
4706
Serban Constantinescu02164b32014-11-13 14:05:07 +00004707void LocationsBuilderARM64::VisitShl(HShl* shl) {
4708 HandleShift(shl);
4709}
4710
4711void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4712 HandleShift(shl);
4713}
4714
4715void LocationsBuilderARM64::VisitShr(HShr* shr) {
4716 HandleShift(shr);
4717}
4718
4719void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4720 HandleShift(shr);
4721}
4722
Alexandre Rames5319def2014-10-23 10:03:10 +01004723void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004724 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004725}
4726
4727void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004728 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004729}
4730
Alexandre Rames67555f72014-11-18 10:55:16 +00004731void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004732 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004733}
4734
4735void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004736 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004737}
4738
4739void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004740 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004741}
4742
Alexandre Rames67555f72014-11-18 10:55:16 +00004743void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004744 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004745}
4746
Calin Juravlee460d1d2015-09-29 04:52:17 +01004747void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4748 HUnresolvedInstanceFieldGet* instruction) {
4749 FieldAccessCallingConventionARM64 calling_convention;
4750 codegen_->CreateUnresolvedFieldLocationSummary(
4751 instruction, instruction->GetFieldType(), calling_convention);
4752}
4753
4754void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4755 HUnresolvedInstanceFieldGet* instruction) {
4756 FieldAccessCallingConventionARM64 calling_convention;
4757 codegen_->GenerateUnresolvedFieldAccess(instruction,
4758 instruction->GetFieldType(),
4759 instruction->GetFieldIndex(),
4760 instruction->GetDexPc(),
4761 calling_convention);
4762}
4763
4764void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4765 HUnresolvedInstanceFieldSet* instruction) {
4766 FieldAccessCallingConventionARM64 calling_convention;
4767 codegen_->CreateUnresolvedFieldLocationSummary(
4768 instruction, instruction->GetFieldType(), calling_convention);
4769}
4770
4771void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4772 HUnresolvedInstanceFieldSet* instruction) {
4773 FieldAccessCallingConventionARM64 calling_convention;
4774 codegen_->GenerateUnresolvedFieldAccess(instruction,
4775 instruction->GetFieldType(),
4776 instruction->GetFieldIndex(),
4777 instruction->GetDexPc(),
4778 calling_convention);
4779}
4780
4781void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4782 HUnresolvedStaticFieldGet* instruction) {
4783 FieldAccessCallingConventionARM64 calling_convention;
4784 codegen_->CreateUnresolvedFieldLocationSummary(
4785 instruction, instruction->GetFieldType(), calling_convention);
4786}
4787
4788void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4789 HUnresolvedStaticFieldGet* instruction) {
4790 FieldAccessCallingConventionARM64 calling_convention;
4791 codegen_->GenerateUnresolvedFieldAccess(instruction,
4792 instruction->GetFieldType(),
4793 instruction->GetFieldIndex(),
4794 instruction->GetDexPc(),
4795 calling_convention);
4796}
4797
4798void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4799 HUnresolvedStaticFieldSet* instruction) {
4800 FieldAccessCallingConventionARM64 calling_convention;
4801 codegen_->CreateUnresolvedFieldLocationSummary(
4802 instruction, instruction->GetFieldType(), calling_convention);
4803}
4804
4805void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4806 HUnresolvedStaticFieldSet* instruction) {
4807 FieldAccessCallingConventionARM64 calling_convention;
4808 codegen_->GenerateUnresolvedFieldAccess(instruction,
4809 instruction->GetFieldType(),
4810 instruction->GetFieldIndex(),
4811 instruction->GetDexPc(),
4812 calling_convention);
4813}
4814
Alexandre Rames5319def2014-10-23 10:03:10 +01004815void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4816 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4817}
4818
4819void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004820 HBasicBlock* block = instruction->GetBlock();
4821 if (block->GetLoopInformation() != nullptr) {
4822 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4823 // The back edge will generate the suspend check.
4824 return;
4825 }
4826 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4827 // The goto will generate the suspend check.
4828 return;
4829 }
4830 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004831}
4832
Alexandre Rames67555f72014-11-18 10:55:16 +00004833void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4834 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004835 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004836 InvokeRuntimeCallingConvention calling_convention;
4837 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4838}
4839
4840void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4841 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004842 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004843 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004844}
4845
4846void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4847 LocationSummary* locations =
4848 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4849 Primitive::Type input_type = conversion->GetInputType();
4850 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004851 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004852 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4853 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4854 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4855 }
4856
Alexandre Rames542361f2015-01-29 16:57:31 +00004857 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004858 locations->SetInAt(0, Location::RequiresFpuRegister());
4859 } else {
4860 locations->SetInAt(0, Location::RequiresRegister());
4861 }
4862
Alexandre Rames542361f2015-01-29 16:57:31 +00004863 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004864 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4865 } else {
4866 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4867 }
4868}
4869
4870void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4871 Primitive::Type result_type = conversion->GetResultType();
4872 Primitive::Type input_type = conversion->GetInputType();
4873
4874 DCHECK_NE(input_type, result_type);
4875
Alexandre Rames542361f2015-01-29 16:57:31 +00004876 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004877 int result_size = Primitive::ComponentSize(result_type);
4878 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004879 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004880 Register output = OutputRegister(conversion);
4881 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004882 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004883 // 'int' values are used directly as W registers, discarding the top
4884 // bits, so we don't need to sign-extend and can just perform a move.
4885 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4886 // top 32 bits of the target register. We theoretically could leave those
4887 // bits unchanged, but we would have to make sure that no code uses a
4888 // 32bit input value as a 64bit value assuming that the top 32 bits are
4889 // zero.
4890 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004891 } else if (result_type == Primitive::kPrimChar ||
4892 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4893 __ Ubfx(output,
4894 output.IsX() ? source.X() : source.W(),
4895 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004896 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004897 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004898 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004899 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004900 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004901 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004902 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4903 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004904 } else if (Primitive::IsFloatingPointType(result_type) &&
4905 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004906 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4907 } else {
4908 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4909 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004910 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004911}
Alexandre Rames67555f72014-11-18 10:55:16 +00004912
Serban Constantinescu02164b32014-11-13 14:05:07 +00004913void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4914 HandleShift(ushr);
4915}
4916
4917void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4918 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004919}
4920
4921void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4922 HandleBinaryOp(instruction);
4923}
4924
4925void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4926 HandleBinaryOp(instruction);
4927}
4928
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004929void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004930 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004931 LOG(FATAL) << "Unreachable";
4932}
4933
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004934void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004935 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004936 LOG(FATAL) << "Unreachable";
4937}
4938
Mark Mendellfe57faa2015-09-18 09:26:15 -04004939// Simple implementation of packed switch - generate cascaded compare/jumps.
4940void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4941 LocationSummary* locations =
4942 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4943 locations->SetInAt(0, Location::RequiresRegister());
4944}
4945
4946void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4947 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004948 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004949 Register value_reg = InputRegisterAt(switch_instr, 0);
4950 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4951
Zheng Xu3927c8b2015-11-18 17:46:25 +08004952 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004953 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08004954 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4955 // make sure we don't emit it if the target may run out of range.
4956 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4957 // ranges and emit the tables only as required.
4958 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004959
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004960 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004961 // Current instruction id is an upper bound of the number of HIRs in the graph.
4962 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4963 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004964 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4965 Register temp = temps.AcquireW();
4966 __ Subs(temp, value_reg, Operand(lower_bound));
4967
Zheng Xu3927c8b2015-11-18 17:46:25 +08004968 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004969 // Jump to successors[0] if value == lower_bound.
4970 __ B(eq, codegen_->GetLabelOf(successors[0]));
4971 int32_t last_index = 0;
4972 for (; num_entries - last_index > 2; last_index += 2) {
4973 __ Subs(temp, temp, Operand(2));
4974 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4975 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4976 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4977 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4978 }
4979 if (num_entries - last_index == 2) {
4980 // The last missing case_value.
4981 __ Cmp(temp, Operand(1));
4982 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004983 }
4984
4985 // And the default for any other value.
4986 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4987 __ B(codegen_->GetLabelOf(default_block));
4988 }
4989 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01004990 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08004991
4992 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4993
4994 // Below instructions should use at most one blocked register. Since there are two blocked
4995 // registers, we are free to block one.
4996 Register temp_w = temps.AcquireW();
4997 Register index;
4998 // Remove the bias.
4999 if (lower_bound != 0) {
5000 index = temp_w;
5001 __ Sub(index, value_reg, Operand(lower_bound));
5002 } else {
5003 index = value_reg;
5004 }
5005
5006 // Jump to default block if index is out of the range.
5007 __ Cmp(index, Operand(num_entries));
5008 __ B(hs, codegen_->GetLabelOf(default_block));
5009
5010 // In current VIXL implementation, it won't require any blocked registers to encode the
5011 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5012 // register pressure.
5013 Register table_base = temps.AcquireX();
5014 // Load jump offset from the table.
5015 __ Adr(table_base, jump_table->GetTableStartLabel());
5016 Register jump_offset = temp_w;
5017 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5018
5019 // Jump to target block by branching to table_base(pc related) + offset.
5020 Register target_address = table_base;
5021 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5022 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005023 }
5024}
5025
Roland Levillain44015862016-01-22 11:47:17 +00005026void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
5027 Location out,
5028 uint32_t offset,
5029 Location maybe_temp) {
5030 Primitive::Type type = Primitive::kPrimNot;
5031 Register out_reg = RegisterFrom(out, type);
5032 if (kEmitCompilerReadBarrier) {
5033 Register temp_reg = RegisterFrom(maybe_temp, type);
5034 if (kUseBakerReadBarrier) {
5035 // Load with fast path based Baker's read barrier.
5036 // /* HeapReference<Object> */ out = *(out + offset)
5037 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5038 out,
5039 out_reg,
5040 offset,
5041 temp_reg,
5042 /* needs_null_check */ false,
5043 /* use_load_acquire */ false);
5044 } else {
5045 // Load with slow path based read barrier.
5046 // Save the value of `out` into `maybe_temp` before overwriting it
5047 // in the following move operation, as we will need it for the
5048 // read barrier below.
5049 __ Mov(temp_reg, out_reg);
5050 // /* HeapReference<Object> */ out = *(out + offset)
5051 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5052 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5053 }
5054 } else {
5055 // Plain load with no read barrier.
5056 // /* HeapReference<Object> */ out = *(out + offset)
5057 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5058 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5059 }
5060}
5061
5062void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5063 Location out,
5064 Location obj,
5065 uint32_t offset,
5066 Location maybe_temp) {
5067 Primitive::Type type = Primitive::kPrimNot;
5068 Register out_reg = RegisterFrom(out, type);
5069 Register obj_reg = RegisterFrom(obj, type);
5070 if (kEmitCompilerReadBarrier) {
5071 if (kUseBakerReadBarrier) {
5072 // Load with fast path based Baker's read barrier.
5073 Register temp_reg = RegisterFrom(maybe_temp, type);
5074 // /* HeapReference<Object> */ out = *(obj + offset)
5075 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5076 out,
5077 obj_reg,
5078 offset,
5079 temp_reg,
5080 /* needs_null_check */ false,
5081 /* use_load_acquire */ false);
5082 } else {
5083 // Load with slow path based read barrier.
5084 // /* HeapReference<Object> */ out = *(obj + offset)
5085 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5086 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5087 }
5088 } else {
5089 // Plain load with no read barrier.
5090 // /* HeapReference<Object> */ out = *(obj + offset)
5091 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5092 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5093 }
5094}
5095
5096void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
5097 Location root,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005098 Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005099 uint32_t offset,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005100 vixl::aarch64::Label* fixup_label) {
Roland Levillain44015862016-01-22 11:47:17 +00005101 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
5102 if (kEmitCompilerReadBarrier) {
5103 if (kUseBakerReadBarrier) {
5104 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5105 // Baker's read barrier are used:
5106 //
5107 // root = obj.field;
5108 // if (Thread::Current()->GetIsGcMarking()) {
5109 // root = ReadBarrier::Mark(root)
5110 // }
5111
5112 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005113 if (fixup_label == nullptr) {
5114 __ Ldr(root_reg, MemOperand(obj, offset));
5115 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005116 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005117 __ Bind(fixup_label);
5118 __ ldr(root_reg, MemOperand(obj, offset));
5119 }
Roland Levillain44015862016-01-22 11:47:17 +00005120 static_assert(
5121 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5122 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5123 "have different sizes.");
5124 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5125 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5126 "have different sizes.");
5127
Vladimir Marko953437b2016-08-24 08:30:46 +00005128 // Slow path marking the GC root `root`.
Roland Levillain44015862016-01-22 11:47:17 +00005129 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005130 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Roland Levillain44015862016-01-22 11:47:17 +00005131 codegen_->AddSlowPath(slow_path);
5132
5133 MacroAssembler* masm = GetVIXLAssembler();
5134 UseScratchRegisterScope temps(masm);
5135 Register temp = temps.AcquireW();
5136 // temp = Thread::Current()->GetIsGcMarking()
Andreas Gampe542451c2016-07-26 09:02:02 -07005137 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64PointerSize>().Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00005138 __ Cbnz(temp, slow_path->GetEntryLabel());
5139 __ Bind(slow_path->GetExitLabel());
5140 } else {
5141 // GC root loaded through a slow path for read barriers other
5142 // than Baker's.
5143 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005144 if (fixup_label == nullptr) {
5145 __ Add(root_reg.X(), obj.X(), offset);
5146 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005147 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005148 __ Bind(fixup_label);
5149 __ add(root_reg.X(), obj.X(), offset);
5150 }
Roland Levillain44015862016-01-22 11:47:17 +00005151 // /* mirror::Object* */ root = root->Read()
5152 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5153 }
5154 } else {
5155 // Plain GC root load with no read barrier.
5156 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005157 if (fixup_label == nullptr) {
5158 __ Ldr(root_reg, MemOperand(obj, offset));
5159 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005160 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005161 __ Bind(fixup_label);
5162 __ ldr(root_reg, MemOperand(obj, offset));
5163 }
Roland Levillain44015862016-01-22 11:47:17 +00005164 // Note that GC roots are not affected by heap poisoning, thus we
5165 // do not have to unpoison `root_reg` here.
5166 }
5167}
5168
5169void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5170 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005171 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005172 uint32_t offset,
5173 Register temp,
5174 bool needs_null_check,
5175 bool use_load_acquire) {
5176 DCHECK(kEmitCompilerReadBarrier);
5177 DCHECK(kUseBakerReadBarrier);
5178
5179 // /* HeapReference<Object> */ ref = *(obj + offset)
5180 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01005181 size_t no_scale_factor = 0U;
5182 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5183 ref,
5184 obj,
5185 offset,
5186 no_index,
5187 no_scale_factor,
5188 temp,
5189 needs_null_check,
5190 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005191}
5192
5193void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5194 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005195 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005196 uint32_t data_offset,
5197 Location index,
5198 Register temp,
5199 bool needs_null_check) {
5200 DCHECK(kEmitCompilerReadBarrier);
5201 DCHECK(kUseBakerReadBarrier);
5202
5203 // Array cells are never volatile variables, therefore array loads
5204 // never use Load-Acquire instructions on ARM64.
5205 const bool use_load_acquire = false;
5206
Roland Levillainbfea3352016-06-23 13:48:47 +01005207 static_assert(
5208 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5209 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005210 // /* HeapReference<Object> */ ref =
5211 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005212 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5213 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5214 ref,
5215 obj,
5216 data_offset,
5217 index,
5218 scale_factor,
5219 temp,
5220 needs_null_check,
5221 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005222}
5223
5224void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5225 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005226 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005227 uint32_t offset,
5228 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005229 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005230 Register temp,
5231 bool needs_null_check,
5232 bool use_load_acquire) {
5233 DCHECK(kEmitCompilerReadBarrier);
5234 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005235 // If we are emitting an array load, we should not be using a
5236 // Load Acquire instruction. In other words:
5237 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5238 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005239
5240 MacroAssembler* masm = GetVIXLAssembler();
5241 UseScratchRegisterScope temps(masm);
5242
5243 // In slow path based read barriers, the read barrier call is
5244 // inserted after the original load. However, in fast path based
5245 // Baker's read barriers, we need to perform the load of
5246 // mirror::Object::monitor_ *before* the original reference load.
5247 // This load-load ordering is required by the read barrier.
5248 // The fast path/slow path (for Baker's algorithm) should look like:
5249 //
5250 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5251 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5252 // HeapReference<Object> ref = *src; // Original reference load.
5253 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5254 // if (is_gray) {
5255 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5256 // }
5257 //
5258 // Note: the original implementation in ReadBarrier::Barrier is
5259 // slightly more complex as it performs additional checks that we do
5260 // not do here for performance reasons.
5261
5262 Primitive::Type type = Primitive::kPrimNot;
5263 Register ref_reg = RegisterFrom(ref, type);
5264 DCHECK(obj.IsW());
5265 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5266
5267 // /* int32_t */ monitor = obj->monitor_
5268 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5269 if (needs_null_check) {
5270 MaybeRecordImplicitNullCheck(instruction);
5271 }
5272 // /* LockWord */ lock_word = LockWord(monitor)
5273 static_assert(sizeof(LockWord) == sizeof(int32_t),
5274 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005275
Vladimir Marko877a0332016-07-11 19:30:56 +01005276 // Introduce a dependency on the lock_word including rb_state,
5277 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005278 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005279 // `obj` is unchanged by this operation, but its value now depends
5280 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005281 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005282
5283 // The actual reference load.
5284 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01005285 // Load types involving an "index".
5286 if (use_load_acquire) {
5287 // UnsafeGetObjectVolatile intrinsic case.
5288 // Register `index` is not an index in an object array, but an
5289 // offset to an object reference field within object `obj`.
5290 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5291 DCHECK(instruction->GetLocations()->Intrinsified());
5292 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5293 << instruction->AsInvoke()->GetIntrinsic();
5294 DCHECK_EQ(offset, 0U);
5295 DCHECK_EQ(scale_factor, 0U);
5296 DCHECK_EQ(needs_null_check, 0U);
5297 // /* HeapReference<Object> */ ref = *(obj + index)
5298 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5299 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005300 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005301 // ArrayGet and UnsafeGetObject intrinsics cases.
5302 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5303 if (index.IsConstant()) {
5304 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5305 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5306 } else {
Vladimir Marko877a0332016-07-11 19:30:56 +01005307 Register temp2 = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +01005308 __ Add(temp2, obj, offset);
5309 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, scale_factor));
5310 temps.Release(temp2);
5311 }
Roland Levillain44015862016-01-22 11:47:17 +00005312 }
Roland Levillain44015862016-01-22 11:47:17 +00005313 } else {
5314 // /* HeapReference<Object> */ ref = *(obj + offset)
5315 MemOperand field = HeapOperand(obj, offset);
5316 if (use_load_acquire) {
5317 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5318 } else {
5319 Load(type, ref_reg, field);
5320 }
5321 }
5322
5323 // Object* ref = ref_addr->AsMirrorPtr()
5324 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5325
Vladimir Marko953437b2016-08-24 08:30:46 +00005326 // Slow path marking the object `ref` when it is gray.
Roland Levillain44015862016-01-22 11:47:17 +00005327 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005328 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
Roland Levillain44015862016-01-22 11:47:17 +00005329 AddSlowPath(slow_path);
5330
5331 // if (rb_state == ReadBarrier::gray_ptr_)
5332 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005333 // Given the numeric representation, it's enough to check the low bit of the rb_state.
5334 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
5335 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
5336 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
5337 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005338 __ Bind(slow_path->GetExitLabel());
5339}
5340
5341void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5342 Location out,
5343 Location ref,
5344 Location obj,
5345 uint32_t offset,
5346 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005347 DCHECK(kEmitCompilerReadBarrier);
5348
Roland Levillain44015862016-01-22 11:47:17 +00005349 // Insert a slow path based read barrier *after* the reference load.
5350 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005351 // If heap poisoning is enabled, the unpoisoning of the loaded
5352 // reference will be carried out by the runtime within the slow
5353 // path.
5354 //
5355 // Note that `ref` currently does not get unpoisoned (when heap
5356 // poisoning is enabled), which is alright as the `ref` argument is
5357 // not used by the artReadBarrierSlow entry point.
5358 //
5359 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5360 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5361 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5362 AddSlowPath(slow_path);
5363
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005364 __ B(slow_path->GetEntryLabel());
5365 __ Bind(slow_path->GetExitLabel());
5366}
5367
Roland Levillain44015862016-01-22 11:47:17 +00005368void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5369 Location out,
5370 Location ref,
5371 Location obj,
5372 uint32_t offset,
5373 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005374 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005375 // Baker's read barriers shall be handled by the fast path
5376 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5377 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005378 // If heap poisoning is enabled, unpoisoning will be taken care of
5379 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005380 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005381 } else if (kPoisonHeapReferences) {
5382 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5383 }
5384}
5385
Roland Levillain44015862016-01-22 11:47:17 +00005386void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5387 Location out,
5388 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005389 DCHECK(kEmitCompilerReadBarrier);
5390
Roland Levillain44015862016-01-22 11:47:17 +00005391 // Insert a slow path based read barrier *after* the GC root load.
5392 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005393 // Note that GC roots are not affected by heap poisoning, so we do
5394 // not need to do anything special for this here.
5395 SlowPathCodeARM64* slow_path =
5396 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5397 AddSlowPath(slow_path);
5398
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005399 __ B(slow_path->GetEntryLabel());
5400 __ Bind(slow_path->GetExitLabel());
5401}
5402
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005403void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5404 LocationSummary* locations =
5405 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5406 locations->SetInAt(0, Location::RequiresRegister());
5407 locations->SetOut(Location::RequiresRegister());
5408}
5409
5410void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5411 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005412 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005413 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005414 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005415 __ Ldr(XRegisterFrom(locations->Out()),
5416 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005417 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005418 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005419 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005420 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5421 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005422 __ Ldr(XRegisterFrom(locations->Out()),
5423 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005424 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005425}
5426
5427
5428
Alexandre Rames67555f72014-11-18 10:55:16 +00005429#undef __
5430#undef QUICK_ENTRY_POINT
5431
Alexandre Rames5319def2014-10-23 10:03:10 +01005432} // namespace arm64
5433} // namespace art