blob: dc5802aff58e146aaeb9faa4856922a988e19247 [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
36
37using namespace vixl; // NOLINT(build/namespaces)
38
39#ifdef __
40#error "ARM64 Codegen VIXL macro-assembler macro already defined."
41#endif
42
Alexandre Rames5319def2014-10-23 10:03:10 +010043namespace art {
44
Roland Levillain22ccc3a2015-11-24 13:10:05 +000045template<class MirrorType>
46class GcRoot;
47
Alexandre Rames5319def2014-10-23 10:03:10 +010048namespace arm64 {
49
Andreas Gampe878d58c2015-01-15 23:24:00 -080050using helpers::CPURegisterFrom;
51using helpers::DRegisterFrom;
52using helpers::FPRegisterFrom;
53using helpers::HeapOperand;
54using helpers::HeapOperandFrom;
55using helpers::InputCPURegisterAt;
56using helpers::InputFPRegisterAt;
57using helpers::InputRegisterAt;
58using helpers::InputOperandAt;
59using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080060using helpers::LocationFrom;
61using helpers::OperandFromMemOperand;
62using helpers::OutputCPURegister;
63using helpers::OutputFPRegister;
64using helpers::OutputRegister;
65using helpers::RegisterFrom;
66using helpers::StackOperandFrom;
67using helpers::VIXLRegCodeFromART;
68using helpers::WRegisterFrom;
69using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000070using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080071using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080072
Alexandre Rames5319def2014-10-23 10:03:10 +010073static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000074// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080075// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
76// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000077static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010078
Alexandre Rames5319def2014-10-23 10:03:10 +010079inline Condition ARM64Condition(IfCondition cond) {
80 switch (cond) {
81 case kCondEQ: return eq;
82 case kCondNE: return ne;
83 case kCondLT: return lt;
84 case kCondLE: return le;
85 case kCondGT: return gt;
86 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070087 case kCondB: return lo;
88 case kCondBE: return ls;
89 case kCondA: return hi;
90 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010091 }
Roland Levillain7f63c522015-07-13 15:54:55 +000092 LOG(FATAL) << "Unreachable";
93 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010094}
95
Vladimir Markod6e069b2016-01-18 11:11:01 +000096inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
97 // The ARM64 condition codes can express all the necessary branches, see the
98 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
99 // There is no dex instruction or HIR that would need the missing conditions
100 // "equal or unordered" or "not equal".
101 switch (cond) {
102 case kCondEQ: return eq;
103 case kCondNE: return ne /* unordered */;
104 case kCondLT: return gt_bias ? cc : lt /* unordered */;
105 case kCondLE: return gt_bias ? ls : le /* unordered */;
106 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
107 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
108 default:
109 LOG(FATAL) << "UNREACHABLE";
110 UNREACHABLE();
111 }
112}
113
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000114Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000115 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
116 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
117 // but we use the exact registers for clarity.
118 if (return_type == Primitive::kPrimFloat) {
119 return LocationFrom(s0);
120 } else if (return_type == Primitive::kPrimDouble) {
121 return LocationFrom(d0);
122 } else if (return_type == Primitive::kPrimLong) {
123 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100124 } else if (return_type == Primitive::kPrimVoid) {
125 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000126 } else {
127 return LocationFrom(w0);
128 }
129}
130
Alexandre Rames5319def2014-10-23 10:03:10 +0100131Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000132 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100133}
134
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700135// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
136#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Alexandre Rames67555f72014-11-18 10:55:16 +0000137#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100138
Zheng Xuda403092015-04-24 17:35:39 +0800139// Calculate memory accessing operand for save/restore live registers.
140static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
141 RegisterSet* register_set,
142 int64_t spill_offset,
143 bool is_save) {
144 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
145 codegen->GetNumberOfCoreRegisters(),
146 register_set->GetFloatingPointRegisters(),
147 codegen->GetNumberOfFloatingPointRegisters()));
148
149 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
150 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000151 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
152 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
Zheng Xuda403092015-04-24 17:35:39 +0800153
154 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
155 UseScratchRegisterScope temps(masm);
156
157 Register base = masm->StackPointer();
158 int64_t core_spill_size = core_list.TotalSizeInBytes();
159 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
160 int64_t reg_size = kXRegSizeInBytes;
161 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
162 uint32_t ls_access_size = WhichPowerOf2(reg_size);
163 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
164 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
165 // If the offset does not fit in the instruction's immediate field, use an alternate register
166 // to compute the base address(float point registers spill base address).
167 Register new_base = temps.AcquireSameSizeAs(base);
168 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
169 base = new_base;
170 spill_offset = -core_spill_size;
171 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
172 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
173 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
174 }
175
176 if (is_save) {
177 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
178 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
179 } else {
180 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
181 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
182 }
183}
184
185void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
186 RegisterSet* register_set = locations->GetLiveRegisters();
187 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
188 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
189 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
190 // If the register holds an object, update the stack mask.
191 if (locations->RegisterContainsObject(i)) {
192 locations->SetStackBit(stack_offset / kVRegSize);
193 }
194 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
195 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
196 saved_core_stack_offsets_[i] = stack_offset;
197 stack_offset += kXRegSizeInBytes;
198 }
199 }
200
201 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
202 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
203 register_set->ContainsFloatingPointRegister(i)) {
204 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
205 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
206 saved_fpu_stack_offsets_[i] = stack_offset;
207 stack_offset += kDRegSizeInBytes;
208 }
209 }
210
211 SaveRestoreLiveRegistersHelper(codegen, register_set,
212 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
213}
214
215void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
216 RegisterSet* register_set = locations->GetLiveRegisters();
217 SaveRestoreLiveRegistersHelper(codegen, register_set,
218 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
219}
220
Alexandre Rames5319def2014-10-23 10:03:10 +0100221class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
222 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000223 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100224
Alexandre Rames67555f72014-11-18 10:55:16 +0000225 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100226 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000227 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100228
Alexandre Rames5319def2014-10-23 10:03:10 +0100229 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000230 if (instruction_->CanThrowIntoCatchBlock()) {
231 // Live registers will be restored in the catch block if caught.
232 SaveLiveRegisters(codegen, instruction_->GetLocations());
233 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000234 // We're moving two locations to locations that could overlap, so we need a parallel
235 // move resolver.
236 InvokeRuntimeCallingConvention calling_convention;
237 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100238 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
239 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000240 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000241 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800242 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100243 }
244
Alexandre Rames8158f282015-08-07 10:26:17 +0100245 bool IsFatal() const OVERRIDE { return true; }
246
Alexandre Rames9931f312015-06-19 14:47:01 +0100247 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
248
Alexandre Rames5319def2014-10-23 10:03:10 +0100249 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100250 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
251};
252
Alexandre Rames67555f72014-11-18 10:55:16 +0000253class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
254 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000255 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000256
257 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
258 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
259 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000260 if (instruction_->CanThrowIntoCatchBlock()) {
261 // Live registers will be restored in the catch block if caught.
262 SaveLiveRegisters(codegen, instruction_->GetLocations());
263 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000264 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000265 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800266 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000267 }
268
Alexandre Rames8158f282015-08-07 10:26:17 +0100269 bool IsFatal() const OVERRIDE { return true; }
270
Alexandre Rames9931f312015-06-19 14:47:01 +0100271 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
272
Alexandre Rames67555f72014-11-18 10:55:16 +0000273 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000274 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
275};
276
277class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
278 public:
279 LoadClassSlowPathARM64(HLoadClass* cls,
280 HInstruction* at,
281 uint32_t dex_pc,
282 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000283 : SlowPathCodeARM64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000284 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
285 }
286
287 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
288 LocationSummary* locations = at_->GetLocations();
289 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
290
291 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000292 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000293
294 InvokeRuntimeCallingConvention calling_convention;
295 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000296 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
297 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000298 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800299 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100300 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800301 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100302 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800303 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000304
305 // Move the class to the desired location.
306 Location out = locations->Out();
307 if (out.IsValid()) {
308 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
309 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000310 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000311 }
312
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000313 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000314 __ B(GetExitLabel());
315 }
316
Alexandre Rames9931f312015-06-19 14:47:01 +0100317 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
318
Alexandre Rames67555f72014-11-18 10:55:16 +0000319 private:
320 // The class this slow path will load.
321 HLoadClass* const cls_;
322
323 // The instruction where this slow path is happening.
324 // (Might be the load class or an initialization check).
325 HInstruction* const at_;
326
327 // The dex PC of `at_`.
328 const uint32_t dex_pc_;
329
330 // Whether to initialize the class.
331 const bool do_clinit_;
332
333 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
334};
335
336class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
337 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000338 explicit LoadStringSlowPathARM64(HLoadString* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000339
340 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
341 LocationSummary* locations = instruction_->GetLocations();
342 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
343 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
344
345 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000346 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000347
348 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000349 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
350 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
Alexandre Rames67555f72014-11-18 10:55:16 +0000351 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000352 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100353 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000354 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000355 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000356
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000357 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000358 __ B(GetExitLabel());
359 }
360
Alexandre Rames9931f312015-06-19 14:47:01 +0100361 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
362
Alexandre Rames67555f72014-11-18 10:55:16 +0000363 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000364 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
365};
366
Alexandre Rames5319def2014-10-23 10:03:10 +0100367class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
368 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000369 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100370
Alexandre Rames67555f72014-11-18 10:55:16 +0000371 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
372 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100373 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000374 if (instruction_->CanThrowIntoCatchBlock()) {
375 // Live registers will be restored in the catch block if caught.
376 SaveLiveRegisters(codegen, instruction_->GetLocations());
377 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000378 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000379 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800380 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100381 }
382
Alexandre Rames8158f282015-08-07 10:26:17 +0100383 bool IsFatal() const OVERRIDE { return true; }
384
Alexandre Rames9931f312015-06-19 14:47:01 +0100385 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
386
Alexandre Rames5319def2014-10-23 10:03:10 +0100387 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100388 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
389};
390
391class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
392 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100393 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000394 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100395
Alexandre Rames67555f72014-11-18 10:55:16 +0000396 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
397 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100398 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000399 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000400 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000401 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800402 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000403 RestoreLiveRegisters(codegen, instruction_->GetLocations());
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
411 vixl::Label* GetReturnLabel() {
412 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.
427 vixl::Label return_label_;
428
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);
Roland Levillain888d0672015-11-23 18:53:50 +0000463 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
464 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++) {
567 vixl::Label* target_label = codegen->GetLabelOf(successors[i]);
568 DCHECK(target_label->IsBound());
569 ptrdiff_t jump_offset = target_label->location() - table_start_.location();
570 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:
580 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000581 : SlowPathCodeARM64(instruction), out_(out), 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();
589 Primitive::Type type = Primitive::kPrimNot;
590 DCHECK(locations->CanCall());
591 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
592 DCHECK(instruction_->IsInstanceFieldGet() ||
593 instruction_->IsStaticFieldGet() ||
594 instruction_->IsArrayGet() ||
595 instruction_->IsLoadClass() ||
596 instruction_->IsLoadString() ||
597 instruction_->IsInstanceOf() ||
598 instruction_->IsCheckCast())
599 << "Unexpected instruction in read barrier marking slow path: "
600 << instruction_->DebugName();
601
602 __ Bind(GetEntryLabel());
603 SaveLiveRegisters(codegen, locations);
604
605 InvokeRuntimeCallingConvention calling_convention;
606 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
607 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)), obj_, type);
608 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
609 instruction_,
610 instruction_->GetDexPc(),
611 this);
612 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
613 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
614
615 RestoreLiveRegisters(codegen, locations);
616 __ B(GetExitLabel());
617 }
618
619 private:
Roland Levillain44015862016-01-22 11:47:17 +0000620 const Location out_;
621 const Location obj_;
622
623 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
624};
625
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000626// Slow path generating a read barrier for a heap reference.
627class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
628 public:
629 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
630 Location out,
631 Location ref,
632 Location obj,
633 uint32_t offset,
634 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000635 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000636 out_(out),
637 ref_(ref),
638 obj_(obj),
639 offset_(offset),
640 index_(index) {
641 DCHECK(kEmitCompilerReadBarrier);
642 // If `obj` is equal to `out` or `ref`, it means the initial object
643 // has been overwritten by (or after) the heap object reference load
644 // to be instrumented, e.g.:
645 //
646 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000647 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000648 //
649 // In that case, we have lost the information about the original
650 // object, and the emitted read barrier cannot work properly.
651 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
652 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
653 }
654
655 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
656 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
657 LocationSummary* locations = instruction_->GetLocations();
658 Primitive::Type type = Primitive::kPrimNot;
659 DCHECK(locations->CanCall());
660 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
661 DCHECK(!instruction_->IsInvoke() ||
662 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain44015862016-01-22 11:47:17 +0000663 instruction_->GetLocations()->Intrinsified()))
664 << "Unexpected instruction in read barrier for heap reference slow path: "
665 << instruction_->DebugName();
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000666 // The read barrier instrumentation does not support the
667 // HArm64IntermediateAddress instruction yet.
668 DCHECK(!(instruction_->IsArrayGet() &&
669 instruction_->AsArrayGet()->GetArray()->IsArm64IntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000670
671 __ Bind(GetEntryLabel());
672
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000673 SaveLiveRegisters(codegen, locations);
674
675 // We may have to change the index's value, but as `index_` is a
676 // constant member (like other "inputs" of this slow path),
677 // introduce a copy of it, `index`.
678 Location index = index_;
679 if (index_.IsValid()) {
680 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
681 if (instruction_->IsArrayGet()) {
682 // Compute the actual memory offset and store it in `index`.
683 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
684 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
685 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
686 // We are about to change the value of `index_reg` (see the
687 // calls to vixl::MacroAssembler::Lsl and
688 // vixl::MacroAssembler::Mov below), but it has
689 // not been saved by the previous call to
690 // art::SlowPathCode::SaveLiveRegisters, as it is a
691 // callee-save register --
692 // art::SlowPathCode::SaveLiveRegisters does not consider
693 // callee-save registers, as it has been designed with the
694 // assumption that callee-save registers are supposed to be
695 // handled by the called function. So, as a callee-save
696 // register, `index_reg` _would_ eventually be saved onto
697 // the stack, but it would be too late: we would have
698 // changed its value earlier. Therefore, we manually save
699 // it here into another freely available register,
700 // `free_reg`, chosen of course among the caller-save
701 // registers (as a callee-save `free_reg` register would
702 // exhibit the same problem).
703 //
704 // Note we could have requested a temporary register from
705 // the register allocator instead; but we prefer not to, as
706 // this is a slow path, and we know we can find a
707 // caller-save register that is available.
708 Register free_reg = FindAvailableCallerSaveRegister(codegen);
709 __ Mov(free_reg.W(), index_reg);
710 index_reg = free_reg;
711 index = LocationFrom(index_reg);
712 } else {
713 // The initial register stored in `index_` has already been
714 // saved in the call to art::SlowPathCode::SaveLiveRegisters
715 // (as it is not a callee-save register), so we can freely
716 // use it.
717 }
718 // Shifting the index value contained in `index_reg` by the scale
719 // factor (2) cannot overflow in practice, as the runtime is
720 // unable to allocate object arrays with a size larger than
721 // 2^26 - 1 (that is, 2^28 - 4 bytes).
722 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
723 static_assert(
724 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
725 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
726 __ Add(index_reg, index_reg, Operand(offset_));
727 } else {
728 DCHECK(instruction_->IsInvoke());
729 DCHECK(instruction_->GetLocations()->Intrinsified());
730 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
731 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
732 << instruction_->AsInvoke()->GetIntrinsic();
733 DCHECK_EQ(offset_, 0U);
734 DCHECK(index_.IsRegisterPair());
735 // UnsafeGet's offset location is a register pair, the low
736 // part contains the correct offset.
737 index = index_.ToLow();
738 }
739 }
740
741 // We're moving two or three locations to locations that could
742 // overlap, so we need a parallel move resolver.
743 InvokeRuntimeCallingConvention calling_convention;
744 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
745 parallel_move.AddMove(ref_,
746 LocationFrom(calling_convention.GetRegisterAt(0)),
747 type,
748 nullptr);
749 parallel_move.AddMove(obj_,
750 LocationFrom(calling_convention.GetRegisterAt(1)),
751 type,
752 nullptr);
753 if (index.IsValid()) {
754 parallel_move.AddMove(index,
755 LocationFrom(calling_convention.GetRegisterAt(2)),
756 Primitive::kPrimInt,
757 nullptr);
758 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
759 } else {
760 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
761 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
762 }
763 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
764 instruction_,
765 instruction_->GetDexPc(),
766 this);
767 CheckEntrypointTypes<
768 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
769 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
770
771 RestoreLiveRegisters(codegen, locations);
772
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000773 __ B(GetExitLabel());
774 }
775
776 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
777
778 private:
779 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
780 size_t ref = static_cast<int>(XRegisterFrom(ref_).code());
781 size_t obj = static_cast<int>(XRegisterFrom(obj_).code());
782 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
783 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
784 return Register(VIXLRegCodeFromART(i), kXRegSize);
785 }
786 }
787 // We shall never fail to find a free caller-save register, as
788 // there are more than two core caller-save registers on ARM64
789 // (meaning it is possible to find one which is different from
790 // `ref` and `obj`).
791 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
792 LOG(FATAL) << "Could not find a free register";
793 UNREACHABLE();
794 }
795
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000796 const Location out_;
797 const Location ref_;
798 const Location obj_;
799 const uint32_t offset_;
800 // An additional location containing an index to an array.
801 // Only used for HArrayGet and the UnsafeGetObject &
802 // UnsafeGetObjectVolatile intrinsics.
803 const Location index_;
804
805 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
806};
807
808// Slow path generating a read barrier for a GC root.
809class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
810 public:
811 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000812 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000813 DCHECK(kEmitCompilerReadBarrier);
814 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000815
816 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
817 LocationSummary* locations = instruction_->GetLocations();
818 Primitive::Type type = Primitive::kPrimNot;
819 DCHECK(locations->CanCall());
820 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000821 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
822 << "Unexpected instruction in read barrier for GC root slow path: "
823 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000824
825 __ Bind(GetEntryLabel());
826 SaveLiveRegisters(codegen, locations);
827
828 InvokeRuntimeCallingConvention calling_convention;
829 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
830 // The argument of the ReadBarrierForRootSlow is not a managed
831 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
832 // thus we need a 64-bit move here, and we cannot use
833 //
834 // arm64_codegen->MoveLocation(
835 // LocationFrom(calling_convention.GetRegisterAt(0)),
836 // root_,
837 // type);
838 //
839 // which would emit a 32-bit move, as `type` is a (32-bit wide)
840 // reference type (`Primitive::kPrimNot`).
841 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
842 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
843 instruction_,
844 instruction_->GetDexPc(),
845 this);
846 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
847 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
848
849 RestoreLiveRegisters(codegen, locations);
850 __ B(GetExitLabel());
851 }
852
853 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
854
855 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000856 const Location out_;
857 const Location root_;
858
859 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
860};
861
Alexandre Rames5319def2014-10-23 10:03:10 +0100862#undef __
863
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100864Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100865 Location next_location;
866 if (type == Primitive::kPrimVoid) {
867 LOG(FATAL) << "Unreachable type " << type;
868 }
869
Alexandre Rames542361f2015-01-29 16:57:31 +0000870 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100871 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
872 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000873 } else if (!Primitive::IsFloatingPointType(type) &&
874 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000875 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
876 } else {
877 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000878 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
879 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100880 }
881
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000882 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000883 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100884 return next_location;
885}
886
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100887Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100888 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100889}
890
Serban Constantinescu579885a2015-02-22 20:51:33 +0000891CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
892 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100893 const CompilerOptions& compiler_options,
894 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100895 : CodeGenerator(graph,
896 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000897 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000898 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000899 callee_saved_core_registers.list(),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000900 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100901 compiler_options,
902 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100903 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800904 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100905 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000906 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000907 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100908 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000909 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000910 uint32_literals_(std::less<uint32_t>(),
911 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100912 uint64_literals_(std::less<uint64_t>(),
913 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
914 method_patches_(MethodReferenceComparator(),
915 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
916 call_patches_(MethodReferenceComparator(),
917 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
918 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000919 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
920 boot_image_string_patches_(StringReferenceValueComparator(),
921 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
922 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
923 boot_image_address_patches_(std::less<uint32_t>(),
924 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000925 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000926 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000927}
Alexandre Rames5319def2014-10-23 10:03:10 +0100928
Alexandre Rames67555f72014-11-18 10:55:16 +0000929#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100930
Zheng Xu3927c8b2015-11-18 17:46:25 +0800931void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100932 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800933 jump_table->EmitTable(this);
934 }
935}
936
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000937void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800938 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000939 // Ensure we emit the literal pool.
940 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000941
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000942 CodeGenerator::Finalize(allocator);
943}
944
Zheng Xuad4450e2015-04-17 18:48:56 +0800945void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
946 // Note: There are 6 kinds of moves:
947 // 1. constant -> GPR/FPR (non-cycle)
948 // 2. constant -> stack (non-cycle)
949 // 3. GPR/FPR -> GPR/FPR
950 // 4. GPR/FPR -> stack
951 // 5. stack -> GPR/FPR
952 // 6. stack -> stack (non-cycle)
953 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
954 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
955 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
956 // dependency.
957 vixl_temps_.Open(GetVIXLAssembler());
958}
959
960void ParallelMoveResolverARM64::FinishEmitNativeCode() {
961 vixl_temps_.Close();
962}
963
964Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
965 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
966 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
967 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
968 Location scratch = GetScratchLocation(kind);
969 if (!scratch.Equals(Location::NoLocation())) {
970 return scratch;
971 }
972 // Allocate from VIXL temp registers.
973 if (kind == Location::kRegister) {
974 scratch = LocationFrom(vixl_temps_.AcquireX());
975 } else {
976 DCHECK(kind == Location::kFpuRegister);
977 scratch = LocationFrom(vixl_temps_.AcquireD());
978 }
979 AddScratchLocation(scratch);
980 return scratch;
981}
982
983void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
984 if (loc.IsRegister()) {
985 vixl_temps_.Release(XRegisterFrom(loc));
986 } else {
987 DCHECK(loc.IsFpuRegister());
988 vixl_temps_.Release(DRegisterFrom(loc));
989 }
990 RemoveScratchLocation(loc);
991}
992
Alexandre Rames3e69f162014-12-10 10:36:50 +0000993void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100994 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100995 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000996}
997
Alexandre Rames5319def2014-10-23 10:03:10 +0100998void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100999 MacroAssembler* masm = GetVIXLAssembler();
1000 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001001 __ Bind(&frame_entry_label_);
1002
Serban Constantinescu02164b32014-11-13 14:05:07 +00001003 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1004 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001005 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001006 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001007 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001008 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001009 __ Ldr(wzr, MemOperand(temp, 0));
1010 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001011 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001012
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001013 if (!HasEmptyFrame()) {
1014 int frame_size = GetFrameSize();
1015 // Stack layout:
1016 // sp[frame_size - 8] : lr.
1017 // ... : other preserved core registers.
1018 // ... : other preserved fp registers.
1019 // ... : reserved frame space.
1020 // sp[0] : current method.
1021 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001022 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001023 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1024 frame_size - GetCoreSpillSize());
1025 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1026 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001027 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001028}
1029
1030void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001031 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001032 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001033 if (!HasEmptyFrame()) {
1034 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001035 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1036 frame_size - FrameEntrySpillSize());
1037 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1038 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001039 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001040 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001041 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001042 __ Ret();
1043 GetAssembler()->cfi().RestoreState();
1044 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001045}
1046
Zheng Xuda403092015-04-24 17:35:39 +08001047vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
1048 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
1049 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
1050 core_spill_mask_);
1051}
1052
1053vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
1054 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1055 GetNumberOfFloatingPointRegisters()));
1056 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
1057 fpu_spill_mask_);
1058}
1059
Alexandre Rames5319def2014-10-23 10:03:10 +01001060void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1061 __ Bind(GetLabelOf(block));
1062}
1063
Calin Juravle175dc732015-08-25 15:42:32 +01001064void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1065 DCHECK(location.IsRegister());
1066 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1067}
1068
Calin Juravlee460d1d2015-09-29 04:52:17 +01001069void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1070 if (location.IsRegister()) {
1071 locations->AddTemp(location);
1072 } else {
1073 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1074 }
1075}
1076
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001077void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001078 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001079 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001080 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +01001081 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001082 if (value_can_be_null) {
1083 __ Cbz(value, &done);
1084 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001085 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
1086 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001087 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001088 if (value_can_be_null) {
1089 __ Bind(&done);
1090 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001091}
1092
David Brazdil58282f42016-01-14 12:45:10 +00001093void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001094 // Blocked core registers:
1095 // lr : Runtime reserved.
1096 // tr : Runtime reserved.
1097 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1098 // ip1 : VIXL core temp.
1099 // ip0 : VIXL core temp.
1100 //
1101 // Blocked fp registers:
1102 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001103 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1104 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001105 while (!reserved_core_registers.IsEmpty()) {
1106 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
1107 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001108
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001109 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001110 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001111 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
1112 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001113
David Brazdil58282f42016-01-14 12:45:10 +00001114 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001115 // Stubs do not save callee-save floating point registers. If the graph
1116 // is debuggable, we need to deal with these registers differently. For
1117 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001118 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1119 while (!reserved_fp_registers_debuggable.IsEmpty()) {
1120 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().code()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001121 }
1122 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001123}
1124
Alexandre Rames3e69f162014-12-10 10:36:50 +00001125size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1126 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1127 __ Str(reg, MemOperand(sp, stack_index));
1128 return kArm64WordSize;
1129}
1130
1131size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1132 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1133 __ Ldr(reg, MemOperand(sp, stack_index));
1134 return kArm64WordSize;
1135}
1136
1137size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1138 FPRegister reg = FPRegister(reg_id, kDRegSize);
1139 __ Str(reg, MemOperand(sp, stack_index));
1140 return kArm64WordSize;
1141}
1142
1143size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1144 FPRegister reg = FPRegister(reg_id, kDRegSize);
1145 __ Ldr(reg, MemOperand(sp, stack_index));
1146 return kArm64WordSize;
1147}
1148
Alexandre Rames5319def2014-10-23 10:03:10 +01001149void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001150 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001151}
1152
1153void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001154 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001155}
1156
Alexandre Rames67555f72014-11-18 10:55:16 +00001157void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001158 if (constant->IsIntConstant()) {
1159 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1160 } else if (constant->IsLongConstant()) {
1161 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1162 } else if (constant->IsNullConstant()) {
1163 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001164 } else if (constant->IsFloatConstant()) {
1165 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1166 } else {
1167 DCHECK(constant->IsDoubleConstant());
1168 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1169 }
1170}
1171
Alexandre Rames3e69f162014-12-10 10:36:50 +00001172
1173static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1174 DCHECK(constant.IsConstant());
1175 HConstant* cst = constant.GetConstant();
1176 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001177 // Null is mapped to a core W register, which we associate with kPrimInt.
1178 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001179 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1180 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1181 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1182}
1183
Calin Juravlee460d1d2015-09-29 04:52:17 +01001184void CodeGeneratorARM64::MoveLocation(Location destination,
1185 Location source,
1186 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001187 if (source.Equals(destination)) {
1188 return;
1189 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001190
1191 // A valid move can always be inferred from the destination and source
1192 // locations. When moving from and to a register, the argument type can be
1193 // used to generate 32bit instead of 64bit moves. In debug mode we also
1194 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001195 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001196
1197 if (destination.IsRegister() || destination.IsFpuRegister()) {
1198 if (unspecified_type) {
1199 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1200 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001201 (src_cst != nullptr && (src_cst->IsIntConstant()
1202 || src_cst->IsFloatConstant()
1203 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001204 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001205 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001206 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001207 // If the source is a double stack slot or a 64bit constant, a 64bit
1208 // type is appropriate. Else the source is a register, and since the
1209 // type has not been specified, we chose a 64bit type to force a 64bit
1210 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001211 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001212 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001213 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001214 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1215 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1216 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001217 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1218 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1219 __ Ldr(dst, StackOperandFrom(source));
1220 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001221 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001222 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001223 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001224 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001225 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001226 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001227 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001228 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1229 ? Primitive::kPrimLong
1230 : Primitive::kPrimInt;
1231 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1232 }
1233 } else {
1234 DCHECK(source.IsFpuRegister());
1235 if (destination.IsRegister()) {
1236 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1237 ? Primitive::kPrimDouble
1238 : Primitive::kPrimFloat;
1239 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1240 } else {
1241 DCHECK(destination.IsFpuRegister());
1242 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001243 }
1244 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001245 } else { // The destination is not a register. It must be a stack slot.
1246 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1247 if (source.IsRegister() || source.IsFpuRegister()) {
1248 if (unspecified_type) {
1249 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001250 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001251 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001252 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001253 }
1254 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001255 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1256 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1257 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001258 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001259 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1260 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001261 UseScratchRegisterScope temps(GetVIXLAssembler());
1262 HConstant* src_cst = source.GetConstant();
1263 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001264 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001265 temp = temps.AcquireW();
1266 } else if (src_cst->IsLongConstant()) {
1267 temp = temps.AcquireX();
1268 } else if (src_cst->IsFloatConstant()) {
1269 temp = temps.AcquireS();
1270 } else {
1271 DCHECK(src_cst->IsDoubleConstant());
1272 temp = temps.AcquireD();
1273 }
1274 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001275 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001276 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001277 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001278 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001279 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001280 // There is generally less pressure on FP registers.
1281 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001282 __ Ldr(temp, StackOperandFrom(source));
1283 __ Str(temp, StackOperandFrom(destination));
1284 }
1285 }
1286}
1287
1288void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001289 CPURegister dst,
1290 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001291 switch (type) {
1292 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001293 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001294 break;
1295 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001296 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001297 break;
1298 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001299 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001300 break;
1301 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001302 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001303 break;
1304 case Primitive::kPrimInt:
1305 case Primitive::kPrimNot:
1306 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001307 case Primitive::kPrimFloat:
1308 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001309 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001310 __ Ldr(dst, src);
1311 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001312 case Primitive::kPrimVoid:
1313 LOG(FATAL) << "Unreachable type " << type;
1314 }
1315}
1316
Calin Juravle77520bc2015-01-12 18:45:46 +00001317void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001318 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001319 const MemOperand& src,
1320 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001321 MacroAssembler* masm = GetVIXLAssembler();
1322 BlockPoolsScope block_pools(masm);
1323 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001324 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001325 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001326
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001327 DCHECK(!src.IsPreIndex());
1328 DCHECK(!src.IsPostIndex());
1329
1330 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001331 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001332 MemOperand base = MemOperand(temp_base);
1333 switch (type) {
1334 case Primitive::kPrimBoolean:
1335 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001336 if (needs_null_check) {
1337 MaybeRecordImplicitNullCheck(instruction);
1338 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001339 break;
1340 case Primitive::kPrimByte:
1341 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001342 if (needs_null_check) {
1343 MaybeRecordImplicitNullCheck(instruction);
1344 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001345 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1346 break;
1347 case Primitive::kPrimChar:
1348 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001349 if (needs_null_check) {
1350 MaybeRecordImplicitNullCheck(instruction);
1351 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001352 break;
1353 case Primitive::kPrimShort:
1354 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001355 if (needs_null_check) {
1356 MaybeRecordImplicitNullCheck(instruction);
1357 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001358 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1359 break;
1360 case Primitive::kPrimInt:
1361 case Primitive::kPrimNot:
1362 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001363 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001364 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001365 if (needs_null_check) {
1366 MaybeRecordImplicitNullCheck(instruction);
1367 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001368 break;
1369 case Primitive::kPrimFloat:
1370 case Primitive::kPrimDouble: {
1371 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001372 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001373
1374 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1375 __ Ldar(temp, 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 __ Fmov(FPRegister(dst), temp);
1380 break;
1381 }
1382 case Primitive::kPrimVoid:
1383 LOG(FATAL) << "Unreachable type " << type;
1384 }
1385}
1386
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001387void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001388 CPURegister src,
1389 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001390 switch (type) {
1391 case Primitive::kPrimBoolean:
1392 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001393 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001394 break;
1395 case Primitive::kPrimChar:
1396 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001397 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001398 break;
1399 case Primitive::kPrimInt:
1400 case Primitive::kPrimNot:
1401 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001402 case Primitive::kPrimFloat:
1403 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001404 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001405 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001406 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001407 case Primitive::kPrimVoid:
1408 LOG(FATAL) << "Unreachable type " << type;
1409 }
1410}
1411
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001412void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1413 CPURegister src,
1414 const MemOperand& dst) {
1415 UseScratchRegisterScope temps(GetVIXLAssembler());
1416 Register temp_base = temps.AcquireX();
1417
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001418 DCHECK(!dst.IsPreIndex());
1419 DCHECK(!dst.IsPostIndex());
1420
1421 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001422 Operand op = OperandFromMemOperand(dst);
1423 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001424 MemOperand base = MemOperand(temp_base);
1425 switch (type) {
1426 case Primitive::kPrimBoolean:
1427 case Primitive::kPrimByte:
1428 __ Stlrb(Register(src), base);
1429 break;
1430 case Primitive::kPrimChar:
1431 case Primitive::kPrimShort:
1432 __ Stlrh(Register(src), base);
1433 break;
1434 case Primitive::kPrimInt:
1435 case Primitive::kPrimNot:
1436 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001437 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001438 __ Stlr(Register(src), base);
1439 break;
1440 case Primitive::kPrimFloat:
1441 case Primitive::kPrimDouble: {
1442 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001443 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001444
1445 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1446 __ Fmov(temp, FPRegister(src));
1447 __ Stlr(temp, base);
1448 break;
1449 }
1450 case Primitive::kPrimVoid:
1451 LOG(FATAL) << "Unreachable type " << type;
1452 }
1453}
1454
Calin Juravle175dc732015-08-25 15:42:32 +01001455void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1456 HInstruction* instruction,
1457 uint32_t dex_pc,
1458 SlowPathCode* slow_path) {
1459 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1460 instruction,
1461 dex_pc,
1462 slow_path);
1463}
1464
Alexandre Rames67555f72014-11-18 10:55:16 +00001465void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1466 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001467 uint32_t dex_pc,
1468 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001469 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001470 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001471 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1472 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001473 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001474}
1475
1476void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1477 vixl::Register class_reg) {
1478 UseScratchRegisterScope temps(GetVIXLAssembler());
1479 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001480 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1481
Serban Constantinescu02164b32014-11-13 14:05:07 +00001482 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001483 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1484 __ Add(temp, class_reg, status_offset);
1485 __ Ldar(temp, HeapOperand(temp));
1486 __ Cmp(temp, mirror::Class::kStatusInitialized);
1487 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001488 __ Bind(slow_path->GetExitLabel());
1489}
Alexandre Rames5319def2014-10-23 10:03:10 +01001490
Roland Levillain44015862016-01-22 11:47:17 +00001491void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001492 BarrierType type = BarrierAll;
1493
1494 switch (kind) {
1495 case MemBarrierKind::kAnyAny:
1496 case MemBarrierKind::kAnyStore: {
1497 type = BarrierAll;
1498 break;
1499 }
1500 case MemBarrierKind::kLoadAny: {
1501 type = BarrierReads;
1502 break;
1503 }
1504 case MemBarrierKind::kStoreStore: {
1505 type = BarrierWrites;
1506 break;
1507 }
1508 default:
1509 LOG(FATAL) << "Unexpected memory barrier " << kind;
1510 }
1511 __ Dmb(InnerShareable, type);
1512}
1513
Serban Constantinescu02164b32014-11-13 14:05:07 +00001514void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1515 HBasicBlock* successor) {
1516 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001517 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1518 if (slow_path == nullptr) {
1519 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1520 instruction->SetSlowPath(slow_path);
1521 codegen_->AddSlowPath(slow_path);
1522 if (successor != nullptr) {
1523 DCHECK(successor->IsLoopHeader());
1524 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1525 }
1526 } else {
1527 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1528 }
1529
Serban Constantinescu02164b32014-11-13 14:05:07 +00001530 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1531 Register temp = temps.AcquireW();
1532
1533 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1534 if (successor == nullptr) {
1535 __ Cbnz(temp, slow_path->GetEntryLabel());
1536 __ Bind(slow_path->GetReturnLabel());
1537 } else {
1538 __ Cbz(temp, codegen_->GetLabelOf(successor));
1539 __ B(slow_path->GetEntryLabel());
1540 // slow_path will return to GetLabelOf(successor).
1541 }
1542}
1543
Alexandre Rames5319def2014-10-23 10:03:10 +01001544InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1545 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001546 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001547 assembler_(codegen->GetAssembler()),
1548 codegen_(codegen) {}
1549
1550#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001551 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001552
1553#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1554
1555enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001556 // Using a base helps identify when we hit such breakpoints.
1557 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001558#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1559 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1560#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1561};
1562
1563#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001564 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001565 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1566 } \
1567 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1568 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1569 locations->SetOut(Location::Any()); \
1570 }
1571 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1572#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1573
1574#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001575#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001576
Alexandre Rames67555f72014-11-18 10:55:16 +00001577void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001578 DCHECK_EQ(instr->InputCount(), 2U);
1579 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1580 Primitive::Type type = instr->GetResultType();
1581 switch (type) {
1582 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001583 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001584 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001585 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001586 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001587 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001588
1589 case Primitive::kPrimFloat:
1590 case Primitive::kPrimDouble:
1591 locations->SetInAt(0, Location::RequiresFpuRegister());
1592 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001593 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001594 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001595
Alexandre Rames5319def2014-10-23 10:03:10 +01001596 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001597 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001598 }
1599}
1600
Alexandre Rames09a99962015-04-15 11:47:56 +01001601void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001602 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1603
1604 bool object_field_get_with_read_barrier =
1605 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001606 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001607 new (GetGraph()->GetArena()) LocationSummary(instruction,
1608 object_field_get_with_read_barrier ?
1609 LocationSummary::kCallOnSlowPath :
1610 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001611 locations->SetInAt(0, Location::RequiresRegister());
1612 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1613 locations->SetOut(Location::RequiresFpuRegister());
1614 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001615 // The output overlaps for an object field get when read barriers
1616 // are enabled: we do not want the load to overwrite the object's
1617 // location, as we need it to emit the read barrier.
1618 locations->SetOut(
1619 Location::RequiresRegister(),
1620 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001621 }
1622}
1623
1624void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1625 const FieldInfo& field_info) {
1626 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001627 LocationSummary* locations = instruction->GetLocations();
1628 Location base_loc = locations->InAt(0);
1629 Location out = locations->Out();
1630 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001631 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001632 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001633 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001634
Roland Levillain44015862016-01-22 11:47:17 +00001635 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1636 // Object FieldGet with Baker's read barrier case.
1637 MacroAssembler* masm = GetVIXLAssembler();
1638 UseScratchRegisterScope temps(masm);
1639 // /* HeapReference<Object> */ out = *(base + offset)
1640 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1641 Register temp = temps.AcquireW();
1642 // Note that potential implicit null checks are handled in this
1643 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1644 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1645 instruction,
1646 out,
1647 base,
1648 offset,
1649 temp,
1650 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001651 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001652 } else {
1653 // General case.
1654 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001655 // Note that a potential implicit null check is handled in this
1656 // CodeGeneratorARM64::LoadAcquire call.
1657 // NB: LoadAcquire will record the pc info if needed.
1658 codegen_->LoadAcquire(
1659 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001660 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001661 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001662 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001663 }
Roland Levillain44015862016-01-22 11:47:17 +00001664 if (field_type == Primitive::kPrimNot) {
1665 // If read barriers are enabled, emit read barriers other than
1666 // Baker's using a slow path (and also unpoison the loaded
1667 // reference, if heap poisoning is enabled).
1668 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1669 }
Roland Levillain4d027112015-07-01 15:41:14 +01001670 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001671}
1672
1673void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1674 LocationSummary* locations =
1675 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1676 locations->SetInAt(0, Location::RequiresRegister());
1677 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1678 locations->SetInAt(1, Location::RequiresFpuRegister());
1679 } else {
1680 locations->SetInAt(1, Location::RequiresRegister());
1681 }
1682}
1683
1684void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001685 const FieldInfo& field_info,
1686 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001687 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001688 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001689
1690 Register obj = InputRegisterAt(instruction, 0);
1691 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001692 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001693 Offset offset = field_info.GetFieldOffset();
1694 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001695
Roland Levillain4d027112015-07-01 15:41:14 +01001696 {
1697 // We use a block to end the scratch scope before the write barrier, thus
1698 // freeing the temporary registers so they can be used in `MarkGCCard`.
1699 UseScratchRegisterScope temps(GetVIXLAssembler());
1700
1701 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1702 DCHECK(value.IsW());
1703 Register temp = temps.AcquireW();
1704 __ Mov(temp, value.W());
1705 GetAssembler()->PoisonHeapReference(temp.W());
1706 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001707 }
Roland Levillain4d027112015-07-01 15:41:14 +01001708
1709 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001710 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1711 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001712 } else {
1713 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1714 codegen_->MaybeRecordImplicitNullCheck(instruction);
1715 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001716 }
1717
1718 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001719 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001720 }
1721}
1722
Alexandre Rames67555f72014-11-18 10:55:16 +00001723void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001724 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001725
1726 switch (type) {
1727 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001728 case Primitive::kPrimLong: {
1729 Register dst = OutputRegister(instr);
1730 Register lhs = InputRegisterAt(instr, 0);
1731 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001732 if (instr->IsAdd()) {
1733 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001734 } else if (instr->IsAnd()) {
1735 __ And(dst, lhs, rhs);
1736 } else if (instr->IsOr()) {
1737 __ Orr(dst, lhs, rhs);
1738 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001739 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001740 } else if (instr->IsRor()) {
1741 if (rhs.IsImmediate()) {
1742 uint32_t shift = rhs.immediate() & (lhs.SizeInBits() - 1);
1743 __ Ror(dst, lhs, shift);
1744 } else {
1745 // Ensure shift distance is in the same size register as the result. If
1746 // we are rotating a long and the shift comes in a w register originally,
1747 // we don't need to sxtw for use as an x since the shift distances are
1748 // all & reg_bits - 1.
1749 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1750 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001751 } else {
1752 DCHECK(instr->IsXor());
1753 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001754 }
1755 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001756 }
1757 case Primitive::kPrimFloat:
1758 case Primitive::kPrimDouble: {
1759 FPRegister dst = OutputFPRegister(instr);
1760 FPRegister lhs = InputFPRegisterAt(instr, 0);
1761 FPRegister rhs = InputFPRegisterAt(instr, 1);
1762 if (instr->IsAdd()) {
1763 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001764 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001765 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001766 } else {
1767 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001768 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001769 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001770 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001771 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001772 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001773 }
1774}
1775
Serban Constantinescu02164b32014-11-13 14:05:07 +00001776void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1777 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1778
1779 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1780 Primitive::Type type = instr->GetResultType();
1781 switch (type) {
1782 case Primitive::kPrimInt:
1783 case Primitive::kPrimLong: {
1784 locations->SetInAt(0, Location::RequiresRegister());
1785 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1786 locations->SetOut(Location::RequiresRegister());
1787 break;
1788 }
1789 default:
1790 LOG(FATAL) << "Unexpected shift type " << type;
1791 }
1792}
1793
1794void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1795 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1796
1797 Primitive::Type type = instr->GetType();
1798 switch (type) {
1799 case Primitive::kPrimInt:
1800 case Primitive::kPrimLong: {
1801 Register dst = OutputRegister(instr);
1802 Register lhs = InputRegisterAt(instr, 0);
1803 Operand rhs = InputOperandAt(instr, 1);
1804 if (rhs.IsImmediate()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001805 uint32_t shift_value = rhs.immediate() &
1806 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001807 if (instr->IsShl()) {
1808 __ Lsl(dst, lhs, shift_value);
1809 } else if (instr->IsShr()) {
1810 __ Asr(dst, lhs, shift_value);
1811 } else {
1812 __ Lsr(dst, lhs, shift_value);
1813 }
1814 } else {
1815 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1816
1817 if (instr->IsShl()) {
1818 __ Lsl(dst, lhs, rhs_reg);
1819 } else if (instr->IsShr()) {
1820 __ Asr(dst, lhs, rhs_reg);
1821 } else {
1822 __ Lsr(dst, lhs, rhs_reg);
1823 }
1824 }
1825 break;
1826 }
1827 default:
1828 LOG(FATAL) << "Unexpected shift operation type " << type;
1829 }
1830}
1831
Alexandre Rames5319def2014-10-23 10:03:10 +01001832void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001833 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001834}
1835
1836void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001837 HandleBinaryOp(instruction);
1838}
1839
1840void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1841 HandleBinaryOp(instruction);
1842}
1843
1844void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1845 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001846}
1847
Artem Serov7fc63502016-02-09 17:15:29 +00001848void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001849 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1850 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1851 locations->SetInAt(0, Location::RequiresRegister());
1852 // There is no immediate variant of negated bitwise instructions in AArch64.
1853 locations->SetInAt(1, Location::RequiresRegister());
1854 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1855}
1856
Artem Serov7fc63502016-02-09 17:15:29 +00001857void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001858 Register dst = OutputRegister(instr);
1859 Register lhs = InputRegisterAt(instr, 0);
1860 Register rhs = InputRegisterAt(instr, 1);
1861
1862 switch (instr->GetOpKind()) {
1863 case HInstruction::kAnd:
1864 __ Bic(dst, lhs, rhs);
1865 break;
1866 case HInstruction::kOr:
1867 __ Orn(dst, lhs, rhs);
1868 break;
1869 case HInstruction::kXor:
1870 __ Eon(dst, lhs, rhs);
1871 break;
1872 default:
1873 LOG(FATAL) << "Unreachable";
1874 }
1875}
1876
Alexandre Rames8626b742015-11-25 16:28:08 +00001877void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1878 HArm64DataProcWithShifterOp* instruction) {
1879 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1880 instruction->GetType() == Primitive::kPrimLong);
1881 LocationSummary* locations =
1882 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1883 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1884 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1885 } else {
1886 locations->SetInAt(0, Location::RequiresRegister());
1887 }
1888 locations->SetInAt(1, Location::RequiresRegister());
1889 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1890}
1891
1892void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1893 HArm64DataProcWithShifterOp* instruction) {
1894 Primitive::Type type = instruction->GetType();
1895 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1896 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1897 Register out = OutputRegister(instruction);
1898 Register left;
1899 if (kind != HInstruction::kNeg) {
1900 left = InputRegisterAt(instruction, 0);
1901 }
1902 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1903 // shifter operand operation, the IR generating `right_reg` (input to the type
1904 // conversion) can have a different type from the current instruction's type,
1905 // so we manually indicate the type.
1906 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001907 int64_t shift_amount = instruction->GetShiftAmount() &
1908 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001909
1910 Operand right_operand(0);
1911
1912 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1913 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1914 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1915 } else {
1916 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1917 }
1918
1919 // Logical binary operations do not support extension operations in the
1920 // operand. Note that VIXL would still manage if it was passed by generating
1921 // the extension as a separate instruction.
1922 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1923 DCHECK(!right_operand.IsExtendedRegister() ||
1924 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1925 kind != HInstruction::kNeg));
1926 switch (kind) {
1927 case HInstruction::kAdd:
1928 __ Add(out, left, right_operand);
1929 break;
1930 case HInstruction::kAnd:
1931 __ And(out, left, right_operand);
1932 break;
1933 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001934 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001935 __ Neg(out, right_operand);
1936 break;
1937 case HInstruction::kOr:
1938 __ Orr(out, left, right_operand);
1939 break;
1940 case HInstruction::kSub:
1941 __ Sub(out, left, right_operand);
1942 break;
1943 case HInstruction::kXor:
1944 __ Eor(out, left, right_operand);
1945 break;
1946 default:
1947 LOG(FATAL) << "Unexpected operation kind: " << kind;
1948 UNREACHABLE();
1949 }
1950}
1951
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001952void LocationsBuilderARM64::VisitArm64IntermediateAddress(HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001953 // The read barrier instrumentation does not support the
1954 // HArm64IntermediateAddress instruction yet.
1955 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001956 LocationSummary* locations =
1957 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1958 locations->SetInAt(0, Location::RequiresRegister());
1959 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1960 locations->SetOut(Location::RequiresRegister());
1961}
1962
1963void InstructionCodeGeneratorARM64::VisitArm64IntermediateAddress(
1964 HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001965 // The read barrier instrumentation does not support the
1966 // HArm64IntermediateAddress instruction yet.
1967 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001968 __ Add(OutputRegister(instruction),
1969 InputRegisterAt(instruction, 0),
1970 Operand(InputOperandAt(instruction, 1)));
1971}
1972
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001973void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00001974 LocationSummary* locations =
1975 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001976 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
1977 if (instr->GetOpKind() == HInstruction::kSub &&
1978 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00001979 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001980 // Don't allocate register for Mneg instruction.
1981 } else {
1982 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
1983 Location::RequiresRegister());
1984 }
1985 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
1986 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00001987 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1988}
1989
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001990void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00001991 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001992 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
1993 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00001994
1995 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
1996 // This fixup should be carried out for all multiply-accumulate instructions:
1997 // madd, msub, smaddl, smsubl, umaddl and umsubl.
1998 if (instr->GetType() == Primitive::kPrimLong &&
1999 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2000 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
2001 vixl::Instruction* prev = masm->GetCursorAddress<vixl::Instruction*>() - vixl::kInstructionSize;
2002 if (prev->IsLoadOrStore()) {
2003 // Make sure we emit only exactly one nop.
2004 vixl::CodeBufferCheckScope scope(masm,
2005 vixl::kInstructionSize,
2006 vixl::CodeBufferCheckScope::kCheck,
2007 vixl::CodeBufferCheckScope::kExactSize);
2008 __ nop();
2009 }
2010 }
2011
2012 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002013 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002014 __ Madd(res, mul_left, mul_right, accumulator);
2015 } else {
2016 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002017 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002018 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002019 __ Mneg(res, mul_left, mul_right);
2020 } else {
2021 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2022 __ Msub(res, mul_left, mul_right, accumulator);
2023 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002024 }
2025}
2026
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002027void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002028 bool object_array_get_with_read_barrier =
2029 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002030 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002031 new (GetGraph()->GetArena()) LocationSummary(instruction,
2032 object_array_get_with_read_barrier ?
2033 LocationSummary::kCallOnSlowPath :
2034 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002035 locations->SetInAt(0, Location::RequiresRegister());
2036 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002037 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2038 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2039 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002040 // The output overlaps in the case of an object array get with
2041 // read barriers enabled: we do not want the move to overwrite the
2042 // array's location, as we need it to emit the read barrier.
2043 locations->SetOut(
2044 Location::RequiresRegister(),
2045 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002046 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002047}
2048
2049void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002050 Primitive::Type type = instruction->GetType();
2051 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002052 LocationSummary* locations = instruction->GetLocations();
2053 Location index = locations->InAt(1);
2054 uint32_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Roland Levillain44015862016-01-22 11:47:17 +00002055 Location out = locations->Out();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002056
Alexandre Ramesd921d642015-04-16 15:07:16 +01002057 MacroAssembler* masm = GetVIXLAssembler();
2058 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002059 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002060 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002061
Roland Levillain44015862016-01-22 11:47:17 +00002062 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2063 // Object ArrayGet with Baker's read barrier case.
2064 Register temp = temps.AcquireW();
2065 // The read barrier instrumentation does not support the
2066 // HArm64IntermediateAddress instruction yet.
2067 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
2068 // Note that a potential implicit null check is handled in the
2069 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2070 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2071 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002072 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002073 // General case.
2074 MemOperand source = HeapOperand(obj);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002075 if (index.IsConstant()) {
Roland Levillain44015862016-01-22 11:47:17 +00002076 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2077 source = HeapOperand(obj, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002078 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002079 Register temp = temps.AcquireSameSizeAs(obj);
2080 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
2081 // The read barrier instrumentation does not support the
2082 // HArm64IntermediateAddress instruction yet.
2083 DCHECK(!kEmitCompilerReadBarrier);
2084 // We do not need to compute the intermediate address from the array: the
2085 // input instruction has done it already. See the comment in
2086 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2087 if (kIsDebugBuild) {
2088 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2089 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2090 }
2091 temp = obj;
2092 } else {
2093 __ Add(temp, obj, offset);
2094 }
2095 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2096 }
2097
2098 codegen_->Load(type, OutputCPURegister(instruction), source);
2099 codegen_->MaybeRecordImplicitNullCheck(instruction);
2100
2101 if (type == Primitive::kPrimNot) {
2102 static_assert(
2103 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2104 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2105 Location obj_loc = locations->InAt(0);
2106 if (index.IsConstant()) {
2107 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2108 } else {
2109 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2110 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002111 }
Roland Levillain4d027112015-07-01 15:41:14 +01002112 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002113}
2114
Alexandre Rames5319def2014-10-23 10:03:10 +01002115void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2116 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2117 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002118 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002119}
2120
2121void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002122 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002123 BlockPoolsScope block_pools(GetVIXLAssembler());
Vladimir Markodce016e2016-04-28 13:10:02 +01002124 __ Ldr(OutputRegister(instruction), HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002125 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002126}
2127
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002128void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002129 Primitive::Type value_type = instruction->GetComponentType();
2130
2131 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2132 bool object_array_set_with_read_barrier =
2133 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002134 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2135 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002136 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2137 LocationSummary::kCallOnSlowPath :
2138 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002139 locations->SetInAt(0, Location::RequiresRegister());
2140 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002141 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002142 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002143 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002144 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002145 }
2146}
2147
2148void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2149 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002150 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002151 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002152 bool needs_write_barrier =
2153 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002154
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002155 Register array = InputRegisterAt(instruction, 0);
2156 CPURegister value = InputCPURegisterAt(instruction, 2);
2157 CPURegister source = value;
2158 Location index = locations->InAt(1);
2159 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2160 MemOperand destination = HeapOperand(array);
2161 MacroAssembler* masm = GetVIXLAssembler();
2162 BlockPoolsScope block_pools(masm);
2163
2164 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002165 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002166 if (index.IsConstant()) {
2167 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2168 destination = HeapOperand(array, offset);
2169 } else {
2170 UseScratchRegisterScope temps(masm);
2171 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002172 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002173 // The read barrier instrumentation does not support the
2174 // HArm64IntermediateAddress instruction yet.
2175 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002176 // We do not need to compute the intermediate address from the array: the
2177 // input instruction has done it already. See the comment in
2178 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2179 if (kIsDebugBuild) {
2180 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2181 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2182 }
2183 temp = array;
2184 } else {
2185 __ Add(temp, array, offset);
2186 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002187 destination = HeapOperand(temp,
2188 XRegisterFrom(index),
2189 LSL,
2190 Primitive::ComponentSizeShift(value_type));
2191 }
2192 codegen_->Store(value_type, value, destination);
2193 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002194 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002195 DCHECK(needs_write_barrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002196 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002197 vixl::Label done;
2198 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002199 {
2200 // We use a block to end the scratch scope before the write barrier, thus
2201 // freeing the temporary registers so they can be used in `MarkGCCard`.
2202 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002203 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002204 if (index.IsConstant()) {
2205 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002206 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002207 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002208 destination = HeapOperand(temp,
2209 XRegisterFrom(index),
2210 LSL,
2211 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002212 }
2213
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002214 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2215 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2216 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2217
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002218 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002219 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2220 codegen_->AddSlowPath(slow_path);
2221 if (instruction->GetValueCanBeNull()) {
2222 vixl::Label non_zero;
2223 __ Cbnz(Register(value), &non_zero);
2224 if (!index.IsConstant()) {
2225 __ Add(temp, array, offset);
2226 }
2227 __ Str(wzr, destination);
2228 codegen_->MaybeRecordImplicitNullCheck(instruction);
2229 __ B(&done);
2230 __ Bind(&non_zero);
2231 }
2232
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002233 if (kEmitCompilerReadBarrier) {
2234 // When read barriers are enabled, the type checking
2235 // instrumentation requires two read barriers:
2236 //
2237 // __ Mov(temp2, temp);
2238 // // /* HeapReference<Class> */ temp = temp->component_type_
2239 // __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002240 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002241 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2242 //
2243 // // /* HeapReference<Class> */ temp2 = value->klass_
2244 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002245 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002246 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2247 //
2248 // __ Cmp(temp, temp2);
2249 //
2250 // However, the second read barrier may trash `temp`, as it
2251 // is a temporary register, and as such would not be saved
2252 // along with live registers before calling the runtime (nor
2253 // restored afterwards). So in this case, we bail out and
2254 // delegate the work to the array set slow path.
2255 //
2256 // TODO: Extend the register allocator to support a new
2257 // "(locally) live temp" location so as to avoid always
2258 // going into the slow path when read barriers are enabled.
2259 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002260 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002261 Register temp2 = temps.AcquireSameSizeAs(array);
2262 // /* HeapReference<Class> */ temp = array->klass_
2263 __ Ldr(temp, HeapOperand(array, class_offset));
2264 codegen_->MaybeRecordImplicitNullCheck(instruction);
2265 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2266
2267 // /* HeapReference<Class> */ temp = temp->component_type_
2268 __ Ldr(temp, HeapOperand(temp, component_offset));
2269 // /* HeapReference<Class> */ temp2 = value->klass_
2270 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2271 // If heap poisoning is enabled, no need to unpoison `temp`
2272 // nor `temp2`, as we are comparing two poisoned references.
2273 __ Cmp(temp, temp2);
2274
2275 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2276 vixl::Label do_put;
2277 __ B(eq, &do_put);
2278 // If heap poisoning is enabled, the `temp` reference has
2279 // not been unpoisoned yet; unpoison it now.
2280 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2281
2282 // /* HeapReference<Class> */ temp = temp->super_class_
2283 __ Ldr(temp, HeapOperand(temp, super_offset));
2284 // If heap poisoning is enabled, no need to unpoison
2285 // `temp`, as we are comparing against null below.
2286 __ Cbnz(temp, slow_path->GetEntryLabel());
2287 __ Bind(&do_put);
2288 } else {
2289 __ B(ne, slow_path->GetEntryLabel());
2290 }
2291 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002292 }
2293 }
2294
2295 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002296 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002297 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002298 __ Mov(temp2, value.W());
2299 GetAssembler()->PoisonHeapReference(temp2);
2300 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002301 }
2302
2303 if (!index.IsConstant()) {
2304 __ Add(temp, array, offset);
2305 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002306 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002307
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002308 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002309 codegen_->MaybeRecordImplicitNullCheck(instruction);
2310 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002311 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002312
2313 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2314
2315 if (done.IsLinked()) {
2316 __ Bind(&done);
2317 }
2318
2319 if (slow_path != nullptr) {
2320 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002321 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002322 }
2323}
2324
Alexandre Rames67555f72014-11-18 10:55:16 +00002325void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002326 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2327 ? LocationSummary::kCallOnSlowPath
2328 : LocationSummary::kNoCall;
2329 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002330 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002331 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002332 if (instruction->HasUses()) {
2333 locations->SetOut(Location::SameAsFirstInput());
2334 }
2335}
2336
2337void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002338 BoundsCheckSlowPathARM64* slow_path =
2339 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002340 codegen_->AddSlowPath(slow_path);
2341
2342 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2343 __ B(slow_path->GetEntryLabel(), hs);
2344}
2345
Alexandre Rames67555f72014-11-18 10:55:16 +00002346void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2347 LocationSummary* locations =
2348 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2349 locations->SetInAt(0, Location::RequiresRegister());
2350 if (check->HasUses()) {
2351 locations->SetOut(Location::SameAsFirstInput());
2352 }
2353}
2354
2355void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2356 // We assume the class is not null.
2357 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2358 check->GetLoadClass(), check, check->GetDexPc(), true);
2359 codegen_->AddSlowPath(slow_path);
2360 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2361}
2362
Roland Levillain1a653882016-03-18 18:05:57 +00002363static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2364 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2365 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2366}
2367
2368void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2369 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2370 Location rhs_loc = instruction->GetLocations()->InAt(1);
2371 if (rhs_loc.IsConstant()) {
2372 // 0.0 is the only immediate that can be encoded directly in
2373 // an FCMP instruction.
2374 //
2375 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2376 // specify that in a floating-point comparison, positive zero
2377 // and negative zero are considered equal, so we can use the
2378 // literal 0.0 for both cases here.
2379 //
2380 // Note however that some methods (Float.equal, Float.compare,
2381 // Float.compareTo, Double.equal, Double.compare,
2382 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2383 // StrictMath.min) consider 0.0 to be (strictly) greater than
2384 // -0.0. So if we ever translate calls to these methods into a
2385 // HCompare instruction, we must handle the -0.0 case with
2386 // care here.
2387 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2388 __ Fcmp(lhs_reg, 0.0);
2389 } else {
2390 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2391 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002392}
2393
Serban Constantinescu02164b32014-11-13 14:05:07 +00002394void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002395 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002396 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2397 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002398 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002399 case Primitive::kPrimBoolean:
2400 case Primitive::kPrimByte:
2401 case Primitive::kPrimShort:
2402 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002403 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002404 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002405 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002406 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002407 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2408 break;
2409 }
2410 case Primitive::kPrimFloat:
2411 case Primitive::kPrimDouble: {
2412 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002413 locations->SetInAt(1,
2414 IsFloatingPointZeroConstant(compare->InputAt(1))
2415 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2416 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002417 locations->SetOut(Location::RequiresRegister());
2418 break;
2419 }
2420 default:
2421 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2422 }
2423}
2424
2425void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2426 Primitive::Type in_type = compare->InputAt(0)->GetType();
2427
2428 // 0 if: left == right
2429 // 1 if: left > right
2430 // -1 if: left < right
2431 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002432 case Primitive::kPrimBoolean:
2433 case Primitive::kPrimByte:
2434 case Primitive::kPrimShort:
2435 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002436 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002437 case Primitive::kPrimLong: {
2438 Register result = OutputRegister(compare);
2439 Register left = InputRegisterAt(compare, 0);
2440 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002441 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002442 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2443 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002444 break;
2445 }
2446 case Primitive::kPrimFloat:
2447 case Primitive::kPrimDouble: {
2448 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002449 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002450 __ Cset(result, ne);
2451 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002452 break;
2453 }
2454 default:
2455 LOG(FATAL) << "Unimplemented compare type " << in_type;
2456 }
2457}
2458
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002459void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002460 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002461
2462 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2463 locations->SetInAt(0, Location::RequiresFpuRegister());
2464 locations->SetInAt(1,
2465 IsFloatingPointZeroConstant(instruction->InputAt(1))
2466 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2467 : Location::RequiresFpuRegister());
2468 } else {
2469 // Integer cases.
2470 locations->SetInAt(0, Location::RequiresRegister());
2471 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2472 }
2473
David Brazdilb3e773e2016-01-26 11:28:37 +00002474 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002475 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002476 }
2477}
2478
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002479void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002480 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002481 return;
2482 }
2483
2484 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002485 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002486 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002487
Roland Levillain7f63c522015-07-13 15:54:55 +00002488 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002489 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002490 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002491 } else {
2492 // Integer cases.
2493 Register lhs = InputRegisterAt(instruction, 0);
2494 Operand rhs = InputOperandAt(instruction, 1);
2495 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002496 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002497 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002498}
2499
2500#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2501 M(Equal) \
2502 M(NotEqual) \
2503 M(LessThan) \
2504 M(LessThanOrEqual) \
2505 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002506 M(GreaterThanOrEqual) \
2507 M(Below) \
2508 M(BelowOrEqual) \
2509 M(Above) \
2510 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002511#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002512void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2513void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002514FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002515#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002516#undef FOR_EACH_CONDITION_INSTRUCTION
2517
Zheng Xuc6667102015-05-15 16:08:45 +08002518void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2519 DCHECK(instruction->IsDiv() || instruction->IsRem());
2520
2521 LocationSummary* locations = instruction->GetLocations();
2522 Location second = locations->InAt(1);
2523 DCHECK(second.IsConstant());
2524
2525 Register out = OutputRegister(instruction);
2526 Register dividend = InputRegisterAt(instruction, 0);
2527 int64_t imm = Int64FromConstant(second.GetConstant());
2528 DCHECK(imm == 1 || imm == -1);
2529
2530 if (instruction->IsRem()) {
2531 __ Mov(out, 0);
2532 } else {
2533 if (imm == 1) {
2534 __ Mov(out, dividend);
2535 } else {
2536 __ Neg(out, dividend);
2537 }
2538 }
2539}
2540
2541void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2542 DCHECK(instruction->IsDiv() || instruction->IsRem());
2543
2544 LocationSummary* locations = instruction->GetLocations();
2545 Location second = locations->InAt(1);
2546 DCHECK(second.IsConstant());
2547
2548 Register out = OutputRegister(instruction);
2549 Register dividend = InputRegisterAt(instruction, 0);
2550 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002551 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002552 int ctz_imm = CTZ(abs_imm);
2553
2554 UseScratchRegisterScope temps(GetVIXLAssembler());
2555 Register temp = temps.AcquireSameSizeAs(out);
2556
2557 if (instruction->IsDiv()) {
2558 __ Add(temp, dividend, abs_imm - 1);
2559 __ Cmp(dividend, 0);
2560 __ Csel(out, temp, dividend, lt);
2561 if (imm > 0) {
2562 __ Asr(out, out, ctz_imm);
2563 } else {
2564 __ Neg(out, Operand(out, ASR, ctz_imm));
2565 }
2566 } else {
2567 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2568 __ Asr(temp, dividend, bits - 1);
2569 __ Lsr(temp, temp, bits - ctz_imm);
2570 __ Add(out, dividend, temp);
2571 __ And(out, out, abs_imm - 1);
2572 __ Sub(out, out, temp);
2573 }
2574}
2575
2576void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2577 DCHECK(instruction->IsDiv() || instruction->IsRem());
2578
2579 LocationSummary* locations = instruction->GetLocations();
2580 Location second = locations->InAt(1);
2581 DCHECK(second.IsConstant());
2582
2583 Register out = OutputRegister(instruction);
2584 Register dividend = InputRegisterAt(instruction, 0);
2585 int64_t imm = Int64FromConstant(second.GetConstant());
2586
2587 Primitive::Type type = instruction->GetResultType();
2588 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2589
2590 int64_t magic;
2591 int shift;
2592 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2593
2594 UseScratchRegisterScope temps(GetVIXLAssembler());
2595 Register temp = temps.AcquireSameSizeAs(out);
2596
2597 // temp = get_high(dividend * magic)
2598 __ Mov(temp, magic);
2599 if (type == Primitive::kPrimLong) {
2600 __ Smulh(temp, dividend, temp);
2601 } else {
2602 __ Smull(temp.X(), dividend, temp);
2603 __ Lsr(temp.X(), temp.X(), 32);
2604 }
2605
2606 if (imm > 0 && magic < 0) {
2607 __ Add(temp, temp, dividend);
2608 } else if (imm < 0 && magic > 0) {
2609 __ Sub(temp, temp, dividend);
2610 }
2611
2612 if (shift != 0) {
2613 __ Asr(temp, temp, shift);
2614 }
2615
2616 if (instruction->IsDiv()) {
2617 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2618 } else {
2619 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2620 // TODO: Strength reduction for msub.
2621 Register temp_imm = temps.AcquireSameSizeAs(out);
2622 __ Mov(temp_imm, imm);
2623 __ Msub(out, temp, temp_imm, dividend);
2624 }
2625}
2626
2627void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2628 DCHECK(instruction->IsDiv() || instruction->IsRem());
2629 Primitive::Type type = instruction->GetResultType();
2630 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2631
2632 LocationSummary* locations = instruction->GetLocations();
2633 Register out = OutputRegister(instruction);
2634 Location second = locations->InAt(1);
2635
2636 if (second.IsConstant()) {
2637 int64_t imm = Int64FromConstant(second.GetConstant());
2638
2639 if (imm == 0) {
2640 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2641 } else if (imm == 1 || imm == -1) {
2642 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002643 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002644 DivRemByPowerOfTwo(instruction);
2645 } else {
2646 DCHECK(imm <= -2 || imm >= 2);
2647 GenerateDivRemWithAnyConstant(instruction);
2648 }
2649 } else {
2650 Register dividend = InputRegisterAt(instruction, 0);
2651 Register divisor = InputRegisterAt(instruction, 1);
2652 if (instruction->IsDiv()) {
2653 __ Sdiv(out, dividend, divisor);
2654 } else {
2655 UseScratchRegisterScope temps(GetVIXLAssembler());
2656 Register temp = temps.AcquireSameSizeAs(out);
2657 __ Sdiv(temp, dividend, divisor);
2658 __ Msub(out, temp, divisor, dividend);
2659 }
2660 }
2661}
2662
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002663void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2664 LocationSummary* locations =
2665 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2666 switch (div->GetResultType()) {
2667 case Primitive::kPrimInt:
2668 case Primitive::kPrimLong:
2669 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002670 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002671 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2672 break;
2673
2674 case Primitive::kPrimFloat:
2675 case Primitive::kPrimDouble:
2676 locations->SetInAt(0, Location::RequiresFpuRegister());
2677 locations->SetInAt(1, Location::RequiresFpuRegister());
2678 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2679 break;
2680
2681 default:
2682 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2683 }
2684}
2685
2686void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2687 Primitive::Type type = div->GetResultType();
2688 switch (type) {
2689 case Primitive::kPrimInt:
2690 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002691 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002692 break;
2693
2694 case Primitive::kPrimFloat:
2695 case Primitive::kPrimDouble:
2696 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2697 break;
2698
2699 default:
2700 LOG(FATAL) << "Unexpected div type " << type;
2701 }
2702}
2703
Alexandre Rames67555f72014-11-18 10:55:16 +00002704void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002705 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2706 ? LocationSummary::kCallOnSlowPath
2707 : LocationSummary::kNoCall;
2708 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002709 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2710 if (instruction->HasUses()) {
2711 locations->SetOut(Location::SameAsFirstInput());
2712 }
2713}
2714
2715void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2716 SlowPathCodeARM64* slow_path =
2717 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2718 codegen_->AddSlowPath(slow_path);
2719 Location value = instruction->GetLocations()->InAt(0);
2720
Alexandre Rames3e69f162014-12-10 10:36:50 +00002721 Primitive::Type type = instruction->GetType();
2722
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002723 if (!Primitive::IsIntegralType(type)) {
2724 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002725 return;
2726 }
2727
Alexandre Rames67555f72014-11-18 10:55:16 +00002728 if (value.IsConstant()) {
2729 int64_t divisor = Int64ConstantFrom(value);
2730 if (divisor == 0) {
2731 __ B(slow_path->GetEntryLabel());
2732 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002733 // A division by a non-null constant is valid. We don't need to perform
2734 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002735 }
2736 } else {
2737 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2738 }
2739}
2740
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002741void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2742 LocationSummary* locations =
2743 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2744 locations->SetOut(Location::ConstantLocation(constant));
2745}
2746
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002747void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2748 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002749 // Will be generated at use site.
2750}
2751
Alexandre Rames5319def2014-10-23 10:03:10 +01002752void LocationsBuilderARM64::VisitExit(HExit* exit) {
2753 exit->SetLocations(nullptr);
2754}
2755
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002756void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002757}
2758
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002759void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2760 LocationSummary* locations =
2761 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2762 locations->SetOut(Location::ConstantLocation(constant));
2763}
2764
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002765void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002766 // Will be generated at use site.
2767}
2768
David Brazdilfc6a86a2015-06-26 10:33:45 +00002769void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002770 DCHECK(!successor->IsExitBlock());
2771 HBasicBlock* block = got->GetBlock();
2772 HInstruction* previous = got->GetPrevious();
2773 HLoopInformation* info = block->GetLoopInformation();
2774
David Brazdil46e2a392015-03-16 17:31:52 +00002775 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002776 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2777 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2778 return;
2779 }
2780 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2781 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2782 }
2783 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002784 __ B(codegen_->GetLabelOf(successor));
2785 }
2786}
2787
David Brazdilfc6a86a2015-06-26 10:33:45 +00002788void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2789 got->SetLocations(nullptr);
2790}
2791
2792void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2793 HandleGoto(got, got->GetSuccessor());
2794}
2795
2796void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2797 try_boundary->SetLocations(nullptr);
2798}
2799
2800void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2801 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2802 if (!successor->IsExitBlock()) {
2803 HandleGoto(try_boundary, successor);
2804 }
2805}
2806
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002807void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002808 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002809 vixl::Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002810 vixl::Label* false_target) {
2811 // FP branching requires both targets to be explicit. If either of the targets
2812 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2813 vixl::Label fallthrough_target;
2814 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002815
David Brazdil0debae72015-11-12 18:37:00 +00002816 if (true_target == nullptr && false_target == nullptr) {
2817 // Nothing to do. The code always falls through.
2818 return;
2819 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002820 // Constant condition, statically compared against "true" (integer value 1).
2821 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002822 if (true_target != nullptr) {
2823 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002824 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002825 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002826 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002827 if (false_target != nullptr) {
2828 __ B(false_target);
2829 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002830 }
David Brazdil0debae72015-11-12 18:37:00 +00002831 return;
2832 }
2833
2834 // The following code generates these patterns:
2835 // (1) true_target == nullptr && false_target != nullptr
2836 // - opposite condition true => branch to false_target
2837 // (2) true_target != nullptr && false_target == nullptr
2838 // - condition true => branch to true_target
2839 // (3) true_target != nullptr && false_target != nullptr
2840 // - condition true => branch to true_target
2841 // - branch to false_target
2842 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002843 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002844 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002845 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002846 if (true_target == nullptr) {
2847 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2848 } else {
2849 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2850 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002851 } else {
2852 // The condition instruction has not been materialized, use its inputs as
2853 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002854 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002855
David Brazdil0debae72015-11-12 18:37:00 +00002856 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002857 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002858 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002859 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002860 IfCondition opposite_condition = condition->GetOppositeCondition();
2861 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002862 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002863 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002864 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002865 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002866 // Integer cases.
2867 Register lhs = InputRegisterAt(condition, 0);
2868 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002869
2870 Condition arm64_cond;
2871 vixl::Label* non_fallthrough_target;
2872 if (true_target == nullptr) {
2873 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2874 non_fallthrough_target = false_target;
2875 } else {
2876 arm64_cond = ARM64Condition(condition->GetCondition());
2877 non_fallthrough_target = true_target;
2878 }
2879
Aart Bik086d27e2016-01-20 17:02:00 -08002880 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
2881 rhs.IsImmediate() && (rhs.immediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002882 switch (arm64_cond) {
2883 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002884 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002885 break;
2886 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002887 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002888 break;
2889 case lt:
2890 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002891 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002892 break;
2893 case ge:
2894 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002895 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002896 break;
2897 default:
2898 // Without the `static_cast` the compiler throws an error for
2899 // `-Werror=sign-promo`.
2900 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2901 }
2902 } else {
2903 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002904 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002905 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002906 }
2907 }
David Brazdil0debae72015-11-12 18:37:00 +00002908
2909 // If neither branch falls through (case 3), the conditional branch to `true_target`
2910 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2911 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002912 __ B(false_target);
2913 }
David Brazdil0debae72015-11-12 18:37:00 +00002914
2915 if (fallthrough_target.IsLinked()) {
2916 __ Bind(&fallthrough_target);
2917 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002918}
2919
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002920void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2921 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002922 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002923 locations->SetInAt(0, Location::RequiresRegister());
2924 }
2925}
2926
2927void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002928 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2929 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2930 vixl::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2931 nullptr : codegen_->GetLabelOf(true_successor);
2932 vixl::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2933 nullptr : codegen_->GetLabelOf(false_successor);
2934 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002935}
2936
2937void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2938 LocationSummary* locations = new (GetGraph()->GetArena())
2939 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002940 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002941 locations->SetInAt(0, Location::RequiresRegister());
2942 }
2943}
2944
2945void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002946 SlowPathCodeARM64* slow_path =
2947 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002948 GenerateTestAndBranch(deoptimize,
2949 /* condition_input_index */ 0,
2950 slow_path->GetEntryLabel(),
2951 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002952}
2953
David Brazdilc0b601b2016-02-08 14:20:45 +00002954static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
2955 return condition->IsCondition() &&
2956 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
2957}
2958
Alexandre Rames880f1192016-06-13 16:04:50 +01002959static inline Condition GetConditionForSelect(HCondition* condition) {
2960 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00002961 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
2962 : ARM64Condition(cond);
2963}
2964
David Brazdil74eb1b22015-12-14 11:44:01 +00002965void LocationsBuilderARM64::VisitSelect(HSelect* select) {
2966 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01002967 if (Primitive::IsFloatingPointType(select->GetType())) {
2968 locations->SetInAt(0, Location::RequiresFpuRegister());
2969 locations->SetInAt(1, Location::RequiresFpuRegister());
2970 locations->SetOut(Location::RequiresFpuRegister());
2971 } else {
2972 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
2973 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
2974 bool is_true_value_constant = cst_true_value != nullptr;
2975 bool is_false_value_constant = cst_false_value != nullptr;
2976 // Ask VIXL whether we should synthesize constants in registers.
2977 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
2978 Operand true_op = is_true_value_constant ?
2979 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
2980 Operand false_op = is_false_value_constant ?
2981 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
2982 bool true_value_in_register = false;
2983 bool false_value_in_register = false;
2984 MacroAssembler::GetCselSynthesisInformation(
2985 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
2986 true_value_in_register |= !is_true_value_constant;
2987 false_value_in_register |= !is_false_value_constant;
2988
2989 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
2990 : Location::ConstantLocation(cst_true_value));
2991 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
2992 : Location::ConstantLocation(cst_false_value));
2993 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00002994 }
Alexandre Rames880f1192016-06-13 16:04:50 +01002995
David Brazdil74eb1b22015-12-14 11:44:01 +00002996 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2997 locations->SetInAt(2, Location::RequiresRegister());
2998 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002999}
3000
3001void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003002 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003003 Condition csel_cond;
3004
3005 if (IsBooleanValueOrMaterializedCondition(cond)) {
3006 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003007 // Use the condition flags set by the previous instruction.
3008 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003009 } else {
3010 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003011 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003012 }
3013 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003014 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003015 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003016 } else {
3017 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003018 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003019 }
3020
Alexandre Rames880f1192016-06-13 16:04:50 +01003021 if (Primitive::IsFloatingPointType(select->GetType())) {
3022 __ Fcsel(OutputFPRegister(select),
3023 InputFPRegisterAt(select, 1),
3024 InputFPRegisterAt(select, 0),
3025 csel_cond);
3026 } else {
3027 __ Csel(OutputRegister(select),
3028 InputOperandAt(select, 1),
3029 InputOperandAt(select, 0),
3030 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003031 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003032}
3033
David Srbecky0cf44932015-12-09 14:09:59 +00003034void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3035 new (GetGraph()->GetArena()) LocationSummary(info);
3036}
3037
David Srbeckyd28f4a02016-03-14 17:14:24 +00003038void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3039 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003040}
3041
3042void CodeGeneratorARM64::GenerateNop() {
3043 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003044}
3045
Alexandre Rames5319def2014-10-23 10:03:10 +01003046void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003047 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003048}
3049
3050void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003051 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003052}
3053
3054void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003055 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003056}
3057
3058void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003059 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003060}
3061
Roland Levillain44015862016-01-22 11:47:17 +00003062static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3063 return kEmitCompilerReadBarrier &&
3064 (kUseBakerReadBarrier ||
3065 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3066 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3067 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3068}
3069
Alexandre Rames67555f72014-11-18 10:55:16 +00003070void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003071 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003072 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3073 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003074 case TypeCheckKind::kExactCheck:
3075 case TypeCheckKind::kAbstractClassCheck:
3076 case TypeCheckKind::kClassHierarchyCheck:
3077 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003078 call_kind =
3079 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003080 break;
3081 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003082 case TypeCheckKind::kUnresolvedCheck:
3083 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003084 call_kind = LocationSummary::kCallOnSlowPath;
3085 break;
3086 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003087
Alexandre Rames67555f72014-11-18 10:55:16 +00003088 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003089 locations->SetInAt(0, Location::RequiresRegister());
3090 locations->SetInAt(1, Location::RequiresRegister());
3091 // The "out" register is used as a temporary, so it overlaps with the inputs.
3092 // Note that TypeCheckSlowPathARM64 uses this register too.
3093 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3094 // When read barriers are enabled, we need a temporary register for
3095 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003096 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003097 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003098 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003099}
3100
3101void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003102 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003103 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003104 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003105 Register obj = InputRegisterAt(instruction, 0);
3106 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003107 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003108 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003109 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3110 locations->GetTemp(0) :
3111 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003112 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3113 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3114 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3115 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003116
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003117 vixl::Label done, zero;
3118 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003119
3120 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003121 // Avoid null check if we know `obj` is not null.
3122 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003123 __ Cbz(obj, &zero);
3124 }
3125
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003126 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003127 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003128
Roland Levillain44015862016-01-22 11:47:17 +00003129 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003130 case TypeCheckKind::kExactCheck: {
3131 __ Cmp(out, cls);
3132 __ Cset(out, eq);
3133 if (zero.IsLinked()) {
3134 __ B(&done);
3135 }
3136 break;
3137 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003138
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003139 case TypeCheckKind::kAbstractClassCheck: {
3140 // If the class is abstract, we eagerly fetch the super class of the
3141 // object to avoid doing a comparison we know will fail.
3142 vixl::Label loop, success;
3143 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003144 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003145 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003146 // If `out` is null, we use it for the result, and jump to `done`.
3147 __ Cbz(out, &done);
3148 __ Cmp(out, cls);
3149 __ B(ne, &loop);
3150 __ Mov(out, 1);
3151 if (zero.IsLinked()) {
3152 __ B(&done);
3153 }
3154 break;
3155 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003156
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003157 case TypeCheckKind::kClassHierarchyCheck: {
3158 // Walk over the class hierarchy to find a match.
3159 vixl::Label loop, success;
3160 __ Bind(&loop);
3161 __ Cmp(out, cls);
3162 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003163 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003164 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003165 __ Cbnz(out, &loop);
3166 // If `out` is null, we use it for the result, and jump to `done`.
3167 __ B(&done);
3168 __ Bind(&success);
3169 __ Mov(out, 1);
3170 if (zero.IsLinked()) {
3171 __ B(&done);
3172 }
3173 break;
3174 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003175
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003176 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003177 // Do an exact check.
3178 vixl::Label exact_check;
3179 __ Cmp(out, cls);
3180 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003181 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003182 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003183 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003184 // If `out` is null, we use it for the result, and jump to `done`.
3185 __ Cbz(out, &done);
3186 __ Ldrh(out, HeapOperand(out, primitive_offset));
3187 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3188 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003189 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003190 __ Mov(out, 1);
3191 __ B(&done);
3192 break;
3193 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003194
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003195 case TypeCheckKind::kArrayCheck: {
3196 __ Cmp(out, cls);
3197 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003198 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3199 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003200 codegen_->AddSlowPath(slow_path);
3201 __ B(ne, slow_path->GetEntryLabel());
3202 __ Mov(out, 1);
3203 if (zero.IsLinked()) {
3204 __ B(&done);
3205 }
3206 break;
3207 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003208
Calin Juravle98893e12015-10-02 21:05:03 +01003209 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003210 case TypeCheckKind::kInterfaceCheck: {
3211 // Note that we indeed only call on slow path, but we always go
3212 // into the slow path for the unresolved and interface check
3213 // cases.
3214 //
3215 // We cannot directly call the InstanceofNonTrivial runtime
3216 // entry point without resorting to a type checking slow path
3217 // here (i.e. by calling InvokeRuntime directly), as it would
3218 // require to assign fixed registers for the inputs of this
3219 // HInstanceOf instruction (following the runtime calling
3220 // convention), which might be cluttered by the potential first
3221 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003222 //
3223 // TODO: Introduce a new runtime entry point taking the object
3224 // to test (instead of its class) as argument, and let it deal
3225 // with the read barrier issues. This will let us refactor this
3226 // case of the `switch` code as it was previously (with a direct
3227 // call to the runtime not using a type checking slow path).
3228 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003229 DCHECK(locations->OnlyCallsOnSlowPath());
3230 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3231 /* is_fatal */ false);
3232 codegen_->AddSlowPath(slow_path);
3233 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003234 if (zero.IsLinked()) {
3235 __ B(&done);
3236 }
3237 break;
3238 }
3239 }
3240
3241 if (zero.IsLinked()) {
3242 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003243 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003244 }
3245
3246 if (done.IsLinked()) {
3247 __ Bind(&done);
3248 }
3249
3250 if (slow_path != nullptr) {
3251 __ Bind(slow_path->GetExitLabel());
3252 }
3253}
3254
3255void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3256 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3257 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3258
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003259 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3260 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003261 case TypeCheckKind::kExactCheck:
3262 case TypeCheckKind::kAbstractClassCheck:
3263 case TypeCheckKind::kClassHierarchyCheck:
3264 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003265 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3266 LocationSummary::kCallOnSlowPath :
3267 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003268 break;
3269 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003270 case TypeCheckKind::kUnresolvedCheck:
3271 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003272 call_kind = LocationSummary::kCallOnSlowPath;
3273 break;
3274 }
3275
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003276 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3277 locations->SetInAt(0, Location::RequiresRegister());
3278 locations->SetInAt(1, Location::RequiresRegister());
3279 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3280 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003281 // When read barriers are enabled, we need an additional temporary
3282 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003283 if (TypeCheckNeedsATemporary(type_check_kind)) {
3284 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003285 }
3286}
3287
3288void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003289 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003290 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003291 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003292 Register obj = InputRegisterAt(instruction, 0);
3293 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003294 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003295 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3296 locations->GetTemp(1) :
3297 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003298 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003299 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3300 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3301 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3302 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003303
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003304 bool is_type_check_slow_path_fatal =
3305 (type_check_kind == TypeCheckKind::kExactCheck ||
3306 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3307 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3308 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3309 !instruction->CanThrowIntoCatchBlock();
3310 SlowPathCodeARM64* type_check_slow_path =
3311 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3312 is_type_check_slow_path_fatal);
3313 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003314
3315 vixl::Label done;
3316 // Avoid null check if we know obj is not null.
3317 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003318 __ Cbz(obj, &done);
3319 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003320
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003321 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003322 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003323
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003324 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003325 case TypeCheckKind::kExactCheck:
3326 case TypeCheckKind::kArrayCheck: {
3327 __ Cmp(temp, cls);
3328 // Jump to slow path for throwing the exception or doing a
3329 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003330 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003331 break;
3332 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003333
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003334 case TypeCheckKind::kAbstractClassCheck: {
3335 // If the class is abstract, we eagerly fetch the super class of the
3336 // object to avoid doing a comparison we know will fail.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003337 vixl::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003338 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003339 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003340 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003341
3342 // If the class reference currently in `temp` is not null, jump
3343 // to the `compare_classes` label to compare it with the checked
3344 // class.
3345 __ Cbnz(temp, &compare_classes);
3346 // Otherwise, jump to the slow path to throw the exception.
3347 //
3348 // But before, move back the object's class into `temp` before
3349 // going into the slow path, as it has been overwritten in the
3350 // meantime.
3351 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003352 GenerateReferenceLoadTwoRegisters(
3353 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003354 __ B(type_check_slow_path->GetEntryLabel());
3355
3356 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003357 __ Cmp(temp, cls);
3358 __ B(ne, &loop);
3359 break;
3360 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003361
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003362 case TypeCheckKind::kClassHierarchyCheck: {
3363 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003364 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003365 __ Bind(&loop);
3366 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003367 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003368
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003369 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003370 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003371
3372 // If the class reference currently in `temp` is not null, jump
3373 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003374 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003375 // Otherwise, jump to the slow path to throw the exception.
3376 //
3377 // But before, move back the object's class into `temp` before
3378 // going into the slow path, as it has been overwritten in the
3379 // meantime.
3380 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003381 GenerateReferenceLoadTwoRegisters(
3382 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003383 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003384 break;
3385 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003386
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003387 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003388 // Do an exact check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003389 vixl::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003390 __ Cmp(temp, cls);
3391 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003392
3393 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003394 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003395 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003396
3397 // If the component type is not null (i.e. the object is indeed
3398 // an array), jump to label `check_non_primitive_component_type`
3399 // to further check that this component type is not a primitive
3400 // type.
3401 __ Cbnz(temp, &check_non_primitive_component_type);
3402 // Otherwise, jump to the slow path to throw the exception.
3403 //
3404 // But before, move back the object's class into `temp` before
3405 // going into the slow path, as it has been overwritten in the
3406 // meantime.
3407 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003408 GenerateReferenceLoadTwoRegisters(
3409 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003410 __ B(type_check_slow_path->GetEntryLabel());
3411
3412 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003413 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3414 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003415 __ Cbz(temp, &done);
3416 // Same comment as above regarding `temp` and the slow path.
3417 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003418 GenerateReferenceLoadTwoRegisters(
3419 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003420 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003421 break;
3422 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003423
Calin Juravle98893e12015-10-02 21:05:03 +01003424 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003425 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003426 // We always go into the type check slow path for the unresolved
3427 // and interface check cases.
3428 //
3429 // We cannot directly call the CheckCast runtime entry point
3430 // without resorting to a type checking slow path here (i.e. by
3431 // calling InvokeRuntime directly), as it would require to
3432 // assign fixed registers for the inputs of this HInstanceOf
3433 // instruction (following the runtime calling convention), which
3434 // might be cluttered by the potential first read barrier
3435 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003436 //
3437 // TODO: Introduce a new runtime entry point taking the object
3438 // to test (instead of its class) as argument, and let it deal
3439 // with the read barrier issues. This will let us refactor this
3440 // case of the `switch` code as it was previously (with a direct
3441 // call to the runtime not using a type checking slow path).
3442 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003443 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003444 break;
3445 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003446 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003447
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003448 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003449}
3450
Alexandre Rames5319def2014-10-23 10:03:10 +01003451void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3452 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3453 locations->SetOut(Location::ConstantLocation(constant));
3454}
3455
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003456void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003457 // Will be generated at use site.
3458}
3459
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003460void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3461 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3462 locations->SetOut(Location::ConstantLocation(constant));
3463}
3464
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003465void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003466 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003467}
3468
Calin Juravle175dc732015-08-25 15:42:32 +01003469void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3470 // The trampoline uses the same calling convention as dex calling conventions,
3471 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3472 // the method_idx.
3473 HandleInvoke(invoke);
3474}
3475
3476void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3477 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3478}
3479
Alexandre Rames5319def2014-10-23 10:03:10 +01003480void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003481 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003482 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003483}
3484
Alexandre Rames67555f72014-11-18 10:55:16 +00003485void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3486 HandleInvoke(invoke);
3487}
3488
3489void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3490 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003491 LocationSummary* locations = invoke->GetLocations();
3492 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003493 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003494 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003495 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003496
3497 // The register ip1 is required to be used for the hidden argument in
3498 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003499 MacroAssembler* masm = GetVIXLAssembler();
3500 UseScratchRegisterScope scratch_scope(masm);
3501 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003502 scratch_scope.Exclude(ip1);
3503 __ Mov(ip1, invoke->GetDexMethodIndex());
3504
Alexandre Rames67555f72014-11-18 10:55:16 +00003505 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003506 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003507 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003508 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003509 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003510 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003511 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003512 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003513 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003514 // Instead of simply (possibly) unpoisoning `temp` here, we should
3515 // emit a read barrier for the previous class reference load.
3516 // However this is not required in practice, as this is an
3517 // intermediate/temporary reference and because the current
3518 // concurrent copying collector keeps the from-space memory
3519 // intact/accessible until the end of the marking phase (the
3520 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003521 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nelli Kimbadee982016-05-13 13:08:53 +03003522 __ Ldr(temp,
3523 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3524 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity50706432016-06-14 11:31:04 -07003525 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003526 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003527 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003528 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003529 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003530 // lr();
3531 __ Blr(lr);
3532 DCHECK(!codegen_->IsLeafMethod());
3533 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3534}
3535
3536void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003537 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3538 if (intrinsic.TryDispatch(invoke)) {
3539 return;
3540 }
3541
Alexandre Rames67555f72014-11-18 10:55:16 +00003542 HandleInvoke(invoke);
3543}
3544
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003545void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003546 // Explicit clinit checks triggered by static invokes must have been pruned by
3547 // art::PrepareForRegisterAllocation.
3548 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003549
Andreas Gampe878d58c2015-01-15 23:24:00 -08003550 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3551 if (intrinsic.TryDispatch(invoke)) {
3552 return;
3553 }
3554
Alexandre Rames67555f72014-11-18 10:55:16 +00003555 HandleInvoke(invoke);
3556}
3557
Andreas Gampe878d58c2015-01-15 23:24:00 -08003558static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3559 if (invoke->GetLocations()->Intrinsified()) {
3560 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3561 intrinsic.Dispatch(invoke);
3562 return true;
3563 }
3564 return false;
3565}
3566
Vladimir Markodc151b22015-10-15 18:02:30 +01003567HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3568 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3569 MethodReference target_method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003570 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003571 return desired_dispatch_info;
3572}
3573
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003574void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003575 // For better instruction scheduling we load the direct code pointer before the method pointer.
3576 bool direct_code_loaded = false;
3577 switch (invoke->GetCodePtrLocation()) {
3578 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3579 // LR = code address from literal pool with link-time patch.
3580 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3581 direct_code_loaded = true;
3582 break;
3583 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3584 // LR = invoke->GetDirectCodePtr();
3585 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3586 direct_code_loaded = true;
3587 break;
3588 default:
3589 break;
3590 }
3591
Andreas Gampe878d58c2015-01-15 23:24:00 -08003592 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003593 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3594 switch (invoke->GetMethodLoadKind()) {
3595 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3596 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003597 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003598 break;
3599 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003600 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003601 break;
3602 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3603 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003604 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003605 break;
3606 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3607 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003608 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003609 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3610 break;
3611 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3612 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003613 const DexFile& dex_file = *invoke->GetTargetMethod().dex_file;
3614 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
3615 vixl::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003616 {
3617 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003618 __ Bind(adrp_label);
3619 __ adrp(XRegisterFrom(temp), /* offset placeholder */ 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003620 }
Vladimir Marko58155012015-08-19 12:49:41 +00003621 // Add LDR with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003622 vixl::Label* ldr_label =
3623 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Alexandre Rames6dc01742015-11-12 14:44:19 +00003624 {
3625 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003626 __ Bind(ldr_label);
3627 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), /* offset placeholder */ 0));
Alexandre Rames6dc01742015-11-12 14:44:19 +00003628 }
Vladimir Marko58155012015-08-19 12:49:41 +00003629 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003630 }
Vladimir Marko58155012015-08-19 12:49:41 +00003631 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003632 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003633 Register reg = XRegisterFrom(temp);
3634 Register method_reg;
3635 if (current_method.IsRegister()) {
3636 method_reg = XRegisterFrom(current_method);
3637 } else {
3638 DCHECK(invoke->GetLocations()->Intrinsified());
3639 DCHECK(!current_method.IsValid());
3640 method_reg = reg;
3641 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3642 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003643
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003644 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003645 __ Ldr(reg.X(),
3646 MemOperand(method_reg.X(),
3647 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003648 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003649 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3650 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003651 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3652 break;
3653 }
3654 }
3655
3656 switch (invoke->GetCodePtrLocation()) {
3657 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3658 __ Bl(&frame_entry_label_);
3659 break;
3660 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3661 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3662 vixl::Label* label = &relative_call_patches_.back().label;
Alexandre Rames6dc01742015-11-12 14:44:19 +00003663 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3664 __ Bind(label);
3665 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003666 break;
3667 }
3668 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3669 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3670 // LR prepared above for better instruction scheduling.
3671 DCHECK(direct_code_loaded);
3672 // lr()
3673 __ Blr(lr);
3674 break;
3675 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3676 // LR = callee_method->entry_point_from_quick_compiled_code_;
3677 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003678 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003679 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3680 // lr()
3681 __ Blr(lr);
3682 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003683 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003684
Andreas Gampe878d58c2015-01-15 23:24:00 -08003685 DCHECK(!IsLeafMethod());
3686}
3687
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003688void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003689 // Use the calling convention instead of the location of the receiver, as
3690 // intrinsics may have put the receiver in a different register. In the intrinsics
3691 // slow path, the arguments have been moved to the right place, so here we are
3692 // guaranteed that the receiver is the first register of the calling convention.
3693 InvokeDexCallingConvention calling_convention;
3694 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003695 Register temp = XRegisterFrom(temp_in);
3696 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3697 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3698 Offset class_offset = mirror::Object::ClassOffset();
3699 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3700
3701 BlockPoolsScope block_pools(GetVIXLAssembler());
3702
3703 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003704 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003705 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003706 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003707 // Instead of simply (possibly) unpoisoning `temp` here, we should
3708 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003709 // intermediate/temporary reference and because the current
3710 // concurrent copying collector keeps the from-space memory
3711 // intact/accessible until the end of the marking phase (the
3712 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003713 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3714 // temp = temp->GetMethodAt(method_offset);
3715 __ Ldr(temp, MemOperand(temp, method_offset));
3716 // lr = temp->GetEntryPoint();
3717 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3718 // lr();
3719 __ Blr(lr);
3720}
3721
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003722vixl::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(const DexFile& dex_file,
3723 uint32_t string_index,
3724 vixl::Label* adrp_label) {
3725 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3726}
3727
3728vixl::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
3729 uint32_t element_offset,
3730 vixl::Label* adrp_label) {
3731 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3732}
3733
3734vixl::Label* CodeGeneratorARM64::NewPcRelativePatch(const DexFile& dex_file,
3735 uint32_t offset_or_index,
3736 vixl::Label* adrp_label,
3737 ArenaDeque<PcRelativePatchInfo>* patches) {
3738 // Add a patch entry and return the label.
3739 patches->emplace_back(dex_file, offset_or_index);
3740 PcRelativePatchInfo* info = &patches->back();
3741 vixl::Label* label = &info->label;
3742 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3743 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3744 return label;
3745}
3746
3747vixl::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
3748 const DexFile& dex_file, uint32_t string_index) {
3749 return boot_image_string_patches_.GetOrCreate(
3750 StringReference(&dex_file, string_index),
3751 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3752}
3753
3754vixl::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(uint64_t address) {
3755 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3756 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3757 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3758}
3759
3760vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(uint64_t address) {
3761 return DeduplicateUint64Literal(address);
3762}
3763
Vladimir Marko58155012015-08-19 12:49:41 +00003764void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3765 DCHECK(linker_patches->empty());
3766 size_t size =
3767 method_patches_.size() +
3768 call_patches_.size() +
3769 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003770 pc_relative_dex_cache_patches_.size() +
3771 boot_image_string_patches_.size() +
3772 pc_relative_string_patches_.size() +
3773 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003774 linker_patches->reserve(size);
3775 for (const auto& entry : method_patches_) {
3776 const MethodReference& target_method = entry.first;
3777 vixl::Literal<uint64_t>* literal = entry.second;
3778 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
3779 target_method.dex_file,
3780 target_method.dex_method_index));
3781 }
3782 for (const auto& entry : call_patches_) {
3783 const MethodReference& target_method = entry.first;
3784 vixl::Literal<uint64_t>* literal = entry.second;
3785 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
3786 target_method.dex_file,
3787 target_method.dex_method_index));
3788 }
3789 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003790 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003791 info.target_method.dex_file,
3792 info.target_method.dex_method_index));
3793 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003794 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003795 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003796 &info.target_dex_file,
Alexandre Rames6dc01742015-11-12 14:44:19 +00003797 info.pc_insn_label->location(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003798 info.offset_or_index));
3799 }
3800 for (const auto& entry : boot_image_string_patches_) {
3801 const StringReference& target_string = entry.first;
3802 vixl::Literal<uint32_t>* literal = entry.second;
3803 linker_patches->push_back(LinkerPatch::StringPatch(literal->offset(),
3804 target_string.dex_file,
3805 target_string.string_index));
3806 }
3807 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
3808 linker_patches->push_back(LinkerPatch::RelativeStringPatch(info.label.location(),
3809 &info.target_dex_file,
3810 info.pc_insn_label->location(),
3811 info.offset_or_index));
3812 }
3813 for (const auto& entry : boot_image_address_patches_) {
3814 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
3815 vixl::Literal<uint32_t>* literal = entry.second;
3816 linker_patches->push_back(LinkerPatch::RecordPosition(literal->offset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003817 }
3818}
3819
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003820vixl::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
3821 Uint32ToLiteralMap* map) {
3822 return map->GetOrCreate(
3823 value,
3824 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3825}
3826
Vladimir Marko58155012015-08-19 12:49:41 +00003827vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003828 return uint64_literals_.GetOrCreate(
3829 value,
3830 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003831}
3832
3833vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
3834 MethodReference target_method,
3835 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003836 return map->GetOrCreate(
3837 target_method,
3838 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003839}
3840
3841vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
3842 MethodReference target_method) {
3843 return DeduplicateMethodLiteral(target_method, &method_patches_);
3844}
3845
3846vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
3847 MethodReference target_method) {
3848 return DeduplicateMethodLiteral(target_method, &call_patches_);
3849}
3850
3851
Andreas Gampe878d58c2015-01-15 23:24:00 -08003852void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003853 // Explicit clinit checks triggered by static invokes must have been pruned by
3854 // art::PrepareForRegisterAllocation.
3855 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003856
Andreas Gampe878d58c2015-01-15 23:24:00 -08003857 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3858 return;
3859 }
3860
Alexandre Ramesd921d642015-04-16 15:07:16 +01003861 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003862 LocationSummary* locations = invoke->GetLocations();
3863 codegen_->GenerateStaticOrDirectCall(
3864 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003865 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003866}
3867
3868void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003869 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3870 return;
3871 }
3872
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003873 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003874 DCHECK(!codegen_->IsLeafMethod());
3875 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3876}
3877
Alexandre Rames67555f72014-11-18 10:55:16 +00003878void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003879 InvokeRuntimeCallingConvention calling_convention;
3880 CodeGenerator::CreateLoadClassLocationSummary(
3881 cls,
3882 LocationFrom(calling_convention.GetRegisterAt(0)),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003883 LocationFrom(vixl::x0),
3884 /* code_generator_supports_read_barrier */ true);
Alexandre Rames67555f72014-11-18 10:55:16 +00003885}
3886
3887void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003888 if (cls->NeedsAccessCheck()) {
3889 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3890 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3891 cls,
3892 cls->GetDexPc(),
3893 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003894 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003895 return;
3896 }
3897
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003898 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01003899 Register out = OutputRegister(cls);
3900 Register current_method = InputRegisterAt(cls, 0);
3901 if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003902 DCHECK(!cls->CanCallRuntime());
3903 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain44015862016-01-22 11:47:17 +00003904 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3905 GenerateGcRootFieldLoad(
3906 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Alexandre Rames67555f72014-11-18 10:55:16 +00003907 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003908 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003909 // /* GcRoot<mirror::Class>[] */ out =
3910 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Vladimir Marko05792b92015-08-03 11:56:49 +01003911 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00003912 // /* GcRoot<mirror::Class> */ out = out[type_index]
3913 GenerateGcRootFieldLoad(
3914 cls, out_loc, out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003915
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003916 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3917 DCHECK(cls->CanCallRuntime());
3918 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3919 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3920 codegen_->AddSlowPath(slow_path);
3921 if (!cls->IsInDexCache()) {
3922 __ Cbz(out, slow_path->GetEntryLabel());
3923 }
3924 if (cls->MustGenerateClinitCheck()) {
3925 GenerateClassInitializationCheck(slow_path, out);
3926 } else {
3927 __ Bind(slow_path->GetExitLabel());
3928 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003929 }
3930 }
3931}
3932
David Brazdilcb1c0552015-08-04 16:22:25 +01003933static MemOperand GetExceptionTlsAddress() {
3934 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3935}
3936
Alexandre Rames67555f72014-11-18 10:55:16 +00003937void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3938 LocationSummary* locations =
3939 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3940 locations->SetOut(Location::RequiresRegister());
3941}
3942
3943void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003944 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3945}
3946
3947void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3948 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3949}
3950
3951void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3952 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003953}
3954
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003955HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
3956 HLoadString::LoadKind desired_string_load_kind) {
3957 if (kEmitCompilerReadBarrier) {
3958 switch (desired_string_load_kind) {
3959 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3960 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
3961 case HLoadString::LoadKind::kBootImageAddress:
3962 // TODO: Implement for read barrier.
3963 return HLoadString::LoadKind::kDexCacheViaMethod;
3964 default:
3965 break;
3966 }
3967 }
3968 switch (desired_string_load_kind) {
3969 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3970 DCHECK(!GetCompilerOptions().GetCompilePic());
3971 break;
3972 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
3973 DCHECK(GetCompilerOptions().GetCompilePic());
3974 break;
3975 case HLoadString::LoadKind::kBootImageAddress:
3976 break;
3977 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01003978 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003979 break;
3980 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01003981 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003982 break;
3983 case HLoadString::LoadKind::kDexCacheViaMethod:
3984 break;
3985 }
3986 return desired_string_load_kind;
3987}
3988
Alexandre Rames67555f72014-11-18 10:55:16 +00003989void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003990 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003991 ? LocationSummary::kCallOnSlowPath
3992 : LocationSummary::kNoCall;
3993 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003994 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
3995 locations->SetInAt(0, Location::RequiresRegister());
3996 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003997 locations->SetOut(Location::RequiresRegister());
3998}
3999
4000void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004001 Location out_loc = load->GetLocations()->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00004002 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004003
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004004 switch (load->GetLoadKind()) {
4005 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4006 DCHECK(!kEmitCompilerReadBarrier);
4007 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4008 load->GetStringIndex()));
4009 return; // No dex cache slow path.
4010 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4011 DCHECK(!kEmitCompilerReadBarrier);
4012 // Add ADRP with its PC-relative String patch.
4013 const DexFile& dex_file = load->GetDexFile();
4014 uint32_t string_index = load->GetStringIndex();
4015 vixl::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
4016 {
4017 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4018 __ Bind(adrp_label);
4019 __ adrp(out.X(), /* offset placeholder */ 0);
4020 }
4021 // Add ADD with its PC-relative String patch.
4022 vixl::Label* add_label =
4023 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4024 {
4025 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4026 __ Bind(add_label);
4027 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
4028 }
4029 return; // No dex cache slow path.
4030 }
4031 case HLoadString::LoadKind::kBootImageAddress: {
4032 DCHECK(!kEmitCompilerReadBarrier);
4033 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4034 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4035 return; // No dex cache slow path.
4036 }
4037 case HLoadString::LoadKind::kDexCacheAddress: {
4038 DCHECK_NE(load->GetAddress(), 0u);
4039 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4040 // that gives a 16KiB range. To try and reduce the number of literals if we load
4041 // multiple strings, simply split the dex cache address to a 16KiB aligned base
4042 // loaded from a literal and the remaining offset embedded in the load.
4043 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
4044 DCHECK_ALIGNED(load->GetAddress(), 4u);
4045 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4046 uint64_t base_address = load->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4047 uint32_t offset = load->GetAddress() & MaxInt<uint64_t>(offset_bits);
4048 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4049 GenerateGcRootFieldLoad(load, out_loc, out.X(), offset);
4050 break;
4051 }
4052 case HLoadString::LoadKind::kDexCachePcRelative: {
4053 // Add ADRP with its PC-relative DexCache access patch.
4054 const DexFile& dex_file = load->GetDexFile();
4055 uint32_t element_offset = load->GetDexCacheElementOffset();
4056 vixl::Label* adrp_label = codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
4057 {
4058 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4059 __ Bind(adrp_label);
4060 __ adrp(out.X(), /* offset placeholder */ 0);
4061 }
4062 // Add LDR with its PC-relative DexCache access patch.
4063 vixl::Label* ldr_label =
4064 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4065 GenerateGcRootFieldLoad(load, out_loc, out.X(), /* offset placeholder */ 0, ldr_label);
4066 break;
4067 }
4068 case HLoadString::LoadKind::kDexCacheViaMethod: {
4069 Register current_method = InputRegisterAt(load, 0);
4070 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4071 GenerateGcRootFieldLoad(
4072 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4073 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
4074 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
4075 // /* GcRoot<mirror::String> */ out = out[string_index]
4076 GenerateGcRootFieldLoad(
4077 load, out_loc, out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex()));
4078 break;
4079 }
4080 default:
4081 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
4082 UNREACHABLE();
4083 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004084
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004085 if (!load->IsInDexCache()) {
4086 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
4087 codegen_->AddSlowPath(slow_path);
4088 __ Cbz(out, slow_path->GetEntryLabel());
4089 __ Bind(slow_path->GetExitLabel());
4090 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004091}
4092
Alexandre Rames5319def2014-10-23 10:03:10 +01004093void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4094 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4095 locations->SetOut(Location::ConstantLocation(constant));
4096}
4097
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004098void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004099 // Will be generated at use site.
4100}
4101
Alexandre Rames67555f72014-11-18 10:55:16 +00004102void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4103 LocationSummary* locations =
4104 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4105 InvokeRuntimeCallingConvention calling_convention;
4106 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4107}
4108
4109void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4110 codegen_->InvokeRuntime(instruction->IsEnter()
4111 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4112 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004113 instruction->GetDexPc(),
4114 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004115 if (instruction->IsEnter()) {
4116 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4117 } else {
4118 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4119 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004120}
4121
Alexandre Rames42d641b2014-10-27 14:00:51 +00004122void LocationsBuilderARM64::VisitMul(HMul* mul) {
4123 LocationSummary* locations =
4124 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4125 switch (mul->GetResultType()) {
4126 case Primitive::kPrimInt:
4127 case Primitive::kPrimLong:
4128 locations->SetInAt(0, Location::RequiresRegister());
4129 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004130 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004131 break;
4132
4133 case Primitive::kPrimFloat:
4134 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004135 locations->SetInAt(0, Location::RequiresFpuRegister());
4136 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004137 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004138 break;
4139
4140 default:
4141 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4142 }
4143}
4144
4145void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4146 switch (mul->GetResultType()) {
4147 case Primitive::kPrimInt:
4148 case Primitive::kPrimLong:
4149 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4150 break;
4151
4152 case Primitive::kPrimFloat:
4153 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004154 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004155 break;
4156
4157 default:
4158 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4159 }
4160}
4161
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004162void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4163 LocationSummary* locations =
4164 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4165 switch (neg->GetResultType()) {
4166 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004167 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004168 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004169 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004170 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004171
4172 case Primitive::kPrimFloat:
4173 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004174 locations->SetInAt(0, Location::RequiresFpuRegister());
4175 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004176 break;
4177
4178 default:
4179 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4180 }
4181}
4182
4183void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4184 switch (neg->GetResultType()) {
4185 case Primitive::kPrimInt:
4186 case Primitive::kPrimLong:
4187 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4188 break;
4189
4190 case Primitive::kPrimFloat:
4191 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004192 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004193 break;
4194
4195 default:
4196 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4197 }
4198}
4199
4200void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4201 LocationSummary* locations =
4202 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4203 InvokeRuntimeCallingConvention calling_convention;
4204 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004205 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004206 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004207 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004208}
4209
4210void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4211 LocationSummary* locations = instruction->GetLocations();
4212 InvokeRuntimeCallingConvention calling_convention;
4213 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4214 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004215 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004216 // Note: if heap poisoning is enabled, the entry point takes cares
4217 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004218 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4219 instruction,
4220 instruction->GetDexPc(),
4221 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004222 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004223}
4224
Alexandre Rames5319def2014-10-23 10:03:10 +01004225void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4226 LocationSummary* locations =
4227 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4228 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004229 if (instruction->IsStringAlloc()) {
4230 locations->AddTemp(LocationFrom(kArtMethodRegister));
4231 } else {
4232 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4233 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4234 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004235 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4236}
4237
4238void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004239 // Note: if heap poisoning is enabled, the entry point takes cares
4240 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004241 if (instruction->IsStringAlloc()) {
4242 // String is allocated through StringFactory. Call NewEmptyString entry point.
4243 Location temp = instruction->GetLocations()->GetTemp(0);
4244 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
4245 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4246 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4247 __ Blr(lr);
4248 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4249 } else {
4250 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4251 instruction,
4252 instruction->GetDexPc(),
4253 nullptr);
4254 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4255 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004256}
4257
4258void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4259 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004260 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004261 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004262}
4263
4264void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004265 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004266 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004267 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004268 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004269 break;
4270
4271 default:
4272 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4273 }
4274}
4275
David Brazdil66d126e2015-04-03 16:02:44 +01004276void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4277 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4278 locations->SetInAt(0, Location::RequiresRegister());
4279 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4280}
4281
4282void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01004283 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
4284}
4285
Alexandre Rames5319def2014-10-23 10:03:10 +01004286void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004287 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4288 ? LocationSummary::kCallOnSlowPath
4289 : LocationSummary::kNoCall;
4290 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004291 locations->SetInAt(0, Location::RequiresRegister());
4292 if (instruction->HasUses()) {
4293 locations->SetOut(Location::SameAsFirstInput());
4294 }
4295}
4296
Calin Juravle2ae48182016-03-16 14:05:09 +00004297void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4298 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004299 return;
4300 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004301
Alexandre Ramesd921d642015-04-16 15:07:16 +01004302 BlockPoolsScope block_pools(GetVIXLAssembler());
4303 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004304 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004305 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004306}
4307
Calin Juravle2ae48182016-03-16 14:05:09 +00004308void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004309 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004310 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004311
4312 LocationSummary* locations = instruction->GetLocations();
4313 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004314
4315 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004316}
4317
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004318void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004319 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004320}
4321
Alexandre Rames67555f72014-11-18 10:55:16 +00004322void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4323 HandleBinaryOp(instruction);
4324}
4325
4326void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4327 HandleBinaryOp(instruction);
4328}
4329
Alexandre Rames3e69f162014-12-10 10:36:50 +00004330void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4331 LOG(FATAL) << "Unreachable";
4332}
4333
4334void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4335 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4336}
4337
Alexandre Rames5319def2014-10-23 10:03:10 +01004338void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4339 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4340 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4341 if (location.IsStackSlot()) {
4342 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4343 } else if (location.IsDoubleStackSlot()) {
4344 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4345 }
4346 locations->SetOut(location);
4347}
4348
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004349void InstructionCodeGeneratorARM64::VisitParameterValue(
4350 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004351 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004352}
4353
4354void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4355 LocationSummary* locations =
4356 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004357 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004358}
4359
4360void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4361 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4362 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004363}
4364
4365void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4366 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004367 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004368 locations->SetInAt(i, Location::Any());
4369 }
4370 locations->SetOut(Location::Any());
4371}
4372
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004373void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004374 LOG(FATAL) << "Unreachable";
4375}
4376
Serban Constantinescu02164b32014-11-13 14:05:07 +00004377void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004378 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004379 LocationSummary::CallKind call_kind =
4380 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004381 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4382
4383 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004384 case Primitive::kPrimInt:
4385 case Primitive::kPrimLong:
4386 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004387 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004388 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4389 break;
4390
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004391 case Primitive::kPrimFloat:
4392 case Primitive::kPrimDouble: {
4393 InvokeRuntimeCallingConvention calling_convention;
4394 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4395 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4396 locations->SetOut(calling_convention.GetReturnLocation(type));
4397
4398 break;
4399 }
4400
Serban Constantinescu02164b32014-11-13 14:05:07 +00004401 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004402 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004403 }
4404}
4405
4406void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4407 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004408
Serban Constantinescu02164b32014-11-13 14:05:07 +00004409 switch (type) {
4410 case Primitive::kPrimInt:
4411 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004412 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004413 break;
4414 }
4415
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004416 case Primitive::kPrimFloat:
4417 case Primitive::kPrimDouble: {
4418 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4419 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004420 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004421 if (type == Primitive::kPrimFloat) {
4422 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4423 } else {
4424 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4425 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004426 break;
4427 }
4428
Serban Constantinescu02164b32014-11-13 14:05:07 +00004429 default:
4430 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004431 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004432 }
4433}
4434
Calin Juravle27df7582015-04-17 19:12:31 +01004435void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4436 memory_barrier->SetLocations(nullptr);
4437}
4438
4439void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004440 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004441}
4442
Alexandre Rames5319def2014-10-23 10:03:10 +01004443void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4444 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4445 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004446 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004447}
4448
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004449void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004450 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004451}
4452
4453void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4454 instruction->SetLocations(nullptr);
4455}
4456
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004457void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004458 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004459}
4460
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004461void LocationsBuilderARM64::VisitRor(HRor* ror) {
4462 HandleBinaryOp(ror);
4463}
4464
4465void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4466 HandleBinaryOp(ror);
4467}
4468
Serban Constantinescu02164b32014-11-13 14:05:07 +00004469void LocationsBuilderARM64::VisitShl(HShl* shl) {
4470 HandleShift(shl);
4471}
4472
4473void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4474 HandleShift(shl);
4475}
4476
4477void LocationsBuilderARM64::VisitShr(HShr* shr) {
4478 HandleShift(shr);
4479}
4480
4481void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4482 HandleShift(shr);
4483}
4484
Alexandre Rames5319def2014-10-23 10:03:10 +01004485void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004486 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004487}
4488
4489void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004490 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004491}
4492
Alexandre Rames67555f72014-11-18 10:55:16 +00004493void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004494 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004495}
4496
4497void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004498 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004499}
4500
4501void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004502 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004503}
4504
Alexandre Rames67555f72014-11-18 10:55:16 +00004505void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004506 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004507}
4508
Calin Juravlee460d1d2015-09-29 04:52:17 +01004509void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4510 HUnresolvedInstanceFieldGet* instruction) {
4511 FieldAccessCallingConventionARM64 calling_convention;
4512 codegen_->CreateUnresolvedFieldLocationSummary(
4513 instruction, instruction->GetFieldType(), calling_convention);
4514}
4515
4516void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4517 HUnresolvedInstanceFieldGet* instruction) {
4518 FieldAccessCallingConventionARM64 calling_convention;
4519 codegen_->GenerateUnresolvedFieldAccess(instruction,
4520 instruction->GetFieldType(),
4521 instruction->GetFieldIndex(),
4522 instruction->GetDexPc(),
4523 calling_convention);
4524}
4525
4526void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4527 HUnresolvedInstanceFieldSet* instruction) {
4528 FieldAccessCallingConventionARM64 calling_convention;
4529 codegen_->CreateUnresolvedFieldLocationSummary(
4530 instruction, instruction->GetFieldType(), calling_convention);
4531}
4532
4533void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4534 HUnresolvedInstanceFieldSet* instruction) {
4535 FieldAccessCallingConventionARM64 calling_convention;
4536 codegen_->GenerateUnresolvedFieldAccess(instruction,
4537 instruction->GetFieldType(),
4538 instruction->GetFieldIndex(),
4539 instruction->GetDexPc(),
4540 calling_convention);
4541}
4542
4543void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4544 HUnresolvedStaticFieldGet* instruction) {
4545 FieldAccessCallingConventionARM64 calling_convention;
4546 codegen_->CreateUnresolvedFieldLocationSummary(
4547 instruction, instruction->GetFieldType(), calling_convention);
4548}
4549
4550void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4551 HUnresolvedStaticFieldGet* instruction) {
4552 FieldAccessCallingConventionARM64 calling_convention;
4553 codegen_->GenerateUnresolvedFieldAccess(instruction,
4554 instruction->GetFieldType(),
4555 instruction->GetFieldIndex(),
4556 instruction->GetDexPc(),
4557 calling_convention);
4558}
4559
4560void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4561 HUnresolvedStaticFieldSet* instruction) {
4562 FieldAccessCallingConventionARM64 calling_convention;
4563 codegen_->CreateUnresolvedFieldLocationSummary(
4564 instruction, instruction->GetFieldType(), calling_convention);
4565}
4566
4567void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4568 HUnresolvedStaticFieldSet* instruction) {
4569 FieldAccessCallingConventionARM64 calling_convention;
4570 codegen_->GenerateUnresolvedFieldAccess(instruction,
4571 instruction->GetFieldType(),
4572 instruction->GetFieldIndex(),
4573 instruction->GetDexPc(),
4574 calling_convention);
4575}
4576
Alexandre Rames5319def2014-10-23 10:03:10 +01004577void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4578 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4579}
4580
4581void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004582 HBasicBlock* block = instruction->GetBlock();
4583 if (block->GetLoopInformation() != nullptr) {
4584 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4585 // The back edge will generate the suspend check.
4586 return;
4587 }
4588 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4589 // The goto will generate the suspend check.
4590 return;
4591 }
4592 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004593}
4594
Alexandre Rames67555f72014-11-18 10:55:16 +00004595void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4596 LocationSummary* locations =
4597 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4598 InvokeRuntimeCallingConvention calling_convention;
4599 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4600}
4601
4602void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4603 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004604 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004605 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004606}
4607
4608void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4609 LocationSummary* locations =
4610 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4611 Primitive::Type input_type = conversion->GetInputType();
4612 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004613 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004614 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4615 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4616 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4617 }
4618
Alexandre Rames542361f2015-01-29 16:57:31 +00004619 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004620 locations->SetInAt(0, Location::RequiresFpuRegister());
4621 } else {
4622 locations->SetInAt(0, Location::RequiresRegister());
4623 }
4624
Alexandre Rames542361f2015-01-29 16:57:31 +00004625 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004626 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4627 } else {
4628 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4629 }
4630}
4631
4632void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4633 Primitive::Type result_type = conversion->GetResultType();
4634 Primitive::Type input_type = conversion->GetInputType();
4635
4636 DCHECK_NE(input_type, result_type);
4637
Alexandre Rames542361f2015-01-29 16:57:31 +00004638 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004639 int result_size = Primitive::ComponentSize(result_type);
4640 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004641 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004642 Register output = OutputRegister(conversion);
4643 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004644 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004645 // 'int' values are used directly as W registers, discarding the top
4646 // bits, so we don't need to sign-extend and can just perform a move.
4647 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4648 // top 32 bits of the target register. We theoretically could leave those
4649 // bits unchanged, but we would have to make sure that no code uses a
4650 // 32bit input value as a 64bit value assuming that the top 32 bits are
4651 // zero.
4652 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004653 } else if (result_type == Primitive::kPrimChar ||
4654 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4655 __ Ubfx(output,
4656 output.IsX() ? source.X() : source.W(),
4657 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004658 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004659 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004660 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004661 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004662 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004663 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004664 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4665 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004666 } else if (Primitive::IsFloatingPointType(result_type) &&
4667 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004668 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4669 } else {
4670 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4671 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004672 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004673}
Alexandre Rames67555f72014-11-18 10:55:16 +00004674
Serban Constantinescu02164b32014-11-13 14:05:07 +00004675void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4676 HandleShift(ushr);
4677}
4678
4679void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4680 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004681}
4682
4683void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4684 HandleBinaryOp(instruction);
4685}
4686
4687void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4688 HandleBinaryOp(instruction);
4689}
4690
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004691void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004692 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004693 LOG(FATAL) << "Unreachable";
4694}
4695
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004696void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004697 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004698 LOG(FATAL) << "Unreachable";
4699}
4700
Mark Mendellfe57faa2015-09-18 09:26:15 -04004701// Simple implementation of packed switch - generate cascaded compare/jumps.
4702void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4703 LocationSummary* locations =
4704 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4705 locations->SetInAt(0, Location::RequiresRegister());
4706}
4707
4708void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4709 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004710 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004711 Register value_reg = InputRegisterAt(switch_instr, 0);
4712 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4713
Zheng Xu3927c8b2015-11-18 17:46:25 +08004714 // Roughly set 16 as max average assemblies generated per HIR in a graph.
4715 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * vixl::kInstructionSize;
4716 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4717 // make sure we don't emit it if the target may run out of range.
4718 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4719 // ranges and emit the tables only as required.
4720 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004721
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004722 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004723 // Current instruction id is an upper bound of the number of HIRs in the graph.
4724 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4725 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004726 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4727 Register temp = temps.AcquireW();
4728 __ Subs(temp, value_reg, Operand(lower_bound));
4729
Zheng Xu3927c8b2015-11-18 17:46:25 +08004730 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004731 // Jump to successors[0] if value == lower_bound.
4732 __ B(eq, codegen_->GetLabelOf(successors[0]));
4733 int32_t last_index = 0;
4734 for (; num_entries - last_index > 2; last_index += 2) {
4735 __ Subs(temp, temp, Operand(2));
4736 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4737 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4738 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4739 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4740 }
4741 if (num_entries - last_index == 2) {
4742 // The last missing case_value.
4743 __ Cmp(temp, Operand(1));
4744 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004745 }
4746
4747 // And the default for any other value.
4748 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4749 __ B(codegen_->GetLabelOf(default_block));
4750 }
4751 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01004752 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08004753
4754 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4755
4756 // Below instructions should use at most one blocked register. Since there are two blocked
4757 // registers, we are free to block one.
4758 Register temp_w = temps.AcquireW();
4759 Register index;
4760 // Remove the bias.
4761 if (lower_bound != 0) {
4762 index = temp_w;
4763 __ Sub(index, value_reg, Operand(lower_bound));
4764 } else {
4765 index = value_reg;
4766 }
4767
4768 // Jump to default block if index is out of the range.
4769 __ Cmp(index, Operand(num_entries));
4770 __ B(hs, codegen_->GetLabelOf(default_block));
4771
4772 // In current VIXL implementation, it won't require any blocked registers to encode the
4773 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4774 // register pressure.
4775 Register table_base = temps.AcquireX();
4776 // Load jump offset from the table.
4777 __ Adr(table_base, jump_table->GetTableStartLabel());
4778 Register jump_offset = temp_w;
4779 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4780
4781 // Jump to target block by branching to table_base(pc related) + offset.
4782 Register target_address = table_base;
4783 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4784 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004785 }
4786}
4787
Roland Levillain44015862016-01-22 11:47:17 +00004788void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4789 Location out,
4790 uint32_t offset,
4791 Location maybe_temp) {
4792 Primitive::Type type = Primitive::kPrimNot;
4793 Register out_reg = RegisterFrom(out, type);
4794 if (kEmitCompilerReadBarrier) {
4795 Register temp_reg = RegisterFrom(maybe_temp, type);
4796 if (kUseBakerReadBarrier) {
4797 // Load with fast path based Baker's read barrier.
4798 // /* HeapReference<Object> */ out = *(out + offset)
4799 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4800 out,
4801 out_reg,
4802 offset,
4803 temp_reg,
4804 /* needs_null_check */ false,
4805 /* use_load_acquire */ false);
4806 } else {
4807 // Load with slow path based read barrier.
4808 // Save the value of `out` into `maybe_temp` before overwriting it
4809 // in the following move operation, as we will need it for the
4810 // read barrier below.
4811 __ Mov(temp_reg, out_reg);
4812 // /* HeapReference<Object> */ out = *(out + offset)
4813 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4814 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4815 }
4816 } else {
4817 // Plain load with no read barrier.
4818 // /* HeapReference<Object> */ out = *(out + offset)
4819 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4820 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4821 }
4822}
4823
4824void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
4825 Location out,
4826 Location obj,
4827 uint32_t offset,
4828 Location maybe_temp) {
4829 Primitive::Type type = Primitive::kPrimNot;
4830 Register out_reg = RegisterFrom(out, type);
4831 Register obj_reg = RegisterFrom(obj, type);
4832 if (kEmitCompilerReadBarrier) {
4833 if (kUseBakerReadBarrier) {
4834 // Load with fast path based Baker's read barrier.
4835 Register temp_reg = RegisterFrom(maybe_temp, type);
4836 // /* HeapReference<Object> */ out = *(obj + offset)
4837 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4838 out,
4839 obj_reg,
4840 offset,
4841 temp_reg,
4842 /* needs_null_check */ false,
4843 /* use_load_acquire */ false);
4844 } else {
4845 // Load with slow path based read barrier.
4846 // /* HeapReference<Object> */ out = *(obj + offset)
4847 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
4848 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4849 }
4850 } else {
4851 // Plain load with no read barrier.
4852 // /* HeapReference<Object> */ out = *(obj + offset)
4853 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
4854 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4855 }
4856}
4857
4858void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
4859 Location root,
4860 vixl::Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004861 uint32_t offset,
4862 vixl::Label* fixup_label) {
Roland Levillain44015862016-01-22 11:47:17 +00004863 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
4864 if (kEmitCompilerReadBarrier) {
4865 if (kUseBakerReadBarrier) {
4866 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4867 // Baker's read barrier are used:
4868 //
4869 // root = obj.field;
4870 // if (Thread::Current()->GetIsGcMarking()) {
4871 // root = ReadBarrier::Mark(root)
4872 // }
4873
4874 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004875 if (fixup_label == nullptr) {
4876 __ Ldr(root_reg, MemOperand(obj, offset));
4877 } else {
4878 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4879 __ Bind(fixup_label);
4880 __ ldr(root_reg, MemOperand(obj, offset));
4881 }
Roland Levillain44015862016-01-22 11:47:17 +00004882 static_assert(
4883 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
4884 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
4885 "have different sizes.");
4886 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
4887 "art::mirror::CompressedReference<mirror::Object> and int32_t "
4888 "have different sizes.");
4889
4890 // Slow path used to mark the GC root `root`.
4891 SlowPathCodeARM64* slow_path =
4892 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root, root);
4893 codegen_->AddSlowPath(slow_path);
4894
4895 MacroAssembler* masm = GetVIXLAssembler();
4896 UseScratchRegisterScope temps(masm);
4897 Register temp = temps.AcquireW();
4898 // temp = Thread::Current()->GetIsGcMarking()
4899 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64WordSize>().Int32Value()));
4900 __ Cbnz(temp, slow_path->GetEntryLabel());
4901 __ Bind(slow_path->GetExitLabel());
4902 } else {
4903 // GC root loaded through a slow path for read barriers other
4904 // than Baker's.
4905 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004906 if (fixup_label == nullptr) {
4907 __ Add(root_reg.X(), obj.X(), offset);
4908 } else {
4909 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4910 __ Bind(fixup_label);
4911 __ add(root_reg.X(), obj.X(), offset);
4912 }
Roland Levillain44015862016-01-22 11:47:17 +00004913 // /* mirror::Object* */ root = root->Read()
4914 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
4915 }
4916 } else {
4917 // Plain GC root load with no read barrier.
4918 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004919 if (fixup_label == nullptr) {
4920 __ Ldr(root_reg, MemOperand(obj, offset));
4921 } else {
4922 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4923 __ Bind(fixup_label);
4924 __ ldr(root_reg, MemOperand(obj, offset));
4925 }
Roland Levillain44015862016-01-22 11:47:17 +00004926 // Note that GC roots are not affected by heap poisoning, thus we
4927 // do not have to unpoison `root_reg` here.
4928 }
4929}
4930
4931void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
4932 Location ref,
4933 vixl::Register obj,
4934 uint32_t offset,
4935 Register temp,
4936 bool needs_null_check,
4937 bool use_load_acquire) {
4938 DCHECK(kEmitCompilerReadBarrier);
4939 DCHECK(kUseBakerReadBarrier);
4940
4941 // /* HeapReference<Object> */ ref = *(obj + offset)
4942 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01004943 size_t no_scale_factor = 0U;
4944 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4945 ref,
4946 obj,
4947 offset,
4948 no_index,
4949 no_scale_factor,
4950 temp,
4951 needs_null_check,
4952 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00004953}
4954
4955void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
4956 Location ref,
4957 vixl::Register obj,
4958 uint32_t data_offset,
4959 Location index,
4960 Register temp,
4961 bool needs_null_check) {
4962 DCHECK(kEmitCompilerReadBarrier);
4963 DCHECK(kUseBakerReadBarrier);
4964
4965 // Array cells are never volatile variables, therefore array loads
4966 // never use Load-Acquire instructions on ARM64.
4967 const bool use_load_acquire = false;
4968
Roland Levillainbfea3352016-06-23 13:48:47 +01004969 static_assert(
4970 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4971 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00004972 // /* HeapReference<Object> */ ref =
4973 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01004974 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
4975 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4976 ref,
4977 obj,
4978 data_offset,
4979 index,
4980 scale_factor,
4981 temp,
4982 needs_null_check,
4983 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00004984}
4985
4986void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
4987 Location ref,
4988 vixl::Register obj,
4989 uint32_t offset,
4990 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01004991 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00004992 Register temp,
4993 bool needs_null_check,
4994 bool use_load_acquire) {
4995 DCHECK(kEmitCompilerReadBarrier);
4996 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01004997 // If we are emitting an array load, we should not be using a
4998 // Load Acquire instruction. In other words:
4999 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5000 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005001
5002 MacroAssembler* masm = GetVIXLAssembler();
5003 UseScratchRegisterScope temps(masm);
5004
5005 // In slow path based read barriers, the read barrier call is
5006 // inserted after the original load. However, in fast path based
5007 // Baker's read barriers, we need to perform the load of
5008 // mirror::Object::monitor_ *before* the original reference load.
5009 // This load-load ordering is required by the read barrier.
5010 // The fast path/slow path (for Baker's algorithm) should look like:
5011 //
5012 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5013 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5014 // HeapReference<Object> ref = *src; // Original reference load.
5015 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5016 // if (is_gray) {
5017 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5018 // }
5019 //
5020 // Note: the original implementation in ReadBarrier::Barrier is
5021 // slightly more complex as it performs additional checks that we do
5022 // not do here for performance reasons.
5023
5024 Primitive::Type type = Primitive::kPrimNot;
5025 Register ref_reg = RegisterFrom(ref, type);
5026 DCHECK(obj.IsW());
5027 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5028
5029 // /* int32_t */ monitor = obj->monitor_
5030 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5031 if (needs_null_check) {
5032 MaybeRecordImplicitNullCheck(instruction);
5033 }
5034 // /* LockWord */ lock_word = LockWord(monitor)
5035 static_assert(sizeof(LockWord) == sizeof(int32_t),
5036 "art::LockWord and int32_t have different sizes.");
5037 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
5038 __ Lsr(temp, temp, LockWord::kReadBarrierStateShift);
5039 __ And(temp, temp, Operand(LockWord::kReadBarrierStateMask));
5040 static_assert(
5041 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
5042 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
5043
5044 // Introduce a dependency on the high bits of rb_state, which shall
5045 // be all zeroes, to prevent load-load reordering, and without using
5046 // a memory barrier (which would be more expensive).
5047 // temp2 = rb_state & ~LockWord::kReadBarrierStateMask = 0
5048 Register temp2 = temps.AcquireW();
5049 __ Bic(temp2, temp, Operand(LockWord::kReadBarrierStateMask));
5050 // obj is unchanged by this operation, but its value now depends on
5051 // temp2, which depends on temp.
5052 __ Add(obj, obj, Operand(temp2));
5053 temps.Release(temp2);
5054
5055 // The actual reference load.
5056 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01005057 // Load types involving an "index".
5058 if (use_load_acquire) {
5059 // UnsafeGetObjectVolatile intrinsic case.
5060 // Register `index` is not an index in an object array, but an
5061 // offset to an object reference field within object `obj`.
5062 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5063 DCHECK(instruction->GetLocations()->Intrinsified());
5064 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5065 << instruction->AsInvoke()->GetIntrinsic();
5066 DCHECK_EQ(offset, 0U);
5067 DCHECK_EQ(scale_factor, 0U);
5068 DCHECK_EQ(needs_null_check, 0U);
5069 // /* HeapReference<Object> */ ref = *(obj + index)
5070 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5071 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005072 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005073 // ArrayGet and UnsafeGetObject intrinsics cases.
5074 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5075 if (index.IsConstant()) {
5076 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5077 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5078 } else {
5079 temp2 = temps.AcquireW();
5080 __ Add(temp2, obj, offset);
5081 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, scale_factor));
5082 temps.Release(temp2);
5083 }
Roland Levillain44015862016-01-22 11:47:17 +00005084 }
Roland Levillain44015862016-01-22 11:47:17 +00005085 } else {
5086 // /* HeapReference<Object> */ ref = *(obj + offset)
5087 MemOperand field = HeapOperand(obj, offset);
5088 if (use_load_acquire) {
5089 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5090 } else {
5091 Load(type, ref_reg, field);
5092 }
5093 }
5094
5095 // Object* ref = ref_addr->AsMirrorPtr()
5096 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5097
5098 // Slow path used to mark the object `ref` when it is gray.
5099 SlowPathCodeARM64* slow_path =
5100 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref, ref);
5101 AddSlowPath(slow_path);
5102
5103 // if (rb_state == ReadBarrier::gray_ptr_)
5104 // ref = ReadBarrier::Mark(ref);
5105 __ Cmp(temp, ReadBarrier::gray_ptr_);
5106 __ B(eq, slow_path->GetEntryLabel());
5107 __ Bind(slow_path->GetExitLabel());
5108}
5109
5110void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5111 Location out,
5112 Location ref,
5113 Location obj,
5114 uint32_t offset,
5115 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005116 DCHECK(kEmitCompilerReadBarrier);
5117
Roland Levillain44015862016-01-22 11:47:17 +00005118 // Insert a slow path based read barrier *after* the reference load.
5119 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005120 // If heap poisoning is enabled, the unpoisoning of the loaded
5121 // reference will be carried out by the runtime within the slow
5122 // path.
5123 //
5124 // Note that `ref` currently does not get unpoisoned (when heap
5125 // poisoning is enabled), which is alright as the `ref` argument is
5126 // not used by the artReadBarrierSlow entry point.
5127 //
5128 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5129 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5130 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5131 AddSlowPath(slow_path);
5132
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005133 __ B(slow_path->GetEntryLabel());
5134 __ Bind(slow_path->GetExitLabel());
5135}
5136
Roland Levillain44015862016-01-22 11:47:17 +00005137void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5138 Location out,
5139 Location ref,
5140 Location obj,
5141 uint32_t offset,
5142 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005143 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005144 // Baker's read barriers shall be handled by the fast path
5145 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5146 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005147 // If heap poisoning is enabled, unpoisoning will be taken care of
5148 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005149 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005150 } else if (kPoisonHeapReferences) {
5151 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5152 }
5153}
5154
Roland Levillain44015862016-01-22 11:47:17 +00005155void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5156 Location out,
5157 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005158 DCHECK(kEmitCompilerReadBarrier);
5159
Roland Levillain44015862016-01-22 11:47:17 +00005160 // Insert a slow path based read barrier *after* the GC root load.
5161 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005162 // Note that GC roots are not affected by heap poisoning, so we do
5163 // not need to do anything special for this here.
5164 SlowPathCodeARM64* slow_path =
5165 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5166 AddSlowPath(slow_path);
5167
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005168 __ B(slow_path->GetEntryLabel());
5169 __ Bind(slow_path->GetExitLabel());
5170}
5171
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005172void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5173 LocationSummary* locations =
5174 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5175 locations->SetInAt(0, Location::RequiresRegister());
5176 locations->SetOut(Location::RequiresRegister());
5177}
5178
5179void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5180 LocationSummary* locations = instruction->GetLocations();
5181 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005182 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005183 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5184 instruction->GetIndex(), kArm64PointerSize).SizeValue();
5185 } else {
Nelli Kimbadee982016-05-13 13:08:53 +03005186 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5187 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
5188 method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity50706432016-06-14 11:31:04 -07005189 instruction->GetIndex(), kArm64PointerSize));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005190 }
5191 __ Ldr(XRegisterFrom(locations->Out()),
5192 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
5193}
5194
5195
5196
Alexandre Rames67555f72014-11-18 10:55:16 +00005197#undef __
5198#undef QUICK_ENTRY_POINT
5199
Alexandre Rames5319def2014-10-23 10:03:10 +01005200} // namespace arm64
5201} // namespace art