blob: 4bdfd57e7b28e4191cf6cd7d9014e4c1c3440459 [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);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100240 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
241 ? QUICK_ENTRY_POINT(pThrowStringBounds)
242 : QUICK_ENTRY_POINT(pThrowArrayBounds);
243 arm64_codegen->InvokeRuntime(entry_point_offset, instruction_, instruction_->GetDexPc(), this);
244 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800245 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100246 }
247
Alexandre Rames8158f282015-08-07 10:26:17 +0100248 bool IsFatal() const OVERRIDE { return true; }
249
Alexandre Rames9931f312015-06-19 14:47:01 +0100250 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
251
Alexandre Rames5319def2014-10-23 10:03:10 +0100252 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100253 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
254};
255
Alexandre Rames67555f72014-11-18 10:55:16 +0000256class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
257 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000258 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000259
260 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
261 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
262 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000263 if (instruction_->CanThrowIntoCatchBlock()) {
264 // Live registers will be restored in the catch block if caught.
265 SaveLiveRegisters(codegen, instruction_->GetLocations());
266 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000267 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000268 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800269 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000270 }
271
Alexandre Rames8158f282015-08-07 10:26:17 +0100272 bool IsFatal() const OVERRIDE { return true; }
273
Alexandre Rames9931f312015-06-19 14:47:01 +0100274 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
275
Alexandre Rames67555f72014-11-18 10:55:16 +0000276 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000277 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
278};
279
280class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
281 public:
282 LoadClassSlowPathARM64(HLoadClass* cls,
283 HInstruction* at,
284 uint32_t dex_pc,
285 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000286 : SlowPathCodeARM64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000287 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
288 }
289
290 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
291 LocationSummary* locations = at_->GetLocations();
292 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
293
294 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000295 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000296
297 InvokeRuntimeCallingConvention calling_convention;
298 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000299 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
300 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000301 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800302 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100303 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800304 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100305 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800306 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000307
308 // Move the class to the desired location.
309 Location out = locations->Out();
310 if (out.IsValid()) {
311 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
312 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000313 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000314 }
315
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000316 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000317 __ B(GetExitLabel());
318 }
319
Alexandre Rames9931f312015-06-19 14:47:01 +0100320 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
321
Alexandre Rames67555f72014-11-18 10:55:16 +0000322 private:
323 // The class this slow path will load.
324 HLoadClass* const cls_;
325
326 // The instruction where this slow path is happening.
327 // (Might be the load class or an initialization check).
328 HInstruction* const at_;
329
330 // The dex PC of `at_`.
331 const uint32_t dex_pc_;
332
333 // Whether to initialize the class.
334 const bool do_clinit_;
335
336 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
337};
338
339class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
340 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000341 explicit LoadStringSlowPathARM64(HLoadString* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000342
343 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
344 LocationSummary* locations = instruction_->GetLocations();
345 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
346 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
347
348 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000349 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000350
351 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000352 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
353 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
Alexandre Rames67555f72014-11-18 10:55:16 +0000354 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000355 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100356 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000357 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000358 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000359
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000360 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000361 __ B(GetExitLabel());
362 }
363
Alexandre Rames9931f312015-06-19 14:47:01 +0100364 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
365
Alexandre Rames67555f72014-11-18 10:55:16 +0000366 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000367 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
368};
369
Alexandre Rames5319def2014-10-23 10:03:10 +0100370class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
371 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000372 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100373
Alexandre Rames67555f72014-11-18 10:55:16 +0000374 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
375 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100376 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000377 if (instruction_->CanThrowIntoCatchBlock()) {
378 // Live registers will be restored in the catch block if caught.
379 SaveLiveRegisters(codegen, instruction_->GetLocations());
380 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000381 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000382 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800383 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100384 }
385
Alexandre Rames8158f282015-08-07 10:26:17 +0100386 bool IsFatal() const OVERRIDE { return true; }
387
Alexandre Rames9931f312015-06-19 14:47:01 +0100388 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
389
Alexandre Rames5319def2014-10-23 10:03:10 +0100390 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100391 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
392};
393
394class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
395 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100396 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000397 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100398
Alexandre Rames67555f72014-11-18 10:55:16 +0000399 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
400 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100401 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000402 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000403 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000404 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800405 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000406 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000407 if (successor_ == nullptr) {
408 __ B(GetReturnLabel());
409 } else {
410 __ B(arm64_codegen->GetLabelOf(successor_));
411 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100412 }
413
414 vixl::Label* GetReturnLabel() {
415 DCHECK(successor_ == nullptr);
416 return &return_label_;
417 }
418
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100419 HBasicBlock* GetSuccessor() const {
420 return successor_;
421 }
422
Alexandre Rames9931f312015-06-19 14:47:01 +0100423 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
424
Alexandre Rames5319def2014-10-23 10:03:10 +0100425 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100426 // If not null, the block to branch to after the suspend check.
427 HBasicBlock* const successor_;
428
429 // If `successor_` is null, the label to branch to after the suspend check.
430 vixl::Label return_label_;
431
432 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
433};
434
Alexandre Rames67555f72014-11-18 10:55:16 +0000435class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
436 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000437 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000438 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000439
440 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000441 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100442 Location class_to_check = locations->InAt(1);
443 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
444 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000445 DCHECK(instruction_->IsCheckCast()
446 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
447 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100448 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000449
Alexandre Rames67555f72014-11-18 10:55:16 +0000450 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000451
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000452 if (!is_fatal_) {
453 SaveLiveRegisters(codegen, locations);
454 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000455
456 // We're moving two locations to locations that could overlap, so we need a parallel
457 // move resolver.
458 InvokeRuntimeCallingConvention calling_convention;
459 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100460 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
461 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000462
463 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000464 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100465 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000466 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
467 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000468 Primitive::Type ret_type = instruction_->GetType();
469 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
470 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
471 } else {
472 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100473 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800474 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000475 }
476
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000477 if (!is_fatal_) {
478 RestoreLiveRegisters(codegen, locations);
479 __ B(GetExitLabel());
480 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000481 }
482
Alexandre Rames9931f312015-06-19 14:47:01 +0100483 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000484 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100485
Alexandre Rames67555f72014-11-18 10:55:16 +0000486 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000487 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000488
Alexandre Rames67555f72014-11-18 10:55:16 +0000489 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
490};
491
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700492class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
493 public:
Aart Bik42249c32016-01-07 15:33:50 -0800494 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000495 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700496
497 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800498 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700499 __ Bind(GetEntryLabel());
500 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800501 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
502 instruction_,
503 instruction_->GetDexPc(),
504 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000505 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700506 }
507
Alexandre Rames9931f312015-06-19 14:47:01 +0100508 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
509
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700510 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700511 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
512};
513
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100514class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
515 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000516 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100517
518 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
519 LocationSummary* locations = instruction_->GetLocations();
520 __ Bind(GetEntryLabel());
521 SaveLiveRegisters(codegen, locations);
522
523 InvokeRuntimeCallingConvention calling_convention;
524 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
525 parallel_move.AddMove(
526 locations->InAt(0),
527 LocationFrom(calling_convention.GetRegisterAt(0)),
528 Primitive::kPrimNot,
529 nullptr);
530 parallel_move.AddMove(
531 locations->InAt(1),
532 LocationFrom(calling_convention.GetRegisterAt(1)),
533 Primitive::kPrimInt,
534 nullptr);
535 parallel_move.AddMove(
536 locations->InAt(2),
537 LocationFrom(calling_convention.GetRegisterAt(2)),
538 Primitive::kPrimNot,
539 nullptr);
540 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
541
542 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
543 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
544 instruction_,
545 instruction_->GetDexPc(),
546 this);
547 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
548 RestoreLiveRegisters(codegen, locations);
549 __ B(GetExitLabel());
550 }
551
552 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
553
554 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100555 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
556};
557
Zheng Xu3927c8b2015-11-18 17:46:25 +0800558void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
559 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000560 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800561
562 // We are about to use the assembler to place literals directly. Make sure we have enough
563 // underlying code buffer and we have generated the jump table with right size.
564 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
565 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
566
567 __ Bind(&table_start_);
568 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
569 for (uint32_t i = 0; i < num_entries; i++) {
570 vixl::Label* target_label = codegen->GetLabelOf(successors[i]);
571 DCHECK(target_label->IsBound());
572 ptrdiff_t jump_offset = target_label->location() - table_start_.location();
573 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
574 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
575 Literal<int32_t> literal(jump_offset);
576 __ place(&literal);
577 }
578}
579
Roland Levillain44015862016-01-22 11:47:17 +0000580// Slow path marking an object during a read barrier.
581class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
582 public:
583 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000584 : SlowPathCodeARM64(instruction), out_(out), obj_(obj) {
Roland Levillain44015862016-01-22 11:47:17 +0000585 DCHECK(kEmitCompilerReadBarrier);
586 }
587
588 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
589
590 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
591 LocationSummary* locations = instruction_->GetLocations();
592 Primitive::Type type = Primitive::kPrimNot;
593 DCHECK(locations->CanCall());
594 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
595 DCHECK(instruction_->IsInstanceFieldGet() ||
596 instruction_->IsStaticFieldGet() ||
597 instruction_->IsArrayGet() ||
598 instruction_->IsLoadClass() ||
599 instruction_->IsLoadString() ||
600 instruction_->IsInstanceOf() ||
601 instruction_->IsCheckCast())
602 << "Unexpected instruction in read barrier marking slow path: "
603 << instruction_->DebugName();
604
605 __ Bind(GetEntryLabel());
606 SaveLiveRegisters(codegen, locations);
607
608 InvokeRuntimeCallingConvention calling_convention;
609 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
610 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)), obj_, type);
611 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
612 instruction_,
613 instruction_->GetDexPc(),
614 this);
615 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
616 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
617
618 RestoreLiveRegisters(codegen, locations);
619 __ B(GetExitLabel());
620 }
621
622 private:
Roland Levillain44015862016-01-22 11:47:17 +0000623 const Location out_;
624 const Location obj_;
625
626 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
627};
628
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000629// Slow path generating a read barrier for a heap reference.
630class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
631 public:
632 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
633 Location out,
634 Location ref,
635 Location obj,
636 uint32_t offset,
637 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000638 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000639 out_(out),
640 ref_(ref),
641 obj_(obj),
642 offset_(offset),
643 index_(index) {
644 DCHECK(kEmitCompilerReadBarrier);
645 // If `obj` is equal to `out` or `ref`, it means the initial object
646 // has been overwritten by (or after) the heap object reference load
647 // to be instrumented, e.g.:
648 //
649 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000650 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000651 //
652 // In that case, we have lost the information about the original
653 // object, and the emitted read barrier cannot work properly.
654 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
655 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
656 }
657
658 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
659 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
660 LocationSummary* locations = instruction_->GetLocations();
661 Primitive::Type type = Primitive::kPrimNot;
662 DCHECK(locations->CanCall());
663 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
664 DCHECK(!instruction_->IsInvoke() ||
665 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain44015862016-01-22 11:47:17 +0000666 instruction_->GetLocations()->Intrinsified()))
667 << "Unexpected instruction in read barrier for heap reference slow path: "
668 << instruction_->DebugName();
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000669 // The read barrier instrumentation does not support the
670 // HArm64IntermediateAddress instruction yet.
671 DCHECK(!(instruction_->IsArrayGet() &&
672 instruction_->AsArrayGet()->GetArray()->IsArm64IntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000673
674 __ Bind(GetEntryLabel());
675
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000676 SaveLiveRegisters(codegen, locations);
677
678 // We may have to change the index's value, but as `index_` is a
679 // constant member (like other "inputs" of this slow path),
680 // introduce a copy of it, `index`.
681 Location index = index_;
682 if (index_.IsValid()) {
683 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
684 if (instruction_->IsArrayGet()) {
685 // Compute the actual memory offset and store it in `index`.
686 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
687 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
688 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
689 // We are about to change the value of `index_reg` (see the
690 // calls to vixl::MacroAssembler::Lsl and
691 // vixl::MacroAssembler::Mov below), but it has
692 // not been saved by the previous call to
693 // art::SlowPathCode::SaveLiveRegisters, as it is a
694 // callee-save register --
695 // art::SlowPathCode::SaveLiveRegisters does not consider
696 // callee-save registers, as it has been designed with the
697 // assumption that callee-save registers are supposed to be
698 // handled by the called function. So, as a callee-save
699 // register, `index_reg` _would_ eventually be saved onto
700 // the stack, but it would be too late: we would have
701 // changed its value earlier. Therefore, we manually save
702 // it here into another freely available register,
703 // `free_reg`, chosen of course among the caller-save
704 // registers (as a callee-save `free_reg` register would
705 // exhibit the same problem).
706 //
707 // Note we could have requested a temporary register from
708 // the register allocator instead; but we prefer not to, as
709 // this is a slow path, and we know we can find a
710 // caller-save register that is available.
711 Register free_reg = FindAvailableCallerSaveRegister(codegen);
712 __ Mov(free_reg.W(), index_reg);
713 index_reg = free_reg;
714 index = LocationFrom(index_reg);
715 } else {
716 // The initial register stored in `index_` has already been
717 // saved in the call to art::SlowPathCode::SaveLiveRegisters
718 // (as it is not a callee-save register), so we can freely
719 // use it.
720 }
721 // Shifting the index value contained in `index_reg` by the scale
722 // factor (2) cannot overflow in practice, as the runtime is
723 // unable to allocate object arrays with a size larger than
724 // 2^26 - 1 (that is, 2^28 - 4 bytes).
725 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
726 static_assert(
727 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
728 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
729 __ Add(index_reg, index_reg, Operand(offset_));
730 } else {
731 DCHECK(instruction_->IsInvoke());
732 DCHECK(instruction_->GetLocations()->Intrinsified());
733 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
734 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
735 << instruction_->AsInvoke()->GetIntrinsic();
736 DCHECK_EQ(offset_, 0U);
737 DCHECK(index_.IsRegisterPair());
738 // UnsafeGet's offset location is a register pair, the low
739 // part contains the correct offset.
740 index = index_.ToLow();
741 }
742 }
743
744 // We're moving two or three locations to locations that could
745 // overlap, so we need a parallel move resolver.
746 InvokeRuntimeCallingConvention calling_convention;
747 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
748 parallel_move.AddMove(ref_,
749 LocationFrom(calling_convention.GetRegisterAt(0)),
750 type,
751 nullptr);
752 parallel_move.AddMove(obj_,
753 LocationFrom(calling_convention.GetRegisterAt(1)),
754 type,
755 nullptr);
756 if (index.IsValid()) {
757 parallel_move.AddMove(index,
758 LocationFrom(calling_convention.GetRegisterAt(2)),
759 Primitive::kPrimInt,
760 nullptr);
761 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
762 } else {
763 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
764 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
765 }
766 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
767 instruction_,
768 instruction_->GetDexPc(),
769 this);
770 CheckEntrypointTypes<
771 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
772 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
773
774 RestoreLiveRegisters(codegen, locations);
775
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000776 __ B(GetExitLabel());
777 }
778
779 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
780
781 private:
782 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
783 size_t ref = static_cast<int>(XRegisterFrom(ref_).code());
784 size_t obj = static_cast<int>(XRegisterFrom(obj_).code());
785 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
786 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
787 return Register(VIXLRegCodeFromART(i), kXRegSize);
788 }
789 }
790 // We shall never fail to find a free caller-save register, as
791 // there are more than two core caller-save registers on ARM64
792 // (meaning it is possible to find one which is different from
793 // `ref` and `obj`).
794 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
795 LOG(FATAL) << "Could not find a free register";
796 UNREACHABLE();
797 }
798
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000799 const Location out_;
800 const Location ref_;
801 const Location obj_;
802 const uint32_t offset_;
803 // An additional location containing an index to an array.
804 // Only used for HArrayGet and the UnsafeGetObject &
805 // UnsafeGetObjectVolatile intrinsics.
806 const Location index_;
807
808 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
809};
810
811// Slow path generating a read barrier for a GC root.
812class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
813 public:
814 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000815 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000816 DCHECK(kEmitCompilerReadBarrier);
817 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000818
819 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
820 LocationSummary* locations = instruction_->GetLocations();
821 Primitive::Type type = Primitive::kPrimNot;
822 DCHECK(locations->CanCall());
823 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000824 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
825 << "Unexpected instruction in read barrier for GC root slow path: "
826 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000827
828 __ Bind(GetEntryLabel());
829 SaveLiveRegisters(codegen, locations);
830
831 InvokeRuntimeCallingConvention calling_convention;
832 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
833 // The argument of the ReadBarrierForRootSlow is not a managed
834 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
835 // thus we need a 64-bit move here, and we cannot use
836 //
837 // arm64_codegen->MoveLocation(
838 // LocationFrom(calling_convention.GetRegisterAt(0)),
839 // root_,
840 // type);
841 //
842 // which would emit a 32-bit move, as `type` is a (32-bit wide)
843 // reference type (`Primitive::kPrimNot`).
844 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
845 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
846 instruction_,
847 instruction_->GetDexPc(),
848 this);
849 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
850 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
851
852 RestoreLiveRegisters(codegen, locations);
853 __ B(GetExitLabel());
854 }
855
856 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
857
858 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000859 const Location out_;
860 const Location root_;
861
862 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
863};
864
Alexandre Rames5319def2014-10-23 10:03:10 +0100865#undef __
866
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100867Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100868 Location next_location;
869 if (type == Primitive::kPrimVoid) {
870 LOG(FATAL) << "Unreachable type " << type;
871 }
872
Alexandre Rames542361f2015-01-29 16:57:31 +0000873 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100874 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
875 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000876 } else if (!Primitive::IsFloatingPointType(type) &&
877 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000878 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
879 } else {
880 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000881 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
882 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100883 }
884
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000885 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000886 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100887 return next_location;
888}
889
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100890Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100891 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100892}
893
Serban Constantinescu579885a2015-02-22 20:51:33 +0000894CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
895 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100896 const CompilerOptions& compiler_options,
897 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100898 : CodeGenerator(graph,
899 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000900 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000901 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000902 callee_saved_core_registers.list(),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000903 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100904 compiler_options,
905 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100906 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800907 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100908 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000909 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000910 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100911 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000912 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000913 uint32_literals_(std::less<uint32_t>(),
914 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100915 uint64_literals_(std::less<uint64_t>(),
916 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
917 method_patches_(MethodReferenceComparator(),
918 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
919 call_patches_(MethodReferenceComparator(),
920 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
921 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000922 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
923 boot_image_string_patches_(StringReferenceValueComparator(),
924 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
925 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
926 boot_image_address_patches_(std::less<uint32_t>(),
927 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000928 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000929 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000930}
Alexandre Rames5319def2014-10-23 10:03:10 +0100931
Alexandre Rames67555f72014-11-18 10:55:16 +0000932#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100933
Zheng Xu3927c8b2015-11-18 17:46:25 +0800934void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100935 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800936 jump_table->EmitTable(this);
937 }
938}
939
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000940void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800941 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000942 // Ensure we emit the literal pool.
943 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000944
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000945 CodeGenerator::Finalize(allocator);
946}
947
Zheng Xuad4450e2015-04-17 18:48:56 +0800948void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
949 // Note: There are 6 kinds of moves:
950 // 1. constant -> GPR/FPR (non-cycle)
951 // 2. constant -> stack (non-cycle)
952 // 3. GPR/FPR -> GPR/FPR
953 // 4. GPR/FPR -> stack
954 // 5. stack -> GPR/FPR
955 // 6. stack -> stack (non-cycle)
956 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
957 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
958 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
959 // dependency.
960 vixl_temps_.Open(GetVIXLAssembler());
961}
962
963void ParallelMoveResolverARM64::FinishEmitNativeCode() {
964 vixl_temps_.Close();
965}
966
967Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
968 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
969 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
970 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
971 Location scratch = GetScratchLocation(kind);
972 if (!scratch.Equals(Location::NoLocation())) {
973 return scratch;
974 }
975 // Allocate from VIXL temp registers.
976 if (kind == Location::kRegister) {
977 scratch = LocationFrom(vixl_temps_.AcquireX());
978 } else {
979 DCHECK(kind == Location::kFpuRegister);
980 scratch = LocationFrom(vixl_temps_.AcquireD());
981 }
982 AddScratchLocation(scratch);
983 return scratch;
984}
985
986void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
987 if (loc.IsRegister()) {
988 vixl_temps_.Release(XRegisterFrom(loc));
989 } else {
990 DCHECK(loc.IsFpuRegister());
991 vixl_temps_.Release(DRegisterFrom(loc));
992 }
993 RemoveScratchLocation(loc);
994}
995
Alexandre Rames3e69f162014-12-10 10:36:50 +0000996void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100997 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100998 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000999}
1000
Alexandre Rames5319def2014-10-23 10:03:10 +01001001void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001002 MacroAssembler* masm = GetVIXLAssembler();
1003 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001004 __ Bind(&frame_entry_label_);
1005
Serban Constantinescu02164b32014-11-13 14:05:07 +00001006 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1007 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001008 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001009 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001010 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001011 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001012 __ Ldr(wzr, MemOperand(temp, 0));
1013 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001014 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001015
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001016 if (!HasEmptyFrame()) {
1017 int frame_size = GetFrameSize();
1018 // Stack layout:
1019 // sp[frame_size - 8] : lr.
1020 // ... : other preserved core registers.
1021 // ... : other preserved fp registers.
1022 // ... : reserved frame space.
1023 // sp[0] : current method.
1024 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001025 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001026 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1027 frame_size - GetCoreSpillSize());
1028 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1029 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001030 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001031}
1032
1033void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001034 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001035 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001036 if (!HasEmptyFrame()) {
1037 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001038 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1039 frame_size - FrameEntrySpillSize());
1040 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1041 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001042 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001043 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001044 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001045 __ Ret();
1046 GetAssembler()->cfi().RestoreState();
1047 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001048}
1049
Zheng Xuda403092015-04-24 17:35:39 +08001050vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
1051 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
1052 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
1053 core_spill_mask_);
1054}
1055
1056vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
1057 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1058 GetNumberOfFloatingPointRegisters()));
1059 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
1060 fpu_spill_mask_);
1061}
1062
Alexandre Rames5319def2014-10-23 10:03:10 +01001063void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1064 __ Bind(GetLabelOf(block));
1065}
1066
Calin Juravle175dc732015-08-25 15:42:32 +01001067void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1068 DCHECK(location.IsRegister());
1069 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1070}
1071
Calin Juravlee460d1d2015-09-29 04:52:17 +01001072void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1073 if (location.IsRegister()) {
1074 locations->AddTemp(location);
1075 } else {
1076 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1077 }
1078}
1079
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001080void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001081 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001082 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001083 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +01001084 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001085 if (value_can_be_null) {
1086 __ Cbz(value, &done);
1087 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001088 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
1089 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001090 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001091 if (value_can_be_null) {
1092 __ Bind(&done);
1093 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001094}
1095
David Brazdil58282f42016-01-14 12:45:10 +00001096void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001097 // Blocked core registers:
1098 // lr : Runtime reserved.
1099 // tr : Runtime reserved.
1100 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1101 // ip1 : VIXL core temp.
1102 // ip0 : VIXL core temp.
1103 //
1104 // Blocked fp registers:
1105 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001106 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1107 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001108 while (!reserved_core_registers.IsEmpty()) {
1109 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
1110 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001111
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001112 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001113 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001114 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
1115 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001116
David Brazdil58282f42016-01-14 12:45:10 +00001117 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001118 // Stubs do not save callee-save floating point registers. If the graph
1119 // is debuggable, we need to deal with these registers differently. For
1120 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001121 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1122 while (!reserved_fp_registers_debuggable.IsEmpty()) {
1123 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().code()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001124 }
1125 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001126}
1127
Alexandre Rames3e69f162014-12-10 10:36:50 +00001128size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1129 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1130 __ Str(reg, MemOperand(sp, stack_index));
1131 return kArm64WordSize;
1132}
1133
1134size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1135 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1136 __ Ldr(reg, MemOperand(sp, stack_index));
1137 return kArm64WordSize;
1138}
1139
1140size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1141 FPRegister reg = FPRegister(reg_id, kDRegSize);
1142 __ Str(reg, MemOperand(sp, stack_index));
1143 return kArm64WordSize;
1144}
1145
1146size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1147 FPRegister reg = FPRegister(reg_id, kDRegSize);
1148 __ Ldr(reg, MemOperand(sp, stack_index));
1149 return kArm64WordSize;
1150}
1151
Alexandre Rames5319def2014-10-23 10:03:10 +01001152void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001153 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001154}
1155
1156void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001157 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001158}
1159
Alexandre Rames67555f72014-11-18 10:55:16 +00001160void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001161 if (constant->IsIntConstant()) {
1162 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1163 } else if (constant->IsLongConstant()) {
1164 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1165 } else if (constant->IsNullConstant()) {
1166 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001167 } else if (constant->IsFloatConstant()) {
1168 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1169 } else {
1170 DCHECK(constant->IsDoubleConstant());
1171 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1172 }
1173}
1174
Alexandre Rames3e69f162014-12-10 10:36:50 +00001175
1176static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1177 DCHECK(constant.IsConstant());
1178 HConstant* cst = constant.GetConstant();
1179 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001180 // Null is mapped to a core W register, which we associate with kPrimInt.
1181 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001182 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1183 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1184 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1185}
1186
Calin Juravlee460d1d2015-09-29 04:52:17 +01001187void CodeGeneratorARM64::MoveLocation(Location destination,
1188 Location source,
1189 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001190 if (source.Equals(destination)) {
1191 return;
1192 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001193
1194 // A valid move can always be inferred from the destination and source
1195 // locations. When moving from and to a register, the argument type can be
1196 // used to generate 32bit instead of 64bit moves. In debug mode we also
1197 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001198 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001199
1200 if (destination.IsRegister() || destination.IsFpuRegister()) {
1201 if (unspecified_type) {
1202 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1203 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001204 (src_cst != nullptr && (src_cst->IsIntConstant()
1205 || src_cst->IsFloatConstant()
1206 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001207 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001208 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001209 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001210 // If the source is a double stack slot or a 64bit constant, a 64bit
1211 // type is appropriate. Else the source is a register, and since the
1212 // type has not been specified, we chose a 64bit type to force a 64bit
1213 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001214 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001215 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001216 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001217 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1218 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1219 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001220 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1221 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1222 __ Ldr(dst, StackOperandFrom(source));
1223 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001224 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001225 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001226 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001227 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001228 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001229 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001230 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001231 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1232 ? Primitive::kPrimLong
1233 : Primitive::kPrimInt;
1234 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1235 }
1236 } else {
1237 DCHECK(source.IsFpuRegister());
1238 if (destination.IsRegister()) {
1239 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1240 ? Primitive::kPrimDouble
1241 : Primitive::kPrimFloat;
1242 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1243 } else {
1244 DCHECK(destination.IsFpuRegister());
1245 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001246 }
1247 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001248 } else { // The destination is not a register. It must be a stack slot.
1249 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1250 if (source.IsRegister() || source.IsFpuRegister()) {
1251 if (unspecified_type) {
1252 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001253 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001254 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001255 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001256 }
1257 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001258 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1259 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1260 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001261 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001262 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1263 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001264 UseScratchRegisterScope temps(GetVIXLAssembler());
1265 HConstant* src_cst = source.GetConstant();
1266 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001267 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001268 temp = temps.AcquireW();
1269 } else if (src_cst->IsLongConstant()) {
1270 temp = temps.AcquireX();
1271 } else if (src_cst->IsFloatConstant()) {
1272 temp = temps.AcquireS();
1273 } else {
1274 DCHECK(src_cst->IsDoubleConstant());
1275 temp = temps.AcquireD();
1276 }
1277 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001278 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001279 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001280 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001281 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001282 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001283 // There is generally less pressure on FP registers.
1284 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001285 __ Ldr(temp, StackOperandFrom(source));
1286 __ Str(temp, StackOperandFrom(destination));
1287 }
1288 }
1289}
1290
1291void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001292 CPURegister dst,
1293 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001294 switch (type) {
1295 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001296 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001297 break;
1298 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001299 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001300 break;
1301 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001302 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001303 break;
1304 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001305 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001306 break;
1307 case Primitive::kPrimInt:
1308 case Primitive::kPrimNot:
1309 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001310 case Primitive::kPrimFloat:
1311 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001312 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001313 __ Ldr(dst, src);
1314 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001315 case Primitive::kPrimVoid:
1316 LOG(FATAL) << "Unreachable type " << type;
1317 }
1318}
1319
Calin Juravle77520bc2015-01-12 18:45:46 +00001320void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001321 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001322 const MemOperand& src,
1323 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001324 MacroAssembler* masm = GetVIXLAssembler();
1325 BlockPoolsScope block_pools(masm);
1326 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001327 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001328 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001329
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001330 DCHECK(!src.IsPreIndex());
1331 DCHECK(!src.IsPostIndex());
1332
1333 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001334 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001335 MemOperand base = MemOperand(temp_base);
1336 switch (type) {
1337 case Primitive::kPrimBoolean:
1338 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001339 if (needs_null_check) {
1340 MaybeRecordImplicitNullCheck(instruction);
1341 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001342 break;
1343 case Primitive::kPrimByte:
1344 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001345 if (needs_null_check) {
1346 MaybeRecordImplicitNullCheck(instruction);
1347 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001348 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1349 break;
1350 case Primitive::kPrimChar:
1351 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001352 if (needs_null_check) {
1353 MaybeRecordImplicitNullCheck(instruction);
1354 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001355 break;
1356 case Primitive::kPrimShort:
1357 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001358 if (needs_null_check) {
1359 MaybeRecordImplicitNullCheck(instruction);
1360 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001361 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1362 break;
1363 case Primitive::kPrimInt:
1364 case Primitive::kPrimNot:
1365 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001366 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001367 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001368 if (needs_null_check) {
1369 MaybeRecordImplicitNullCheck(instruction);
1370 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001371 break;
1372 case Primitive::kPrimFloat:
1373 case Primitive::kPrimDouble: {
1374 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001375 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001376
1377 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1378 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001379 if (needs_null_check) {
1380 MaybeRecordImplicitNullCheck(instruction);
1381 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001382 __ Fmov(FPRegister(dst), temp);
1383 break;
1384 }
1385 case Primitive::kPrimVoid:
1386 LOG(FATAL) << "Unreachable type " << type;
1387 }
1388}
1389
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001390void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001391 CPURegister src,
1392 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001393 switch (type) {
1394 case Primitive::kPrimBoolean:
1395 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001396 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001397 break;
1398 case Primitive::kPrimChar:
1399 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001400 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001401 break;
1402 case Primitive::kPrimInt:
1403 case Primitive::kPrimNot:
1404 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001405 case Primitive::kPrimFloat:
1406 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001407 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001408 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001409 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001410 case Primitive::kPrimVoid:
1411 LOG(FATAL) << "Unreachable type " << type;
1412 }
1413}
1414
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001415void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1416 CPURegister src,
1417 const MemOperand& dst) {
1418 UseScratchRegisterScope temps(GetVIXLAssembler());
1419 Register temp_base = temps.AcquireX();
1420
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001421 DCHECK(!dst.IsPreIndex());
1422 DCHECK(!dst.IsPostIndex());
1423
1424 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001425 Operand op = OperandFromMemOperand(dst);
1426 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001427 MemOperand base = MemOperand(temp_base);
1428 switch (type) {
1429 case Primitive::kPrimBoolean:
1430 case Primitive::kPrimByte:
1431 __ Stlrb(Register(src), base);
1432 break;
1433 case Primitive::kPrimChar:
1434 case Primitive::kPrimShort:
1435 __ Stlrh(Register(src), base);
1436 break;
1437 case Primitive::kPrimInt:
1438 case Primitive::kPrimNot:
1439 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001440 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001441 __ Stlr(Register(src), base);
1442 break;
1443 case Primitive::kPrimFloat:
1444 case Primitive::kPrimDouble: {
1445 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001446 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001447
1448 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1449 __ Fmov(temp, FPRegister(src));
1450 __ Stlr(temp, base);
1451 break;
1452 }
1453 case Primitive::kPrimVoid:
1454 LOG(FATAL) << "Unreachable type " << type;
1455 }
1456}
1457
Calin Juravle175dc732015-08-25 15:42:32 +01001458void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1459 HInstruction* instruction,
1460 uint32_t dex_pc,
1461 SlowPathCode* slow_path) {
1462 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1463 instruction,
1464 dex_pc,
1465 slow_path);
1466}
1467
Alexandre Rames67555f72014-11-18 10:55:16 +00001468void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1469 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001470 uint32_t dex_pc,
1471 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001472 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001473 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001474 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1475 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001476 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001477}
1478
1479void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1480 vixl::Register class_reg) {
1481 UseScratchRegisterScope temps(GetVIXLAssembler());
1482 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001483 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1484
Serban Constantinescu02164b32014-11-13 14:05:07 +00001485 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001486 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1487 __ Add(temp, class_reg, status_offset);
1488 __ Ldar(temp, HeapOperand(temp));
1489 __ Cmp(temp, mirror::Class::kStatusInitialized);
1490 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001491 __ Bind(slow_path->GetExitLabel());
1492}
Alexandre Rames5319def2014-10-23 10:03:10 +01001493
Roland Levillain44015862016-01-22 11:47:17 +00001494void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001495 BarrierType type = BarrierAll;
1496
1497 switch (kind) {
1498 case MemBarrierKind::kAnyAny:
1499 case MemBarrierKind::kAnyStore: {
1500 type = BarrierAll;
1501 break;
1502 }
1503 case MemBarrierKind::kLoadAny: {
1504 type = BarrierReads;
1505 break;
1506 }
1507 case MemBarrierKind::kStoreStore: {
1508 type = BarrierWrites;
1509 break;
1510 }
1511 default:
1512 LOG(FATAL) << "Unexpected memory barrier " << kind;
1513 }
1514 __ Dmb(InnerShareable, type);
1515}
1516
Serban Constantinescu02164b32014-11-13 14:05:07 +00001517void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1518 HBasicBlock* successor) {
1519 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001520 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1521 if (slow_path == nullptr) {
1522 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1523 instruction->SetSlowPath(slow_path);
1524 codegen_->AddSlowPath(slow_path);
1525 if (successor != nullptr) {
1526 DCHECK(successor->IsLoopHeader());
1527 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1528 }
1529 } else {
1530 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1531 }
1532
Serban Constantinescu02164b32014-11-13 14:05:07 +00001533 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1534 Register temp = temps.AcquireW();
1535
1536 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1537 if (successor == nullptr) {
1538 __ Cbnz(temp, slow_path->GetEntryLabel());
1539 __ Bind(slow_path->GetReturnLabel());
1540 } else {
1541 __ Cbz(temp, codegen_->GetLabelOf(successor));
1542 __ B(slow_path->GetEntryLabel());
1543 // slow_path will return to GetLabelOf(successor).
1544 }
1545}
1546
Alexandre Rames5319def2014-10-23 10:03:10 +01001547InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1548 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001549 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001550 assembler_(codegen->GetAssembler()),
1551 codegen_(codegen) {}
1552
1553#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001554 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001555
1556#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1557
1558enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001559 // Using a base helps identify when we hit such breakpoints.
1560 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001561#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1562 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1563#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1564};
1565
1566#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001567 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001568 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1569 } \
1570 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1571 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1572 locations->SetOut(Location::Any()); \
1573 }
1574 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1575#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1576
1577#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001578#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001579
Alexandre Rames67555f72014-11-18 10:55:16 +00001580void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001581 DCHECK_EQ(instr->InputCount(), 2U);
1582 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1583 Primitive::Type type = instr->GetResultType();
1584 switch (type) {
1585 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001586 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001587 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001588 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001589 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001590 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001591
1592 case Primitive::kPrimFloat:
1593 case Primitive::kPrimDouble:
1594 locations->SetInAt(0, Location::RequiresFpuRegister());
1595 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001596 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001597 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001598
Alexandre Rames5319def2014-10-23 10:03:10 +01001599 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001600 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001601 }
1602}
1603
Alexandre Rames09a99962015-04-15 11:47:56 +01001604void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001605 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1606
1607 bool object_field_get_with_read_barrier =
1608 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001609 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001610 new (GetGraph()->GetArena()) LocationSummary(instruction,
1611 object_field_get_with_read_barrier ?
1612 LocationSummary::kCallOnSlowPath :
1613 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001614 locations->SetInAt(0, Location::RequiresRegister());
1615 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1616 locations->SetOut(Location::RequiresFpuRegister());
1617 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001618 // The output overlaps for an object field get when read barriers
1619 // are enabled: we do not want the load to overwrite the object's
1620 // location, as we need it to emit the read barrier.
1621 locations->SetOut(
1622 Location::RequiresRegister(),
1623 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001624 }
1625}
1626
1627void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1628 const FieldInfo& field_info) {
1629 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001630 LocationSummary* locations = instruction->GetLocations();
1631 Location base_loc = locations->InAt(0);
1632 Location out = locations->Out();
1633 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001634 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001635 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001636 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001637
Roland Levillain44015862016-01-22 11:47:17 +00001638 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1639 // Object FieldGet with Baker's read barrier case.
1640 MacroAssembler* masm = GetVIXLAssembler();
1641 UseScratchRegisterScope temps(masm);
1642 // /* HeapReference<Object> */ out = *(base + offset)
1643 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1644 Register temp = temps.AcquireW();
1645 // Note that potential implicit null checks are handled in this
1646 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1647 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1648 instruction,
1649 out,
1650 base,
1651 offset,
1652 temp,
1653 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001654 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001655 } else {
1656 // General case.
1657 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001658 // Note that a potential implicit null check is handled in this
1659 // CodeGeneratorARM64::LoadAcquire call.
1660 // NB: LoadAcquire will record the pc info if needed.
1661 codegen_->LoadAcquire(
1662 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001663 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001664 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001665 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001666 }
Roland Levillain44015862016-01-22 11:47:17 +00001667 if (field_type == Primitive::kPrimNot) {
1668 // If read barriers are enabled, emit read barriers other than
1669 // Baker's using a slow path (and also unpoison the loaded
1670 // reference, if heap poisoning is enabled).
1671 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1672 }
Roland Levillain4d027112015-07-01 15:41:14 +01001673 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001674}
1675
1676void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1677 LocationSummary* locations =
1678 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1679 locations->SetInAt(0, Location::RequiresRegister());
1680 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1681 locations->SetInAt(1, Location::RequiresFpuRegister());
1682 } else {
1683 locations->SetInAt(1, Location::RequiresRegister());
1684 }
1685}
1686
1687void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001688 const FieldInfo& field_info,
1689 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001690 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001691 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001692
1693 Register obj = InputRegisterAt(instruction, 0);
1694 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001695 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001696 Offset offset = field_info.GetFieldOffset();
1697 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001698
Roland Levillain4d027112015-07-01 15:41:14 +01001699 {
1700 // We use a block to end the scratch scope before the write barrier, thus
1701 // freeing the temporary registers so they can be used in `MarkGCCard`.
1702 UseScratchRegisterScope temps(GetVIXLAssembler());
1703
1704 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1705 DCHECK(value.IsW());
1706 Register temp = temps.AcquireW();
1707 __ Mov(temp, value.W());
1708 GetAssembler()->PoisonHeapReference(temp.W());
1709 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001710 }
Roland Levillain4d027112015-07-01 15:41:14 +01001711
1712 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001713 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1714 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001715 } else {
1716 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1717 codegen_->MaybeRecordImplicitNullCheck(instruction);
1718 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001719 }
1720
1721 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001722 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001723 }
1724}
1725
Alexandre Rames67555f72014-11-18 10:55:16 +00001726void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001727 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001728
1729 switch (type) {
1730 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001731 case Primitive::kPrimLong: {
1732 Register dst = OutputRegister(instr);
1733 Register lhs = InputRegisterAt(instr, 0);
1734 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001735 if (instr->IsAdd()) {
1736 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001737 } else if (instr->IsAnd()) {
1738 __ And(dst, lhs, rhs);
1739 } else if (instr->IsOr()) {
1740 __ Orr(dst, lhs, rhs);
1741 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001742 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001743 } else if (instr->IsRor()) {
1744 if (rhs.IsImmediate()) {
1745 uint32_t shift = rhs.immediate() & (lhs.SizeInBits() - 1);
1746 __ Ror(dst, lhs, shift);
1747 } else {
1748 // Ensure shift distance is in the same size register as the result. If
1749 // we are rotating a long and the shift comes in a w register originally,
1750 // we don't need to sxtw for use as an x since the shift distances are
1751 // all & reg_bits - 1.
1752 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1753 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001754 } else {
1755 DCHECK(instr->IsXor());
1756 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001757 }
1758 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001759 }
1760 case Primitive::kPrimFloat:
1761 case Primitive::kPrimDouble: {
1762 FPRegister dst = OutputFPRegister(instr);
1763 FPRegister lhs = InputFPRegisterAt(instr, 0);
1764 FPRegister rhs = InputFPRegisterAt(instr, 1);
1765 if (instr->IsAdd()) {
1766 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001767 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001768 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001769 } else {
1770 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001771 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001772 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001773 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001774 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001775 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001776 }
1777}
1778
Serban Constantinescu02164b32014-11-13 14:05:07 +00001779void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1780 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1781
1782 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1783 Primitive::Type type = instr->GetResultType();
1784 switch (type) {
1785 case Primitive::kPrimInt:
1786 case Primitive::kPrimLong: {
1787 locations->SetInAt(0, Location::RequiresRegister());
1788 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1789 locations->SetOut(Location::RequiresRegister());
1790 break;
1791 }
1792 default:
1793 LOG(FATAL) << "Unexpected shift type " << type;
1794 }
1795}
1796
1797void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1798 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1799
1800 Primitive::Type type = instr->GetType();
1801 switch (type) {
1802 case Primitive::kPrimInt:
1803 case Primitive::kPrimLong: {
1804 Register dst = OutputRegister(instr);
1805 Register lhs = InputRegisterAt(instr, 0);
1806 Operand rhs = InputOperandAt(instr, 1);
1807 if (rhs.IsImmediate()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001808 uint32_t shift_value = rhs.immediate() &
1809 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001810 if (instr->IsShl()) {
1811 __ Lsl(dst, lhs, shift_value);
1812 } else if (instr->IsShr()) {
1813 __ Asr(dst, lhs, shift_value);
1814 } else {
1815 __ Lsr(dst, lhs, shift_value);
1816 }
1817 } else {
1818 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1819
1820 if (instr->IsShl()) {
1821 __ Lsl(dst, lhs, rhs_reg);
1822 } else if (instr->IsShr()) {
1823 __ Asr(dst, lhs, rhs_reg);
1824 } else {
1825 __ Lsr(dst, lhs, rhs_reg);
1826 }
1827 }
1828 break;
1829 }
1830 default:
1831 LOG(FATAL) << "Unexpected shift operation type " << type;
1832 }
1833}
1834
Alexandre Rames5319def2014-10-23 10:03:10 +01001835void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001836 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001837}
1838
1839void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001840 HandleBinaryOp(instruction);
1841}
1842
1843void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1844 HandleBinaryOp(instruction);
1845}
1846
1847void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1848 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001849}
1850
Artem Serov7fc63502016-02-09 17:15:29 +00001851void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001852 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1853 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1854 locations->SetInAt(0, Location::RequiresRegister());
1855 // There is no immediate variant of negated bitwise instructions in AArch64.
1856 locations->SetInAt(1, Location::RequiresRegister());
1857 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1858}
1859
Artem Serov7fc63502016-02-09 17:15:29 +00001860void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001861 Register dst = OutputRegister(instr);
1862 Register lhs = InputRegisterAt(instr, 0);
1863 Register rhs = InputRegisterAt(instr, 1);
1864
1865 switch (instr->GetOpKind()) {
1866 case HInstruction::kAnd:
1867 __ Bic(dst, lhs, rhs);
1868 break;
1869 case HInstruction::kOr:
1870 __ Orn(dst, lhs, rhs);
1871 break;
1872 case HInstruction::kXor:
1873 __ Eon(dst, lhs, rhs);
1874 break;
1875 default:
1876 LOG(FATAL) << "Unreachable";
1877 }
1878}
1879
Alexandre Rames8626b742015-11-25 16:28:08 +00001880void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1881 HArm64DataProcWithShifterOp* instruction) {
1882 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1883 instruction->GetType() == Primitive::kPrimLong);
1884 LocationSummary* locations =
1885 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1886 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1887 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1888 } else {
1889 locations->SetInAt(0, Location::RequiresRegister());
1890 }
1891 locations->SetInAt(1, Location::RequiresRegister());
1892 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1893}
1894
1895void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1896 HArm64DataProcWithShifterOp* instruction) {
1897 Primitive::Type type = instruction->GetType();
1898 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1899 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1900 Register out = OutputRegister(instruction);
1901 Register left;
1902 if (kind != HInstruction::kNeg) {
1903 left = InputRegisterAt(instruction, 0);
1904 }
1905 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1906 // shifter operand operation, the IR generating `right_reg` (input to the type
1907 // conversion) can have a different type from the current instruction's type,
1908 // so we manually indicate the type.
1909 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001910 int64_t shift_amount = instruction->GetShiftAmount() &
1911 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001912
1913 Operand right_operand(0);
1914
1915 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1916 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1917 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1918 } else {
1919 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1920 }
1921
1922 // Logical binary operations do not support extension operations in the
1923 // operand. Note that VIXL would still manage if it was passed by generating
1924 // the extension as a separate instruction.
1925 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1926 DCHECK(!right_operand.IsExtendedRegister() ||
1927 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1928 kind != HInstruction::kNeg));
1929 switch (kind) {
1930 case HInstruction::kAdd:
1931 __ Add(out, left, right_operand);
1932 break;
1933 case HInstruction::kAnd:
1934 __ And(out, left, right_operand);
1935 break;
1936 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001937 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001938 __ Neg(out, right_operand);
1939 break;
1940 case HInstruction::kOr:
1941 __ Orr(out, left, right_operand);
1942 break;
1943 case HInstruction::kSub:
1944 __ Sub(out, left, right_operand);
1945 break;
1946 case HInstruction::kXor:
1947 __ Eor(out, left, right_operand);
1948 break;
1949 default:
1950 LOG(FATAL) << "Unexpected operation kind: " << kind;
1951 UNREACHABLE();
1952 }
1953}
1954
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001955void LocationsBuilderARM64::VisitArm64IntermediateAddress(HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001956 // The read barrier instrumentation does not support the
1957 // HArm64IntermediateAddress instruction yet.
1958 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001959 LocationSummary* locations =
1960 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1961 locations->SetInAt(0, Location::RequiresRegister());
1962 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1963 locations->SetOut(Location::RequiresRegister());
1964}
1965
1966void InstructionCodeGeneratorARM64::VisitArm64IntermediateAddress(
1967 HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001968 // The read barrier instrumentation does not support the
1969 // HArm64IntermediateAddress instruction yet.
1970 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001971 __ Add(OutputRegister(instruction),
1972 InputRegisterAt(instruction, 0),
1973 Operand(InputOperandAt(instruction, 1)));
1974}
1975
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001976void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00001977 LocationSummary* locations =
1978 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001979 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
1980 if (instr->GetOpKind() == HInstruction::kSub &&
1981 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00001982 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001983 // Don't allocate register for Mneg instruction.
1984 } else {
1985 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
1986 Location::RequiresRegister());
1987 }
1988 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
1989 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00001990 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1991}
1992
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001993void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00001994 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001995 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
1996 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00001997
1998 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
1999 // This fixup should be carried out for all multiply-accumulate instructions:
2000 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2001 if (instr->GetType() == Primitive::kPrimLong &&
2002 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2003 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
2004 vixl::Instruction* prev = masm->GetCursorAddress<vixl::Instruction*>() - vixl::kInstructionSize;
2005 if (prev->IsLoadOrStore()) {
2006 // Make sure we emit only exactly one nop.
2007 vixl::CodeBufferCheckScope scope(masm,
2008 vixl::kInstructionSize,
2009 vixl::CodeBufferCheckScope::kCheck,
2010 vixl::CodeBufferCheckScope::kExactSize);
2011 __ nop();
2012 }
2013 }
2014
2015 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002016 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002017 __ Madd(res, mul_left, mul_right, accumulator);
2018 } else {
2019 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002020 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002021 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002022 __ Mneg(res, mul_left, mul_right);
2023 } else {
2024 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2025 __ Msub(res, mul_left, mul_right, accumulator);
2026 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002027 }
2028}
2029
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002030void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002031 bool object_array_get_with_read_barrier =
2032 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002033 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002034 new (GetGraph()->GetArena()) LocationSummary(instruction,
2035 object_array_get_with_read_barrier ?
2036 LocationSummary::kCallOnSlowPath :
2037 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002038 locations->SetInAt(0, Location::RequiresRegister());
2039 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002040 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2041 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2042 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002043 // The output overlaps in the case of an object array get with
2044 // read barriers enabled: we do not want the move to overwrite the
2045 // array's location, as we need it to emit the read barrier.
2046 locations->SetOut(
2047 Location::RequiresRegister(),
2048 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002049 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002050}
2051
2052void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002053 Primitive::Type type = instruction->GetType();
2054 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002055 LocationSummary* locations = instruction->GetLocations();
2056 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002057 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002058 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002059
Alexandre Ramesd921d642015-04-16 15:07:16 +01002060 MacroAssembler* masm = GetVIXLAssembler();
2061 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002062 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002063 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002064
Roland Levillain44015862016-01-22 11:47:17 +00002065 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2066 // Object ArrayGet with Baker's read barrier case.
2067 Register temp = temps.AcquireW();
2068 // The read barrier instrumentation does not support the
2069 // HArm64IntermediateAddress instruction yet.
2070 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
2071 // Note that a potential implicit null check is handled in the
2072 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2073 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2074 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002075 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002076 // General case.
2077 MemOperand source = HeapOperand(obj);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002078 if (index.IsConstant()) {
Roland Levillain44015862016-01-22 11:47:17 +00002079 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2080 source = HeapOperand(obj, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002081 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002082 Register temp = temps.AcquireSameSizeAs(obj);
2083 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
2084 // The read barrier instrumentation does not support the
2085 // HArm64IntermediateAddress instruction yet.
2086 DCHECK(!kEmitCompilerReadBarrier);
2087 // We do not need to compute the intermediate address from the array: the
2088 // input instruction has done it already. See the comment in
2089 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2090 if (kIsDebugBuild) {
2091 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2092 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2093 }
2094 temp = obj;
2095 } else {
2096 __ Add(temp, obj, offset);
2097 }
2098 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2099 }
2100
2101 codegen_->Load(type, OutputCPURegister(instruction), source);
2102 codegen_->MaybeRecordImplicitNullCheck(instruction);
2103
2104 if (type == Primitive::kPrimNot) {
2105 static_assert(
2106 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2107 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2108 Location obj_loc = locations->InAt(0);
2109 if (index.IsConstant()) {
2110 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2111 } else {
2112 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2113 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002114 }
Roland Levillain4d027112015-07-01 15:41:14 +01002115 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002116}
2117
Alexandre Rames5319def2014-10-23 10:03:10 +01002118void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2119 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2120 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002121 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002122}
2123
2124void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002125 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002126 BlockPoolsScope block_pools(GetVIXLAssembler());
Vladimir Markodce016e2016-04-28 13:10:02 +01002127 __ Ldr(OutputRegister(instruction), HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002128 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002129}
2130
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002131void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002132 Primitive::Type value_type = instruction->GetComponentType();
2133
2134 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2135 bool object_array_set_with_read_barrier =
2136 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002137 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2138 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002139 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2140 LocationSummary::kCallOnSlowPath :
2141 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002142 locations->SetInAt(0, Location::RequiresRegister());
2143 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002144 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002145 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002146 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002147 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002148 }
2149}
2150
2151void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2152 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002153 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002154 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002155 bool needs_write_barrier =
2156 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002157
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002158 Register array = InputRegisterAt(instruction, 0);
2159 CPURegister value = InputCPURegisterAt(instruction, 2);
2160 CPURegister source = value;
2161 Location index = locations->InAt(1);
2162 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2163 MemOperand destination = HeapOperand(array);
2164 MacroAssembler* masm = GetVIXLAssembler();
2165 BlockPoolsScope block_pools(masm);
2166
2167 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002168 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002169 if (index.IsConstant()) {
2170 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2171 destination = HeapOperand(array, offset);
2172 } else {
2173 UseScratchRegisterScope temps(masm);
2174 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002175 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002176 // The read barrier instrumentation does not support the
2177 // HArm64IntermediateAddress instruction yet.
2178 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002179 // We do not need to compute the intermediate address from the array: the
2180 // input instruction has done it already. See the comment in
2181 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2182 if (kIsDebugBuild) {
2183 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2184 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2185 }
2186 temp = array;
2187 } else {
2188 __ Add(temp, array, offset);
2189 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002190 destination = HeapOperand(temp,
2191 XRegisterFrom(index),
2192 LSL,
2193 Primitive::ComponentSizeShift(value_type));
2194 }
2195 codegen_->Store(value_type, value, destination);
2196 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002197 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002198 DCHECK(needs_write_barrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002199 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002200 vixl::Label done;
2201 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002202 {
2203 // We use a block to end the scratch scope before the write barrier, thus
2204 // freeing the temporary registers so they can be used in `MarkGCCard`.
2205 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002206 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002207 if (index.IsConstant()) {
2208 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002209 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002210 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002211 destination = HeapOperand(temp,
2212 XRegisterFrom(index),
2213 LSL,
2214 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002215 }
2216
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002217 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2218 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2219 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2220
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002221 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002222 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2223 codegen_->AddSlowPath(slow_path);
2224 if (instruction->GetValueCanBeNull()) {
2225 vixl::Label non_zero;
2226 __ Cbnz(Register(value), &non_zero);
2227 if (!index.IsConstant()) {
2228 __ Add(temp, array, offset);
2229 }
2230 __ Str(wzr, destination);
2231 codegen_->MaybeRecordImplicitNullCheck(instruction);
2232 __ B(&done);
2233 __ Bind(&non_zero);
2234 }
2235
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002236 if (kEmitCompilerReadBarrier) {
2237 // When read barriers are enabled, the type checking
2238 // instrumentation requires two read barriers:
2239 //
2240 // __ Mov(temp2, temp);
2241 // // /* HeapReference<Class> */ temp = temp->component_type_
2242 // __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002243 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002244 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2245 //
2246 // // /* HeapReference<Class> */ temp2 = value->klass_
2247 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002248 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002249 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2250 //
2251 // __ Cmp(temp, temp2);
2252 //
2253 // However, the second read barrier may trash `temp`, as it
2254 // is a temporary register, and as such would not be saved
2255 // along with live registers before calling the runtime (nor
2256 // restored afterwards). So in this case, we bail out and
2257 // delegate the work to the array set slow path.
2258 //
2259 // TODO: Extend the register allocator to support a new
2260 // "(locally) live temp" location so as to avoid always
2261 // going into the slow path when read barriers are enabled.
2262 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002263 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002264 Register temp2 = temps.AcquireSameSizeAs(array);
2265 // /* HeapReference<Class> */ temp = array->klass_
2266 __ Ldr(temp, HeapOperand(array, class_offset));
2267 codegen_->MaybeRecordImplicitNullCheck(instruction);
2268 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2269
2270 // /* HeapReference<Class> */ temp = temp->component_type_
2271 __ Ldr(temp, HeapOperand(temp, component_offset));
2272 // /* HeapReference<Class> */ temp2 = value->klass_
2273 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2274 // If heap poisoning is enabled, no need to unpoison `temp`
2275 // nor `temp2`, as we are comparing two poisoned references.
2276 __ Cmp(temp, temp2);
2277
2278 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2279 vixl::Label do_put;
2280 __ B(eq, &do_put);
2281 // If heap poisoning is enabled, the `temp` reference has
2282 // not been unpoisoned yet; unpoison it now.
2283 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2284
2285 // /* HeapReference<Class> */ temp = temp->super_class_
2286 __ Ldr(temp, HeapOperand(temp, super_offset));
2287 // If heap poisoning is enabled, no need to unpoison
2288 // `temp`, as we are comparing against null below.
2289 __ Cbnz(temp, slow_path->GetEntryLabel());
2290 __ Bind(&do_put);
2291 } else {
2292 __ B(ne, slow_path->GetEntryLabel());
2293 }
2294 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002295 }
2296 }
2297
2298 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002299 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002300 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002301 __ Mov(temp2, value.W());
2302 GetAssembler()->PoisonHeapReference(temp2);
2303 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002304 }
2305
2306 if (!index.IsConstant()) {
2307 __ Add(temp, array, offset);
2308 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002309 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002310
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002311 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002312 codegen_->MaybeRecordImplicitNullCheck(instruction);
2313 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002314 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002315
2316 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2317
2318 if (done.IsLinked()) {
2319 __ Bind(&done);
2320 }
2321
2322 if (slow_path != nullptr) {
2323 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002324 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002325 }
2326}
2327
Alexandre Rames67555f72014-11-18 10:55:16 +00002328void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002329 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2330 ? LocationSummary::kCallOnSlowPath
2331 : LocationSummary::kNoCall;
2332 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002333 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002334 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002335 if (instruction->HasUses()) {
2336 locations->SetOut(Location::SameAsFirstInput());
2337 }
2338}
2339
2340void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002341 BoundsCheckSlowPathARM64* slow_path =
2342 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002343 codegen_->AddSlowPath(slow_path);
2344
2345 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2346 __ B(slow_path->GetEntryLabel(), hs);
2347}
2348
Alexandre Rames67555f72014-11-18 10:55:16 +00002349void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2350 LocationSummary* locations =
2351 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2352 locations->SetInAt(0, Location::RequiresRegister());
2353 if (check->HasUses()) {
2354 locations->SetOut(Location::SameAsFirstInput());
2355 }
2356}
2357
2358void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2359 // We assume the class is not null.
2360 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2361 check->GetLoadClass(), check, check->GetDexPc(), true);
2362 codegen_->AddSlowPath(slow_path);
2363 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2364}
2365
Roland Levillain1a653882016-03-18 18:05:57 +00002366static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2367 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2368 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2369}
2370
2371void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2372 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2373 Location rhs_loc = instruction->GetLocations()->InAt(1);
2374 if (rhs_loc.IsConstant()) {
2375 // 0.0 is the only immediate that can be encoded directly in
2376 // an FCMP instruction.
2377 //
2378 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2379 // specify that in a floating-point comparison, positive zero
2380 // and negative zero are considered equal, so we can use the
2381 // literal 0.0 for both cases here.
2382 //
2383 // Note however that some methods (Float.equal, Float.compare,
2384 // Float.compareTo, Double.equal, Double.compare,
2385 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2386 // StrictMath.min) consider 0.0 to be (strictly) greater than
2387 // -0.0. So if we ever translate calls to these methods into a
2388 // HCompare instruction, we must handle the -0.0 case with
2389 // care here.
2390 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2391 __ Fcmp(lhs_reg, 0.0);
2392 } else {
2393 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2394 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002395}
2396
Serban Constantinescu02164b32014-11-13 14:05:07 +00002397void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002398 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002399 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2400 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002401 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002402 case Primitive::kPrimBoolean:
2403 case Primitive::kPrimByte:
2404 case Primitive::kPrimShort:
2405 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002406 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002407 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002408 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002409 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002410 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2411 break;
2412 }
2413 case Primitive::kPrimFloat:
2414 case Primitive::kPrimDouble: {
2415 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002416 locations->SetInAt(1,
2417 IsFloatingPointZeroConstant(compare->InputAt(1))
2418 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2419 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002420 locations->SetOut(Location::RequiresRegister());
2421 break;
2422 }
2423 default:
2424 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2425 }
2426}
2427
2428void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2429 Primitive::Type in_type = compare->InputAt(0)->GetType();
2430
2431 // 0 if: left == right
2432 // 1 if: left > right
2433 // -1 if: left < right
2434 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002435 case Primitive::kPrimBoolean:
2436 case Primitive::kPrimByte:
2437 case Primitive::kPrimShort:
2438 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002439 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002440 case Primitive::kPrimLong: {
2441 Register result = OutputRegister(compare);
2442 Register left = InputRegisterAt(compare, 0);
2443 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002444 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002445 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2446 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002447 break;
2448 }
2449 case Primitive::kPrimFloat:
2450 case Primitive::kPrimDouble: {
2451 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002452 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002453 __ Cset(result, ne);
2454 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002455 break;
2456 }
2457 default:
2458 LOG(FATAL) << "Unimplemented compare type " << in_type;
2459 }
2460}
2461
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002462void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002463 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002464
2465 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2466 locations->SetInAt(0, Location::RequiresFpuRegister());
2467 locations->SetInAt(1,
2468 IsFloatingPointZeroConstant(instruction->InputAt(1))
2469 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2470 : Location::RequiresFpuRegister());
2471 } else {
2472 // Integer cases.
2473 locations->SetInAt(0, Location::RequiresRegister());
2474 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2475 }
2476
David Brazdilb3e773e2016-01-26 11:28:37 +00002477 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002478 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002479 }
2480}
2481
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002482void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002483 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002484 return;
2485 }
2486
2487 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002488 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002489 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002490
Roland Levillain7f63c522015-07-13 15:54:55 +00002491 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002492 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002493 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002494 } else {
2495 // Integer cases.
2496 Register lhs = InputRegisterAt(instruction, 0);
2497 Operand rhs = InputOperandAt(instruction, 1);
2498 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002499 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002500 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002501}
2502
2503#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2504 M(Equal) \
2505 M(NotEqual) \
2506 M(LessThan) \
2507 M(LessThanOrEqual) \
2508 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002509 M(GreaterThanOrEqual) \
2510 M(Below) \
2511 M(BelowOrEqual) \
2512 M(Above) \
2513 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002514#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002515void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2516void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002517FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002518#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002519#undef FOR_EACH_CONDITION_INSTRUCTION
2520
Zheng Xuc6667102015-05-15 16:08:45 +08002521void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2522 DCHECK(instruction->IsDiv() || instruction->IsRem());
2523
2524 LocationSummary* locations = instruction->GetLocations();
2525 Location second = locations->InAt(1);
2526 DCHECK(second.IsConstant());
2527
2528 Register out = OutputRegister(instruction);
2529 Register dividend = InputRegisterAt(instruction, 0);
2530 int64_t imm = Int64FromConstant(second.GetConstant());
2531 DCHECK(imm == 1 || imm == -1);
2532
2533 if (instruction->IsRem()) {
2534 __ Mov(out, 0);
2535 } else {
2536 if (imm == 1) {
2537 __ Mov(out, dividend);
2538 } else {
2539 __ Neg(out, dividend);
2540 }
2541 }
2542}
2543
2544void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2545 DCHECK(instruction->IsDiv() || instruction->IsRem());
2546
2547 LocationSummary* locations = instruction->GetLocations();
2548 Location second = locations->InAt(1);
2549 DCHECK(second.IsConstant());
2550
2551 Register out = OutputRegister(instruction);
2552 Register dividend = InputRegisterAt(instruction, 0);
2553 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002554 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002555 int ctz_imm = CTZ(abs_imm);
2556
2557 UseScratchRegisterScope temps(GetVIXLAssembler());
2558 Register temp = temps.AcquireSameSizeAs(out);
2559
2560 if (instruction->IsDiv()) {
2561 __ Add(temp, dividend, abs_imm - 1);
2562 __ Cmp(dividend, 0);
2563 __ Csel(out, temp, dividend, lt);
2564 if (imm > 0) {
2565 __ Asr(out, out, ctz_imm);
2566 } else {
2567 __ Neg(out, Operand(out, ASR, ctz_imm));
2568 }
2569 } else {
2570 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2571 __ Asr(temp, dividend, bits - 1);
2572 __ Lsr(temp, temp, bits - ctz_imm);
2573 __ Add(out, dividend, temp);
2574 __ And(out, out, abs_imm - 1);
2575 __ Sub(out, out, temp);
2576 }
2577}
2578
2579void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2580 DCHECK(instruction->IsDiv() || instruction->IsRem());
2581
2582 LocationSummary* locations = instruction->GetLocations();
2583 Location second = locations->InAt(1);
2584 DCHECK(second.IsConstant());
2585
2586 Register out = OutputRegister(instruction);
2587 Register dividend = InputRegisterAt(instruction, 0);
2588 int64_t imm = Int64FromConstant(second.GetConstant());
2589
2590 Primitive::Type type = instruction->GetResultType();
2591 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2592
2593 int64_t magic;
2594 int shift;
2595 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2596
2597 UseScratchRegisterScope temps(GetVIXLAssembler());
2598 Register temp = temps.AcquireSameSizeAs(out);
2599
2600 // temp = get_high(dividend * magic)
2601 __ Mov(temp, magic);
2602 if (type == Primitive::kPrimLong) {
2603 __ Smulh(temp, dividend, temp);
2604 } else {
2605 __ Smull(temp.X(), dividend, temp);
2606 __ Lsr(temp.X(), temp.X(), 32);
2607 }
2608
2609 if (imm > 0 && magic < 0) {
2610 __ Add(temp, temp, dividend);
2611 } else if (imm < 0 && magic > 0) {
2612 __ Sub(temp, temp, dividend);
2613 }
2614
2615 if (shift != 0) {
2616 __ Asr(temp, temp, shift);
2617 }
2618
2619 if (instruction->IsDiv()) {
2620 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2621 } else {
2622 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2623 // TODO: Strength reduction for msub.
2624 Register temp_imm = temps.AcquireSameSizeAs(out);
2625 __ Mov(temp_imm, imm);
2626 __ Msub(out, temp, temp_imm, dividend);
2627 }
2628}
2629
2630void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2631 DCHECK(instruction->IsDiv() || instruction->IsRem());
2632 Primitive::Type type = instruction->GetResultType();
2633 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2634
2635 LocationSummary* locations = instruction->GetLocations();
2636 Register out = OutputRegister(instruction);
2637 Location second = locations->InAt(1);
2638
2639 if (second.IsConstant()) {
2640 int64_t imm = Int64FromConstant(second.GetConstant());
2641
2642 if (imm == 0) {
2643 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2644 } else if (imm == 1 || imm == -1) {
2645 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002646 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002647 DivRemByPowerOfTwo(instruction);
2648 } else {
2649 DCHECK(imm <= -2 || imm >= 2);
2650 GenerateDivRemWithAnyConstant(instruction);
2651 }
2652 } else {
2653 Register dividend = InputRegisterAt(instruction, 0);
2654 Register divisor = InputRegisterAt(instruction, 1);
2655 if (instruction->IsDiv()) {
2656 __ Sdiv(out, dividend, divisor);
2657 } else {
2658 UseScratchRegisterScope temps(GetVIXLAssembler());
2659 Register temp = temps.AcquireSameSizeAs(out);
2660 __ Sdiv(temp, dividend, divisor);
2661 __ Msub(out, temp, divisor, dividend);
2662 }
2663 }
2664}
2665
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002666void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2667 LocationSummary* locations =
2668 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2669 switch (div->GetResultType()) {
2670 case Primitive::kPrimInt:
2671 case Primitive::kPrimLong:
2672 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002673 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002674 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2675 break;
2676
2677 case Primitive::kPrimFloat:
2678 case Primitive::kPrimDouble:
2679 locations->SetInAt(0, Location::RequiresFpuRegister());
2680 locations->SetInAt(1, Location::RequiresFpuRegister());
2681 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2682 break;
2683
2684 default:
2685 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2686 }
2687}
2688
2689void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2690 Primitive::Type type = div->GetResultType();
2691 switch (type) {
2692 case Primitive::kPrimInt:
2693 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002694 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002695 break;
2696
2697 case Primitive::kPrimFloat:
2698 case Primitive::kPrimDouble:
2699 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2700 break;
2701
2702 default:
2703 LOG(FATAL) << "Unexpected div type " << type;
2704 }
2705}
2706
Alexandre Rames67555f72014-11-18 10:55:16 +00002707void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002708 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2709 ? LocationSummary::kCallOnSlowPath
2710 : LocationSummary::kNoCall;
2711 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002712 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2713 if (instruction->HasUses()) {
2714 locations->SetOut(Location::SameAsFirstInput());
2715 }
2716}
2717
2718void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2719 SlowPathCodeARM64* slow_path =
2720 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2721 codegen_->AddSlowPath(slow_path);
2722 Location value = instruction->GetLocations()->InAt(0);
2723
Alexandre Rames3e69f162014-12-10 10:36:50 +00002724 Primitive::Type type = instruction->GetType();
2725
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002726 if (!Primitive::IsIntegralType(type)) {
2727 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002728 return;
2729 }
2730
Alexandre Rames67555f72014-11-18 10:55:16 +00002731 if (value.IsConstant()) {
2732 int64_t divisor = Int64ConstantFrom(value);
2733 if (divisor == 0) {
2734 __ B(slow_path->GetEntryLabel());
2735 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002736 // A division by a non-null constant is valid. We don't need to perform
2737 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002738 }
2739 } else {
2740 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2741 }
2742}
2743
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002744void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2745 LocationSummary* locations =
2746 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2747 locations->SetOut(Location::ConstantLocation(constant));
2748}
2749
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002750void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2751 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002752 // Will be generated at use site.
2753}
2754
Alexandre Rames5319def2014-10-23 10:03:10 +01002755void LocationsBuilderARM64::VisitExit(HExit* exit) {
2756 exit->SetLocations(nullptr);
2757}
2758
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002759void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002760}
2761
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002762void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2763 LocationSummary* locations =
2764 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2765 locations->SetOut(Location::ConstantLocation(constant));
2766}
2767
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002768void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002769 // Will be generated at use site.
2770}
2771
David Brazdilfc6a86a2015-06-26 10:33:45 +00002772void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002773 DCHECK(!successor->IsExitBlock());
2774 HBasicBlock* block = got->GetBlock();
2775 HInstruction* previous = got->GetPrevious();
2776 HLoopInformation* info = block->GetLoopInformation();
2777
David Brazdil46e2a392015-03-16 17:31:52 +00002778 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002779 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2780 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2781 return;
2782 }
2783 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2784 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2785 }
2786 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002787 __ B(codegen_->GetLabelOf(successor));
2788 }
2789}
2790
David Brazdilfc6a86a2015-06-26 10:33:45 +00002791void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2792 got->SetLocations(nullptr);
2793}
2794
2795void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2796 HandleGoto(got, got->GetSuccessor());
2797}
2798
2799void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2800 try_boundary->SetLocations(nullptr);
2801}
2802
2803void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2804 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2805 if (!successor->IsExitBlock()) {
2806 HandleGoto(try_boundary, successor);
2807 }
2808}
2809
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002810void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002811 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002812 vixl::Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002813 vixl::Label* false_target) {
2814 // FP branching requires both targets to be explicit. If either of the targets
2815 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2816 vixl::Label fallthrough_target;
2817 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002818
David Brazdil0debae72015-11-12 18:37:00 +00002819 if (true_target == nullptr && false_target == nullptr) {
2820 // Nothing to do. The code always falls through.
2821 return;
2822 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002823 // Constant condition, statically compared against "true" (integer value 1).
2824 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002825 if (true_target != nullptr) {
2826 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002827 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002828 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002829 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002830 if (false_target != nullptr) {
2831 __ B(false_target);
2832 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002833 }
David Brazdil0debae72015-11-12 18:37:00 +00002834 return;
2835 }
2836
2837 // The following code generates these patterns:
2838 // (1) true_target == nullptr && false_target != nullptr
2839 // - opposite condition true => branch to false_target
2840 // (2) true_target != nullptr && false_target == nullptr
2841 // - condition true => branch to true_target
2842 // (3) true_target != nullptr && false_target != nullptr
2843 // - condition true => branch to true_target
2844 // - branch to false_target
2845 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002846 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002847 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002848 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002849 if (true_target == nullptr) {
2850 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2851 } else {
2852 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2853 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002854 } else {
2855 // The condition instruction has not been materialized, use its inputs as
2856 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002857 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002858
David Brazdil0debae72015-11-12 18:37:00 +00002859 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002860 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002861 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002862 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002863 IfCondition opposite_condition = condition->GetOppositeCondition();
2864 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002865 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002866 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002867 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002868 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002869 // Integer cases.
2870 Register lhs = InputRegisterAt(condition, 0);
2871 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002872
2873 Condition arm64_cond;
2874 vixl::Label* non_fallthrough_target;
2875 if (true_target == nullptr) {
2876 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2877 non_fallthrough_target = false_target;
2878 } else {
2879 arm64_cond = ARM64Condition(condition->GetCondition());
2880 non_fallthrough_target = true_target;
2881 }
2882
Aart Bik086d27e2016-01-20 17:02:00 -08002883 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
2884 rhs.IsImmediate() && (rhs.immediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002885 switch (arm64_cond) {
2886 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002887 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002888 break;
2889 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002890 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002891 break;
2892 case lt:
2893 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002894 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002895 break;
2896 case ge:
2897 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002898 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002899 break;
2900 default:
2901 // Without the `static_cast` the compiler throws an error for
2902 // `-Werror=sign-promo`.
2903 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2904 }
2905 } else {
2906 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002907 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002908 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002909 }
2910 }
David Brazdil0debae72015-11-12 18:37:00 +00002911
2912 // If neither branch falls through (case 3), the conditional branch to `true_target`
2913 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2914 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002915 __ B(false_target);
2916 }
David Brazdil0debae72015-11-12 18:37:00 +00002917
2918 if (fallthrough_target.IsLinked()) {
2919 __ Bind(&fallthrough_target);
2920 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002921}
2922
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002923void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2924 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002925 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002926 locations->SetInAt(0, Location::RequiresRegister());
2927 }
2928}
2929
2930void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002931 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2932 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2933 vixl::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2934 nullptr : codegen_->GetLabelOf(true_successor);
2935 vixl::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2936 nullptr : codegen_->GetLabelOf(false_successor);
2937 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002938}
2939
2940void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2941 LocationSummary* locations = new (GetGraph()->GetArena())
2942 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002943 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002944 locations->SetInAt(0, Location::RequiresRegister());
2945 }
2946}
2947
2948void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002949 SlowPathCodeARM64* slow_path =
2950 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002951 GenerateTestAndBranch(deoptimize,
2952 /* condition_input_index */ 0,
2953 slow_path->GetEntryLabel(),
2954 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002955}
2956
David Brazdilc0b601b2016-02-08 14:20:45 +00002957static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
2958 return condition->IsCondition() &&
2959 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
2960}
2961
Alexandre Rames880f1192016-06-13 16:04:50 +01002962static inline Condition GetConditionForSelect(HCondition* condition) {
2963 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00002964 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
2965 : ARM64Condition(cond);
2966}
2967
David Brazdil74eb1b22015-12-14 11:44:01 +00002968void LocationsBuilderARM64::VisitSelect(HSelect* select) {
2969 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01002970 if (Primitive::IsFloatingPointType(select->GetType())) {
2971 locations->SetInAt(0, Location::RequiresFpuRegister());
2972 locations->SetInAt(1, Location::RequiresFpuRegister());
2973 locations->SetOut(Location::RequiresFpuRegister());
2974 } else {
2975 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
2976 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
2977 bool is_true_value_constant = cst_true_value != nullptr;
2978 bool is_false_value_constant = cst_false_value != nullptr;
2979 // Ask VIXL whether we should synthesize constants in registers.
2980 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
2981 Operand true_op = is_true_value_constant ?
2982 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
2983 Operand false_op = is_false_value_constant ?
2984 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
2985 bool true_value_in_register = false;
2986 bool false_value_in_register = false;
2987 MacroAssembler::GetCselSynthesisInformation(
2988 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
2989 true_value_in_register |= !is_true_value_constant;
2990 false_value_in_register |= !is_false_value_constant;
2991
2992 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
2993 : Location::ConstantLocation(cst_true_value));
2994 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
2995 : Location::ConstantLocation(cst_false_value));
2996 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00002997 }
Alexandre Rames880f1192016-06-13 16:04:50 +01002998
David Brazdil74eb1b22015-12-14 11:44:01 +00002999 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3000 locations->SetInAt(2, Location::RequiresRegister());
3001 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003002}
3003
3004void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003005 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003006 Condition csel_cond;
3007
3008 if (IsBooleanValueOrMaterializedCondition(cond)) {
3009 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003010 // Use the condition flags set by the previous instruction.
3011 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003012 } else {
3013 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003014 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003015 }
3016 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003017 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003018 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003019 } else {
3020 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003021 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003022 }
3023
Alexandre Rames880f1192016-06-13 16:04:50 +01003024 if (Primitive::IsFloatingPointType(select->GetType())) {
3025 __ Fcsel(OutputFPRegister(select),
3026 InputFPRegisterAt(select, 1),
3027 InputFPRegisterAt(select, 0),
3028 csel_cond);
3029 } else {
3030 __ Csel(OutputRegister(select),
3031 InputOperandAt(select, 1),
3032 InputOperandAt(select, 0),
3033 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003034 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003035}
3036
David Srbecky0cf44932015-12-09 14:09:59 +00003037void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3038 new (GetGraph()->GetArena()) LocationSummary(info);
3039}
3040
David Srbeckyd28f4a02016-03-14 17:14:24 +00003041void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3042 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003043}
3044
3045void CodeGeneratorARM64::GenerateNop() {
3046 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003047}
3048
Alexandre Rames5319def2014-10-23 10:03:10 +01003049void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003050 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003051}
3052
3053void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003054 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003055}
3056
3057void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003058 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003059}
3060
3061void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003062 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003063}
3064
Roland Levillain44015862016-01-22 11:47:17 +00003065static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3066 return kEmitCompilerReadBarrier &&
3067 (kUseBakerReadBarrier ||
3068 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3069 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3070 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3071}
3072
Alexandre Rames67555f72014-11-18 10:55:16 +00003073void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003074 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003075 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3076 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003077 case TypeCheckKind::kExactCheck:
3078 case TypeCheckKind::kAbstractClassCheck:
3079 case TypeCheckKind::kClassHierarchyCheck:
3080 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003081 call_kind =
3082 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003083 break;
3084 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003085 case TypeCheckKind::kUnresolvedCheck:
3086 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003087 call_kind = LocationSummary::kCallOnSlowPath;
3088 break;
3089 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003090
Alexandre Rames67555f72014-11-18 10:55:16 +00003091 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003092 locations->SetInAt(0, Location::RequiresRegister());
3093 locations->SetInAt(1, Location::RequiresRegister());
3094 // The "out" register is used as a temporary, so it overlaps with the inputs.
3095 // Note that TypeCheckSlowPathARM64 uses this register too.
3096 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3097 // When read barriers are enabled, we need a temporary register for
3098 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003099 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003100 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003101 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003102}
3103
3104void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003105 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003106 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003107 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003108 Register obj = InputRegisterAt(instruction, 0);
3109 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003110 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003111 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003112 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3113 locations->GetTemp(0) :
3114 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003115 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3116 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3117 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3118 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003119
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003120 vixl::Label done, zero;
3121 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003122
3123 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003124 // Avoid null check if we know `obj` is not null.
3125 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003126 __ Cbz(obj, &zero);
3127 }
3128
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003129 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003130 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003131
Roland Levillain44015862016-01-22 11:47:17 +00003132 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003133 case TypeCheckKind::kExactCheck: {
3134 __ Cmp(out, cls);
3135 __ Cset(out, eq);
3136 if (zero.IsLinked()) {
3137 __ B(&done);
3138 }
3139 break;
3140 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003141
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003142 case TypeCheckKind::kAbstractClassCheck: {
3143 // If the class is abstract, we eagerly fetch the super class of the
3144 // object to avoid doing a comparison we know will fail.
3145 vixl::Label loop, success;
3146 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003147 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003148 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003149 // If `out` is null, we use it for the result, and jump to `done`.
3150 __ Cbz(out, &done);
3151 __ Cmp(out, cls);
3152 __ B(ne, &loop);
3153 __ Mov(out, 1);
3154 if (zero.IsLinked()) {
3155 __ B(&done);
3156 }
3157 break;
3158 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003159
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003160 case TypeCheckKind::kClassHierarchyCheck: {
3161 // Walk over the class hierarchy to find a match.
3162 vixl::Label loop, success;
3163 __ Bind(&loop);
3164 __ Cmp(out, cls);
3165 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003166 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003167 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003168 __ Cbnz(out, &loop);
3169 // If `out` is null, we use it for the result, and jump to `done`.
3170 __ B(&done);
3171 __ Bind(&success);
3172 __ Mov(out, 1);
3173 if (zero.IsLinked()) {
3174 __ B(&done);
3175 }
3176 break;
3177 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003178
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003179 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003180 // Do an exact check.
3181 vixl::Label exact_check;
3182 __ Cmp(out, cls);
3183 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003184 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003185 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003186 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003187 // If `out` is null, we use it for the result, and jump to `done`.
3188 __ Cbz(out, &done);
3189 __ Ldrh(out, HeapOperand(out, primitive_offset));
3190 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3191 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003192 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003193 __ Mov(out, 1);
3194 __ B(&done);
3195 break;
3196 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003197
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003198 case TypeCheckKind::kArrayCheck: {
3199 __ Cmp(out, cls);
3200 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003201 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3202 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003203 codegen_->AddSlowPath(slow_path);
3204 __ B(ne, slow_path->GetEntryLabel());
3205 __ Mov(out, 1);
3206 if (zero.IsLinked()) {
3207 __ B(&done);
3208 }
3209 break;
3210 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003211
Calin Juravle98893e12015-10-02 21:05:03 +01003212 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003213 case TypeCheckKind::kInterfaceCheck: {
3214 // Note that we indeed only call on slow path, but we always go
3215 // into the slow path for the unresolved and interface check
3216 // cases.
3217 //
3218 // We cannot directly call the InstanceofNonTrivial runtime
3219 // entry point without resorting to a type checking slow path
3220 // here (i.e. by calling InvokeRuntime directly), as it would
3221 // require to assign fixed registers for the inputs of this
3222 // HInstanceOf instruction (following the runtime calling
3223 // convention), which might be cluttered by the potential first
3224 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003225 //
3226 // TODO: Introduce a new runtime entry point taking the object
3227 // to test (instead of its class) as argument, and let it deal
3228 // with the read barrier issues. This will let us refactor this
3229 // case of the `switch` code as it was previously (with a direct
3230 // call to the runtime not using a type checking slow path).
3231 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003232 DCHECK(locations->OnlyCallsOnSlowPath());
3233 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3234 /* is_fatal */ false);
3235 codegen_->AddSlowPath(slow_path);
3236 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003237 if (zero.IsLinked()) {
3238 __ B(&done);
3239 }
3240 break;
3241 }
3242 }
3243
3244 if (zero.IsLinked()) {
3245 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003246 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003247 }
3248
3249 if (done.IsLinked()) {
3250 __ Bind(&done);
3251 }
3252
3253 if (slow_path != nullptr) {
3254 __ Bind(slow_path->GetExitLabel());
3255 }
3256}
3257
3258void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3259 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3260 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3261
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003262 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3263 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003264 case TypeCheckKind::kExactCheck:
3265 case TypeCheckKind::kAbstractClassCheck:
3266 case TypeCheckKind::kClassHierarchyCheck:
3267 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003268 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3269 LocationSummary::kCallOnSlowPath :
3270 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003271 break;
3272 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003273 case TypeCheckKind::kUnresolvedCheck:
3274 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003275 call_kind = LocationSummary::kCallOnSlowPath;
3276 break;
3277 }
3278
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003279 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3280 locations->SetInAt(0, Location::RequiresRegister());
3281 locations->SetInAt(1, Location::RequiresRegister());
3282 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3283 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003284 // When read barriers are enabled, we need an additional temporary
3285 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003286 if (TypeCheckNeedsATemporary(type_check_kind)) {
3287 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003288 }
3289}
3290
3291void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003292 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003293 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003294 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003295 Register obj = InputRegisterAt(instruction, 0);
3296 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003297 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003298 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3299 locations->GetTemp(1) :
3300 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003301 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003302 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3303 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3304 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3305 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003306
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003307 bool is_type_check_slow_path_fatal =
3308 (type_check_kind == TypeCheckKind::kExactCheck ||
3309 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3310 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3311 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3312 !instruction->CanThrowIntoCatchBlock();
3313 SlowPathCodeARM64* type_check_slow_path =
3314 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3315 is_type_check_slow_path_fatal);
3316 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003317
3318 vixl::Label done;
3319 // Avoid null check if we know obj is not null.
3320 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003321 __ Cbz(obj, &done);
3322 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003323
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003324 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003325 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003326
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003327 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003328 case TypeCheckKind::kExactCheck:
3329 case TypeCheckKind::kArrayCheck: {
3330 __ Cmp(temp, cls);
3331 // Jump to slow path for throwing the exception or doing a
3332 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003333 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003334 break;
3335 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003336
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003337 case TypeCheckKind::kAbstractClassCheck: {
3338 // If the class is abstract, we eagerly fetch the super class of the
3339 // object to avoid doing a comparison we know will fail.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003340 vixl::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003341 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003342 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003343 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003344
3345 // If the class reference currently in `temp` is not null, jump
3346 // to the `compare_classes` label to compare it with the checked
3347 // class.
3348 __ Cbnz(temp, &compare_classes);
3349 // Otherwise, jump to the slow path to throw the exception.
3350 //
3351 // But before, move back the object's class into `temp` before
3352 // going into the slow path, as it has been overwritten in the
3353 // meantime.
3354 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003355 GenerateReferenceLoadTwoRegisters(
3356 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003357 __ B(type_check_slow_path->GetEntryLabel());
3358
3359 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003360 __ Cmp(temp, cls);
3361 __ B(ne, &loop);
3362 break;
3363 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003364
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003365 case TypeCheckKind::kClassHierarchyCheck: {
3366 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003367 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003368 __ Bind(&loop);
3369 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003370 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003371
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003372 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003373 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003374
3375 // If the class reference currently in `temp` is not null, jump
3376 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003377 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003378 // Otherwise, jump to the slow path to throw the exception.
3379 //
3380 // But before, move back the object's class into `temp` before
3381 // going into the slow path, as it has been overwritten in the
3382 // meantime.
3383 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003384 GenerateReferenceLoadTwoRegisters(
3385 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003386 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003387 break;
3388 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003389
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003390 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003391 // Do an exact check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003392 vixl::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003393 __ Cmp(temp, cls);
3394 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003395
3396 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003397 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003398 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003399
3400 // If the component type is not null (i.e. the object is indeed
3401 // an array), jump to label `check_non_primitive_component_type`
3402 // to further check that this component type is not a primitive
3403 // type.
3404 __ Cbnz(temp, &check_non_primitive_component_type);
3405 // Otherwise, jump to the slow path to throw the exception.
3406 //
3407 // But before, move back the object's class into `temp` before
3408 // going into the slow path, as it has been overwritten in the
3409 // meantime.
3410 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003411 GenerateReferenceLoadTwoRegisters(
3412 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003413 __ B(type_check_slow_path->GetEntryLabel());
3414
3415 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003416 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3417 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003418 __ Cbz(temp, &done);
3419 // Same comment as above regarding `temp` and the slow path.
3420 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003421 GenerateReferenceLoadTwoRegisters(
3422 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003423 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003424 break;
3425 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003426
Calin Juravle98893e12015-10-02 21:05:03 +01003427 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003428 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003429 // We always go into the type check slow path for the unresolved
3430 // and interface check cases.
3431 //
3432 // We cannot directly call the CheckCast runtime entry point
3433 // without resorting to a type checking slow path here (i.e. by
3434 // calling InvokeRuntime directly), as it would require to
3435 // assign fixed registers for the inputs of this HInstanceOf
3436 // instruction (following the runtime calling convention), which
3437 // might be cluttered by the potential first read barrier
3438 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003439 //
3440 // TODO: Introduce a new runtime entry point taking the object
3441 // to test (instead of its class) as argument, and let it deal
3442 // with the read barrier issues. This will let us refactor this
3443 // case of the `switch` code as it was previously (with a direct
3444 // call to the runtime not using a type checking slow path).
3445 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003446 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003447 break;
3448 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003449 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003450
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003451 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003452}
3453
Alexandre Rames5319def2014-10-23 10:03:10 +01003454void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3455 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3456 locations->SetOut(Location::ConstantLocation(constant));
3457}
3458
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003459void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003460 // Will be generated at use site.
3461}
3462
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003463void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3464 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3465 locations->SetOut(Location::ConstantLocation(constant));
3466}
3467
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003468void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003469 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003470}
3471
Calin Juravle175dc732015-08-25 15:42:32 +01003472void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3473 // The trampoline uses the same calling convention as dex calling conventions,
3474 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3475 // the method_idx.
3476 HandleInvoke(invoke);
3477}
3478
3479void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3480 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3481}
3482
Alexandre Rames5319def2014-10-23 10:03:10 +01003483void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003484 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003485 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003486}
3487
Alexandre Rames67555f72014-11-18 10:55:16 +00003488void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3489 HandleInvoke(invoke);
3490}
3491
3492void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3493 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003494 LocationSummary* locations = invoke->GetLocations();
3495 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003496 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003497 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003498 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003499
3500 // The register ip1 is required to be used for the hidden argument in
3501 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003502 MacroAssembler* masm = GetVIXLAssembler();
3503 UseScratchRegisterScope scratch_scope(masm);
3504 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003505 scratch_scope.Exclude(ip1);
3506 __ Mov(ip1, invoke->GetDexMethodIndex());
3507
Alexandre Rames67555f72014-11-18 10:55:16 +00003508 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003509 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003510 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003511 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003512 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003513 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003514 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003515 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003516 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003517 // Instead of simply (possibly) unpoisoning `temp` here, we should
3518 // emit a read barrier for the previous class reference load.
3519 // However this is not required in practice, as this is an
3520 // intermediate/temporary reference and because the current
3521 // concurrent copying collector keeps the from-space memory
3522 // intact/accessible until the end of the marking phase (the
3523 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003524 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nelli Kimbadee982016-05-13 13:08:53 +03003525 __ Ldr(temp,
3526 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3527 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity50706432016-06-14 11:31:04 -07003528 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003529 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003530 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003531 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003532 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003533 // lr();
3534 __ Blr(lr);
3535 DCHECK(!codegen_->IsLeafMethod());
3536 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3537}
3538
3539void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003540 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3541 if (intrinsic.TryDispatch(invoke)) {
3542 return;
3543 }
3544
Alexandre Rames67555f72014-11-18 10:55:16 +00003545 HandleInvoke(invoke);
3546}
3547
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003548void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003549 // Explicit clinit checks triggered by static invokes must have been pruned by
3550 // art::PrepareForRegisterAllocation.
3551 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003552
Andreas Gampe878d58c2015-01-15 23:24:00 -08003553 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3554 if (intrinsic.TryDispatch(invoke)) {
3555 return;
3556 }
3557
Alexandre Rames67555f72014-11-18 10:55:16 +00003558 HandleInvoke(invoke);
3559}
3560
Andreas Gampe878d58c2015-01-15 23:24:00 -08003561static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3562 if (invoke->GetLocations()->Intrinsified()) {
3563 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3564 intrinsic.Dispatch(invoke);
3565 return true;
3566 }
3567 return false;
3568}
3569
Vladimir Markodc151b22015-10-15 18:02:30 +01003570HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3571 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3572 MethodReference target_method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003573 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003574 return desired_dispatch_info;
3575}
3576
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003577void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003578 // For better instruction scheduling we load the direct code pointer before the method pointer.
3579 bool direct_code_loaded = false;
3580 switch (invoke->GetCodePtrLocation()) {
3581 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3582 // LR = code address from literal pool with link-time patch.
3583 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3584 direct_code_loaded = true;
3585 break;
3586 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3587 // LR = invoke->GetDirectCodePtr();
3588 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3589 direct_code_loaded = true;
3590 break;
3591 default:
3592 break;
3593 }
3594
Andreas Gampe878d58c2015-01-15 23:24:00 -08003595 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003596 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3597 switch (invoke->GetMethodLoadKind()) {
3598 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3599 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003600 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003601 break;
3602 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003603 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003604 break;
3605 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3606 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003607 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003608 break;
3609 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3610 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003611 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003612 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3613 break;
3614 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3615 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003616 const DexFile& dex_file = *invoke->GetTargetMethod().dex_file;
3617 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
3618 vixl::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003619 {
3620 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003621 __ Bind(adrp_label);
3622 __ adrp(XRegisterFrom(temp), /* offset placeholder */ 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003623 }
Vladimir Marko58155012015-08-19 12:49:41 +00003624 // Add LDR with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003625 vixl::Label* ldr_label =
3626 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Alexandre Rames6dc01742015-11-12 14:44:19 +00003627 {
3628 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003629 __ Bind(ldr_label);
3630 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), /* offset placeholder */ 0));
Alexandre Rames6dc01742015-11-12 14:44:19 +00003631 }
Vladimir Marko58155012015-08-19 12:49:41 +00003632 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003633 }
Vladimir Marko58155012015-08-19 12:49:41 +00003634 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003635 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003636 Register reg = XRegisterFrom(temp);
3637 Register method_reg;
3638 if (current_method.IsRegister()) {
3639 method_reg = XRegisterFrom(current_method);
3640 } else {
3641 DCHECK(invoke->GetLocations()->Intrinsified());
3642 DCHECK(!current_method.IsValid());
3643 method_reg = reg;
3644 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3645 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003646
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003647 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003648 __ Ldr(reg.X(),
3649 MemOperand(method_reg.X(),
3650 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003651 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003652 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3653 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003654 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3655 break;
3656 }
3657 }
3658
3659 switch (invoke->GetCodePtrLocation()) {
3660 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3661 __ Bl(&frame_entry_label_);
3662 break;
3663 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3664 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3665 vixl::Label* label = &relative_call_patches_.back().label;
Alexandre Rames6dc01742015-11-12 14:44:19 +00003666 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3667 __ Bind(label);
3668 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003669 break;
3670 }
3671 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3672 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3673 // LR prepared above for better instruction scheduling.
3674 DCHECK(direct_code_loaded);
3675 // lr()
3676 __ Blr(lr);
3677 break;
3678 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3679 // LR = callee_method->entry_point_from_quick_compiled_code_;
3680 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003681 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003682 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3683 // lr()
3684 __ Blr(lr);
3685 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003686 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003687
Andreas Gampe878d58c2015-01-15 23:24:00 -08003688 DCHECK(!IsLeafMethod());
3689}
3690
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003691void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003692 // Use the calling convention instead of the location of the receiver, as
3693 // intrinsics may have put the receiver in a different register. In the intrinsics
3694 // slow path, the arguments have been moved to the right place, so here we are
3695 // guaranteed that the receiver is the first register of the calling convention.
3696 InvokeDexCallingConvention calling_convention;
3697 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003698 Register temp = XRegisterFrom(temp_in);
3699 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3700 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3701 Offset class_offset = mirror::Object::ClassOffset();
3702 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3703
3704 BlockPoolsScope block_pools(GetVIXLAssembler());
3705
3706 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003707 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003708 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003709 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003710 // Instead of simply (possibly) unpoisoning `temp` here, we should
3711 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003712 // intermediate/temporary reference and because the current
3713 // concurrent copying collector keeps the from-space memory
3714 // intact/accessible until the end of the marking phase (the
3715 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003716 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3717 // temp = temp->GetMethodAt(method_offset);
3718 __ Ldr(temp, MemOperand(temp, method_offset));
3719 // lr = temp->GetEntryPoint();
3720 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3721 // lr();
3722 __ Blr(lr);
3723}
3724
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003725vixl::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(const DexFile& dex_file,
3726 uint32_t string_index,
3727 vixl::Label* adrp_label) {
3728 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3729}
3730
3731vixl::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
3732 uint32_t element_offset,
3733 vixl::Label* adrp_label) {
3734 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3735}
3736
3737vixl::Label* CodeGeneratorARM64::NewPcRelativePatch(const DexFile& dex_file,
3738 uint32_t offset_or_index,
3739 vixl::Label* adrp_label,
3740 ArenaDeque<PcRelativePatchInfo>* patches) {
3741 // Add a patch entry and return the label.
3742 patches->emplace_back(dex_file, offset_or_index);
3743 PcRelativePatchInfo* info = &patches->back();
3744 vixl::Label* label = &info->label;
3745 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3746 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3747 return label;
3748}
3749
3750vixl::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
3751 const DexFile& dex_file, uint32_t string_index) {
3752 return boot_image_string_patches_.GetOrCreate(
3753 StringReference(&dex_file, string_index),
3754 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3755}
3756
3757vixl::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(uint64_t address) {
3758 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3759 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3760 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3761}
3762
3763vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(uint64_t address) {
3764 return DeduplicateUint64Literal(address);
3765}
3766
Vladimir Marko58155012015-08-19 12:49:41 +00003767void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3768 DCHECK(linker_patches->empty());
3769 size_t size =
3770 method_patches_.size() +
3771 call_patches_.size() +
3772 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003773 pc_relative_dex_cache_patches_.size() +
3774 boot_image_string_patches_.size() +
3775 pc_relative_string_patches_.size() +
3776 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003777 linker_patches->reserve(size);
3778 for (const auto& entry : method_patches_) {
3779 const MethodReference& target_method = entry.first;
3780 vixl::Literal<uint64_t>* literal = entry.second;
3781 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
3782 target_method.dex_file,
3783 target_method.dex_method_index));
3784 }
3785 for (const auto& entry : call_patches_) {
3786 const MethodReference& target_method = entry.first;
3787 vixl::Literal<uint64_t>* literal = entry.second;
3788 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
3789 target_method.dex_file,
3790 target_method.dex_method_index));
3791 }
3792 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003793 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003794 info.target_method.dex_file,
3795 info.target_method.dex_method_index));
3796 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003797 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003798 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003799 &info.target_dex_file,
Alexandre Rames6dc01742015-11-12 14:44:19 +00003800 info.pc_insn_label->location(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003801 info.offset_or_index));
3802 }
3803 for (const auto& entry : boot_image_string_patches_) {
3804 const StringReference& target_string = entry.first;
3805 vixl::Literal<uint32_t>* literal = entry.second;
3806 linker_patches->push_back(LinkerPatch::StringPatch(literal->offset(),
3807 target_string.dex_file,
3808 target_string.string_index));
3809 }
3810 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
3811 linker_patches->push_back(LinkerPatch::RelativeStringPatch(info.label.location(),
3812 &info.target_dex_file,
3813 info.pc_insn_label->location(),
3814 info.offset_or_index));
3815 }
3816 for (const auto& entry : boot_image_address_patches_) {
3817 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
3818 vixl::Literal<uint32_t>* literal = entry.second;
3819 linker_patches->push_back(LinkerPatch::RecordPosition(literal->offset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003820 }
3821}
3822
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003823vixl::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
3824 Uint32ToLiteralMap* map) {
3825 return map->GetOrCreate(
3826 value,
3827 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3828}
3829
Vladimir Marko58155012015-08-19 12:49:41 +00003830vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003831 return uint64_literals_.GetOrCreate(
3832 value,
3833 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003834}
3835
3836vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
3837 MethodReference target_method,
3838 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003839 return map->GetOrCreate(
3840 target_method,
3841 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003842}
3843
3844vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
3845 MethodReference target_method) {
3846 return DeduplicateMethodLiteral(target_method, &method_patches_);
3847}
3848
3849vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
3850 MethodReference target_method) {
3851 return DeduplicateMethodLiteral(target_method, &call_patches_);
3852}
3853
3854
Andreas Gampe878d58c2015-01-15 23:24:00 -08003855void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003856 // Explicit clinit checks triggered by static invokes must have been pruned by
3857 // art::PrepareForRegisterAllocation.
3858 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003859
Andreas Gampe878d58c2015-01-15 23:24:00 -08003860 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3861 return;
3862 }
3863
Alexandre Ramesd921d642015-04-16 15:07:16 +01003864 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003865 LocationSummary* locations = invoke->GetLocations();
3866 codegen_->GenerateStaticOrDirectCall(
3867 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003868 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003869}
3870
3871void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003872 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3873 return;
3874 }
3875
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003876 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003877 DCHECK(!codegen_->IsLeafMethod());
3878 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3879}
3880
Alexandre Rames67555f72014-11-18 10:55:16 +00003881void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003882 InvokeRuntimeCallingConvention calling_convention;
3883 CodeGenerator::CreateLoadClassLocationSummary(
3884 cls,
3885 LocationFrom(calling_convention.GetRegisterAt(0)),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003886 LocationFrom(vixl::x0),
3887 /* code_generator_supports_read_barrier */ true);
Alexandre Rames67555f72014-11-18 10:55:16 +00003888}
3889
3890void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003891 if (cls->NeedsAccessCheck()) {
3892 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3893 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3894 cls,
3895 cls->GetDexPc(),
3896 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003897 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003898 return;
3899 }
3900
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003901 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01003902 Register out = OutputRegister(cls);
3903 Register current_method = InputRegisterAt(cls, 0);
3904 if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003905 DCHECK(!cls->CanCallRuntime());
3906 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain44015862016-01-22 11:47:17 +00003907 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3908 GenerateGcRootFieldLoad(
3909 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Alexandre Rames67555f72014-11-18 10:55:16 +00003910 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003911 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003912 // /* GcRoot<mirror::Class>[] */ out =
3913 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Vladimir Marko05792b92015-08-03 11:56:49 +01003914 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00003915 // /* GcRoot<mirror::Class> */ out = out[type_index]
3916 GenerateGcRootFieldLoad(
3917 cls, out_loc, out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003918
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003919 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3920 DCHECK(cls->CanCallRuntime());
3921 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3922 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3923 codegen_->AddSlowPath(slow_path);
3924 if (!cls->IsInDexCache()) {
3925 __ Cbz(out, slow_path->GetEntryLabel());
3926 }
3927 if (cls->MustGenerateClinitCheck()) {
3928 GenerateClassInitializationCheck(slow_path, out);
3929 } else {
3930 __ Bind(slow_path->GetExitLabel());
3931 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003932 }
3933 }
3934}
3935
David Brazdilcb1c0552015-08-04 16:22:25 +01003936static MemOperand GetExceptionTlsAddress() {
3937 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3938}
3939
Alexandre Rames67555f72014-11-18 10:55:16 +00003940void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3941 LocationSummary* locations =
3942 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3943 locations->SetOut(Location::RequiresRegister());
3944}
3945
3946void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003947 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3948}
3949
3950void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3951 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3952}
3953
3954void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3955 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003956}
3957
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003958HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
3959 HLoadString::LoadKind desired_string_load_kind) {
3960 if (kEmitCompilerReadBarrier) {
3961 switch (desired_string_load_kind) {
3962 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3963 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
3964 case HLoadString::LoadKind::kBootImageAddress:
3965 // TODO: Implement for read barrier.
3966 return HLoadString::LoadKind::kDexCacheViaMethod;
3967 default:
3968 break;
3969 }
3970 }
3971 switch (desired_string_load_kind) {
3972 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3973 DCHECK(!GetCompilerOptions().GetCompilePic());
3974 break;
3975 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
3976 DCHECK(GetCompilerOptions().GetCompilePic());
3977 break;
3978 case HLoadString::LoadKind::kBootImageAddress:
3979 break;
3980 case HLoadString::LoadKind::kDexCacheAddress:
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::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01003984 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003985 break;
3986 case HLoadString::LoadKind::kDexCacheViaMethod:
3987 break;
3988 }
3989 return desired_string_load_kind;
3990}
3991
Alexandre Rames67555f72014-11-18 10:55:16 +00003992void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003993 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003994 ? LocationSummary::kCallOnSlowPath
3995 : LocationSummary::kNoCall;
3996 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003997 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
3998 locations->SetInAt(0, Location::RequiresRegister());
3999 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004000 locations->SetOut(Location::RequiresRegister());
4001}
4002
4003void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004004 Location out_loc = load->GetLocations()->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00004005 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004006
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004007 switch (load->GetLoadKind()) {
4008 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4009 DCHECK(!kEmitCompilerReadBarrier);
4010 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4011 load->GetStringIndex()));
4012 return; // No dex cache slow path.
4013 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4014 DCHECK(!kEmitCompilerReadBarrier);
4015 // Add ADRP with its PC-relative String patch.
4016 const DexFile& dex_file = load->GetDexFile();
4017 uint32_t string_index = load->GetStringIndex();
4018 vixl::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
4019 {
4020 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4021 __ Bind(adrp_label);
4022 __ adrp(out.X(), /* offset placeholder */ 0);
4023 }
4024 // Add ADD with its PC-relative String patch.
4025 vixl::Label* add_label =
4026 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4027 {
4028 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4029 __ Bind(add_label);
4030 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
4031 }
4032 return; // No dex cache slow path.
4033 }
4034 case HLoadString::LoadKind::kBootImageAddress: {
4035 DCHECK(!kEmitCompilerReadBarrier);
4036 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4037 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4038 return; // No dex cache slow path.
4039 }
4040 case HLoadString::LoadKind::kDexCacheAddress: {
4041 DCHECK_NE(load->GetAddress(), 0u);
4042 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4043 // that gives a 16KiB range. To try and reduce the number of literals if we load
4044 // multiple strings, simply split the dex cache address to a 16KiB aligned base
4045 // loaded from a literal and the remaining offset embedded in the load.
4046 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
4047 DCHECK_ALIGNED(load->GetAddress(), 4u);
4048 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4049 uint64_t base_address = load->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4050 uint32_t offset = load->GetAddress() & MaxInt<uint64_t>(offset_bits);
4051 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4052 GenerateGcRootFieldLoad(load, out_loc, out.X(), offset);
4053 break;
4054 }
4055 case HLoadString::LoadKind::kDexCachePcRelative: {
4056 // Add ADRP with its PC-relative DexCache access patch.
4057 const DexFile& dex_file = load->GetDexFile();
4058 uint32_t element_offset = load->GetDexCacheElementOffset();
4059 vixl::Label* adrp_label = codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
4060 {
4061 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4062 __ Bind(adrp_label);
4063 __ adrp(out.X(), /* offset placeholder */ 0);
4064 }
4065 // Add LDR with its PC-relative DexCache access patch.
4066 vixl::Label* ldr_label =
4067 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4068 GenerateGcRootFieldLoad(load, out_loc, out.X(), /* offset placeholder */ 0, ldr_label);
4069 break;
4070 }
4071 case HLoadString::LoadKind::kDexCacheViaMethod: {
4072 Register current_method = InputRegisterAt(load, 0);
4073 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4074 GenerateGcRootFieldLoad(
4075 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4076 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
4077 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
4078 // /* GcRoot<mirror::String> */ out = out[string_index]
4079 GenerateGcRootFieldLoad(
4080 load, out_loc, out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex()));
4081 break;
4082 }
4083 default:
4084 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
4085 UNREACHABLE();
4086 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004087
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004088 if (!load->IsInDexCache()) {
4089 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
4090 codegen_->AddSlowPath(slow_path);
4091 __ Cbz(out, slow_path->GetEntryLabel());
4092 __ Bind(slow_path->GetExitLabel());
4093 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004094}
4095
Alexandre Rames5319def2014-10-23 10:03:10 +01004096void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4097 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4098 locations->SetOut(Location::ConstantLocation(constant));
4099}
4100
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004101void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004102 // Will be generated at use site.
4103}
4104
Alexandre Rames67555f72014-11-18 10:55:16 +00004105void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4106 LocationSummary* locations =
4107 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4108 InvokeRuntimeCallingConvention calling_convention;
4109 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4110}
4111
4112void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4113 codegen_->InvokeRuntime(instruction->IsEnter()
4114 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4115 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004116 instruction->GetDexPc(),
4117 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004118 if (instruction->IsEnter()) {
4119 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4120 } else {
4121 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4122 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004123}
4124
Alexandre Rames42d641b2014-10-27 14:00:51 +00004125void LocationsBuilderARM64::VisitMul(HMul* mul) {
4126 LocationSummary* locations =
4127 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4128 switch (mul->GetResultType()) {
4129 case Primitive::kPrimInt:
4130 case Primitive::kPrimLong:
4131 locations->SetInAt(0, Location::RequiresRegister());
4132 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004133 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004134 break;
4135
4136 case Primitive::kPrimFloat:
4137 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004138 locations->SetInAt(0, Location::RequiresFpuRegister());
4139 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004140 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004141 break;
4142
4143 default:
4144 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4145 }
4146}
4147
4148void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4149 switch (mul->GetResultType()) {
4150 case Primitive::kPrimInt:
4151 case Primitive::kPrimLong:
4152 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4153 break;
4154
4155 case Primitive::kPrimFloat:
4156 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004157 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004158 break;
4159
4160 default:
4161 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4162 }
4163}
4164
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004165void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4166 LocationSummary* locations =
4167 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4168 switch (neg->GetResultType()) {
4169 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004170 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004171 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004172 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004173 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004174
4175 case Primitive::kPrimFloat:
4176 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004177 locations->SetInAt(0, Location::RequiresFpuRegister());
4178 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004179 break;
4180
4181 default:
4182 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4183 }
4184}
4185
4186void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4187 switch (neg->GetResultType()) {
4188 case Primitive::kPrimInt:
4189 case Primitive::kPrimLong:
4190 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4191 break;
4192
4193 case Primitive::kPrimFloat:
4194 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004195 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004196 break;
4197
4198 default:
4199 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4200 }
4201}
4202
4203void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4204 LocationSummary* locations =
4205 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4206 InvokeRuntimeCallingConvention calling_convention;
4207 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004208 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004209 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004210 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004211}
4212
4213void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4214 LocationSummary* locations = instruction->GetLocations();
4215 InvokeRuntimeCallingConvention calling_convention;
4216 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4217 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004218 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004219 // Note: if heap poisoning is enabled, the entry point takes cares
4220 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004221 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4222 instruction,
4223 instruction->GetDexPc(),
4224 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004225 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004226}
4227
Alexandre Rames5319def2014-10-23 10:03:10 +01004228void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4229 LocationSummary* locations =
4230 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4231 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004232 if (instruction->IsStringAlloc()) {
4233 locations->AddTemp(LocationFrom(kArtMethodRegister));
4234 } else {
4235 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4236 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4237 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004238 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4239}
4240
4241void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004242 // Note: if heap poisoning is enabled, the entry point takes cares
4243 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004244 if (instruction->IsStringAlloc()) {
4245 // String is allocated through StringFactory. Call NewEmptyString entry point.
4246 Location temp = instruction->GetLocations()->GetTemp(0);
4247 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
4248 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4249 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4250 __ Blr(lr);
4251 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4252 } else {
4253 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4254 instruction,
4255 instruction->GetDexPc(),
4256 nullptr);
4257 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4258 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004259}
4260
4261void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4262 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004263 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004264 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004265}
4266
4267void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004268 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004269 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004270 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004271 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004272 break;
4273
4274 default:
4275 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4276 }
4277}
4278
David Brazdil66d126e2015-04-03 16:02:44 +01004279void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4280 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4281 locations->SetInAt(0, Location::RequiresRegister());
4282 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4283}
4284
4285void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01004286 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
4287}
4288
Alexandre Rames5319def2014-10-23 10:03:10 +01004289void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004290 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4291 ? LocationSummary::kCallOnSlowPath
4292 : LocationSummary::kNoCall;
4293 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004294 locations->SetInAt(0, Location::RequiresRegister());
4295 if (instruction->HasUses()) {
4296 locations->SetOut(Location::SameAsFirstInput());
4297 }
4298}
4299
Calin Juravle2ae48182016-03-16 14:05:09 +00004300void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4301 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004302 return;
4303 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004304
Alexandre Ramesd921d642015-04-16 15:07:16 +01004305 BlockPoolsScope block_pools(GetVIXLAssembler());
4306 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004307 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004308 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004309}
4310
Calin Juravle2ae48182016-03-16 14:05:09 +00004311void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004312 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004313 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004314
4315 LocationSummary* locations = instruction->GetLocations();
4316 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004317
4318 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004319}
4320
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004321void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004322 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004323}
4324
Alexandre Rames67555f72014-11-18 10:55:16 +00004325void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4326 HandleBinaryOp(instruction);
4327}
4328
4329void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4330 HandleBinaryOp(instruction);
4331}
4332
Alexandre Rames3e69f162014-12-10 10:36:50 +00004333void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4334 LOG(FATAL) << "Unreachable";
4335}
4336
4337void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4338 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4339}
4340
Alexandre Rames5319def2014-10-23 10:03:10 +01004341void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4342 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4343 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4344 if (location.IsStackSlot()) {
4345 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4346 } else if (location.IsDoubleStackSlot()) {
4347 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4348 }
4349 locations->SetOut(location);
4350}
4351
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004352void InstructionCodeGeneratorARM64::VisitParameterValue(
4353 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004354 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004355}
4356
4357void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4358 LocationSummary* locations =
4359 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004360 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004361}
4362
4363void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4364 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4365 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004366}
4367
4368void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4369 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004370 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004371 locations->SetInAt(i, Location::Any());
4372 }
4373 locations->SetOut(Location::Any());
4374}
4375
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004376void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004377 LOG(FATAL) << "Unreachable";
4378}
4379
Serban Constantinescu02164b32014-11-13 14:05:07 +00004380void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004381 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004382 LocationSummary::CallKind call_kind =
4383 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004384 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4385
4386 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004387 case Primitive::kPrimInt:
4388 case Primitive::kPrimLong:
4389 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004390 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004391 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4392 break;
4393
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004394 case Primitive::kPrimFloat:
4395 case Primitive::kPrimDouble: {
4396 InvokeRuntimeCallingConvention calling_convention;
4397 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4398 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4399 locations->SetOut(calling_convention.GetReturnLocation(type));
4400
4401 break;
4402 }
4403
Serban Constantinescu02164b32014-11-13 14:05:07 +00004404 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004405 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004406 }
4407}
4408
4409void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4410 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004411
Serban Constantinescu02164b32014-11-13 14:05:07 +00004412 switch (type) {
4413 case Primitive::kPrimInt:
4414 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004415 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004416 break;
4417 }
4418
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004419 case Primitive::kPrimFloat:
4420 case Primitive::kPrimDouble: {
4421 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4422 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004423 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004424 if (type == Primitive::kPrimFloat) {
4425 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4426 } else {
4427 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4428 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004429 break;
4430 }
4431
Serban Constantinescu02164b32014-11-13 14:05:07 +00004432 default:
4433 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004434 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004435 }
4436}
4437
Calin Juravle27df7582015-04-17 19:12:31 +01004438void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4439 memory_barrier->SetLocations(nullptr);
4440}
4441
4442void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004443 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004444}
4445
Alexandre Rames5319def2014-10-23 10:03:10 +01004446void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4447 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4448 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004449 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004450}
4451
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004452void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004453 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004454}
4455
4456void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4457 instruction->SetLocations(nullptr);
4458}
4459
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004460void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004461 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004462}
4463
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004464void LocationsBuilderARM64::VisitRor(HRor* ror) {
4465 HandleBinaryOp(ror);
4466}
4467
4468void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4469 HandleBinaryOp(ror);
4470}
4471
Serban Constantinescu02164b32014-11-13 14:05:07 +00004472void LocationsBuilderARM64::VisitShl(HShl* shl) {
4473 HandleShift(shl);
4474}
4475
4476void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4477 HandleShift(shl);
4478}
4479
4480void LocationsBuilderARM64::VisitShr(HShr* shr) {
4481 HandleShift(shr);
4482}
4483
4484void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4485 HandleShift(shr);
4486}
4487
Alexandre Rames5319def2014-10-23 10:03:10 +01004488void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004489 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004490}
4491
4492void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004493 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004494}
4495
Alexandre Rames67555f72014-11-18 10:55:16 +00004496void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004497 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004498}
4499
4500void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004501 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004502}
4503
4504void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004505 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004506}
4507
Alexandre Rames67555f72014-11-18 10:55:16 +00004508void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004509 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004510}
4511
Calin Juravlee460d1d2015-09-29 04:52:17 +01004512void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4513 HUnresolvedInstanceFieldGet* instruction) {
4514 FieldAccessCallingConventionARM64 calling_convention;
4515 codegen_->CreateUnresolvedFieldLocationSummary(
4516 instruction, instruction->GetFieldType(), calling_convention);
4517}
4518
4519void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4520 HUnresolvedInstanceFieldGet* instruction) {
4521 FieldAccessCallingConventionARM64 calling_convention;
4522 codegen_->GenerateUnresolvedFieldAccess(instruction,
4523 instruction->GetFieldType(),
4524 instruction->GetFieldIndex(),
4525 instruction->GetDexPc(),
4526 calling_convention);
4527}
4528
4529void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4530 HUnresolvedInstanceFieldSet* instruction) {
4531 FieldAccessCallingConventionARM64 calling_convention;
4532 codegen_->CreateUnresolvedFieldLocationSummary(
4533 instruction, instruction->GetFieldType(), calling_convention);
4534}
4535
4536void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4537 HUnresolvedInstanceFieldSet* instruction) {
4538 FieldAccessCallingConventionARM64 calling_convention;
4539 codegen_->GenerateUnresolvedFieldAccess(instruction,
4540 instruction->GetFieldType(),
4541 instruction->GetFieldIndex(),
4542 instruction->GetDexPc(),
4543 calling_convention);
4544}
4545
4546void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4547 HUnresolvedStaticFieldGet* instruction) {
4548 FieldAccessCallingConventionARM64 calling_convention;
4549 codegen_->CreateUnresolvedFieldLocationSummary(
4550 instruction, instruction->GetFieldType(), calling_convention);
4551}
4552
4553void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4554 HUnresolvedStaticFieldGet* instruction) {
4555 FieldAccessCallingConventionARM64 calling_convention;
4556 codegen_->GenerateUnresolvedFieldAccess(instruction,
4557 instruction->GetFieldType(),
4558 instruction->GetFieldIndex(),
4559 instruction->GetDexPc(),
4560 calling_convention);
4561}
4562
4563void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4564 HUnresolvedStaticFieldSet* instruction) {
4565 FieldAccessCallingConventionARM64 calling_convention;
4566 codegen_->CreateUnresolvedFieldLocationSummary(
4567 instruction, instruction->GetFieldType(), calling_convention);
4568}
4569
4570void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4571 HUnresolvedStaticFieldSet* instruction) {
4572 FieldAccessCallingConventionARM64 calling_convention;
4573 codegen_->GenerateUnresolvedFieldAccess(instruction,
4574 instruction->GetFieldType(),
4575 instruction->GetFieldIndex(),
4576 instruction->GetDexPc(),
4577 calling_convention);
4578}
4579
Alexandre Rames5319def2014-10-23 10:03:10 +01004580void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4581 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4582}
4583
4584void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004585 HBasicBlock* block = instruction->GetBlock();
4586 if (block->GetLoopInformation() != nullptr) {
4587 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4588 // The back edge will generate the suspend check.
4589 return;
4590 }
4591 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4592 // The goto will generate the suspend check.
4593 return;
4594 }
4595 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004596}
4597
Alexandre Rames67555f72014-11-18 10:55:16 +00004598void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4599 LocationSummary* locations =
4600 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4601 InvokeRuntimeCallingConvention calling_convention;
4602 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4603}
4604
4605void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4606 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004607 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004608 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004609}
4610
4611void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4612 LocationSummary* locations =
4613 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4614 Primitive::Type input_type = conversion->GetInputType();
4615 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004616 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004617 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4618 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4619 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4620 }
4621
Alexandre Rames542361f2015-01-29 16:57:31 +00004622 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004623 locations->SetInAt(0, Location::RequiresFpuRegister());
4624 } else {
4625 locations->SetInAt(0, Location::RequiresRegister());
4626 }
4627
Alexandre Rames542361f2015-01-29 16:57:31 +00004628 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004629 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4630 } else {
4631 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4632 }
4633}
4634
4635void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4636 Primitive::Type result_type = conversion->GetResultType();
4637 Primitive::Type input_type = conversion->GetInputType();
4638
4639 DCHECK_NE(input_type, result_type);
4640
Alexandre Rames542361f2015-01-29 16:57:31 +00004641 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004642 int result_size = Primitive::ComponentSize(result_type);
4643 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004644 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004645 Register output = OutputRegister(conversion);
4646 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004647 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004648 // 'int' values are used directly as W registers, discarding the top
4649 // bits, so we don't need to sign-extend and can just perform a move.
4650 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4651 // top 32 bits of the target register. We theoretically could leave those
4652 // bits unchanged, but we would have to make sure that no code uses a
4653 // 32bit input value as a 64bit value assuming that the top 32 bits are
4654 // zero.
4655 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004656 } else if (result_type == Primitive::kPrimChar ||
4657 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4658 __ Ubfx(output,
4659 output.IsX() ? source.X() : source.W(),
4660 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004661 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004662 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004663 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004664 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004665 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004666 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004667 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4668 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004669 } else if (Primitive::IsFloatingPointType(result_type) &&
4670 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004671 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4672 } else {
4673 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4674 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004675 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004676}
Alexandre Rames67555f72014-11-18 10:55:16 +00004677
Serban Constantinescu02164b32014-11-13 14:05:07 +00004678void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4679 HandleShift(ushr);
4680}
4681
4682void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4683 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004684}
4685
4686void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4687 HandleBinaryOp(instruction);
4688}
4689
4690void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4691 HandleBinaryOp(instruction);
4692}
4693
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004694void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004695 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004696 LOG(FATAL) << "Unreachable";
4697}
4698
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004699void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004700 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004701 LOG(FATAL) << "Unreachable";
4702}
4703
Mark Mendellfe57faa2015-09-18 09:26:15 -04004704// Simple implementation of packed switch - generate cascaded compare/jumps.
4705void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4706 LocationSummary* locations =
4707 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4708 locations->SetInAt(0, Location::RequiresRegister());
4709}
4710
4711void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4712 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004713 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004714 Register value_reg = InputRegisterAt(switch_instr, 0);
4715 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4716
Zheng Xu3927c8b2015-11-18 17:46:25 +08004717 // Roughly set 16 as max average assemblies generated per HIR in a graph.
4718 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * vixl::kInstructionSize;
4719 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4720 // make sure we don't emit it if the target may run out of range.
4721 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4722 // ranges and emit the tables only as required.
4723 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004724
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004725 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004726 // Current instruction id is an upper bound of the number of HIRs in the graph.
4727 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4728 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004729 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4730 Register temp = temps.AcquireW();
4731 __ Subs(temp, value_reg, Operand(lower_bound));
4732
Zheng Xu3927c8b2015-11-18 17:46:25 +08004733 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004734 // Jump to successors[0] if value == lower_bound.
4735 __ B(eq, codegen_->GetLabelOf(successors[0]));
4736 int32_t last_index = 0;
4737 for (; num_entries - last_index > 2; last_index += 2) {
4738 __ Subs(temp, temp, Operand(2));
4739 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4740 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4741 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4742 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4743 }
4744 if (num_entries - last_index == 2) {
4745 // The last missing case_value.
4746 __ Cmp(temp, Operand(1));
4747 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004748 }
4749
4750 // And the default for any other value.
4751 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4752 __ B(codegen_->GetLabelOf(default_block));
4753 }
4754 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01004755 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08004756
4757 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4758
4759 // Below instructions should use at most one blocked register. Since there are two blocked
4760 // registers, we are free to block one.
4761 Register temp_w = temps.AcquireW();
4762 Register index;
4763 // Remove the bias.
4764 if (lower_bound != 0) {
4765 index = temp_w;
4766 __ Sub(index, value_reg, Operand(lower_bound));
4767 } else {
4768 index = value_reg;
4769 }
4770
4771 // Jump to default block if index is out of the range.
4772 __ Cmp(index, Operand(num_entries));
4773 __ B(hs, codegen_->GetLabelOf(default_block));
4774
4775 // In current VIXL implementation, it won't require any blocked registers to encode the
4776 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4777 // register pressure.
4778 Register table_base = temps.AcquireX();
4779 // Load jump offset from the table.
4780 __ Adr(table_base, jump_table->GetTableStartLabel());
4781 Register jump_offset = temp_w;
4782 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4783
4784 // Jump to target block by branching to table_base(pc related) + offset.
4785 Register target_address = table_base;
4786 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4787 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004788 }
4789}
4790
Roland Levillain44015862016-01-22 11:47:17 +00004791void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4792 Location out,
4793 uint32_t offset,
4794 Location maybe_temp) {
4795 Primitive::Type type = Primitive::kPrimNot;
4796 Register out_reg = RegisterFrom(out, type);
4797 if (kEmitCompilerReadBarrier) {
4798 Register temp_reg = RegisterFrom(maybe_temp, type);
4799 if (kUseBakerReadBarrier) {
4800 // Load with fast path based Baker's read barrier.
4801 // /* HeapReference<Object> */ out = *(out + offset)
4802 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4803 out,
4804 out_reg,
4805 offset,
4806 temp_reg,
4807 /* needs_null_check */ false,
4808 /* use_load_acquire */ false);
4809 } else {
4810 // Load with slow path based read barrier.
4811 // Save the value of `out` into `maybe_temp` before overwriting it
4812 // in the following move operation, as we will need it for the
4813 // read barrier below.
4814 __ Mov(temp_reg, out_reg);
4815 // /* HeapReference<Object> */ out = *(out + offset)
4816 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4817 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4818 }
4819 } else {
4820 // Plain load with no read barrier.
4821 // /* HeapReference<Object> */ out = *(out + offset)
4822 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4823 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4824 }
4825}
4826
4827void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
4828 Location out,
4829 Location obj,
4830 uint32_t offset,
4831 Location maybe_temp) {
4832 Primitive::Type type = Primitive::kPrimNot;
4833 Register out_reg = RegisterFrom(out, type);
4834 Register obj_reg = RegisterFrom(obj, type);
4835 if (kEmitCompilerReadBarrier) {
4836 if (kUseBakerReadBarrier) {
4837 // Load with fast path based Baker's read barrier.
4838 Register temp_reg = RegisterFrom(maybe_temp, type);
4839 // /* HeapReference<Object> */ out = *(obj + offset)
4840 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4841 out,
4842 obj_reg,
4843 offset,
4844 temp_reg,
4845 /* needs_null_check */ false,
4846 /* use_load_acquire */ false);
4847 } else {
4848 // Load with slow path based read barrier.
4849 // /* HeapReference<Object> */ out = *(obj + offset)
4850 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
4851 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4852 }
4853 } else {
4854 // Plain load with no read barrier.
4855 // /* HeapReference<Object> */ out = *(obj + offset)
4856 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
4857 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4858 }
4859}
4860
4861void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
4862 Location root,
4863 vixl::Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004864 uint32_t offset,
4865 vixl::Label* fixup_label) {
Roland Levillain44015862016-01-22 11:47:17 +00004866 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
4867 if (kEmitCompilerReadBarrier) {
4868 if (kUseBakerReadBarrier) {
4869 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4870 // Baker's read barrier are used:
4871 //
4872 // root = obj.field;
4873 // if (Thread::Current()->GetIsGcMarking()) {
4874 // root = ReadBarrier::Mark(root)
4875 // }
4876
4877 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004878 if (fixup_label == nullptr) {
4879 __ Ldr(root_reg, MemOperand(obj, offset));
4880 } else {
4881 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4882 __ Bind(fixup_label);
4883 __ ldr(root_reg, MemOperand(obj, offset));
4884 }
Roland Levillain44015862016-01-22 11:47:17 +00004885 static_assert(
4886 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
4887 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
4888 "have different sizes.");
4889 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
4890 "art::mirror::CompressedReference<mirror::Object> and int32_t "
4891 "have different sizes.");
4892
4893 // Slow path used to mark the GC root `root`.
4894 SlowPathCodeARM64* slow_path =
4895 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root, root);
4896 codegen_->AddSlowPath(slow_path);
4897
4898 MacroAssembler* masm = GetVIXLAssembler();
4899 UseScratchRegisterScope temps(masm);
4900 Register temp = temps.AcquireW();
4901 // temp = Thread::Current()->GetIsGcMarking()
4902 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64WordSize>().Int32Value()));
4903 __ Cbnz(temp, slow_path->GetEntryLabel());
4904 __ Bind(slow_path->GetExitLabel());
4905 } else {
4906 // GC root loaded through a slow path for read barriers other
4907 // than Baker's.
4908 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004909 if (fixup_label == nullptr) {
4910 __ Add(root_reg.X(), obj.X(), offset);
4911 } else {
4912 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4913 __ Bind(fixup_label);
4914 __ add(root_reg.X(), obj.X(), offset);
4915 }
Roland Levillain44015862016-01-22 11:47:17 +00004916 // /* mirror::Object* */ root = root->Read()
4917 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
4918 }
4919 } else {
4920 // Plain GC root load with no read barrier.
4921 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004922 if (fixup_label == nullptr) {
4923 __ Ldr(root_reg, MemOperand(obj, offset));
4924 } else {
4925 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4926 __ Bind(fixup_label);
4927 __ ldr(root_reg, MemOperand(obj, offset));
4928 }
Roland Levillain44015862016-01-22 11:47:17 +00004929 // Note that GC roots are not affected by heap poisoning, thus we
4930 // do not have to unpoison `root_reg` here.
4931 }
4932}
4933
4934void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
4935 Location ref,
4936 vixl::Register obj,
4937 uint32_t offset,
4938 Register temp,
4939 bool needs_null_check,
4940 bool use_load_acquire) {
4941 DCHECK(kEmitCompilerReadBarrier);
4942 DCHECK(kUseBakerReadBarrier);
4943
4944 // /* HeapReference<Object> */ ref = *(obj + offset)
4945 Location no_index = Location::NoLocation();
4946 GenerateReferenceLoadWithBakerReadBarrier(
4947 instruction, ref, obj, offset, no_index, temp, needs_null_check, use_load_acquire);
4948}
4949
4950void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
4951 Location ref,
4952 vixl::Register obj,
4953 uint32_t data_offset,
4954 Location index,
4955 Register temp,
4956 bool needs_null_check) {
4957 DCHECK(kEmitCompilerReadBarrier);
4958 DCHECK(kUseBakerReadBarrier);
4959
4960 // Array cells are never volatile variables, therefore array loads
4961 // never use Load-Acquire instructions on ARM64.
4962 const bool use_load_acquire = false;
4963
4964 // /* HeapReference<Object> */ ref =
4965 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4966 GenerateReferenceLoadWithBakerReadBarrier(
4967 instruction, ref, obj, data_offset, index, temp, needs_null_check, use_load_acquire);
4968}
4969
4970void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
4971 Location ref,
4972 vixl::Register obj,
4973 uint32_t offset,
4974 Location index,
4975 Register temp,
4976 bool needs_null_check,
4977 bool use_load_acquire) {
4978 DCHECK(kEmitCompilerReadBarrier);
4979 DCHECK(kUseBakerReadBarrier);
4980 // If `index` is a valid location, then we are emitting an array
4981 // load, so we shouldn't be using a Load Acquire instruction.
4982 // In other words: `index.IsValid()` => `!use_load_acquire`.
4983 DCHECK(!index.IsValid() || !use_load_acquire);
4984
4985 MacroAssembler* masm = GetVIXLAssembler();
4986 UseScratchRegisterScope temps(masm);
4987
4988 // In slow path based read barriers, the read barrier call is
4989 // inserted after the original load. However, in fast path based
4990 // Baker's read barriers, we need to perform the load of
4991 // mirror::Object::monitor_ *before* the original reference load.
4992 // This load-load ordering is required by the read barrier.
4993 // The fast path/slow path (for Baker's algorithm) should look like:
4994 //
4995 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
4996 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
4997 // HeapReference<Object> ref = *src; // Original reference load.
4998 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
4999 // if (is_gray) {
5000 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5001 // }
5002 //
5003 // Note: the original implementation in ReadBarrier::Barrier is
5004 // slightly more complex as it performs additional checks that we do
5005 // not do here for performance reasons.
5006
5007 Primitive::Type type = Primitive::kPrimNot;
5008 Register ref_reg = RegisterFrom(ref, type);
5009 DCHECK(obj.IsW());
5010 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5011
5012 // /* int32_t */ monitor = obj->monitor_
5013 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5014 if (needs_null_check) {
5015 MaybeRecordImplicitNullCheck(instruction);
5016 }
5017 // /* LockWord */ lock_word = LockWord(monitor)
5018 static_assert(sizeof(LockWord) == sizeof(int32_t),
5019 "art::LockWord and int32_t have different sizes.");
5020 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
5021 __ Lsr(temp, temp, LockWord::kReadBarrierStateShift);
5022 __ And(temp, temp, Operand(LockWord::kReadBarrierStateMask));
5023 static_assert(
5024 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
5025 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
5026
5027 // Introduce a dependency on the high bits of rb_state, which shall
5028 // be all zeroes, to prevent load-load reordering, and without using
5029 // a memory barrier (which would be more expensive).
5030 // temp2 = rb_state & ~LockWord::kReadBarrierStateMask = 0
5031 Register temp2 = temps.AcquireW();
5032 __ Bic(temp2, temp, Operand(LockWord::kReadBarrierStateMask));
5033 // obj is unchanged by this operation, but its value now depends on
5034 // temp2, which depends on temp.
5035 __ Add(obj, obj, Operand(temp2));
5036 temps.Release(temp2);
5037
5038 // The actual reference load.
5039 if (index.IsValid()) {
5040 static_assert(
5041 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5042 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005043 // /* HeapReference<Object> */ ref =
5044 // *(obj + offset + index * sizeof(HeapReference<Object>))
Roland Levillainca0bf032016-02-09 12:49:18 +00005045 const size_t shift_amount = Primitive::ComponentSizeShift(type);
Roland Levillain44015862016-01-22 11:47:17 +00005046 if (index.IsConstant()) {
Roland Levillainca0bf032016-02-09 12:49:18 +00005047 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << shift_amount);
5048 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillain44015862016-01-22 11:47:17 +00005049 } else {
Roland Levillainca0bf032016-02-09 12:49:18 +00005050 temp2 = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00005051 __ Add(temp2, obj, offset);
Roland Levillainca0bf032016-02-09 12:49:18 +00005052 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, shift_amount));
5053 temps.Release(temp2);
Roland Levillain44015862016-01-22 11:47:17 +00005054 }
Roland Levillain44015862016-01-22 11:47:17 +00005055 } else {
5056 // /* HeapReference<Object> */ ref = *(obj + offset)
5057 MemOperand field = HeapOperand(obj, offset);
5058 if (use_load_acquire) {
5059 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5060 } else {
5061 Load(type, ref_reg, field);
5062 }
5063 }
5064
5065 // Object* ref = ref_addr->AsMirrorPtr()
5066 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5067
5068 // Slow path used to mark the object `ref` when it is gray.
5069 SlowPathCodeARM64* slow_path =
5070 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref, ref);
5071 AddSlowPath(slow_path);
5072
5073 // if (rb_state == ReadBarrier::gray_ptr_)
5074 // ref = ReadBarrier::Mark(ref);
5075 __ Cmp(temp, ReadBarrier::gray_ptr_);
5076 __ B(eq, slow_path->GetEntryLabel());
5077 __ Bind(slow_path->GetExitLabel());
5078}
5079
5080void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5081 Location out,
5082 Location ref,
5083 Location obj,
5084 uint32_t offset,
5085 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005086 DCHECK(kEmitCompilerReadBarrier);
5087
Roland Levillain44015862016-01-22 11:47:17 +00005088 // Insert a slow path based read barrier *after* the reference load.
5089 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005090 // If heap poisoning is enabled, the unpoisoning of the loaded
5091 // reference will be carried out by the runtime within the slow
5092 // path.
5093 //
5094 // Note that `ref` currently does not get unpoisoned (when heap
5095 // poisoning is enabled), which is alright as the `ref` argument is
5096 // not used by the artReadBarrierSlow entry point.
5097 //
5098 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5099 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5100 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5101 AddSlowPath(slow_path);
5102
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005103 __ B(slow_path->GetEntryLabel());
5104 __ Bind(slow_path->GetExitLabel());
5105}
5106
Roland Levillain44015862016-01-22 11:47:17 +00005107void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5108 Location out,
5109 Location ref,
5110 Location obj,
5111 uint32_t offset,
5112 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005113 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005114 // Baker's read barriers shall be handled by the fast path
5115 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5116 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005117 // If heap poisoning is enabled, unpoisoning will be taken care of
5118 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005119 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005120 } else if (kPoisonHeapReferences) {
5121 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5122 }
5123}
5124
Roland Levillain44015862016-01-22 11:47:17 +00005125void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5126 Location out,
5127 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005128 DCHECK(kEmitCompilerReadBarrier);
5129
Roland Levillain44015862016-01-22 11:47:17 +00005130 // Insert a slow path based read barrier *after* the GC root load.
5131 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005132 // Note that GC roots are not affected by heap poisoning, so we do
5133 // not need to do anything special for this here.
5134 SlowPathCodeARM64* slow_path =
5135 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5136 AddSlowPath(slow_path);
5137
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005138 __ B(slow_path->GetEntryLabel());
5139 __ Bind(slow_path->GetExitLabel());
5140}
5141
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005142void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5143 LocationSummary* locations =
5144 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5145 locations->SetInAt(0, Location::RequiresRegister());
5146 locations->SetOut(Location::RequiresRegister());
5147}
5148
5149void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5150 LocationSummary* locations = instruction->GetLocations();
5151 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005152 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005153 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5154 instruction->GetIndex(), kArm64PointerSize).SizeValue();
5155 } else {
Nelli Kimbadee982016-05-13 13:08:53 +03005156 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5157 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
5158 method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity50706432016-06-14 11:31:04 -07005159 instruction->GetIndex(), kArm64PointerSize));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005160 }
5161 __ Ldr(XRegisterFrom(locations->Out()),
5162 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
5163}
5164
5165
5166
Alexandre Rames67555f72014-11-18 10:55:16 +00005167#undef __
5168#undef QUICK_ENTRY_POINT
5169
Alexandre Rames5319def2014-10-23 10:03:10 +01005170} // namespace arm64
5171} // namespace art