blob: 46fd8526a5e4fff7a3093fe7db5f4821cc02620b [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
Alexandre Rames67555f72014-11-18 10:55:16 +0000135#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
136#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100137
Zheng Xuda403092015-04-24 17:35:39 +0800138// Calculate memory accessing operand for save/restore live registers.
139static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
140 RegisterSet* register_set,
141 int64_t spill_offset,
142 bool is_save) {
143 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
144 codegen->GetNumberOfCoreRegisters(),
145 register_set->GetFloatingPointRegisters(),
146 codegen->GetNumberOfFloatingPointRegisters()));
147
148 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
149 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000150 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
151 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
Zheng Xuda403092015-04-24 17:35:39 +0800152
153 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
154 UseScratchRegisterScope temps(masm);
155
156 Register base = masm->StackPointer();
157 int64_t core_spill_size = core_list.TotalSizeInBytes();
158 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
159 int64_t reg_size = kXRegSizeInBytes;
160 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
161 uint32_t ls_access_size = WhichPowerOf2(reg_size);
162 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
163 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
164 // If the offset does not fit in the instruction's immediate field, use an alternate register
165 // to compute the base address(float point registers spill base address).
166 Register new_base = temps.AcquireSameSizeAs(base);
167 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
168 base = new_base;
169 spill_offset = -core_spill_size;
170 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
171 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
172 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
173 }
174
175 if (is_save) {
176 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
177 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
178 } else {
179 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
180 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
181 }
182}
183
184void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
185 RegisterSet* register_set = locations->GetLiveRegisters();
186 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
187 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
188 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
189 // If the register holds an object, update the stack mask.
190 if (locations->RegisterContainsObject(i)) {
191 locations->SetStackBit(stack_offset / kVRegSize);
192 }
193 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
194 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
195 saved_core_stack_offsets_[i] = stack_offset;
196 stack_offset += kXRegSizeInBytes;
197 }
198 }
199
200 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
201 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
202 register_set->ContainsFloatingPointRegister(i)) {
203 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
204 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
205 saved_fpu_stack_offsets_[i] = stack_offset;
206 stack_offset += kDRegSizeInBytes;
207 }
208 }
209
210 SaveRestoreLiveRegistersHelper(codegen, register_set,
211 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
212}
213
214void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
215 RegisterSet* register_set = locations->GetLiveRegisters();
216 SaveRestoreLiveRegistersHelper(codegen, register_set,
217 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
218}
219
Alexandre Rames5319def2014-10-23 10:03:10 +0100220class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
221 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000222 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100223
Alexandre Rames67555f72014-11-18 10:55:16 +0000224 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100225 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000226 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100227
Alexandre Rames5319def2014-10-23 10:03:10 +0100228 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000229 if (instruction_->CanThrowIntoCatchBlock()) {
230 // Live registers will be restored in the catch block if caught.
231 SaveLiveRegisters(codegen, instruction_->GetLocations());
232 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000233 // We're moving two locations to locations that could overlap, so we need a parallel
234 // move resolver.
235 InvokeRuntimeCallingConvention calling_convention;
236 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100237 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
238 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000239 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000240 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800241 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100242 }
243
Alexandre Rames8158f282015-08-07 10:26:17 +0100244 bool IsFatal() const OVERRIDE { return true; }
245
Alexandre Rames9931f312015-06-19 14:47:01 +0100246 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
247
Alexandre Rames5319def2014-10-23 10:03:10 +0100248 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100249 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
250};
251
Alexandre Rames67555f72014-11-18 10:55:16 +0000252class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
253 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000254 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000255
256 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
257 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
258 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000259 if (instruction_->CanThrowIntoCatchBlock()) {
260 // Live registers will be restored in the catch block if caught.
261 SaveLiveRegisters(codegen, instruction_->GetLocations());
262 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000263 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000264 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800265 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000266 }
267
Alexandre Rames8158f282015-08-07 10:26:17 +0100268 bool IsFatal() const OVERRIDE { return true; }
269
Alexandre Rames9931f312015-06-19 14:47:01 +0100270 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
271
Alexandre Rames67555f72014-11-18 10:55:16 +0000272 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000273 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
274};
275
276class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
277 public:
278 LoadClassSlowPathARM64(HLoadClass* cls,
279 HInstruction* at,
280 uint32_t dex_pc,
281 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000282 : SlowPathCodeARM64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000283 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
284 }
285
286 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
287 LocationSummary* locations = at_->GetLocations();
288 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
289
290 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000291 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000292
293 InvokeRuntimeCallingConvention calling_convention;
294 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000295 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
296 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000297 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800298 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100299 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800300 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100301 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800302 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000303
304 // Move the class to the desired location.
305 Location out = locations->Out();
306 if (out.IsValid()) {
307 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
308 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000309 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000310 }
311
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000312 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000313 __ B(GetExitLabel());
314 }
315
Alexandre Rames9931f312015-06-19 14:47:01 +0100316 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
317
Alexandre Rames67555f72014-11-18 10:55:16 +0000318 private:
319 // The class this slow path will load.
320 HLoadClass* const cls_;
321
322 // The instruction where this slow path is happening.
323 // (Might be the load class or an initialization check).
324 HInstruction* const at_;
325
326 // The dex PC of `at_`.
327 const uint32_t dex_pc_;
328
329 // Whether to initialize the class.
330 const bool do_clinit_;
331
332 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
333};
334
335class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
336 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000337 explicit LoadStringSlowPathARM64(HLoadString* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000338
339 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
340 LocationSummary* locations = instruction_->GetLocations();
341 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
342 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
343
344 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000345 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000346
347 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000348 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
349 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
Alexandre Rames67555f72014-11-18 10:55:16 +0000350 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000351 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100352 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000353 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000354 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000355
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000356 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000357 __ B(GetExitLabel());
358 }
359
Alexandre Rames9931f312015-06-19 14:47:01 +0100360 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
361
Alexandre Rames67555f72014-11-18 10:55:16 +0000362 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000363 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
364};
365
Alexandre Rames5319def2014-10-23 10:03:10 +0100366class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
367 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000368 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100369
Alexandre Rames67555f72014-11-18 10:55:16 +0000370 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
371 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100372 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000373 if (instruction_->CanThrowIntoCatchBlock()) {
374 // Live registers will be restored in the catch block if caught.
375 SaveLiveRegisters(codegen, instruction_->GetLocations());
376 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000377 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000378 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800379 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100380 }
381
Alexandre Rames8158f282015-08-07 10:26:17 +0100382 bool IsFatal() const OVERRIDE { return true; }
383
Alexandre Rames9931f312015-06-19 14:47:01 +0100384 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
385
Alexandre Rames5319def2014-10-23 10:03:10 +0100386 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100387 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
388};
389
390class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
391 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100392 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000393 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100394
Alexandre Rames67555f72014-11-18 10:55:16 +0000395 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
396 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100397 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000398 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000399 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000400 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800401 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000402 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000403 if (successor_ == nullptr) {
404 __ B(GetReturnLabel());
405 } else {
406 __ B(arm64_codegen->GetLabelOf(successor_));
407 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100408 }
409
410 vixl::Label* GetReturnLabel() {
411 DCHECK(successor_ == nullptr);
412 return &return_label_;
413 }
414
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100415 HBasicBlock* GetSuccessor() const {
416 return successor_;
417 }
418
Alexandre Rames9931f312015-06-19 14:47:01 +0100419 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
420
Alexandre Rames5319def2014-10-23 10:03:10 +0100421 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100422 // If not null, the block to branch to after the suspend check.
423 HBasicBlock* const successor_;
424
425 // If `successor_` is null, the label to branch to after the suspend check.
426 vixl::Label return_label_;
427
428 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
429};
430
Alexandre Rames67555f72014-11-18 10:55:16 +0000431class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
432 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000433 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000434 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000435
436 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000437 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100438 Location class_to_check = locations->InAt(1);
439 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
440 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000441 DCHECK(instruction_->IsCheckCast()
442 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
443 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100444 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000445
Alexandre Rames67555f72014-11-18 10:55:16 +0000446 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000447
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000448 if (!is_fatal_) {
449 SaveLiveRegisters(codegen, locations);
450 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000451
452 // We're moving two locations to locations that could overlap, so we need a parallel
453 // move resolver.
454 InvokeRuntimeCallingConvention calling_convention;
455 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100456 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
457 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000458
459 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000460 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100461 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000462 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
463 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000464 Primitive::Type ret_type = instruction_->GetType();
465 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
466 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
467 } else {
468 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100469 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800470 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000471 }
472
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000473 if (!is_fatal_) {
474 RestoreLiveRegisters(codegen, locations);
475 __ B(GetExitLabel());
476 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000477 }
478
Alexandre Rames9931f312015-06-19 14:47:01 +0100479 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000480 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100481
Alexandre Rames67555f72014-11-18 10:55:16 +0000482 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000483 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000484
Alexandre Rames67555f72014-11-18 10:55:16 +0000485 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
486};
487
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700488class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
489 public:
Aart Bik42249c32016-01-07 15:33:50 -0800490 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000491 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700492
493 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800494 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700495 __ Bind(GetEntryLabel());
496 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800497 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
498 instruction_,
499 instruction_->GetDexPc(),
500 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000501 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700502 }
503
Alexandre Rames9931f312015-06-19 14:47:01 +0100504 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
505
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700506 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700507 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
508};
509
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100510class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
511 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000512 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100513
514 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
515 LocationSummary* locations = instruction_->GetLocations();
516 __ Bind(GetEntryLabel());
517 SaveLiveRegisters(codegen, locations);
518
519 InvokeRuntimeCallingConvention calling_convention;
520 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
521 parallel_move.AddMove(
522 locations->InAt(0),
523 LocationFrom(calling_convention.GetRegisterAt(0)),
524 Primitive::kPrimNot,
525 nullptr);
526 parallel_move.AddMove(
527 locations->InAt(1),
528 LocationFrom(calling_convention.GetRegisterAt(1)),
529 Primitive::kPrimInt,
530 nullptr);
531 parallel_move.AddMove(
532 locations->InAt(2),
533 LocationFrom(calling_convention.GetRegisterAt(2)),
534 Primitive::kPrimNot,
535 nullptr);
536 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
537
538 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
539 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
540 instruction_,
541 instruction_->GetDexPc(),
542 this);
543 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
544 RestoreLiveRegisters(codegen, locations);
545 __ B(GetExitLabel());
546 }
547
548 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
549
550 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100551 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
552};
553
Zheng Xu3927c8b2015-11-18 17:46:25 +0800554void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
555 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000556 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800557
558 // We are about to use the assembler to place literals directly. Make sure we have enough
559 // underlying code buffer and we have generated the jump table with right size.
560 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
561 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
562
563 __ Bind(&table_start_);
564 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
565 for (uint32_t i = 0; i < num_entries; i++) {
566 vixl::Label* target_label = codegen->GetLabelOf(successors[i]);
567 DCHECK(target_label->IsBound());
568 ptrdiff_t jump_offset = target_label->location() - table_start_.location();
569 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
570 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
571 Literal<int32_t> literal(jump_offset);
572 __ place(&literal);
573 }
574}
575
Roland Levillain44015862016-01-22 11:47:17 +0000576// Slow path marking an object during a read barrier.
577class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
578 public:
579 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000580 : SlowPathCodeARM64(instruction), out_(out), obj_(obj) {
Roland Levillain44015862016-01-22 11:47:17 +0000581 DCHECK(kEmitCompilerReadBarrier);
582 }
583
584 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
585
586 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
587 LocationSummary* locations = instruction_->GetLocations();
588 Primitive::Type type = Primitive::kPrimNot;
589 DCHECK(locations->CanCall());
590 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
591 DCHECK(instruction_->IsInstanceFieldGet() ||
592 instruction_->IsStaticFieldGet() ||
593 instruction_->IsArrayGet() ||
594 instruction_->IsLoadClass() ||
595 instruction_->IsLoadString() ||
596 instruction_->IsInstanceOf() ||
597 instruction_->IsCheckCast())
598 << "Unexpected instruction in read barrier marking slow path: "
599 << instruction_->DebugName();
600
601 __ Bind(GetEntryLabel());
602 SaveLiveRegisters(codegen, locations);
603
604 InvokeRuntimeCallingConvention calling_convention;
605 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
606 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)), obj_, type);
607 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
608 instruction_,
609 instruction_->GetDexPc(),
610 this);
611 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
612 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
613
614 RestoreLiveRegisters(codegen, locations);
615 __ B(GetExitLabel());
616 }
617
618 private:
Roland Levillain44015862016-01-22 11:47:17 +0000619 const Location out_;
620 const Location obj_;
621
622 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
623};
624
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000625// Slow path generating a read barrier for a heap reference.
626class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
627 public:
628 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
629 Location out,
630 Location ref,
631 Location obj,
632 uint32_t offset,
633 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000634 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000635 out_(out),
636 ref_(ref),
637 obj_(obj),
638 offset_(offset),
639 index_(index) {
640 DCHECK(kEmitCompilerReadBarrier);
641 // If `obj` is equal to `out` or `ref`, it means the initial object
642 // has been overwritten by (or after) the heap object reference load
643 // to be instrumented, e.g.:
644 //
645 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000646 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000647 //
648 // In that case, we have lost the information about the original
649 // object, and the emitted read barrier cannot work properly.
650 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
651 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
652 }
653
654 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
655 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
656 LocationSummary* locations = instruction_->GetLocations();
657 Primitive::Type type = Primitive::kPrimNot;
658 DCHECK(locations->CanCall());
659 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
660 DCHECK(!instruction_->IsInvoke() ||
661 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain44015862016-01-22 11:47:17 +0000662 instruction_->GetLocations()->Intrinsified()))
663 << "Unexpected instruction in read barrier for heap reference slow path: "
664 << instruction_->DebugName();
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000665 // The read barrier instrumentation does not support the
666 // HArm64IntermediateAddress instruction yet.
667 DCHECK(!(instruction_->IsArrayGet() &&
668 instruction_->AsArrayGet()->GetArray()->IsArm64IntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000669
670 __ Bind(GetEntryLabel());
671
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000672 SaveLiveRegisters(codegen, locations);
673
674 // We may have to change the index's value, but as `index_` is a
675 // constant member (like other "inputs" of this slow path),
676 // introduce a copy of it, `index`.
677 Location index = index_;
678 if (index_.IsValid()) {
679 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
680 if (instruction_->IsArrayGet()) {
681 // Compute the actual memory offset and store it in `index`.
682 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
683 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
684 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
685 // We are about to change the value of `index_reg` (see the
686 // calls to vixl::MacroAssembler::Lsl and
687 // vixl::MacroAssembler::Mov below), but it has
688 // not been saved by the previous call to
689 // art::SlowPathCode::SaveLiveRegisters, as it is a
690 // callee-save register --
691 // art::SlowPathCode::SaveLiveRegisters does not consider
692 // callee-save registers, as it has been designed with the
693 // assumption that callee-save registers are supposed to be
694 // handled by the called function. So, as a callee-save
695 // register, `index_reg` _would_ eventually be saved onto
696 // the stack, but it would be too late: we would have
697 // changed its value earlier. Therefore, we manually save
698 // it here into another freely available register,
699 // `free_reg`, chosen of course among the caller-save
700 // registers (as a callee-save `free_reg` register would
701 // exhibit the same problem).
702 //
703 // Note we could have requested a temporary register from
704 // the register allocator instead; but we prefer not to, as
705 // this is a slow path, and we know we can find a
706 // caller-save register that is available.
707 Register free_reg = FindAvailableCallerSaveRegister(codegen);
708 __ Mov(free_reg.W(), index_reg);
709 index_reg = free_reg;
710 index = LocationFrom(index_reg);
711 } else {
712 // The initial register stored in `index_` has already been
713 // saved in the call to art::SlowPathCode::SaveLiveRegisters
714 // (as it is not a callee-save register), so we can freely
715 // use it.
716 }
717 // Shifting the index value contained in `index_reg` by the scale
718 // factor (2) cannot overflow in practice, as the runtime is
719 // unable to allocate object arrays with a size larger than
720 // 2^26 - 1 (that is, 2^28 - 4 bytes).
721 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
722 static_assert(
723 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
724 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
725 __ Add(index_reg, index_reg, Operand(offset_));
726 } else {
727 DCHECK(instruction_->IsInvoke());
728 DCHECK(instruction_->GetLocations()->Intrinsified());
729 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
730 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
731 << instruction_->AsInvoke()->GetIntrinsic();
732 DCHECK_EQ(offset_, 0U);
733 DCHECK(index_.IsRegisterPair());
734 // UnsafeGet's offset location is a register pair, the low
735 // part contains the correct offset.
736 index = index_.ToLow();
737 }
738 }
739
740 // We're moving two or three locations to locations that could
741 // overlap, so we need a parallel move resolver.
742 InvokeRuntimeCallingConvention calling_convention;
743 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
744 parallel_move.AddMove(ref_,
745 LocationFrom(calling_convention.GetRegisterAt(0)),
746 type,
747 nullptr);
748 parallel_move.AddMove(obj_,
749 LocationFrom(calling_convention.GetRegisterAt(1)),
750 type,
751 nullptr);
752 if (index.IsValid()) {
753 parallel_move.AddMove(index,
754 LocationFrom(calling_convention.GetRegisterAt(2)),
755 Primitive::kPrimInt,
756 nullptr);
757 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
758 } else {
759 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
760 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
761 }
762 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
763 instruction_,
764 instruction_->GetDexPc(),
765 this);
766 CheckEntrypointTypes<
767 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
768 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
769
770 RestoreLiveRegisters(codegen, locations);
771
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000772 __ B(GetExitLabel());
773 }
774
775 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
776
777 private:
778 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
779 size_t ref = static_cast<int>(XRegisterFrom(ref_).code());
780 size_t obj = static_cast<int>(XRegisterFrom(obj_).code());
781 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
782 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
783 return Register(VIXLRegCodeFromART(i), kXRegSize);
784 }
785 }
786 // We shall never fail to find a free caller-save register, as
787 // there are more than two core caller-save registers on ARM64
788 // (meaning it is possible to find one which is different from
789 // `ref` and `obj`).
790 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
791 LOG(FATAL) << "Could not find a free register";
792 UNREACHABLE();
793 }
794
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000795 const Location out_;
796 const Location ref_;
797 const Location obj_;
798 const uint32_t offset_;
799 // An additional location containing an index to an array.
800 // Only used for HArrayGet and the UnsafeGetObject &
801 // UnsafeGetObjectVolatile intrinsics.
802 const Location index_;
803
804 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
805};
806
807// Slow path generating a read barrier for a GC root.
808class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
809 public:
810 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000811 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000812 DCHECK(kEmitCompilerReadBarrier);
813 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000814
815 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
816 LocationSummary* locations = instruction_->GetLocations();
817 Primitive::Type type = Primitive::kPrimNot;
818 DCHECK(locations->CanCall());
819 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000820 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
821 << "Unexpected instruction in read barrier for GC root slow path: "
822 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000823
824 __ Bind(GetEntryLabel());
825 SaveLiveRegisters(codegen, locations);
826
827 InvokeRuntimeCallingConvention calling_convention;
828 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
829 // The argument of the ReadBarrierForRootSlow is not a managed
830 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
831 // thus we need a 64-bit move here, and we cannot use
832 //
833 // arm64_codegen->MoveLocation(
834 // LocationFrom(calling_convention.GetRegisterAt(0)),
835 // root_,
836 // type);
837 //
838 // which would emit a 32-bit move, as `type` is a (32-bit wide)
839 // reference type (`Primitive::kPrimNot`).
840 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
841 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
842 instruction_,
843 instruction_->GetDexPc(),
844 this);
845 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
846 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
847
848 RestoreLiveRegisters(codegen, locations);
849 __ B(GetExitLabel());
850 }
851
852 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
853
854 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000855 const Location out_;
856 const Location root_;
857
858 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
859};
860
Alexandre Rames5319def2014-10-23 10:03:10 +0100861#undef __
862
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100863Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100864 Location next_location;
865 if (type == Primitive::kPrimVoid) {
866 LOG(FATAL) << "Unreachable type " << type;
867 }
868
Alexandre Rames542361f2015-01-29 16:57:31 +0000869 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100870 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
871 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000872 } else if (!Primitive::IsFloatingPointType(type) &&
873 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000874 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
875 } else {
876 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000877 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
878 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100879 }
880
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000881 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000882 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100883 return next_location;
884}
885
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100886Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100887 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100888}
889
Serban Constantinescu579885a2015-02-22 20:51:33 +0000890CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
891 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100892 const CompilerOptions& compiler_options,
893 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100894 : CodeGenerator(graph,
895 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000896 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000897 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000898 callee_saved_core_registers.list(),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000899 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100900 compiler_options,
901 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100902 block_labels_(nullptr),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800903 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100904 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000905 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000906 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000907 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100908 uint64_literals_(std::less<uint64_t>(),
909 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
910 method_patches_(MethodReferenceComparator(),
911 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
912 call_patches_(MethodReferenceComparator(),
913 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
914 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000915 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000916 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000917 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000918}
Alexandre Rames5319def2014-10-23 10:03:10 +0100919
Alexandre Rames67555f72014-11-18 10:55:16 +0000920#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100921
Zheng Xu3927c8b2015-11-18 17:46:25 +0800922void CodeGeneratorARM64::EmitJumpTables() {
923 for (auto jump_table : jump_tables_) {
924 jump_table->EmitTable(this);
925 }
926}
927
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000928void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800929 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000930 // Ensure we emit the literal pool.
931 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000932
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000933 CodeGenerator::Finalize(allocator);
934}
935
Zheng Xuad4450e2015-04-17 18:48:56 +0800936void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
937 // Note: There are 6 kinds of moves:
938 // 1. constant -> GPR/FPR (non-cycle)
939 // 2. constant -> stack (non-cycle)
940 // 3. GPR/FPR -> GPR/FPR
941 // 4. GPR/FPR -> stack
942 // 5. stack -> GPR/FPR
943 // 6. stack -> stack (non-cycle)
944 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
945 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
946 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
947 // dependency.
948 vixl_temps_.Open(GetVIXLAssembler());
949}
950
951void ParallelMoveResolverARM64::FinishEmitNativeCode() {
952 vixl_temps_.Close();
953}
954
955Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
956 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
957 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
958 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
959 Location scratch = GetScratchLocation(kind);
960 if (!scratch.Equals(Location::NoLocation())) {
961 return scratch;
962 }
963 // Allocate from VIXL temp registers.
964 if (kind == Location::kRegister) {
965 scratch = LocationFrom(vixl_temps_.AcquireX());
966 } else {
967 DCHECK(kind == Location::kFpuRegister);
968 scratch = LocationFrom(vixl_temps_.AcquireD());
969 }
970 AddScratchLocation(scratch);
971 return scratch;
972}
973
974void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
975 if (loc.IsRegister()) {
976 vixl_temps_.Release(XRegisterFrom(loc));
977 } else {
978 DCHECK(loc.IsFpuRegister());
979 vixl_temps_.Release(DRegisterFrom(loc));
980 }
981 RemoveScratchLocation(loc);
982}
983
Alexandre Rames3e69f162014-12-10 10:36:50 +0000984void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100985 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100986 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000987}
988
Alexandre Rames5319def2014-10-23 10:03:10 +0100989void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100990 MacroAssembler* masm = GetVIXLAssembler();
991 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000992 __ Bind(&frame_entry_label_);
993
Serban Constantinescu02164b32014-11-13 14:05:07 +0000994 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
995 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100996 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000997 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000998 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000999 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001000 __ Ldr(wzr, MemOperand(temp, 0));
1001 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001002 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001003
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001004 if (!HasEmptyFrame()) {
1005 int frame_size = GetFrameSize();
1006 // Stack layout:
1007 // sp[frame_size - 8] : lr.
1008 // ... : other preserved core registers.
1009 // ... : other preserved fp registers.
1010 // ... : reserved frame space.
1011 // sp[0] : current method.
1012 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001013 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001014 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1015 frame_size - GetCoreSpillSize());
1016 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1017 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001018 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001019}
1020
1021void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001022 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001023 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001024 if (!HasEmptyFrame()) {
1025 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001026 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1027 frame_size - FrameEntrySpillSize());
1028 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1029 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001030 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001031 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001032 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001033 __ Ret();
1034 GetAssembler()->cfi().RestoreState();
1035 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001036}
1037
Zheng Xuda403092015-04-24 17:35:39 +08001038vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
1039 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
1040 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
1041 core_spill_mask_);
1042}
1043
1044vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
1045 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1046 GetNumberOfFloatingPointRegisters()));
1047 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
1048 fpu_spill_mask_);
1049}
1050
Alexandre Rames5319def2014-10-23 10:03:10 +01001051void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1052 __ Bind(GetLabelOf(block));
1053}
1054
Calin Juravle175dc732015-08-25 15:42:32 +01001055void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1056 DCHECK(location.IsRegister());
1057 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1058}
1059
Calin Juravlee460d1d2015-09-29 04:52:17 +01001060void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1061 if (location.IsRegister()) {
1062 locations->AddTemp(location);
1063 } else {
1064 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1065 }
1066}
1067
Alexandre Rames5319def2014-10-23 10:03:10 +01001068Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
1069 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001070
Alexandre Rames5319def2014-10-23 10:03:10 +01001071 switch (type) {
1072 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001073 case Primitive::kPrimInt:
1074 case Primitive::kPrimFloat:
1075 return Location::StackSlot(GetStackSlot(load->GetLocal()));
1076
1077 case Primitive::kPrimLong:
1078 case Primitive::kPrimDouble:
1079 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
1080
Alexandre Rames5319def2014-10-23 10:03:10 +01001081 case Primitive::kPrimBoolean:
1082 case Primitive::kPrimByte:
1083 case Primitive::kPrimChar:
1084 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +01001085 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +01001086 LOG(FATAL) << "Unexpected type " << type;
1087 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001088
Alexandre Rames5319def2014-10-23 10:03:10 +01001089 LOG(FATAL) << "Unreachable";
1090 return Location::NoLocation();
1091}
1092
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001093void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001094 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001095 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001096 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +01001097 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001098 if (value_can_be_null) {
1099 __ Cbz(value, &done);
1100 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001101 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
1102 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001103 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001104 if (value_can_be_null) {
1105 __ Bind(&done);
1106 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001107}
1108
David Brazdil58282f42016-01-14 12:45:10 +00001109void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001110 // Blocked core registers:
1111 // lr : Runtime reserved.
1112 // tr : Runtime reserved.
1113 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1114 // ip1 : VIXL core temp.
1115 // ip0 : VIXL core temp.
1116 //
1117 // Blocked fp registers:
1118 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001119 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1120 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001121 while (!reserved_core_registers.IsEmpty()) {
1122 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
1123 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001124
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001125 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001126 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001127 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
1128 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001129
David Brazdil58282f42016-01-14 12:45:10 +00001130 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001131 // Stubs do not save callee-save floating point registers. If the graph
1132 // is debuggable, we need to deal with these registers differently. For
1133 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001134 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1135 while (!reserved_fp_registers_debuggable.IsEmpty()) {
1136 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().code()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001137 }
1138 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001139}
1140
Alexandre Rames3e69f162014-12-10 10:36:50 +00001141size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1142 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1143 __ Str(reg, MemOperand(sp, stack_index));
1144 return kArm64WordSize;
1145}
1146
1147size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1148 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1149 __ Ldr(reg, MemOperand(sp, stack_index));
1150 return kArm64WordSize;
1151}
1152
1153size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1154 FPRegister reg = FPRegister(reg_id, kDRegSize);
1155 __ Str(reg, MemOperand(sp, stack_index));
1156 return kArm64WordSize;
1157}
1158
1159size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1160 FPRegister reg = FPRegister(reg_id, kDRegSize);
1161 __ Ldr(reg, MemOperand(sp, stack_index));
1162 return kArm64WordSize;
1163}
1164
Alexandre Rames5319def2014-10-23 10:03:10 +01001165void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001166 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001167}
1168
1169void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001170 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001171}
1172
Alexandre Rames67555f72014-11-18 10:55:16 +00001173void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001174 if (constant->IsIntConstant()) {
1175 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1176 } else if (constant->IsLongConstant()) {
1177 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1178 } else if (constant->IsNullConstant()) {
1179 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001180 } else if (constant->IsFloatConstant()) {
1181 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1182 } else {
1183 DCHECK(constant->IsDoubleConstant());
1184 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1185 }
1186}
1187
Alexandre Rames3e69f162014-12-10 10:36:50 +00001188
1189static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1190 DCHECK(constant.IsConstant());
1191 HConstant* cst = constant.GetConstant();
1192 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001193 // Null is mapped to a core W register, which we associate with kPrimInt.
1194 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001195 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1196 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1197 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1198}
1199
Calin Juravlee460d1d2015-09-29 04:52:17 +01001200void CodeGeneratorARM64::MoveLocation(Location destination,
1201 Location source,
1202 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001203 if (source.Equals(destination)) {
1204 return;
1205 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001206
1207 // A valid move can always be inferred from the destination and source
1208 // locations. When moving from and to a register, the argument type can be
1209 // used to generate 32bit instead of 64bit moves. In debug mode we also
1210 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001211 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001212
1213 if (destination.IsRegister() || destination.IsFpuRegister()) {
1214 if (unspecified_type) {
1215 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1216 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001217 (src_cst != nullptr && (src_cst->IsIntConstant()
1218 || src_cst->IsFloatConstant()
1219 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001220 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001221 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001222 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001223 // If the source is a double stack slot or a 64bit constant, a 64bit
1224 // type is appropriate. Else the source is a register, and since the
1225 // type has not been specified, we chose a 64bit type to force a 64bit
1226 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001227 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001228 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001229 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001230 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1231 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1232 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001233 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1234 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1235 __ Ldr(dst, StackOperandFrom(source));
1236 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001237 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001238 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001239 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001240 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001241 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001242 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001243 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001244 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1245 ? Primitive::kPrimLong
1246 : Primitive::kPrimInt;
1247 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1248 }
1249 } else {
1250 DCHECK(source.IsFpuRegister());
1251 if (destination.IsRegister()) {
1252 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1253 ? Primitive::kPrimDouble
1254 : Primitive::kPrimFloat;
1255 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1256 } else {
1257 DCHECK(destination.IsFpuRegister());
1258 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001259 }
1260 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001261 } else { // The destination is not a register. It must be a stack slot.
1262 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1263 if (source.IsRegister() || source.IsFpuRegister()) {
1264 if (unspecified_type) {
1265 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001266 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001267 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001268 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001269 }
1270 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001271 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1272 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1273 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001274 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001275 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1276 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001277 UseScratchRegisterScope temps(GetVIXLAssembler());
1278 HConstant* src_cst = source.GetConstant();
1279 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001280 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001281 temp = temps.AcquireW();
1282 } else if (src_cst->IsLongConstant()) {
1283 temp = temps.AcquireX();
1284 } else if (src_cst->IsFloatConstant()) {
1285 temp = temps.AcquireS();
1286 } else {
1287 DCHECK(src_cst->IsDoubleConstant());
1288 temp = temps.AcquireD();
1289 }
1290 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001291 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001292 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001293 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001294 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001295 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001296 // There is generally less pressure on FP registers.
1297 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001298 __ Ldr(temp, StackOperandFrom(source));
1299 __ Str(temp, StackOperandFrom(destination));
1300 }
1301 }
1302}
1303
1304void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001305 CPURegister dst,
1306 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001307 switch (type) {
1308 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001309 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001310 break;
1311 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001312 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001313 break;
1314 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001315 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001316 break;
1317 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001318 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001319 break;
1320 case Primitive::kPrimInt:
1321 case Primitive::kPrimNot:
1322 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001323 case Primitive::kPrimFloat:
1324 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001325 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001326 __ Ldr(dst, src);
1327 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001328 case Primitive::kPrimVoid:
1329 LOG(FATAL) << "Unreachable type " << type;
1330 }
1331}
1332
Calin Juravle77520bc2015-01-12 18:45:46 +00001333void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001334 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001335 const MemOperand& src,
1336 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001337 MacroAssembler* masm = GetVIXLAssembler();
1338 BlockPoolsScope block_pools(masm);
1339 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001340 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001341 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001342
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001343 DCHECK(!src.IsPreIndex());
1344 DCHECK(!src.IsPostIndex());
1345
1346 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001347 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001348 MemOperand base = MemOperand(temp_base);
1349 switch (type) {
1350 case Primitive::kPrimBoolean:
1351 __ Ldarb(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::kPrimByte:
1357 __ Ldarb(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::kPrimChar:
1364 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001365 if (needs_null_check) {
1366 MaybeRecordImplicitNullCheck(instruction);
1367 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001368 break;
1369 case Primitive::kPrimShort:
1370 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001371 if (needs_null_check) {
1372 MaybeRecordImplicitNullCheck(instruction);
1373 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001374 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1375 break;
1376 case Primitive::kPrimInt:
1377 case Primitive::kPrimNot:
1378 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001379 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001380 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001381 if (needs_null_check) {
1382 MaybeRecordImplicitNullCheck(instruction);
1383 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001384 break;
1385 case Primitive::kPrimFloat:
1386 case Primitive::kPrimDouble: {
1387 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001388 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001389
1390 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1391 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001392 if (needs_null_check) {
1393 MaybeRecordImplicitNullCheck(instruction);
1394 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001395 __ Fmov(FPRegister(dst), temp);
1396 break;
1397 }
1398 case Primitive::kPrimVoid:
1399 LOG(FATAL) << "Unreachable type " << type;
1400 }
1401}
1402
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001403void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001404 CPURegister src,
1405 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001406 switch (type) {
1407 case Primitive::kPrimBoolean:
1408 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001409 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001410 break;
1411 case Primitive::kPrimChar:
1412 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001413 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001414 break;
1415 case Primitive::kPrimInt:
1416 case Primitive::kPrimNot:
1417 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001418 case Primitive::kPrimFloat:
1419 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001420 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001421 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001422 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001423 case Primitive::kPrimVoid:
1424 LOG(FATAL) << "Unreachable type " << type;
1425 }
1426}
1427
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001428void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1429 CPURegister src,
1430 const MemOperand& dst) {
1431 UseScratchRegisterScope temps(GetVIXLAssembler());
1432 Register temp_base = temps.AcquireX();
1433
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001434 DCHECK(!dst.IsPreIndex());
1435 DCHECK(!dst.IsPostIndex());
1436
1437 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001438 Operand op = OperandFromMemOperand(dst);
1439 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001440 MemOperand base = MemOperand(temp_base);
1441 switch (type) {
1442 case Primitive::kPrimBoolean:
1443 case Primitive::kPrimByte:
1444 __ Stlrb(Register(src), base);
1445 break;
1446 case Primitive::kPrimChar:
1447 case Primitive::kPrimShort:
1448 __ Stlrh(Register(src), base);
1449 break;
1450 case Primitive::kPrimInt:
1451 case Primitive::kPrimNot:
1452 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001453 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001454 __ Stlr(Register(src), base);
1455 break;
1456 case Primitive::kPrimFloat:
1457 case Primitive::kPrimDouble: {
1458 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001459 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001460
1461 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1462 __ Fmov(temp, FPRegister(src));
1463 __ Stlr(temp, base);
1464 break;
1465 }
1466 case Primitive::kPrimVoid:
1467 LOG(FATAL) << "Unreachable type " << type;
1468 }
1469}
1470
Calin Juravle175dc732015-08-25 15:42:32 +01001471void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1472 HInstruction* instruction,
1473 uint32_t dex_pc,
1474 SlowPathCode* slow_path) {
1475 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1476 instruction,
1477 dex_pc,
1478 slow_path);
1479}
1480
Alexandre Rames67555f72014-11-18 10:55:16 +00001481void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1482 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001483 uint32_t dex_pc,
1484 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001485 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001486 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001487 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1488 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001489 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001490}
1491
1492void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1493 vixl::Register class_reg) {
1494 UseScratchRegisterScope temps(GetVIXLAssembler());
1495 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001496 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1497
Serban Constantinescu02164b32014-11-13 14:05:07 +00001498 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001499 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1500 __ Add(temp, class_reg, status_offset);
1501 __ Ldar(temp, HeapOperand(temp));
1502 __ Cmp(temp, mirror::Class::kStatusInitialized);
1503 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001504 __ Bind(slow_path->GetExitLabel());
1505}
Alexandre Rames5319def2014-10-23 10:03:10 +01001506
Roland Levillain44015862016-01-22 11:47:17 +00001507void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001508 BarrierType type = BarrierAll;
1509
1510 switch (kind) {
1511 case MemBarrierKind::kAnyAny:
1512 case MemBarrierKind::kAnyStore: {
1513 type = BarrierAll;
1514 break;
1515 }
1516 case MemBarrierKind::kLoadAny: {
1517 type = BarrierReads;
1518 break;
1519 }
1520 case MemBarrierKind::kStoreStore: {
1521 type = BarrierWrites;
1522 break;
1523 }
1524 default:
1525 LOG(FATAL) << "Unexpected memory barrier " << kind;
1526 }
1527 __ Dmb(InnerShareable, type);
1528}
1529
Serban Constantinescu02164b32014-11-13 14:05:07 +00001530void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1531 HBasicBlock* successor) {
1532 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001533 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1534 if (slow_path == nullptr) {
1535 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1536 instruction->SetSlowPath(slow_path);
1537 codegen_->AddSlowPath(slow_path);
1538 if (successor != nullptr) {
1539 DCHECK(successor->IsLoopHeader());
1540 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1541 }
1542 } else {
1543 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1544 }
1545
Serban Constantinescu02164b32014-11-13 14:05:07 +00001546 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1547 Register temp = temps.AcquireW();
1548
1549 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1550 if (successor == nullptr) {
1551 __ Cbnz(temp, slow_path->GetEntryLabel());
1552 __ Bind(slow_path->GetReturnLabel());
1553 } else {
1554 __ Cbz(temp, codegen_->GetLabelOf(successor));
1555 __ B(slow_path->GetEntryLabel());
1556 // slow_path will return to GetLabelOf(successor).
1557 }
1558}
1559
Alexandre Rames5319def2014-10-23 10:03:10 +01001560InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1561 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001562 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001563 assembler_(codegen->GetAssembler()),
1564 codegen_(codegen) {}
1565
1566#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001567 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001568
1569#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1570
1571enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001572 // Using a base helps identify when we hit such breakpoints.
1573 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001574#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1575 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1576#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1577};
1578
1579#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001580 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001581 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1582 } \
1583 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1584 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1585 locations->SetOut(Location::Any()); \
1586 }
1587 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1588#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1589
1590#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001591#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001592
Alexandre Rames67555f72014-11-18 10:55:16 +00001593void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001594 DCHECK_EQ(instr->InputCount(), 2U);
1595 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1596 Primitive::Type type = instr->GetResultType();
1597 switch (type) {
1598 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001599 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001600 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001601 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001602 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001603 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001604
1605 case Primitive::kPrimFloat:
1606 case Primitive::kPrimDouble:
1607 locations->SetInAt(0, Location::RequiresFpuRegister());
1608 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001609 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001610 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001611
Alexandre Rames5319def2014-10-23 10:03:10 +01001612 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001613 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001614 }
1615}
1616
Alexandre Rames09a99962015-04-15 11:47:56 +01001617void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001618 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1619
1620 bool object_field_get_with_read_barrier =
1621 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001622 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001623 new (GetGraph()->GetArena()) LocationSummary(instruction,
1624 object_field_get_with_read_barrier ?
1625 LocationSummary::kCallOnSlowPath :
1626 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001627 locations->SetInAt(0, Location::RequiresRegister());
1628 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1629 locations->SetOut(Location::RequiresFpuRegister());
1630 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001631 // The output overlaps for an object field get when read barriers
1632 // are enabled: we do not want the load to overwrite the object's
1633 // location, as we need it to emit the read barrier.
1634 locations->SetOut(
1635 Location::RequiresRegister(),
1636 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001637 }
1638}
1639
1640void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1641 const FieldInfo& field_info) {
1642 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001643 LocationSummary* locations = instruction->GetLocations();
1644 Location base_loc = locations->InAt(0);
1645 Location out = locations->Out();
1646 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001647 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001648 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001649 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001650
Roland Levillain44015862016-01-22 11:47:17 +00001651 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1652 // Object FieldGet with Baker's read barrier case.
1653 MacroAssembler* masm = GetVIXLAssembler();
1654 UseScratchRegisterScope temps(masm);
1655 // /* HeapReference<Object> */ out = *(base + offset)
1656 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1657 Register temp = temps.AcquireW();
1658 // Note that potential implicit null checks are handled in this
1659 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1660 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1661 instruction,
1662 out,
1663 base,
1664 offset,
1665 temp,
1666 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001667 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001668 } else {
1669 // General case.
1670 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001671 // Note that a potential implicit null check is handled in this
1672 // CodeGeneratorARM64::LoadAcquire call.
1673 // NB: LoadAcquire will record the pc info if needed.
1674 codegen_->LoadAcquire(
1675 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001676 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001677 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001678 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001679 }
Roland Levillain44015862016-01-22 11:47:17 +00001680 if (field_type == Primitive::kPrimNot) {
1681 // If read barriers are enabled, emit read barriers other than
1682 // Baker's using a slow path (and also unpoison the loaded
1683 // reference, if heap poisoning is enabled).
1684 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1685 }
Roland Levillain4d027112015-07-01 15:41:14 +01001686 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001687}
1688
1689void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1690 LocationSummary* locations =
1691 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1692 locations->SetInAt(0, Location::RequiresRegister());
1693 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1694 locations->SetInAt(1, Location::RequiresFpuRegister());
1695 } else {
1696 locations->SetInAt(1, Location::RequiresRegister());
1697 }
1698}
1699
1700void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001701 const FieldInfo& field_info,
1702 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001703 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001704 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001705
1706 Register obj = InputRegisterAt(instruction, 0);
1707 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001708 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001709 Offset offset = field_info.GetFieldOffset();
1710 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001711
Roland Levillain4d027112015-07-01 15:41:14 +01001712 {
1713 // We use a block to end the scratch scope before the write barrier, thus
1714 // freeing the temporary registers so they can be used in `MarkGCCard`.
1715 UseScratchRegisterScope temps(GetVIXLAssembler());
1716
1717 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1718 DCHECK(value.IsW());
1719 Register temp = temps.AcquireW();
1720 __ Mov(temp, value.W());
1721 GetAssembler()->PoisonHeapReference(temp.W());
1722 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001723 }
Roland Levillain4d027112015-07-01 15:41:14 +01001724
1725 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001726 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1727 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001728 } else {
1729 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1730 codegen_->MaybeRecordImplicitNullCheck(instruction);
1731 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001732 }
1733
1734 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001735 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001736 }
1737}
1738
Alexandre Rames67555f72014-11-18 10:55:16 +00001739void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001740 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001741
1742 switch (type) {
1743 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001744 case Primitive::kPrimLong: {
1745 Register dst = OutputRegister(instr);
1746 Register lhs = InputRegisterAt(instr, 0);
1747 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001748 if (instr->IsAdd()) {
1749 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001750 } else if (instr->IsAnd()) {
1751 __ And(dst, lhs, rhs);
1752 } else if (instr->IsOr()) {
1753 __ Orr(dst, lhs, rhs);
1754 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001755 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001756 } else if (instr->IsRor()) {
1757 if (rhs.IsImmediate()) {
1758 uint32_t shift = rhs.immediate() & (lhs.SizeInBits() - 1);
1759 __ Ror(dst, lhs, shift);
1760 } else {
1761 // Ensure shift distance is in the same size register as the result. If
1762 // we are rotating a long and the shift comes in a w register originally,
1763 // we don't need to sxtw for use as an x since the shift distances are
1764 // all & reg_bits - 1.
1765 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1766 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001767 } else {
1768 DCHECK(instr->IsXor());
1769 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001770 }
1771 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001772 }
1773 case Primitive::kPrimFloat:
1774 case Primitive::kPrimDouble: {
1775 FPRegister dst = OutputFPRegister(instr);
1776 FPRegister lhs = InputFPRegisterAt(instr, 0);
1777 FPRegister rhs = InputFPRegisterAt(instr, 1);
1778 if (instr->IsAdd()) {
1779 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001780 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001781 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001782 } else {
1783 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001784 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001785 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001786 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001787 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001788 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001789 }
1790}
1791
Serban Constantinescu02164b32014-11-13 14:05:07 +00001792void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1793 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1794
1795 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1796 Primitive::Type type = instr->GetResultType();
1797 switch (type) {
1798 case Primitive::kPrimInt:
1799 case Primitive::kPrimLong: {
1800 locations->SetInAt(0, Location::RequiresRegister());
1801 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1802 locations->SetOut(Location::RequiresRegister());
1803 break;
1804 }
1805 default:
1806 LOG(FATAL) << "Unexpected shift type " << type;
1807 }
1808}
1809
1810void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1811 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1812
1813 Primitive::Type type = instr->GetType();
1814 switch (type) {
1815 case Primitive::kPrimInt:
1816 case Primitive::kPrimLong: {
1817 Register dst = OutputRegister(instr);
1818 Register lhs = InputRegisterAt(instr, 0);
1819 Operand rhs = InputOperandAt(instr, 1);
1820 if (rhs.IsImmediate()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001821 uint32_t shift_value = rhs.immediate() &
1822 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001823 if (instr->IsShl()) {
1824 __ Lsl(dst, lhs, shift_value);
1825 } else if (instr->IsShr()) {
1826 __ Asr(dst, lhs, shift_value);
1827 } else {
1828 __ Lsr(dst, lhs, shift_value);
1829 }
1830 } else {
1831 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1832
1833 if (instr->IsShl()) {
1834 __ Lsl(dst, lhs, rhs_reg);
1835 } else if (instr->IsShr()) {
1836 __ Asr(dst, lhs, rhs_reg);
1837 } else {
1838 __ Lsr(dst, lhs, rhs_reg);
1839 }
1840 }
1841 break;
1842 }
1843 default:
1844 LOG(FATAL) << "Unexpected shift operation type " << type;
1845 }
1846}
1847
Alexandre Rames5319def2014-10-23 10:03:10 +01001848void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001849 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001850}
1851
1852void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001853 HandleBinaryOp(instruction);
1854}
1855
1856void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1857 HandleBinaryOp(instruction);
1858}
1859
1860void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1861 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001862}
1863
Artem Serov7fc63502016-02-09 17:15:29 +00001864void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001865 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1866 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1867 locations->SetInAt(0, Location::RequiresRegister());
1868 // There is no immediate variant of negated bitwise instructions in AArch64.
1869 locations->SetInAt(1, Location::RequiresRegister());
1870 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1871}
1872
Artem Serov7fc63502016-02-09 17:15:29 +00001873void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001874 Register dst = OutputRegister(instr);
1875 Register lhs = InputRegisterAt(instr, 0);
1876 Register rhs = InputRegisterAt(instr, 1);
1877
1878 switch (instr->GetOpKind()) {
1879 case HInstruction::kAnd:
1880 __ Bic(dst, lhs, rhs);
1881 break;
1882 case HInstruction::kOr:
1883 __ Orn(dst, lhs, rhs);
1884 break;
1885 case HInstruction::kXor:
1886 __ Eon(dst, lhs, rhs);
1887 break;
1888 default:
1889 LOG(FATAL) << "Unreachable";
1890 }
1891}
1892
Alexandre Rames8626b742015-11-25 16:28:08 +00001893void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1894 HArm64DataProcWithShifterOp* instruction) {
1895 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1896 instruction->GetType() == Primitive::kPrimLong);
1897 LocationSummary* locations =
1898 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1899 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1900 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1901 } else {
1902 locations->SetInAt(0, Location::RequiresRegister());
1903 }
1904 locations->SetInAt(1, Location::RequiresRegister());
1905 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1906}
1907
1908void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1909 HArm64DataProcWithShifterOp* instruction) {
1910 Primitive::Type type = instruction->GetType();
1911 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1912 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1913 Register out = OutputRegister(instruction);
1914 Register left;
1915 if (kind != HInstruction::kNeg) {
1916 left = InputRegisterAt(instruction, 0);
1917 }
1918 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1919 // shifter operand operation, the IR generating `right_reg` (input to the type
1920 // conversion) can have a different type from the current instruction's type,
1921 // so we manually indicate the type.
1922 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001923 int64_t shift_amount = instruction->GetShiftAmount() &
1924 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001925
1926 Operand right_operand(0);
1927
1928 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1929 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1930 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1931 } else {
1932 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1933 }
1934
1935 // Logical binary operations do not support extension operations in the
1936 // operand. Note that VIXL would still manage if it was passed by generating
1937 // the extension as a separate instruction.
1938 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1939 DCHECK(!right_operand.IsExtendedRegister() ||
1940 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1941 kind != HInstruction::kNeg));
1942 switch (kind) {
1943 case HInstruction::kAdd:
1944 __ Add(out, left, right_operand);
1945 break;
1946 case HInstruction::kAnd:
1947 __ And(out, left, right_operand);
1948 break;
1949 case HInstruction::kNeg:
1950 DCHECK(instruction->InputAt(0)->AsConstant()->IsZero());
1951 __ Neg(out, right_operand);
1952 break;
1953 case HInstruction::kOr:
1954 __ Orr(out, left, right_operand);
1955 break;
1956 case HInstruction::kSub:
1957 __ Sub(out, left, right_operand);
1958 break;
1959 case HInstruction::kXor:
1960 __ Eor(out, left, right_operand);
1961 break;
1962 default:
1963 LOG(FATAL) << "Unexpected operation kind: " << kind;
1964 UNREACHABLE();
1965 }
1966}
1967
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001968void LocationsBuilderARM64::VisitArm64IntermediateAddress(HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001969 // The read barrier instrumentation does not support the
1970 // HArm64IntermediateAddress instruction yet.
1971 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001972 LocationSummary* locations =
1973 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1974 locations->SetInAt(0, Location::RequiresRegister());
1975 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1976 locations->SetOut(Location::RequiresRegister());
1977}
1978
1979void InstructionCodeGeneratorARM64::VisitArm64IntermediateAddress(
1980 HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001981 // The read barrier instrumentation does not support the
1982 // HArm64IntermediateAddress instruction yet.
1983 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001984 __ Add(OutputRegister(instruction),
1985 InputRegisterAt(instruction, 0),
1986 Operand(InputOperandAt(instruction, 1)));
1987}
1988
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001989void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00001990 LocationSummary* locations =
1991 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001992 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
1993 if (instr->GetOpKind() == HInstruction::kSub &&
1994 accumulator->IsConstant() &&
1995 accumulator->AsConstant()->IsZero()) {
1996 // Don't allocate register for Mneg instruction.
1997 } else {
1998 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
1999 Location::RequiresRegister());
2000 }
2001 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2002 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002003 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2004}
2005
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002006void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002007 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002008 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2009 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002010
2011 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2012 // This fixup should be carried out for all multiply-accumulate instructions:
2013 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2014 if (instr->GetType() == Primitive::kPrimLong &&
2015 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2016 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
2017 vixl::Instruction* prev = masm->GetCursorAddress<vixl::Instruction*>() - vixl::kInstructionSize;
2018 if (prev->IsLoadOrStore()) {
2019 // Make sure we emit only exactly one nop.
2020 vixl::CodeBufferCheckScope scope(masm,
2021 vixl::kInstructionSize,
2022 vixl::CodeBufferCheckScope::kCheck,
2023 vixl::CodeBufferCheckScope::kExactSize);
2024 __ nop();
2025 }
2026 }
2027
2028 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002029 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002030 __ Madd(res, mul_left, mul_right, accumulator);
2031 } else {
2032 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002033 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2034 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsZero()) {
2035 __ Mneg(res, mul_left, mul_right);
2036 } else {
2037 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2038 __ Msub(res, mul_left, mul_right, accumulator);
2039 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002040 }
2041}
2042
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002043void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002044 bool object_array_get_with_read_barrier =
2045 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002046 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002047 new (GetGraph()->GetArena()) LocationSummary(instruction,
2048 object_array_get_with_read_barrier ?
2049 LocationSummary::kCallOnSlowPath :
2050 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002051 locations->SetInAt(0, Location::RequiresRegister());
2052 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002053 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2054 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2055 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002056 // The output overlaps in the case of an object array get with
2057 // read barriers enabled: we do not want the move to overwrite the
2058 // array's location, as we need it to emit the read barrier.
2059 locations->SetOut(
2060 Location::RequiresRegister(),
2061 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002062 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002063}
2064
2065void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002066 Primitive::Type type = instruction->GetType();
2067 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002068 LocationSummary* locations = instruction->GetLocations();
2069 Location index = locations->InAt(1);
2070 uint32_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Roland Levillain44015862016-01-22 11:47:17 +00002071 Location out = locations->Out();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002072
Alexandre Ramesd921d642015-04-16 15:07:16 +01002073 MacroAssembler* masm = GetVIXLAssembler();
2074 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002075 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002076 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002077
Roland Levillain44015862016-01-22 11:47:17 +00002078 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2079 // Object ArrayGet with Baker's read barrier case.
2080 Register temp = temps.AcquireW();
2081 // The read barrier instrumentation does not support the
2082 // HArm64IntermediateAddress instruction yet.
2083 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
2084 // Note that a potential implicit null check is handled in the
2085 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2086 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2087 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002088 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002089 // General case.
2090 MemOperand source = HeapOperand(obj);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002091 if (index.IsConstant()) {
Roland Levillain44015862016-01-22 11:47:17 +00002092 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2093 source = HeapOperand(obj, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002094 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002095 Register temp = temps.AcquireSameSizeAs(obj);
2096 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
2097 // The read barrier instrumentation does not support the
2098 // HArm64IntermediateAddress instruction yet.
2099 DCHECK(!kEmitCompilerReadBarrier);
2100 // We do not need to compute the intermediate address from the array: the
2101 // input instruction has done it already. See the comment in
2102 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2103 if (kIsDebugBuild) {
2104 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2105 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2106 }
2107 temp = obj;
2108 } else {
2109 __ Add(temp, obj, offset);
2110 }
2111 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2112 }
2113
2114 codegen_->Load(type, OutputCPURegister(instruction), source);
2115 codegen_->MaybeRecordImplicitNullCheck(instruction);
2116
2117 if (type == Primitive::kPrimNot) {
2118 static_assert(
2119 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2120 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2121 Location obj_loc = locations->InAt(0);
2122 if (index.IsConstant()) {
2123 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2124 } else {
2125 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2126 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002127 }
Roland Levillain4d027112015-07-01 15:41:14 +01002128 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002129}
2130
Alexandre Rames5319def2014-10-23 10:03:10 +01002131void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2132 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2133 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002134 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002135}
2136
2137void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01002138 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01002139 __ Ldr(OutputRegister(instruction),
2140 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00002141 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002142}
2143
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002144void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002145 Primitive::Type value_type = instruction->GetComponentType();
2146
2147 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2148 bool object_array_set_with_read_barrier =
2149 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002150 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2151 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002152 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2153 LocationSummary::kCallOnSlowPath :
2154 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002155 locations->SetInAt(0, Location::RequiresRegister());
2156 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002157 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002158 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002159 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002160 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002161 }
2162}
2163
2164void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2165 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002166 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002167 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002168 bool needs_write_barrier =
2169 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002170
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002171 Register array = InputRegisterAt(instruction, 0);
2172 CPURegister value = InputCPURegisterAt(instruction, 2);
2173 CPURegister source = value;
2174 Location index = locations->InAt(1);
2175 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2176 MemOperand destination = HeapOperand(array);
2177 MacroAssembler* masm = GetVIXLAssembler();
2178 BlockPoolsScope block_pools(masm);
2179
2180 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002181 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002182 if (index.IsConstant()) {
2183 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2184 destination = HeapOperand(array, offset);
2185 } else {
2186 UseScratchRegisterScope temps(masm);
2187 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002188 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002189 // The read barrier instrumentation does not support the
2190 // HArm64IntermediateAddress instruction yet.
2191 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002192 // We do not need to compute the intermediate address from the array: the
2193 // input instruction has done it already. See the comment in
2194 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2195 if (kIsDebugBuild) {
2196 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2197 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2198 }
2199 temp = array;
2200 } else {
2201 __ Add(temp, array, offset);
2202 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002203 destination = HeapOperand(temp,
2204 XRegisterFrom(index),
2205 LSL,
2206 Primitive::ComponentSizeShift(value_type));
2207 }
2208 codegen_->Store(value_type, value, destination);
2209 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002210 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002211 DCHECK(needs_write_barrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002212 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002213 vixl::Label done;
2214 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002215 {
2216 // We use a block to end the scratch scope before the write barrier, thus
2217 // freeing the temporary registers so they can be used in `MarkGCCard`.
2218 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002219 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002220 if (index.IsConstant()) {
2221 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002222 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002223 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002224 destination = HeapOperand(temp,
2225 XRegisterFrom(index),
2226 LSL,
2227 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002228 }
2229
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002230 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2231 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2232 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2233
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002234 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002235 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2236 codegen_->AddSlowPath(slow_path);
2237 if (instruction->GetValueCanBeNull()) {
2238 vixl::Label non_zero;
2239 __ Cbnz(Register(value), &non_zero);
2240 if (!index.IsConstant()) {
2241 __ Add(temp, array, offset);
2242 }
2243 __ Str(wzr, destination);
2244 codegen_->MaybeRecordImplicitNullCheck(instruction);
2245 __ B(&done);
2246 __ Bind(&non_zero);
2247 }
2248
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002249 if (kEmitCompilerReadBarrier) {
2250 // When read barriers are enabled, the type checking
2251 // instrumentation requires two read barriers:
2252 //
2253 // __ Mov(temp2, temp);
2254 // // /* HeapReference<Class> */ temp = temp->component_type_
2255 // __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002256 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002257 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2258 //
2259 // // /* HeapReference<Class> */ temp2 = value->klass_
2260 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002261 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002262 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2263 //
2264 // __ Cmp(temp, temp2);
2265 //
2266 // However, the second read barrier may trash `temp`, as it
2267 // is a temporary register, and as such would not be saved
2268 // along with live registers before calling the runtime (nor
2269 // restored afterwards). So in this case, we bail out and
2270 // delegate the work to the array set slow path.
2271 //
2272 // TODO: Extend the register allocator to support a new
2273 // "(locally) live temp" location so as to avoid always
2274 // going into the slow path when read barriers are enabled.
2275 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002276 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002277 Register temp2 = temps.AcquireSameSizeAs(array);
2278 // /* HeapReference<Class> */ temp = array->klass_
2279 __ Ldr(temp, HeapOperand(array, class_offset));
2280 codegen_->MaybeRecordImplicitNullCheck(instruction);
2281 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2282
2283 // /* HeapReference<Class> */ temp = temp->component_type_
2284 __ Ldr(temp, HeapOperand(temp, component_offset));
2285 // /* HeapReference<Class> */ temp2 = value->klass_
2286 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2287 // If heap poisoning is enabled, no need to unpoison `temp`
2288 // nor `temp2`, as we are comparing two poisoned references.
2289 __ Cmp(temp, temp2);
2290
2291 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2292 vixl::Label do_put;
2293 __ B(eq, &do_put);
2294 // If heap poisoning is enabled, the `temp` reference has
2295 // not been unpoisoned yet; unpoison it now.
2296 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2297
2298 // /* HeapReference<Class> */ temp = temp->super_class_
2299 __ Ldr(temp, HeapOperand(temp, super_offset));
2300 // If heap poisoning is enabled, no need to unpoison
2301 // `temp`, as we are comparing against null below.
2302 __ Cbnz(temp, slow_path->GetEntryLabel());
2303 __ Bind(&do_put);
2304 } else {
2305 __ B(ne, slow_path->GetEntryLabel());
2306 }
2307 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002308 }
2309 }
2310
2311 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002312 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002313 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002314 __ Mov(temp2, value.W());
2315 GetAssembler()->PoisonHeapReference(temp2);
2316 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002317 }
2318
2319 if (!index.IsConstant()) {
2320 __ Add(temp, array, offset);
2321 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002322 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002323
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002324 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002325 codegen_->MaybeRecordImplicitNullCheck(instruction);
2326 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002327 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002328
2329 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2330
2331 if (done.IsLinked()) {
2332 __ Bind(&done);
2333 }
2334
2335 if (slow_path != nullptr) {
2336 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002337 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002338 }
2339}
2340
Alexandre Rames67555f72014-11-18 10:55:16 +00002341void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002342 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2343 ? LocationSummary::kCallOnSlowPath
2344 : LocationSummary::kNoCall;
2345 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002346 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002347 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002348 if (instruction->HasUses()) {
2349 locations->SetOut(Location::SameAsFirstInput());
2350 }
2351}
2352
2353void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002354 BoundsCheckSlowPathARM64* slow_path =
2355 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002356 codegen_->AddSlowPath(slow_path);
2357
2358 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2359 __ B(slow_path->GetEntryLabel(), hs);
2360}
2361
Alexandre Rames67555f72014-11-18 10:55:16 +00002362void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2363 LocationSummary* locations =
2364 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2365 locations->SetInAt(0, Location::RequiresRegister());
2366 if (check->HasUses()) {
2367 locations->SetOut(Location::SameAsFirstInput());
2368 }
2369}
2370
2371void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2372 // We assume the class is not null.
2373 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2374 check->GetLoadClass(), check, check->GetDexPc(), true);
2375 codegen_->AddSlowPath(slow_path);
2376 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2377}
2378
Roland Levillain7f63c522015-07-13 15:54:55 +00002379static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
2380 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
2381 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
2382}
2383
Serban Constantinescu02164b32014-11-13 14:05:07 +00002384void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002385 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002386 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2387 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002388 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002389 case Primitive::kPrimBoolean:
2390 case Primitive::kPrimByte:
2391 case Primitive::kPrimShort:
2392 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002393 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002394 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002395 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002396 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002397 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2398 break;
2399 }
2400 case Primitive::kPrimFloat:
2401 case Primitive::kPrimDouble: {
2402 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002403 locations->SetInAt(1,
2404 IsFloatingPointZeroConstant(compare->InputAt(1))
2405 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2406 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002407 locations->SetOut(Location::RequiresRegister());
2408 break;
2409 }
2410 default:
2411 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2412 }
2413}
2414
2415void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2416 Primitive::Type in_type = compare->InputAt(0)->GetType();
2417
2418 // 0 if: left == right
2419 // 1 if: left > right
2420 // -1 if: left < right
2421 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002422 case Primitive::kPrimBoolean:
2423 case Primitive::kPrimByte:
2424 case Primitive::kPrimShort:
2425 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002426 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002427 case Primitive::kPrimLong: {
2428 Register result = OutputRegister(compare);
2429 Register left = InputRegisterAt(compare, 0);
2430 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002431 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002432 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2433 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002434 break;
2435 }
2436 case Primitive::kPrimFloat:
2437 case Primitive::kPrimDouble: {
2438 Register result = OutputRegister(compare);
2439 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00002440 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002441 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
2442 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00002443 __ Fcmp(left, 0.0);
2444 } else {
2445 __ Fcmp(left, InputFPRegisterAt(compare, 1));
2446 }
Vladimir Markod6e069b2016-01-18 11:11:01 +00002447 __ Cset(result, ne);
2448 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002449 break;
2450 }
2451 default:
2452 LOG(FATAL) << "Unimplemented compare type " << in_type;
2453 }
2454}
2455
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002456void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002457 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002458
2459 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2460 locations->SetInAt(0, Location::RequiresFpuRegister());
2461 locations->SetInAt(1,
2462 IsFloatingPointZeroConstant(instruction->InputAt(1))
2463 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2464 : Location::RequiresFpuRegister());
2465 } else {
2466 // Integer cases.
2467 locations->SetInAt(0, Location::RequiresRegister());
2468 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2469 }
2470
David Brazdilb3e773e2016-01-26 11:28:37 +00002471 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002472 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002473 }
2474}
2475
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002476void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002477 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002478 return;
2479 }
2480
2481 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002482 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002483 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002484
Roland Levillain7f63c522015-07-13 15:54:55 +00002485 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2486 FPRegister lhs = InputFPRegisterAt(instruction, 0);
2487 if (locations->InAt(1).IsConstant()) {
2488 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
2489 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2490 __ Fcmp(lhs, 0.0);
2491 } else {
2492 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
2493 }
Vladimir Markod6e069b2016-01-18 11:11:01 +00002494 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002495 } else {
2496 // Integer cases.
2497 Register lhs = InputRegisterAt(instruction, 0);
2498 Operand rhs = InputOperandAt(instruction, 1);
2499 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002500 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002501 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002502}
2503
2504#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2505 M(Equal) \
2506 M(NotEqual) \
2507 M(LessThan) \
2508 M(LessThanOrEqual) \
2509 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002510 M(GreaterThanOrEqual) \
2511 M(Below) \
2512 M(BelowOrEqual) \
2513 M(Above) \
2514 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002515#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002516void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2517void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002518FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002519#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002520#undef FOR_EACH_CONDITION_INSTRUCTION
2521
Zheng Xuc6667102015-05-15 16:08:45 +08002522void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2523 DCHECK(instruction->IsDiv() || instruction->IsRem());
2524
2525 LocationSummary* locations = instruction->GetLocations();
2526 Location second = locations->InAt(1);
2527 DCHECK(second.IsConstant());
2528
2529 Register out = OutputRegister(instruction);
2530 Register dividend = InputRegisterAt(instruction, 0);
2531 int64_t imm = Int64FromConstant(second.GetConstant());
2532 DCHECK(imm == 1 || imm == -1);
2533
2534 if (instruction->IsRem()) {
2535 __ Mov(out, 0);
2536 } else {
2537 if (imm == 1) {
2538 __ Mov(out, dividend);
2539 } else {
2540 __ Neg(out, dividend);
2541 }
2542 }
2543}
2544
2545void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2546 DCHECK(instruction->IsDiv() || instruction->IsRem());
2547
2548 LocationSummary* locations = instruction->GetLocations();
2549 Location second = locations->InAt(1);
2550 DCHECK(second.IsConstant());
2551
2552 Register out = OutputRegister(instruction);
2553 Register dividend = InputRegisterAt(instruction, 0);
2554 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002555 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002556 int ctz_imm = CTZ(abs_imm);
2557
2558 UseScratchRegisterScope temps(GetVIXLAssembler());
2559 Register temp = temps.AcquireSameSizeAs(out);
2560
2561 if (instruction->IsDiv()) {
2562 __ Add(temp, dividend, abs_imm - 1);
2563 __ Cmp(dividend, 0);
2564 __ Csel(out, temp, dividend, lt);
2565 if (imm > 0) {
2566 __ Asr(out, out, ctz_imm);
2567 } else {
2568 __ Neg(out, Operand(out, ASR, ctz_imm));
2569 }
2570 } else {
2571 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2572 __ Asr(temp, dividend, bits - 1);
2573 __ Lsr(temp, temp, bits - ctz_imm);
2574 __ Add(out, dividend, temp);
2575 __ And(out, out, abs_imm - 1);
2576 __ Sub(out, out, temp);
2577 }
2578}
2579
2580void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2581 DCHECK(instruction->IsDiv() || instruction->IsRem());
2582
2583 LocationSummary* locations = instruction->GetLocations();
2584 Location second = locations->InAt(1);
2585 DCHECK(second.IsConstant());
2586
2587 Register out = OutputRegister(instruction);
2588 Register dividend = InputRegisterAt(instruction, 0);
2589 int64_t imm = Int64FromConstant(second.GetConstant());
2590
2591 Primitive::Type type = instruction->GetResultType();
2592 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2593
2594 int64_t magic;
2595 int shift;
2596 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2597
2598 UseScratchRegisterScope temps(GetVIXLAssembler());
2599 Register temp = temps.AcquireSameSizeAs(out);
2600
2601 // temp = get_high(dividend * magic)
2602 __ Mov(temp, magic);
2603 if (type == Primitive::kPrimLong) {
2604 __ Smulh(temp, dividend, temp);
2605 } else {
2606 __ Smull(temp.X(), dividend, temp);
2607 __ Lsr(temp.X(), temp.X(), 32);
2608 }
2609
2610 if (imm > 0 && magic < 0) {
2611 __ Add(temp, temp, dividend);
2612 } else if (imm < 0 && magic > 0) {
2613 __ Sub(temp, temp, dividend);
2614 }
2615
2616 if (shift != 0) {
2617 __ Asr(temp, temp, shift);
2618 }
2619
2620 if (instruction->IsDiv()) {
2621 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2622 } else {
2623 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2624 // TODO: Strength reduction for msub.
2625 Register temp_imm = temps.AcquireSameSizeAs(out);
2626 __ Mov(temp_imm, imm);
2627 __ Msub(out, temp, temp_imm, dividend);
2628 }
2629}
2630
2631void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2632 DCHECK(instruction->IsDiv() || instruction->IsRem());
2633 Primitive::Type type = instruction->GetResultType();
2634 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2635
2636 LocationSummary* locations = instruction->GetLocations();
2637 Register out = OutputRegister(instruction);
2638 Location second = locations->InAt(1);
2639
2640 if (second.IsConstant()) {
2641 int64_t imm = Int64FromConstant(second.GetConstant());
2642
2643 if (imm == 0) {
2644 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2645 } else if (imm == 1 || imm == -1) {
2646 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002647 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002648 DivRemByPowerOfTwo(instruction);
2649 } else {
2650 DCHECK(imm <= -2 || imm >= 2);
2651 GenerateDivRemWithAnyConstant(instruction);
2652 }
2653 } else {
2654 Register dividend = InputRegisterAt(instruction, 0);
2655 Register divisor = InputRegisterAt(instruction, 1);
2656 if (instruction->IsDiv()) {
2657 __ Sdiv(out, dividend, divisor);
2658 } else {
2659 UseScratchRegisterScope temps(GetVIXLAssembler());
2660 Register temp = temps.AcquireSameSizeAs(out);
2661 __ Sdiv(temp, dividend, divisor);
2662 __ Msub(out, temp, divisor, dividend);
2663 }
2664 }
2665}
2666
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002667void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2668 LocationSummary* locations =
2669 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2670 switch (div->GetResultType()) {
2671 case Primitive::kPrimInt:
2672 case Primitive::kPrimLong:
2673 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002674 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002675 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2676 break;
2677
2678 case Primitive::kPrimFloat:
2679 case Primitive::kPrimDouble:
2680 locations->SetInAt(0, Location::RequiresFpuRegister());
2681 locations->SetInAt(1, Location::RequiresFpuRegister());
2682 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2683 break;
2684
2685 default:
2686 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2687 }
2688}
2689
2690void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2691 Primitive::Type type = div->GetResultType();
2692 switch (type) {
2693 case Primitive::kPrimInt:
2694 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002695 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002696 break;
2697
2698 case Primitive::kPrimFloat:
2699 case Primitive::kPrimDouble:
2700 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2701 break;
2702
2703 default:
2704 LOG(FATAL) << "Unexpected div type " << type;
2705 }
2706}
2707
Alexandre Rames67555f72014-11-18 10:55:16 +00002708void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002709 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2710 ? LocationSummary::kCallOnSlowPath
2711 : LocationSummary::kNoCall;
2712 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002713 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2714 if (instruction->HasUses()) {
2715 locations->SetOut(Location::SameAsFirstInput());
2716 }
2717}
2718
2719void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2720 SlowPathCodeARM64* slow_path =
2721 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2722 codegen_->AddSlowPath(slow_path);
2723 Location value = instruction->GetLocations()->InAt(0);
2724
Alexandre Rames3e69f162014-12-10 10:36:50 +00002725 Primitive::Type type = instruction->GetType();
2726
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002727 if (!Primitive::IsIntegralType(type)) {
2728 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002729 return;
2730 }
2731
Alexandre Rames67555f72014-11-18 10:55:16 +00002732 if (value.IsConstant()) {
2733 int64_t divisor = Int64ConstantFrom(value);
2734 if (divisor == 0) {
2735 __ B(slow_path->GetEntryLabel());
2736 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002737 // A division by a non-null constant is valid. We don't need to perform
2738 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002739 }
2740 } else {
2741 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2742 }
2743}
2744
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002745void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2746 LocationSummary* locations =
2747 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2748 locations->SetOut(Location::ConstantLocation(constant));
2749}
2750
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002751void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2752 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002753 // Will be generated at use site.
2754}
2755
Alexandre Rames5319def2014-10-23 10:03:10 +01002756void LocationsBuilderARM64::VisitExit(HExit* exit) {
2757 exit->SetLocations(nullptr);
2758}
2759
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002760void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002761}
2762
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002763void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2764 LocationSummary* locations =
2765 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2766 locations->SetOut(Location::ConstantLocation(constant));
2767}
2768
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002769void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002770 // Will be generated at use site.
2771}
2772
David Brazdilfc6a86a2015-06-26 10:33:45 +00002773void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002774 DCHECK(!successor->IsExitBlock());
2775 HBasicBlock* block = got->GetBlock();
2776 HInstruction* previous = got->GetPrevious();
2777 HLoopInformation* info = block->GetLoopInformation();
2778
David Brazdil46e2a392015-03-16 17:31:52 +00002779 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002780 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2781 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2782 return;
2783 }
2784 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2785 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2786 }
2787 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002788 __ B(codegen_->GetLabelOf(successor));
2789 }
2790}
2791
David Brazdilfc6a86a2015-06-26 10:33:45 +00002792void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2793 got->SetLocations(nullptr);
2794}
2795
2796void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2797 HandleGoto(got, got->GetSuccessor());
2798}
2799
2800void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2801 try_boundary->SetLocations(nullptr);
2802}
2803
2804void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2805 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2806 if (!successor->IsExitBlock()) {
2807 HandleGoto(try_boundary, successor);
2808 }
2809}
2810
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002811void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002812 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002813 vixl::Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002814 vixl::Label* false_target) {
2815 // FP branching requires both targets to be explicit. If either of the targets
2816 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2817 vixl::Label fallthrough_target;
2818 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002819
David Brazdil0debae72015-11-12 18:37:00 +00002820 if (true_target == nullptr && false_target == nullptr) {
2821 // Nothing to do. The code always falls through.
2822 return;
2823 } else if (cond->IsIntConstant()) {
2824 // Constant condition, statically compared against 1.
2825 if (cond->AsIntConstant()->IsOne()) {
2826 if (true_target != nullptr) {
2827 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002828 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002829 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002830 DCHECK(cond->AsIntConstant()->IsZero());
2831 if (false_target != nullptr) {
2832 __ B(false_target);
2833 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002834 }
David Brazdil0debae72015-11-12 18:37:00 +00002835 return;
2836 }
2837
2838 // The following code generates these patterns:
2839 // (1) true_target == nullptr && false_target != nullptr
2840 // - opposite condition true => branch to false_target
2841 // (2) true_target != nullptr && false_target == nullptr
2842 // - condition true => branch to true_target
2843 // (3) true_target != nullptr && false_target != nullptr
2844 // - condition true => branch to true_target
2845 // - branch to false_target
2846 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002847 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002848 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002849 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002850 if (true_target == nullptr) {
2851 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2852 } else {
2853 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2854 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002855 } else {
2856 // The condition instruction has not been materialized, use its inputs as
2857 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002858 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002859
David Brazdil0debae72015-11-12 18:37:00 +00002860 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002861 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002862 FPRegister lhs = InputFPRegisterAt(condition, 0);
2863 if (condition->GetLocations()->InAt(1).IsConstant()) {
2864 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2865 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2866 __ Fcmp(lhs, 0.0);
2867 } else {
2868 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2869 }
David Brazdil0debae72015-11-12 18:37:00 +00002870 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002871 IfCondition opposite_condition = condition->GetOppositeCondition();
2872 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002873 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002874 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002875 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002876 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002877 // Integer cases.
2878 Register lhs = InputRegisterAt(condition, 0);
2879 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002880
2881 Condition arm64_cond;
2882 vixl::Label* non_fallthrough_target;
2883 if (true_target == nullptr) {
2884 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2885 non_fallthrough_target = false_target;
2886 } else {
2887 arm64_cond = ARM64Condition(condition->GetCondition());
2888 non_fallthrough_target = true_target;
2889 }
2890
Aart Bik086d27e2016-01-20 17:02:00 -08002891 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
2892 rhs.IsImmediate() && (rhs.immediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002893 switch (arm64_cond) {
2894 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002895 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002896 break;
2897 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002898 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002899 break;
2900 case lt:
2901 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002902 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002903 break;
2904 case ge:
2905 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002906 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002907 break;
2908 default:
2909 // Without the `static_cast` the compiler throws an error for
2910 // `-Werror=sign-promo`.
2911 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2912 }
2913 } else {
2914 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002915 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002916 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002917 }
2918 }
David Brazdil0debae72015-11-12 18:37:00 +00002919
2920 // If neither branch falls through (case 3), the conditional branch to `true_target`
2921 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2922 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002923 __ B(false_target);
2924 }
David Brazdil0debae72015-11-12 18:37:00 +00002925
2926 if (fallthrough_target.IsLinked()) {
2927 __ Bind(&fallthrough_target);
2928 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002929}
2930
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002931void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2932 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002933 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002934 locations->SetInAt(0, Location::RequiresRegister());
2935 }
2936}
2937
2938void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002939 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2940 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2941 vixl::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2942 nullptr : codegen_->GetLabelOf(true_successor);
2943 vixl::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2944 nullptr : codegen_->GetLabelOf(false_successor);
2945 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002946}
2947
2948void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2949 LocationSummary* locations = new (GetGraph()->GetArena())
2950 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002951 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002952 locations->SetInAt(0, Location::RequiresRegister());
2953 }
2954}
2955
2956void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002957 SlowPathCodeARM64* slow_path =
2958 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002959 GenerateTestAndBranch(deoptimize,
2960 /* condition_input_index */ 0,
2961 slow_path->GetEntryLabel(),
2962 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002963}
2964
David Brazdilc0b601b2016-02-08 14:20:45 +00002965enum SelectVariant {
2966 kCsel,
2967 kCselFalseConst,
2968 kCselTrueConst,
2969 kFcsel,
2970};
2971
2972static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
2973 return condition->IsCondition() &&
2974 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
2975}
2976
2977static inline bool IsRecognizedCselConstant(HInstruction* constant) {
2978 if (constant->IsConstant()) {
2979 int64_t value = Int64FromConstant(constant->AsConstant());
2980 if ((value == -1) || (value == 0) || (value == 1)) {
2981 return true;
2982 }
2983 }
2984 return false;
2985}
2986
2987static inline SelectVariant GetSelectVariant(HSelect* select) {
2988 if (Primitive::IsFloatingPointType(select->GetType())) {
2989 return kFcsel;
2990 } else if (IsRecognizedCselConstant(select->GetFalseValue())) {
2991 return kCselFalseConst;
2992 } else if (IsRecognizedCselConstant(select->GetTrueValue())) {
2993 return kCselTrueConst;
2994 } else {
2995 return kCsel;
2996 }
2997}
2998
2999static inline bool HasSwappedInputs(SelectVariant variant) {
3000 return variant == kCselTrueConst;
3001}
3002
3003static inline Condition GetConditionForSelect(HCondition* condition, SelectVariant variant) {
3004 IfCondition cond = HasSwappedInputs(variant) ? condition->GetOppositeCondition()
3005 : condition->GetCondition();
3006 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3007 : ARM64Condition(cond);
3008}
3009
David Brazdil74eb1b22015-12-14 11:44:01 +00003010void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3011 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
David Brazdilc0b601b2016-02-08 14:20:45 +00003012 switch (GetSelectVariant(select)) {
3013 case kCsel:
3014 locations->SetInAt(0, Location::RequiresRegister());
3015 locations->SetInAt(1, Location::RequiresRegister());
3016 locations->SetOut(Location::RequiresRegister());
3017 break;
3018 case kCselFalseConst:
3019 locations->SetInAt(0, Location::ConstantLocation(select->InputAt(0)->AsConstant()));
3020 locations->SetInAt(1, Location::RequiresRegister());
3021 locations->SetOut(Location::RequiresRegister());
3022 break;
3023 case kCselTrueConst:
3024 locations->SetInAt(0, Location::RequiresRegister());
3025 locations->SetInAt(1, Location::ConstantLocation(select->InputAt(1)->AsConstant()));
3026 locations->SetOut(Location::RequiresRegister());
3027 break;
3028 case kFcsel:
3029 locations->SetInAt(0, Location::RequiresFpuRegister());
3030 locations->SetInAt(1, Location::RequiresFpuRegister());
3031 locations->SetOut(Location::RequiresFpuRegister());
3032 break;
David Brazdil74eb1b22015-12-14 11:44:01 +00003033 }
3034 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3035 locations->SetInAt(2, Location::RequiresRegister());
3036 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003037}
3038
3039void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003040 HInstruction* cond = select->GetCondition();
3041 SelectVariant variant = GetSelectVariant(select);
3042 Condition csel_cond;
3043
3044 if (IsBooleanValueOrMaterializedCondition(cond)) {
3045 if (cond->IsCondition() && cond->GetNext() == select) {
3046 // Condition codes set from previous instruction.
3047 csel_cond = GetConditionForSelect(cond->AsCondition(), variant);
3048 } else {
3049 __ Cmp(InputRegisterAt(select, 2), 0);
3050 csel_cond = HasSwappedInputs(variant) ? eq : ne;
3051 }
3052 } else if (IsConditionOnFloatingPointValues(cond)) {
3053 Location rhs = cond->GetLocations()->InAt(1);
3054 if (rhs.IsConstant()) {
3055 DCHECK(IsFloatingPointZeroConstant(rhs.GetConstant()));
3056 __ Fcmp(InputFPRegisterAt(cond, 0), 0.0);
3057 } else {
3058 __ Fcmp(InputFPRegisterAt(cond, 0), InputFPRegisterAt(cond, 1));
3059 }
3060 csel_cond = GetConditionForSelect(cond->AsCondition(), variant);
3061 } else {
3062 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
3063 csel_cond = GetConditionForSelect(cond->AsCondition(), variant);
3064 }
3065
3066 switch (variant) {
3067 case kCsel:
3068 case kCselFalseConst:
3069 __ Csel(OutputRegister(select),
3070 InputRegisterAt(select, 1),
3071 InputOperandAt(select, 0),
3072 csel_cond);
3073 break;
3074 case kCselTrueConst:
3075 __ Csel(OutputRegister(select),
3076 InputRegisterAt(select, 0),
3077 InputOperandAt(select, 1),
3078 csel_cond);
3079 break;
3080 case kFcsel:
3081 __ Fcsel(OutputFPRegister(select),
3082 InputFPRegisterAt(select, 1),
3083 InputFPRegisterAt(select, 0),
3084 csel_cond);
3085 break;
3086 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003087}
3088
David Srbecky0cf44932015-12-09 14:09:59 +00003089void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3090 new (GetGraph()->GetArena()) LocationSummary(info);
3091}
3092
David Srbeckyd28f4a02016-03-14 17:14:24 +00003093void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3094 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003095}
3096
3097void CodeGeneratorARM64::GenerateNop() {
3098 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003099}
3100
Alexandre Rames5319def2014-10-23 10:03:10 +01003101void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003102 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003103}
3104
3105void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003106 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003107}
3108
3109void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003110 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003111}
3112
3113void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003114 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003115}
3116
Roland Levillain44015862016-01-22 11:47:17 +00003117static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3118 return kEmitCompilerReadBarrier &&
3119 (kUseBakerReadBarrier ||
3120 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3121 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3122 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3123}
3124
Alexandre Rames67555f72014-11-18 10:55:16 +00003125void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003126 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003127 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3128 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003129 case TypeCheckKind::kExactCheck:
3130 case TypeCheckKind::kAbstractClassCheck:
3131 case TypeCheckKind::kClassHierarchyCheck:
3132 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003133 call_kind =
3134 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003135 break;
3136 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003137 case TypeCheckKind::kUnresolvedCheck:
3138 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003139 call_kind = LocationSummary::kCallOnSlowPath;
3140 break;
3141 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003142
Alexandre Rames67555f72014-11-18 10:55:16 +00003143 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003144 locations->SetInAt(0, Location::RequiresRegister());
3145 locations->SetInAt(1, Location::RequiresRegister());
3146 // The "out" register is used as a temporary, so it overlaps with the inputs.
3147 // Note that TypeCheckSlowPathARM64 uses this register too.
3148 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3149 // When read barriers are enabled, we need a temporary register for
3150 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003151 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003152 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003153 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003154}
3155
3156void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003157 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003158 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003159 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003160 Register obj = InputRegisterAt(instruction, 0);
3161 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003162 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003163 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003164 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3165 locations->GetTemp(0) :
3166 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003167 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3168 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3169 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3170 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003171
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003172 vixl::Label done, zero;
3173 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003174
3175 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003176 // Avoid null check if we know `obj` is not null.
3177 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003178 __ Cbz(obj, &zero);
3179 }
3180
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003181 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003182 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003183
Roland Levillain44015862016-01-22 11:47:17 +00003184 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003185 case TypeCheckKind::kExactCheck: {
3186 __ Cmp(out, cls);
3187 __ Cset(out, eq);
3188 if (zero.IsLinked()) {
3189 __ B(&done);
3190 }
3191 break;
3192 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003193
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003194 case TypeCheckKind::kAbstractClassCheck: {
3195 // If the class is abstract, we eagerly fetch the super class of the
3196 // object to avoid doing a comparison we know will fail.
3197 vixl::Label loop, success;
3198 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003199 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003200 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003201 // If `out` is null, we use it for the result, and jump to `done`.
3202 __ Cbz(out, &done);
3203 __ Cmp(out, cls);
3204 __ B(ne, &loop);
3205 __ Mov(out, 1);
3206 if (zero.IsLinked()) {
3207 __ B(&done);
3208 }
3209 break;
3210 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003211
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003212 case TypeCheckKind::kClassHierarchyCheck: {
3213 // Walk over the class hierarchy to find a match.
3214 vixl::Label loop, success;
3215 __ Bind(&loop);
3216 __ Cmp(out, cls);
3217 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003218 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003219 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003220 __ Cbnz(out, &loop);
3221 // If `out` is null, we use it for the result, and jump to `done`.
3222 __ B(&done);
3223 __ Bind(&success);
3224 __ Mov(out, 1);
3225 if (zero.IsLinked()) {
3226 __ B(&done);
3227 }
3228 break;
3229 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003230
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003231 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003232 // Do an exact check.
3233 vixl::Label exact_check;
3234 __ Cmp(out, cls);
3235 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003236 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003237 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003238 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003239 // If `out` is null, we use it for the result, and jump to `done`.
3240 __ Cbz(out, &done);
3241 __ Ldrh(out, HeapOperand(out, primitive_offset));
3242 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3243 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003244 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003245 __ Mov(out, 1);
3246 __ B(&done);
3247 break;
3248 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003249
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003250 case TypeCheckKind::kArrayCheck: {
3251 __ Cmp(out, cls);
3252 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003253 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3254 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003255 codegen_->AddSlowPath(slow_path);
3256 __ B(ne, slow_path->GetEntryLabel());
3257 __ Mov(out, 1);
3258 if (zero.IsLinked()) {
3259 __ B(&done);
3260 }
3261 break;
3262 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003263
Calin Juravle98893e12015-10-02 21:05:03 +01003264 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003265 case TypeCheckKind::kInterfaceCheck: {
3266 // Note that we indeed only call on slow path, but we always go
3267 // into the slow path for the unresolved and interface check
3268 // cases.
3269 //
3270 // We cannot directly call the InstanceofNonTrivial runtime
3271 // entry point without resorting to a type checking slow path
3272 // here (i.e. by calling InvokeRuntime directly), as it would
3273 // require to assign fixed registers for the inputs of this
3274 // HInstanceOf instruction (following the runtime calling
3275 // convention), which might be cluttered by the potential first
3276 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003277 //
3278 // TODO: Introduce a new runtime entry point taking the object
3279 // to test (instead of its class) as argument, and let it deal
3280 // with the read barrier issues. This will let us refactor this
3281 // case of the `switch` code as it was previously (with a direct
3282 // call to the runtime not using a type checking slow path).
3283 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003284 DCHECK(locations->OnlyCallsOnSlowPath());
3285 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3286 /* is_fatal */ false);
3287 codegen_->AddSlowPath(slow_path);
3288 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003289 if (zero.IsLinked()) {
3290 __ B(&done);
3291 }
3292 break;
3293 }
3294 }
3295
3296 if (zero.IsLinked()) {
3297 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003298 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003299 }
3300
3301 if (done.IsLinked()) {
3302 __ Bind(&done);
3303 }
3304
3305 if (slow_path != nullptr) {
3306 __ Bind(slow_path->GetExitLabel());
3307 }
3308}
3309
3310void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3311 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3312 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3313
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003314 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3315 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003316 case TypeCheckKind::kExactCheck:
3317 case TypeCheckKind::kAbstractClassCheck:
3318 case TypeCheckKind::kClassHierarchyCheck:
3319 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003320 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3321 LocationSummary::kCallOnSlowPath :
3322 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003323 break;
3324 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003325 case TypeCheckKind::kUnresolvedCheck:
3326 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003327 call_kind = LocationSummary::kCallOnSlowPath;
3328 break;
3329 }
3330
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003331 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3332 locations->SetInAt(0, Location::RequiresRegister());
3333 locations->SetInAt(1, Location::RequiresRegister());
3334 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3335 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003336 // When read barriers are enabled, we need an additional temporary
3337 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003338 if (TypeCheckNeedsATemporary(type_check_kind)) {
3339 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003340 }
3341}
3342
3343void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003344 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003345 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003346 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003347 Register obj = InputRegisterAt(instruction, 0);
3348 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003349 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003350 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3351 locations->GetTemp(1) :
3352 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003353 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003354 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3355 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3356 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3357 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003358
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003359 bool is_type_check_slow_path_fatal =
3360 (type_check_kind == TypeCheckKind::kExactCheck ||
3361 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3362 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3363 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3364 !instruction->CanThrowIntoCatchBlock();
3365 SlowPathCodeARM64* type_check_slow_path =
3366 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3367 is_type_check_slow_path_fatal);
3368 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003369
3370 vixl::Label done;
3371 // Avoid null check if we know obj is not null.
3372 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003373 __ Cbz(obj, &done);
3374 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003375
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003376 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003377 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003378
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003379 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003380 case TypeCheckKind::kExactCheck:
3381 case TypeCheckKind::kArrayCheck: {
3382 __ Cmp(temp, cls);
3383 // Jump to slow path for throwing the exception or doing a
3384 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003385 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003386 break;
3387 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003388
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003389 case TypeCheckKind::kAbstractClassCheck: {
3390 // If the class is abstract, we eagerly fetch the super class of the
3391 // object to avoid doing a comparison we know will fail.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003392 vixl::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003393 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003394 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003395 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003396
3397 // If the class reference currently in `temp` is not null, jump
3398 // to the `compare_classes` label to compare it with the checked
3399 // class.
3400 __ Cbnz(temp, &compare_classes);
3401 // Otherwise, jump to the slow path to throw the exception.
3402 //
3403 // But before, move back the object's class into `temp` before
3404 // going into the slow path, as it has been overwritten in the
3405 // meantime.
3406 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003407 GenerateReferenceLoadTwoRegisters(
3408 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003409 __ B(type_check_slow_path->GetEntryLabel());
3410
3411 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003412 __ Cmp(temp, cls);
3413 __ B(ne, &loop);
3414 break;
3415 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003416
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003417 case TypeCheckKind::kClassHierarchyCheck: {
3418 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003419 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003420 __ Bind(&loop);
3421 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003422 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003423
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003424 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003425 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003426
3427 // If the class reference currently in `temp` is not null, jump
3428 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003429 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003430 // Otherwise, jump to the slow path to throw the exception.
3431 //
3432 // But before, move back the object's class into `temp` before
3433 // going into the slow path, as it has been overwritten in the
3434 // meantime.
3435 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003436 GenerateReferenceLoadTwoRegisters(
3437 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003438 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003439 break;
3440 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003441
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003442 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003443 // Do an exact check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003444 vixl::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003445 __ Cmp(temp, cls);
3446 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003447
3448 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003449 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003450 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003451
3452 // If the component type is not null (i.e. the object is indeed
3453 // an array), jump to label `check_non_primitive_component_type`
3454 // to further check that this component type is not a primitive
3455 // type.
3456 __ Cbnz(temp, &check_non_primitive_component_type);
3457 // Otherwise, jump to the slow path to throw the exception.
3458 //
3459 // But before, move back the object's class into `temp` before
3460 // going into the slow path, as it has been overwritten in the
3461 // meantime.
3462 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003463 GenerateReferenceLoadTwoRegisters(
3464 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003465 __ B(type_check_slow_path->GetEntryLabel());
3466
3467 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003468 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3469 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003470 __ Cbz(temp, &done);
3471 // Same comment as above regarding `temp` and the slow path.
3472 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003473 GenerateReferenceLoadTwoRegisters(
3474 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003475 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003476 break;
3477 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003478
Calin Juravle98893e12015-10-02 21:05:03 +01003479 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003480 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003481 // We always go into the type check slow path for the unresolved
3482 // and interface check cases.
3483 //
3484 // We cannot directly call the CheckCast runtime entry point
3485 // without resorting to a type checking slow path here (i.e. by
3486 // calling InvokeRuntime directly), as it would require to
3487 // assign fixed registers for the inputs of this HInstanceOf
3488 // instruction (following the runtime calling convention), which
3489 // might be cluttered by the potential first read barrier
3490 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003491 //
3492 // TODO: Introduce a new runtime entry point taking the object
3493 // to test (instead of its class) as argument, and let it deal
3494 // with the read barrier issues. This will let us refactor this
3495 // case of the `switch` code as it was previously (with a direct
3496 // call to the runtime not using a type checking slow path).
3497 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003498 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003499 break;
3500 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003501 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003502
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003503 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003504}
3505
Alexandre Rames5319def2014-10-23 10:03:10 +01003506void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3507 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3508 locations->SetOut(Location::ConstantLocation(constant));
3509}
3510
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003511void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003512 // Will be generated at use site.
3513}
3514
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003515void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3516 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3517 locations->SetOut(Location::ConstantLocation(constant));
3518}
3519
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003520void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003521 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003522}
3523
Calin Juravle175dc732015-08-25 15:42:32 +01003524void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3525 // The trampoline uses the same calling convention as dex calling conventions,
3526 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3527 // the method_idx.
3528 HandleInvoke(invoke);
3529}
3530
3531void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3532 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3533}
3534
Alexandre Rames5319def2014-10-23 10:03:10 +01003535void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003536 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003537 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003538}
3539
Alexandre Rames67555f72014-11-18 10:55:16 +00003540void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3541 HandleInvoke(invoke);
3542}
3543
3544void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3545 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003546 LocationSummary* locations = invoke->GetLocations();
3547 Register temp = XRegisterFrom(locations->GetTemp(0));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003548 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3549 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003550 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003551 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003552 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003553
3554 // The register ip1 is required to be used for the hidden argument in
3555 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003556 MacroAssembler* masm = GetVIXLAssembler();
3557 UseScratchRegisterScope scratch_scope(masm);
3558 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003559 scratch_scope.Exclude(ip1);
3560 __ Mov(ip1, invoke->GetDexMethodIndex());
3561
Alexandre Rames67555f72014-11-18 10:55:16 +00003562 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003563 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003564 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003565 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003566 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003567 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003568 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003569 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003570 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003571 // Instead of simply (possibly) unpoisoning `temp` here, we should
3572 // emit a read barrier for the previous class reference load.
3573 // However this is not required in practice, as this is an
3574 // intermediate/temporary reference and because the current
3575 // concurrent copying collector keeps the from-space memory
3576 // intact/accessible until the end of the marking phase (the
3577 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003578 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00003579 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003580 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003581 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003582 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003583 // lr();
3584 __ Blr(lr);
3585 DCHECK(!codegen_->IsLeafMethod());
3586 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3587}
3588
3589void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003590 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3591 if (intrinsic.TryDispatch(invoke)) {
3592 return;
3593 }
3594
Alexandre Rames67555f72014-11-18 10:55:16 +00003595 HandleInvoke(invoke);
3596}
3597
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003598void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003599 // Explicit clinit checks triggered by static invokes must have been pruned by
3600 // art::PrepareForRegisterAllocation.
3601 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003602
Andreas Gampe878d58c2015-01-15 23:24:00 -08003603 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3604 if (intrinsic.TryDispatch(invoke)) {
3605 return;
3606 }
3607
Alexandre Rames67555f72014-11-18 10:55:16 +00003608 HandleInvoke(invoke);
3609}
3610
Andreas Gampe878d58c2015-01-15 23:24:00 -08003611static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3612 if (invoke->GetLocations()->Intrinsified()) {
3613 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3614 intrinsic.Dispatch(invoke);
3615 return true;
3616 }
3617 return false;
3618}
3619
Vladimir Markodc151b22015-10-15 18:02:30 +01003620HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3621 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3622 MethodReference target_method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003623 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003624 return desired_dispatch_info;
3625}
3626
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003627void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003628 // For better instruction scheduling we load the direct code pointer before the method pointer.
3629 bool direct_code_loaded = false;
3630 switch (invoke->GetCodePtrLocation()) {
3631 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3632 // LR = code address from literal pool with link-time patch.
3633 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3634 direct_code_loaded = true;
3635 break;
3636 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3637 // LR = invoke->GetDirectCodePtr();
3638 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3639 direct_code_loaded = true;
3640 break;
3641 default:
3642 break;
3643 }
3644
Andreas Gampe878d58c2015-01-15 23:24:00 -08003645 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003646 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3647 switch (invoke->GetMethodLoadKind()) {
3648 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3649 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003650 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003651 break;
3652 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003653 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003654 break;
3655 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3656 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003657 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003658 break;
3659 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3660 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003661 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003662 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3663 break;
3664 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3665 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003666 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3667 invoke->GetDexCacheArrayOffset());
3668 vixl::Label* pc_insn_label = &pc_relative_dex_cache_patches_.back().label;
Vladimir Marko58155012015-08-19 12:49:41 +00003669 {
3670 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003671 __ Bind(pc_insn_label);
3672 __ adrp(XRegisterFrom(temp), 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003673 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003674 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
Vladimir Marko58155012015-08-19 12:49:41 +00003675 // Add LDR with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003676 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3677 invoke->GetDexCacheArrayOffset());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003678 {
3679 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3680 __ Bind(&pc_relative_dex_cache_patches_.back().label);
3681 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), 0));
3682 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
3683 }
Vladimir Marko58155012015-08-19 12:49:41 +00003684 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003685 }
Vladimir Marko58155012015-08-19 12:49:41 +00003686 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003687 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003688 Register reg = XRegisterFrom(temp);
3689 Register method_reg;
3690 if (current_method.IsRegister()) {
3691 method_reg = XRegisterFrom(current_method);
3692 } else {
3693 DCHECK(invoke->GetLocations()->Intrinsified());
3694 DCHECK(!current_method.IsValid());
3695 method_reg = reg;
3696 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3697 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003698
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003699 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003700 __ Ldr(reg.X(),
3701 MemOperand(method_reg.X(),
3702 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003703 // temp = temp[index_in_cache];
3704 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3705 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3706 break;
3707 }
3708 }
3709
3710 switch (invoke->GetCodePtrLocation()) {
3711 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3712 __ Bl(&frame_entry_label_);
3713 break;
3714 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3715 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3716 vixl::Label* label = &relative_call_patches_.back().label;
Alexandre Rames6dc01742015-11-12 14:44:19 +00003717 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3718 __ Bind(label);
3719 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003720 break;
3721 }
3722 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3723 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3724 // LR prepared above for better instruction scheduling.
3725 DCHECK(direct_code_loaded);
3726 // lr()
3727 __ Blr(lr);
3728 break;
3729 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3730 // LR = callee_method->entry_point_from_quick_compiled_code_;
3731 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003732 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003733 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3734 // lr()
3735 __ Blr(lr);
3736 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003737 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003738
Andreas Gampe878d58c2015-01-15 23:24:00 -08003739 DCHECK(!IsLeafMethod());
3740}
3741
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003742void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003743 // Use the calling convention instead of the location of the receiver, as
3744 // intrinsics may have put the receiver in a different register. In the intrinsics
3745 // slow path, the arguments have been moved to the right place, so here we are
3746 // guaranteed that the receiver is the first register of the calling convention.
3747 InvokeDexCallingConvention calling_convention;
3748 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003749 Register temp = XRegisterFrom(temp_in);
3750 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3751 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3752 Offset class_offset = mirror::Object::ClassOffset();
3753 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3754
3755 BlockPoolsScope block_pools(GetVIXLAssembler());
3756
3757 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003758 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003759 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003760 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003761 // Instead of simply (possibly) unpoisoning `temp` here, we should
3762 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003763 // intermediate/temporary reference and because the current
3764 // concurrent copying collector keeps the from-space memory
3765 // intact/accessible until the end of the marking phase (the
3766 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003767 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3768 // temp = temp->GetMethodAt(method_offset);
3769 __ Ldr(temp, MemOperand(temp, method_offset));
3770 // lr = temp->GetEntryPoint();
3771 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3772 // lr();
3773 __ Blr(lr);
3774}
3775
Vladimir Marko58155012015-08-19 12:49:41 +00003776void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3777 DCHECK(linker_patches->empty());
3778 size_t size =
3779 method_patches_.size() +
3780 call_patches_.size() +
3781 relative_call_patches_.size() +
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003782 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003783 linker_patches->reserve(size);
3784 for (const auto& entry : method_patches_) {
3785 const MethodReference& target_method = entry.first;
3786 vixl::Literal<uint64_t>* literal = entry.second;
3787 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
3788 target_method.dex_file,
3789 target_method.dex_method_index));
3790 }
3791 for (const auto& entry : call_patches_) {
3792 const MethodReference& target_method = entry.first;
3793 vixl::Literal<uint64_t>* literal = entry.second;
3794 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
3795 target_method.dex_file,
3796 target_method.dex_method_index));
3797 }
3798 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003799 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003800 info.target_method.dex_file,
3801 info.target_method.dex_method_index));
3802 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003803 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003804 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003805 &info.target_dex_file,
Alexandre Rames6dc01742015-11-12 14:44:19 +00003806 info.pc_insn_label->location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003807 info.element_offset));
3808 }
3809}
3810
3811vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
3812 // Look up the literal for value.
3813 auto lb = uint64_literals_.lower_bound(value);
3814 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
3815 return lb->second;
3816 }
3817 // We don't have a literal for this value, insert a new one.
3818 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
3819 uint64_literals_.PutBefore(lb, value, literal);
3820 return literal;
3821}
3822
3823vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
3824 MethodReference target_method,
3825 MethodToLiteralMap* map) {
3826 // Look up the literal for target_method.
3827 auto lb = map->lower_bound(target_method);
3828 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
3829 return lb->second;
3830 }
3831 // We don't have a literal for this method yet, insert a new one.
3832 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
3833 map->PutBefore(lb, target_method, literal);
3834 return literal;
3835}
3836
3837vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
3838 MethodReference target_method) {
3839 return DeduplicateMethodLiteral(target_method, &method_patches_);
3840}
3841
3842vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
3843 MethodReference target_method) {
3844 return DeduplicateMethodLiteral(target_method, &call_patches_);
3845}
3846
3847
Andreas Gampe878d58c2015-01-15 23:24:00 -08003848void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003849 // Explicit clinit checks triggered by static invokes must have been pruned by
3850 // art::PrepareForRegisterAllocation.
3851 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003852
Andreas Gampe878d58c2015-01-15 23:24:00 -08003853 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3854 return;
3855 }
3856
Alexandre Ramesd921d642015-04-16 15:07:16 +01003857 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003858 LocationSummary* locations = invoke->GetLocations();
3859 codegen_->GenerateStaticOrDirectCall(
3860 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003861 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003862}
3863
3864void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003865 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3866 return;
3867 }
3868
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003869 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003870 DCHECK(!codegen_->IsLeafMethod());
3871 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3872}
3873
Alexandre Rames67555f72014-11-18 10:55:16 +00003874void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003875 InvokeRuntimeCallingConvention calling_convention;
3876 CodeGenerator::CreateLoadClassLocationSummary(
3877 cls,
3878 LocationFrom(calling_convention.GetRegisterAt(0)),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003879 LocationFrom(vixl::x0),
3880 /* code_generator_supports_read_barrier */ true);
Alexandre Rames67555f72014-11-18 10:55:16 +00003881}
3882
3883void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003884 if (cls->NeedsAccessCheck()) {
3885 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3886 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3887 cls,
3888 cls->GetDexPc(),
3889 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003890 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003891 return;
3892 }
3893
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003894 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01003895 Register out = OutputRegister(cls);
3896 Register current_method = InputRegisterAt(cls, 0);
3897 if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003898 DCHECK(!cls->CanCallRuntime());
3899 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain44015862016-01-22 11:47:17 +00003900 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3901 GenerateGcRootFieldLoad(
3902 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Alexandre Rames67555f72014-11-18 10:55:16 +00003903 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003904 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003905 // /* GcRoot<mirror::Class>[] */ out =
3906 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Vladimir Marko05792b92015-08-03 11:56:49 +01003907 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00003908 // /* GcRoot<mirror::Class> */ out = out[type_index]
3909 GenerateGcRootFieldLoad(
3910 cls, out_loc, out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003911
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003912 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3913 DCHECK(cls->CanCallRuntime());
3914 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3915 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3916 codegen_->AddSlowPath(slow_path);
3917 if (!cls->IsInDexCache()) {
3918 __ Cbz(out, slow_path->GetEntryLabel());
3919 }
3920 if (cls->MustGenerateClinitCheck()) {
3921 GenerateClassInitializationCheck(slow_path, out);
3922 } else {
3923 __ Bind(slow_path->GetExitLabel());
3924 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003925 }
3926 }
3927}
3928
David Brazdilcb1c0552015-08-04 16:22:25 +01003929static MemOperand GetExceptionTlsAddress() {
3930 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3931}
3932
Alexandre Rames67555f72014-11-18 10:55:16 +00003933void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3934 LocationSummary* locations =
3935 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3936 locations->SetOut(Location::RequiresRegister());
3937}
3938
3939void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003940 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3941}
3942
3943void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3944 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3945}
3946
3947void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3948 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003949}
3950
Alexandre Rames5319def2014-10-23 10:03:10 +01003951void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3952 load->SetLocations(nullptr);
3953}
3954
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003955void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003956 // Nothing to do, this is driven by the code generator.
3957}
3958
Alexandre Rames67555f72014-11-18 10:55:16 +00003959void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003960 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
3961 ? LocationSummary::kCallOnSlowPath
3962 : LocationSummary::kNoCall;
3963 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003964 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003965 locations->SetOut(Location::RequiresRegister());
3966}
3967
3968void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003969 Location out_loc = load->GetLocations()->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003970 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003971 Register current_method = InputRegisterAt(load, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003972
Roland Levillain44015862016-01-22 11:47:17 +00003973 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3974 GenerateGcRootFieldLoad(
3975 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003976 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
3977 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00003978 // /* GcRoot<mirror::String> */ out = out[string_index]
3979 GenerateGcRootFieldLoad(
3980 load, out_loc, out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003981
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003982 if (!load->IsInDexCache()) {
3983 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3984 codegen_->AddSlowPath(slow_path);
3985 __ Cbz(out, slow_path->GetEntryLabel());
3986 __ Bind(slow_path->GetExitLabel());
3987 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003988}
3989
Alexandre Rames5319def2014-10-23 10:03:10 +01003990void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3991 local->SetLocations(nullptr);
3992}
3993
3994void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3995 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3996}
3997
3998void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3999 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4000 locations->SetOut(Location::ConstantLocation(constant));
4001}
4002
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004003void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004004 // Will be generated at use site.
4005}
4006
Alexandre Rames67555f72014-11-18 10:55:16 +00004007void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4008 LocationSummary* locations =
4009 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4010 InvokeRuntimeCallingConvention calling_convention;
4011 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4012}
4013
4014void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4015 codegen_->InvokeRuntime(instruction->IsEnter()
4016 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4017 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004018 instruction->GetDexPc(),
4019 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004020 if (instruction->IsEnter()) {
4021 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4022 } else {
4023 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4024 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004025}
4026
Alexandre Rames42d641b2014-10-27 14:00:51 +00004027void LocationsBuilderARM64::VisitMul(HMul* mul) {
4028 LocationSummary* locations =
4029 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4030 switch (mul->GetResultType()) {
4031 case Primitive::kPrimInt:
4032 case Primitive::kPrimLong:
4033 locations->SetInAt(0, Location::RequiresRegister());
4034 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004035 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004036 break;
4037
4038 case Primitive::kPrimFloat:
4039 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004040 locations->SetInAt(0, Location::RequiresFpuRegister());
4041 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004042 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004043 break;
4044
4045 default:
4046 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4047 }
4048}
4049
4050void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4051 switch (mul->GetResultType()) {
4052 case Primitive::kPrimInt:
4053 case Primitive::kPrimLong:
4054 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4055 break;
4056
4057 case Primitive::kPrimFloat:
4058 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004059 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004060 break;
4061
4062 default:
4063 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4064 }
4065}
4066
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004067void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4068 LocationSummary* locations =
4069 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4070 switch (neg->GetResultType()) {
4071 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004072 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004073 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004074 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004075 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004076
4077 case Primitive::kPrimFloat:
4078 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004079 locations->SetInAt(0, Location::RequiresFpuRegister());
4080 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004081 break;
4082
4083 default:
4084 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4085 }
4086}
4087
4088void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4089 switch (neg->GetResultType()) {
4090 case Primitive::kPrimInt:
4091 case Primitive::kPrimLong:
4092 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4093 break;
4094
4095 case Primitive::kPrimFloat:
4096 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004097 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004098 break;
4099
4100 default:
4101 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4102 }
4103}
4104
4105void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4106 LocationSummary* locations =
4107 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4108 InvokeRuntimeCallingConvention calling_convention;
4109 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004110 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004111 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004112 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004113}
4114
4115void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4116 LocationSummary* locations = instruction->GetLocations();
4117 InvokeRuntimeCallingConvention calling_convention;
4118 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4119 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004120 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004121 // Note: if heap poisoning is enabled, the entry point takes cares
4122 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004123 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4124 instruction,
4125 instruction->GetDexPc(),
4126 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004127 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004128}
4129
Alexandre Rames5319def2014-10-23 10:03:10 +01004130void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4131 LocationSummary* locations =
4132 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4133 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004134 if (instruction->IsStringAlloc()) {
4135 locations->AddTemp(LocationFrom(kArtMethodRegister));
4136 } else {
4137 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4138 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4139 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004140 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4141}
4142
4143void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004144 // Note: if heap poisoning is enabled, the entry point takes cares
4145 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004146 if (instruction->IsStringAlloc()) {
4147 // String is allocated through StringFactory. Call NewEmptyString entry point.
4148 Location temp = instruction->GetLocations()->GetTemp(0);
4149 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
4150 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4151 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4152 __ Blr(lr);
4153 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4154 } else {
4155 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4156 instruction,
4157 instruction->GetDexPc(),
4158 nullptr);
4159 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4160 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004161}
4162
4163void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4164 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004165 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004166 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004167}
4168
4169void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004170 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004171 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004172 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004173 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004174 break;
4175
4176 default:
4177 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4178 }
4179}
4180
David Brazdil66d126e2015-04-03 16:02:44 +01004181void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4182 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4183 locations->SetInAt(0, Location::RequiresRegister());
4184 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4185}
4186
4187void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01004188 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
4189}
4190
Alexandre Rames5319def2014-10-23 10:03:10 +01004191void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004192 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4193 ? LocationSummary::kCallOnSlowPath
4194 : LocationSummary::kNoCall;
4195 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004196 locations->SetInAt(0, Location::RequiresRegister());
4197 if (instruction->HasUses()) {
4198 locations->SetOut(Location::SameAsFirstInput());
4199 }
4200}
4201
Calin Juravle2ae48182016-03-16 14:05:09 +00004202void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4203 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004204 return;
4205 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004206
Alexandre Ramesd921d642015-04-16 15:07:16 +01004207 BlockPoolsScope block_pools(GetVIXLAssembler());
4208 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004209 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004210 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004211}
4212
Calin Juravle2ae48182016-03-16 14:05:09 +00004213void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004214 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004215 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004216
4217 LocationSummary* locations = instruction->GetLocations();
4218 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004219
4220 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004221}
4222
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004223void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004224 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004225}
4226
Alexandre Rames67555f72014-11-18 10:55:16 +00004227void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4228 HandleBinaryOp(instruction);
4229}
4230
4231void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4232 HandleBinaryOp(instruction);
4233}
4234
Alexandre Rames3e69f162014-12-10 10:36:50 +00004235void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4236 LOG(FATAL) << "Unreachable";
4237}
4238
4239void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4240 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4241}
4242
Alexandre Rames5319def2014-10-23 10:03:10 +01004243void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4244 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4245 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4246 if (location.IsStackSlot()) {
4247 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4248 } else if (location.IsDoubleStackSlot()) {
4249 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4250 }
4251 locations->SetOut(location);
4252}
4253
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004254void InstructionCodeGeneratorARM64::VisitParameterValue(
4255 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004256 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004257}
4258
4259void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4260 LocationSummary* locations =
4261 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004262 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004263}
4264
4265void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4266 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4267 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004268}
4269
4270void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4271 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4272 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4273 locations->SetInAt(i, Location::Any());
4274 }
4275 locations->SetOut(Location::Any());
4276}
4277
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004278void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004279 LOG(FATAL) << "Unreachable";
4280}
4281
Serban Constantinescu02164b32014-11-13 14:05:07 +00004282void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004283 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004284 LocationSummary::CallKind call_kind =
4285 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004286 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4287
4288 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004289 case Primitive::kPrimInt:
4290 case Primitive::kPrimLong:
4291 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004292 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004293 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4294 break;
4295
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004296 case Primitive::kPrimFloat:
4297 case Primitive::kPrimDouble: {
4298 InvokeRuntimeCallingConvention calling_convention;
4299 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4300 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4301 locations->SetOut(calling_convention.GetReturnLocation(type));
4302
4303 break;
4304 }
4305
Serban Constantinescu02164b32014-11-13 14:05:07 +00004306 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004307 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004308 }
4309}
4310
4311void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4312 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004313
Serban Constantinescu02164b32014-11-13 14:05:07 +00004314 switch (type) {
4315 case Primitive::kPrimInt:
4316 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004317 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004318 break;
4319 }
4320
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004321 case Primitive::kPrimFloat:
4322 case Primitive::kPrimDouble: {
4323 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4324 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004325 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004326 if (type == Primitive::kPrimFloat) {
4327 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4328 } else {
4329 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4330 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004331 break;
4332 }
4333
Serban Constantinescu02164b32014-11-13 14:05:07 +00004334 default:
4335 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004336 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004337 }
4338}
4339
Calin Juravle27df7582015-04-17 19:12:31 +01004340void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4341 memory_barrier->SetLocations(nullptr);
4342}
4343
4344void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004345 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004346}
4347
Alexandre Rames5319def2014-10-23 10:03:10 +01004348void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4349 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4350 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004351 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004352}
4353
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004354void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004355 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004356}
4357
4358void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4359 instruction->SetLocations(nullptr);
4360}
4361
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004362void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004363 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004364}
4365
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004366void LocationsBuilderARM64::VisitRor(HRor* ror) {
4367 HandleBinaryOp(ror);
4368}
4369
4370void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4371 HandleBinaryOp(ror);
4372}
4373
Serban Constantinescu02164b32014-11-13 14:05:07 +00004374void LocationsBuilderARM64::VisitShl(HShl* shl) {
4375 HandleShift(shl);
4376}
4377
4378void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4379 HandleShift(shl);
4380}
4381
4382void LocationsBuilderARM64::VisitShr(HShr* shr) {
4383 HandleShift(shr);
4384}
4385
4386void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4387 HandleShift(shr);
4388}
4389
Alexandre Rames5319def2014-10-23 10:03:10 +01004390void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
4391 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
4392 Primitive::Type field_type = store->InputAt(1)->GetType();
4393 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004394 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01004395 case Primitive::kPrimBoolean:
4396 case Primitive::kPrimByte:
4397 case Primitive::kPrimChar:
4398 case Primitive::kPrimShort:
4399 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004400 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01004401 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
4402 break;
4403
4404 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004405 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01004406 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
4407 break;
4408
4409 default:
4410 LOG(FATAL) << "Unimplemented local type " << field_type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004411 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +01004412 }
4413}
4414
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004415void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004416}
4417
4418void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004419 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004420}
4421
4422void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004423 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004424}
4425
Alexandre Rames67555f72014-11-18 10:55:16 +00004426void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004427 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004428}
4429
4430void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004431 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004432}
4433
4434void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004435 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004436}
4437
Alexandre Rames67555f72014-11-18 10:55:16 +00004438void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004439 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004440}
4441
Calin Juravlee460d1d2015-09-29 04:52:17 +01004442void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4443 HUnresolvedInstanceFieldGet* instruction) {
4444 FieldAccessCallingConventionARM64 calling_convention;
4445 codegen_->CreateUnresolvedFieldLocationSummary(
4446 instruction, instruction->GetFieldType(), calling_convention);
4447}
4448
4449void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4450 HUnresolvedInstanceFieldGet* instruction) {
4451 FieldAccessCallingConventionARM64 calling_convention;
4452 codegen_->GenerateUnresolvedFieldAccess(instruction,
4453 instruction->GetFieldType(),
4454 instruction->GetFieldIndex(),
4455 instruction->GetDexPc(),
4456 calling_convention);
4457}
4458
4459void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4460 HUnresolvedInstanceFieldSet* instruction) {
4461 FieldAccessCallingConventionARM64 calling_convention;
4462 codegen_->CreateUnresolvedFieldLocationSummary(
4463 instruction, instruction->GetFieldType(), calling_convention);
4464}
4465
4466void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4467 HUnresolvedInstanceFieldSet* instruction) {
4468 FieldAccessCallingConventionARM64 calling_convention;
4469 codegen_->GenerateUnresolvedFieldAccess(instruction,
4470 instruction->GetFieldType(),
4471 instruction->GetFieldIndex(),
4472 instruction->GetDexPc(),
4473 calling_convention);
4474}
4475
4476void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4477 HUnresolvedStaticFieldGet* instruction) {
4478 FieldAccessCallingConventionARM64 calling_convention;
4479 codegen_->CreateUnresolvedFieldLocationSummary(
4480 instruction, instruction->GetFieldType(), calling_convention);
4481}
4482
4483void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4484 HUnresolvedStaticFieldGet* instruction) {
4485 FieldAccessCallingConventionARM64 calling_convention;
4486 codegen_->GenerateUnresolvedFieldAccess(instruction,
4487 instruction->GetFieldType(),
4488 instruction->GetFieldIndex(),
4489 instruction->GetDexPc(),
4490 calling_convention);
4491}
4492
4493void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4494 HUnresolvedStaticFieldSet* instruction) {
4495 FieldAccessCallingConventionARM64 calling_convention;
4496 codegen_->CreateUnresolvedFieldLocationSummary(
4497 instruction, instruction->GetFieldType(), calling_convention);
4498}
4499
4500void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4501 HUnresolvedStaticFieldSet* instruction) {
4502 FieldAccessCallingConventionARM64 calling_convention;
4503 codegen_->GenerateUnresolvedFieldAccess(instruction,
4504 instruction->GetFieldType(),
4505 instruction->GetFieldIndex(),
4506 instruction->GetDexPc(),
4507 calling_convention);
4508}
4509
Alexandre Rames5319def2014-10-23 10:03:10 +01004510void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4511 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4512}
4513
4514void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004515 HBasicBlock* block = instruction->GetBlock();
4516 if (block->GetLoopInformation() != nullptr) {
4517 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4518 // The back edge will generate the suspend check.
4519 return;
4520 }
4521 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4522 // The goto will generate the suspend check.
4523 return;
4524 }
4525 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004526}
4527
Alexandre Rames67555f72014-11-18 10:55:16 +00004528void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4529 LocationSummary* locations =
4530 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4531 InvokeRuntimeCallingConvention calling_convention;
4532 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4533}
4534
4535void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4536 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004537 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004538 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004539}
4540
4541void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4542 LocationSummary* locations =
4543 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4544 Primitive::Type input_type = conversion->GetInputType();
4545 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004546 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004547 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4548 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4549 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4550 }
4551
Alexandre Rames542361f2015-01-29 16:57:31 +00004552 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004553 locations->SetInAt(0, Location::RequiresFpuRegister());
4554 } else {
4555 locations->SetInAt(0, Location::RequiresRegister());
4556 }
4557
Alexandre Rames542361f2015-01-29 16:57:31 +00004558 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004559 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4560 } else {
4561 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4562 }
4563}
4564
4565void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4566 Primitive::Type result_type = conversion->GetResultType();
4567 Primitive::Type input_type = conversion->GetInputType();
4568
4569 DCHECK_NE(input_type, result_type);
4570
Alexandre Rames542361f2015-01-29 16:57:31 +00004571 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004572 int result_size = Primitive::ComponentSize(result_type);
4573 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004574 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004575 Register output = OutputRegister(conversion);
4576 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004577 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004578 // 'int' values are used directly as W registers, discarding the top
4579 // bits, so we don't need to sign-extend and can just perform a move.
4580 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4581 // top 32 bits of the target register. We theoretically could leave those
4582 // bits unchanged, but we would have to make sure that no code uses a
4583 // 32bit input value as a 64bit value assuming that the top 32 bits are
4584 // zero.
4585 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004586 } else if (result_type == Primitive::kPrimChar ||
4587 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4588 __ Ubfx(output,
4589 output.IsX() ? source.X() : source.W(),
4590 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004591 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004592 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004593 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004594 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004595 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004596 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004597 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4598 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004599 } else if (Primitive::IsFloatingPointType(result_type) &&
4600 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004601 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4602 } else {
4603 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4604 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004605 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004606}
Alexandre Rames67555f72014-11-18 10:55:16 +00004607
Serban Constantinescu02164b32014-11-13 14:05:07 +00004608void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4609 HandleShift(ushr);
4610}
4611
4612void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4613 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004614}
4615
4616void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4617 HandleBinaryOp(instruction);
4618}
4619
4620void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4621 HandleBinaryOp(instruction);
4622}
4623
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004624void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004625 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004626 LOG(FATAL) << "Unreachable";
4627}
4628
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004629void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004630 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004631 LOG(FATAL) << "Unreachable";
4632}
4633
Mark Mendellfe57faa2015-09-18 09:26:15 -04004634// Simple implementation of packed switch - generate cascaded compare/jumps.
4635void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4636 LocationSummary* locations =
4637 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4638 locations->SetInAt(0, Location::RequiresRegister());
4639}
4640
4641void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4642 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004643 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004644 Register value_reg = InputRegisterAt(switch_instr, 0);
4645 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4646
Zheng Xu3927c8b2015-11-18 17:46:25 +08004647 // Roughly set 16 as max average assemblies generated per HIR in a graph.
4648 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * vixl::kInstructionSize;
4649 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4650 // make sure we don't emit it if the target may run out of range.
4651 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4652 // ranges and emit the tables only as required.
4653 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004654
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004655 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004656 // Current instruction id is an upper bound of the number of HIRs in the graph.
4657 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4658 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004659 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4660 Register temp = temps.AcquireW();
4661 __ Subs(temp, value_reg, Operand(lower_bound));
4662
Zheng Xu3927c8b2015-11-18 17:46:25 +08004663 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004664 // Jump to successors[0] if value == lower_bound.
4665 __ B(eq, codegen_->GetLabelOf(successors[0]));
4666 int32_t last_index = 0;
4667 for (; num_entries - last_index > 2; last_index += 2) {
4668 __ Subs(temp, temp, Operand(2));
4669 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4670 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4671 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4672 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4673 }
4674 if (num_entries - last_index == 2) {
4675 // The last missing case_value.
4676 __ Cmp(temp, Operand(1));
4677 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004678 }
4679
4680 // And the default for any other value.
4681 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4682 __ B(codegen_->GetLabelOf(default_block));
4683 }
4684 } else {
4685 JumpTableARM64* jump_table = new (GetGraph()->GetArena()) JumpTableARM64(switch_instr);
4686 codegen_->AddJumpTable(jump_table);
4687
4688 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4689
4690 // Below instructions should use at most one blocked register. Since there are two blocked
4691 // registers, we are free to block one.
4692 Register temp_w = temps.AcquireW();
4693 Register index;
4694 // Remove the bias.
4695 if (lower_bound != 0) {
4696 index = temp_w;
4697 __ Sub(index, value_reg, Operand(lower_bound));
4698 } else {
4699 index = value_reg;
4700 }
4701
4702 // Jump to default block if index is out of the range.
4703 __ Cmp(index, Operand(num_entries));
4704 __ B(hs, codegen_->GetLabelOf(default_block));
4705
4706 // In current VIXL implementation, it won't require any blocked registers to encode the
4707 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4708 // register pressure.
4709 Register table_base = temps.AcquireX();
4710 // Load jump offset from the table.
4711 __ Adr(table_base, jump_table->GetTableStartLabel());
4712 Register jump_offset = temp_w;
4713 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4714
4715 // Jump to target block by branching to table_base(pc related) + offset.
4716 Register target_address = table_base;
4717 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4718 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004719 }
4720}
4721
Roland Levillain44015862016-01-22 11:47:17 +00004722void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4723 Location out,
4724 uint32_t offset,
4725 Location maybe_temp) {
4726 Primitive::Type type = Primitive::kPrimNot;
4727 Register out_reg = RegisterFrom(out, type);
4728 if (kEmitCompilerReadBarrier) {
4729 Register temp_reg = RegisterFrom(maybe_temp, type);
4730 if (kUseBakerReadBarrier) {
4731 // Load with fast path based Baker's read barrier.
4732 // /* HeapReference<Object> */ out = *(out + offset)
4733 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4734 out,
4735 out_reg,
4736 offset,
4737 temp_reg,
4738 /* needs_null_check */ false,
4739 /* use_load_acquire */ false);
4740 } else {
4741 // Load with slow path based read barrier.
4742 // Save the value of `out` into `maybe_temp` before overwriting it
4743 // in the following move operation, as we will need it for the
4744 // read barrier below.
4745 __ Mov(temp_reg, out_reg);
4746 // /* HeapReference<Object> */ out = *(out + offset)
4747 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4748 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4749 }
4750 } else {
4751 // Plain load with no read barrier.
4752 // /* HeapReference<Object> */ out = *(out + offset)
4753 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4754 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4755 }
4756}
4757
4758void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
4759 Location out,
4760 Location obj,
4761 uint32_t offset,
4762 Location maybe_temp) {
4763 Primitive::Type type = Primitive::kPrimNot;
4764 Register out_reg = RegisterFrom(out, type);
4765 Register obj_reg = RegisterFrom(obj, type);
4766 if (kEmitCompilerReadBarrier) {
4767 if (kUseBakerReadBarrier) {
4768 // Load with fast path based Baker's read barrier.
4769 Register temp_reg = RegisterFrom(maybe_temp, type);
4770 // /* HeapReference<Object> */ out = *(obj + offset)
4771 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4772 out,
4773 obj_reg,
4774 offset,
4775 temp_reg,
4776 /* needs_null_check */ false,
4777 /* use_load_acquire */ false);
4778 } else {
4779 // Load with slow path based read barrier.
4780 // /* HeapReference<Object> */ out = *(obj + offset)
4781 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
4782 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4783 }
4784 } else {
4785 // Plain load with no read barrier.
4786 // /* HeapReference<Object> */ out = *(obj + offset)
4787 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
4788 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4789 }
4790}
4791
4792void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
4793 Location root,
4794 vixl::Register obj,
4795 uint32_t offset) {
4796 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
4797 if (kEmitCompilerReadBarrier) {
4798 if (kUseBakerReadBarrier) {
4799 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4800 // Baker's read barrier are used:
4801 //
4802 // root = obj.field;
4803 // if (Thread::Current()->GetIsGcMarking()) {
4804 // root = ReadBarrier::Mark(root)
4805 // }
4806
4807 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4808 __ Ldr(root_reg, MemOperand(obj, offset));
4809 static_assert(
4810 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
4811 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
4812 "have different sizes.");
4813 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
4814 "art::mirror::CompressedReference<mirror::Object> and int32_t "
4815 "have different sizes.");
4816
4817 // Slow path used to mark the GC root `root`.
4818 SlowPathCodeARM64* slow_path =
4819 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root, root);
4820 codegen_->AddSlowPath(slow_path);
4821
4822 MacroAssembler* masm = GetVIXLAssembler();
4823 UseScratchRegisterScope temps(masm);
4824 Register temp = temps.AcquireW();
4825 // temp = Thread::Current()->GetIsGcMarking()
4826 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64WordSize>().Int32Value()));
4827 __ Cbnz(temp, slow_path->GetEntryLabel());
4828 __ Bind(slow_path->GetExitLabel());
4829 } else {
4830 // GC root loaded through a slow path for read barriers other
4831 // than Baker's.
4832 // /* GcRoot<mirror::Object>* */ root = obj + offset
4833 __ Add(root_reg.X(), obj.X(), offset);
4834 // /* mirror::Object* */ root = root->Read()
4835 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
4836 }
4837 } else {
4838 // Plain GC root load with no read barrier.
4839 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4840 __ Ldr(root_reg, MemOperand(obj, offset));
4841 // Note that GC roots are not affected by heap poisoning, thus we
4842 // do not have to unpoison `root_reg` here.
4843 }
4844}
4845
4846void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
4847 Location ref,
4848 vixl::Register obj,
4849 uint32_t offset,
4850 Register temp,
4851 bool needs_null_check,
4852 bool use_load_acquire) {
4853 DCHECK(kEmitCompilerReadBarrier);
4854 DCHECK(kUseBakerReadBarrier);
4855
4856 // /* HeapReference<Object> */ ref = *(obj + offset)
4857 Location no_index = Location::NoLocation();
4858 GenerateReferenceLoadWithBakerReadBarrier(
4859 instruction, ref, obj, offset, no_index, temp, needs_null_check, use_load_acquire);
4860}
4861
4862void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
4863 Location ref,
4864 vixl::Register obj,
4865 uint32_t data_offset,
4866 Location index,
4867 Register temp,
4868 bool needs_null_check) {
4869 DCHECK(kEmitCompilerReadBarrier);
4870 DCHECK(kUseBakerReadBarrier);
4871
4872 // Array cells are never volatile variables, therefore array loads
4873 // never use Load-Acquire instructions on ARM64.
4874 const bool use_load_acquire = false;
4875
4876 // /* HeapReference<Object> */ ref =
4877 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4878 GenerateReferenceLoadWithBakerReadBarrier(
4879 instruction, ref, obj, data_offset, index, temp, needs_null_check, use_load_acquire);
4880}
4881
4882void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
4883 Location ref,
4884 vixl::Register obj,
4885 uint32_t offset,
4886 Location index,
4887 Register temp,
4888 bool needs_null_check,
4889 bool use_load_acquire) {
4890 DCHECK(kEmitCompilerReadBarrier);
4891 DCHECK(kUseBakerReadBarrier);
4892 // If `index` is a valid location, then we are emitting an array
4893 // load, so we shouldn't be using a Load Acquire instruction.
4894 // In other words: `index.IsValid()` => `!use_load_acquire`.
4895 DCHECK(!index.IsValid() || !use_load_acquire);
4896
4897 MacroAssembler* masm = GetVIXLAssembler();
4898 UseScratchRegisterScope temps(masm);
4899
4900 // In slow path based read barriers, the read barrier call is
4901 // inserted after the original load. However, in fast path based
4902 // Baker's read barriers, we need to perform the load of
4903 // mirror::Object::monitor_ *before* the original reference load.
4904 // This load-load ordering is required by the read barrier.
4905 // The fast path/slow path (for Baker's algorithm) should look like:
4906 //
4907 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
4908 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
4909 // HeapReference<Object> ref = *src; // Original reference load.
4910 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
4911 // if (is_gray) {
4912 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
4913 // }
4914 //
4915 // Note: the original implementation in ReadBarrier::Barrier is
4916 // slightly more complex as it performs additional checks that we do
4917 // not do here for performance reasons.
4918
4919 Primitive::Type type = Primitive::kPrimNot;
4920 Register ref_reg = RegisterFrom(ref, type);
4921 DCHECK(obj.IsW());
4922 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
4923
4924 // /* int32_t */ monitor = obj->monitor_
4925 __ Ldr(temp, HeapOperand(obj, monitor_offset));
4926 if (needs_null_check) {
4927 MaybeRecordImplicitNullCheck(instruction);
4928 }
4929 // /* LockWord */ lock_word = LockWord(monitor)
4930 static_assert(sizeof(LockWord) == sizeof(int32_t),
4931 "art::LockWord and int32_t have different sizes.");
4932 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
4933 __ Lsr(temp, temp, LockWord::kReadBarrierStateShift);
4934 __ And(temp, temp, Operand(LockWord::kReadBarrierStateMask));
4935 static_assert(
4936 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
4937 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
4938
4939 // Introduce a dependency on the high bits of rb_state, which shall
4940 // be all zeroes, to prevent load-load reordering, and without using
4941 // a memory barrier (which would be more expensive).
4942 // temp2 = rb_state & ~LockWord::kReadBarrierStateMask = 0
4943 Register temp2 = temps.AcquireW();
4944 __ Bic(temp2, temp, Operand(LockWord::kReadBarrierStateMask));
4945 // obj is unchanged by this operation, but its value now depends on
4946 // temp2, which depends on temp.
4947 __ Add(obj, obj, Operand(temp2));
4948 temps.Release(temp2);
4949
4950 // The actual reference load.
4951 if (index.IsValid()) {
4952 static_assert(
4953 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4954 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00004955 // /* HeapReference<Object> */ ref =
4956 // *(obj + offset + index * sizeof(HeapReference<Object>))
Roland Levillainca0bf032016-02-09 12:49:18 +00004957 const size_t shift_amount = Primitive::ComponentSizeShift(type);
Roland Levillain44015862016-01-22 11:47:17 +00004958 if (index.IsConstant()) {
Roland Levillainca0bf032016-02-09 12:49:18 +00004959 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << shift_amount);
4960 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillain44015862016-01-22 11:47:17 +00004961 } else {
Roland Levillainca0bf032016-02-09 12:49:18 +00004962 temp2 = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00004963 __ Add(temp2, obj, offset);
Roland Levillainca0bf032016-02-09 12:49:18 +00004964 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, shift_amount));
4965 temps.Release(temp2);
Roland Levillain44015862016-01-22 11:47:17 +00004966 }
Roland Levillain44015862016-01-22 11:47:17 +00004967 } else {
4968 // /* HeapReference<Object> */ ref = *(obj + offset)
4969 MemOperand field = HeapOperand(obj, offset);
4970 if (use_load_acquire) {
4971 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
4972 } else {
4973 Load(type, ref_reg, field);
4974 }
4975 }
4976
4977 // Object* ref = ref_addr->AsMirrorPtr()
4978 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
4979
4980 // Slow path used to mark the object `ref` when it is gray.
4981 SlowPathCodeARM64* slow_path =
4982 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref, ref);
4983 AddSlowPath(slow_path);
4984
4985 // if (rb_state == ReadBarrier::gray_ptr_)
4986 // ref = ReadBarrier::Mark(ref);
4987 __ Cmp(temp, ReadBarrier::gray_ptr_);
4988 __ B(eq, slow_path->GetEntryLabel());
4989 __ Bind(slow_path->GetExitLabel());
4990}
4991
4992void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
4993 Location out,
4994 Location ref,
4995 Location obj,
4996 uint32_t offset,
4997 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004998 DCHECK(kEmitCompilerReadBarrier);
4999
Roland Levillain44015862016-01-22 11:47:17 +00005000 // Insert a slow path based read barrier *after* the reference load.
5001 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005002 // If heap poisoning is enabled, the unpoisoning of the loaded
5003 // reference will be carried out by the runtime within the slow
5004 // path.
5005 //
5006 // Note that `ref` currently does not get unpoisoned (when heap
5007 // poisoning is enabled), which is alright as the `ref` argument is
5008 // not used by the artReadBarrierSlow entry point.
5009 //
5010 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5011 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5012 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5013 AddSlowPath(slow_path);
5014
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005015 __ B(slow_path->GetEntryLabel());
5016 __ Bind(slow_path->GetExitLabel());
5017}
5018
Roland Levillain44015862016-01-22 11:47:17 +00005019void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5020 Location out,
5021 Location ref,
5022 Location obj,
5023 uint32_t offset,
5024 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005025 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005026 // Baker's read barriers shall be handled by the fast path
5027 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5028 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005029 // If heap poisoning is enabled, unpoisoning will be taken care of
5030 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005031 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005032 } else if (kPoisonHeapReferences) {
5033 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5034 }
5035}
5036
Roland Levillain44015862016-01-22 11:47:17 +00005037void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5038 Location out,
5039 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005040 DCHECK(kEmitCompilerReadBarrier);
5041
Roland Levillain44015862016-01-22 11:47:17 +00005042 // Insert a slow path based read barrier *after* the GC root load.
5043 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005044 // Note that GC roots are not affected by heap poisoning, so we do
5045 // not need to do anything special for this here.
5046 SlowPathCodeARM64* slow_path =
5047 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5048 AddSlowPath(slow_path);
5049
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005050 __ B(slow_path->GetEntryLabel());
5051 __ Bind(slow_path->GetExitLabel());
5052}
5053
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005054void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5055 LocationSummary* locations =
5056 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5057 locations->SetInAt(0, Location::RequiresRegister());
5058 locations->SetOut(Location::RequiresRegister());
5059}
5060
5061void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5062 LocationSummary* locations = instruction->GetLocations();
5063 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005064 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005065 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5066 instruction->GetIndex(), kArm64PointerSize).SizeValue();
5067 } else {
5068 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
5069 instruction->GetIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
5070 }
5071 __ Ldr(XRegisterFrom(locations->Out()),
5072 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
5073}
5074
5075
5076
Alexandre Rames67555f72014-11-18 10:55:16 +00005077#undef __
5078#undef QUICK_ENTRY_POINT
5079
Alexandre Rames5319def2014-10-23 10:03:10 +01005080} // namespace arm64
5081} // namespace art