blob: f401bda9f7283718de490a378462bd3268c121e1 [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 Markocac5a7e2016-02-22 10:39:50 +0000908 uint32_literals_(std::less<uint32_t>(),
909 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100910 uint64_literals_(std::less<uint64_t>(),
911 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
912 method_patches_(MethodReferenceComparator(),
913 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
914 call_patches_(MethodReferenceComparator(),
915 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
916 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000917 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
918 boot_image_string_patches_(StringReferenceValueComparator(),
919 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
920 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
921 boot_image_address_patches_(std::less<uint32_t>(),
922 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000923 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000924 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000925}
Alexandre Rames5319def2014-10-23 10:03:10 +0100926
Alexandre Rames67555f72014-11-18 10:55:16 +0000927#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100928
Zheng Xu3927c8b2015-11-18 17:46:25 +0800929void CodeGeneratorARM64::EmitJumpTables() {
930 for (auto jump_table : jump_tables_) {
931 jump_table->EmitTable(this);
932 }
933}
934
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000935void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800936 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000937 // Ensure we emit the literal pool.
938 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000939
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000940 CodeGenerator::Finalize(allocator);
941}
942
Zheng Xuad4450e2015-04-17 18:48:56 +0800943void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
944 // Note: There are 6 kinds of moves:
945 // 1. constant -> GPR/FPR (non-cycle)
946 // 2. constant -> stack (non-cycle)
947 // 3. GPR/FPR -> GPR/FPR
948 // 4. GPR/FPR -> stack
949 // 5. stack -> GPR/FPR
950 // 6. stack -> stack (non-cycle)
951 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
952 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
953 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
954 // dependency.
955 vixl_temps_.Open(GetVIXLAssembler());
956}
957
958void ParallelMoveResolverARM64::FinishEmitNativeCode() {
959 vixl_temps_.Close();
960}
961
962Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
963 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
964 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
965 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
966 Location scratch = GetScratchLocation(kind);
967 if (!scratch.Equals(Location::NoLocation())) {
968 return scratch;
969 }
970 // Allocate from VIXL temp registers.
971 if (kind == Location::kRegister) {
972 scratch = LocationFrom(vixl_temps_.AcquireX());
973 } else {
974 DCHECK(kind == Location::kFpuRegister);
975 scratch = LocationFrom(vixl_temps_.AcquireD());
976 }
977 AddScratchLocation(scratch);
978 return scratch;
979}
980
981void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
982 if (loc.IsRegister()) {
983 vixl_temps_.Release(XRegisterFrom(loc));
984 } else {
985 DCHECK(loc.IsFpuRegister());
986 vixl_temps_.Release(DRegisterFrom(loc));
987 }
988 RemoveScratchLocation(loc);
989}
990
Alexandre Rames3e69f162014-12-10 10:36:50 +0000991void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100992 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100993 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000994}
995
Alexandre Rames5319def2014-10-23 10:03:10 +0100996void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100997 MacroAssembler* masm = GetVIXLAssembler();
998 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000999 __ Bind(&frame_entry_label_);
1000
Serban Constantinescu02164b32014-11-13 14:05:07 +00001001 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1002 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001003 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001004 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001005 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001006 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001007 __ Ldr(wzr, MemOperand(temp, 0));
1008 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001009 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001010
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001011 if (!HasEmptyFrame()) {
1012 int frame_size = GetFrameSize();
1013 // Stack layout:
1014 // sp[frame_size - 8] : lr.
1015 // ... : other preserved core registers.
1016 // ... : other preserved fp registers.
1017 // ... : reserved frame space.
1018 // sp[0] : current method.
1019 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001020 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001021 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1022 frame_size - GetCoreSpillSize());
1023 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1024 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001025 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001026}
1027
1028void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001029 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001030 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001031 if (!HasEmptyFrame()) {
1032 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001033 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1034 frame_size - FrameEntrySpillSize());
1035 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1036 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001037 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001038 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001039 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001040 __ Ret();
1041 GetAssembler()->cfi().RestoreState();
1042 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001043}
1044
Zheng Xuda403092015-04-24 17:35:39 +08001045vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
1046 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
1047 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
1048 core_spill_mask_);
1049}
1050
1051vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
1052 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1053 GetNumberOfFloatingPointRegisters()));
1054 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
1055 fpu_spill_mask_);
1056}
1057
Alexandre Rames5319def2014-10-23 10:03:10 +01001058void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1059 __ Bind(GetLabelOf(block));
1060}
1061
Calin Juravle175dc732015-08-25 15:42:32 +01001062void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1063 DCHECK(location.IsRegister());
1064 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1065}
1066
Calin Juravlee460d1d2015-09-29 04:52:17 +01001067void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1068 if (location.IsRegister()) {
1069 locations->AddTemp(location);
1070 } else {
1071 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1072 }
1073}
1074
David Brazdil60328912016-04-04 17:47:42 +00001075Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
1076 Primitive::Type type = load->GetType();
1077
1078 switch (type) {
1079 case Primitive::kPrimNot:
1080 case Primitive::kPrimInt:
1081 case Primitive::kPrimFloat:
1082 return Location::StackSlot(GetStackSlot(load->GetLocal()));
1083
1084 case Primitive::kPrimLong:
1085 case Primitive::kPrimDouble:
1086 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
1087
1088 case Primitive::kPrimBoolean:
1089 case Primitive::kPrimByte:
1090 case Primitive::kPrimChar:
1091 case Primitive::kPrimShort:
1092 case Primitive::kPrimVoid:
1093 LOG(FATAL) << "Unexpected type " << type;
1094 }
1095
1096 LOG(FATAL) << "Unreachable";
1097 return Location::NoLocation();
1098}
1099
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001100void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001101 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001102 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001103 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +01001104 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001105 if (value_can_be_null) {
1106 __ Cbz(value, &done);
1107 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001108 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
1109 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001110 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001111 if (value_can_be_null) {
1112 __ Bind(&done);
1113 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001114}
1115
David Brazdil58282f42016-01-14 12:45:10 +00001116void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001117 // Blocked core registers:
1118 // lr : Runtime reserved.
1119 // tr : Runtime reserved.
1120 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1121 // ip1 : VIXL core temp.
1122 // ip0 : VIXL core temp.
1123 //
1124 // Blocked fp registers:
1125 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001126 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1127 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001128 while (!reserved_core_registers.IsEmpty()) {
1129 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
1130 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001131
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001132 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001133 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001134 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
1135 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001136
David Brazdil58282f42016-01-14 12:45:10 +00001137 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001138 // Stubs do not save callee-save floating point registers. If the graph
1139 // is debuggable, we need to deal with these registers differently. For
1140 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001141 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1142 while (!reserved_fp_registers_debuggable.IsEmpty()) {
1143 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().code()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001144 }
1145 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001146}
1147
Alexandre Rames3e69f162014-12-10 10:36:50 +00001148size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1149 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1150 __ Str(reg, MemOperand(sp, stack_index));
1151 return kArm64WordSize;
1152}
1153
1154size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1155 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1156 __ Ldr(reg, MemOperand(sp, stack_index));
1157 return kArm64WordSize;
1158}
1159
1160size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1161 FPRegister reg = FPRegister(reg_id, kDRegSize);
1162 __ Str(reg, MemOperand(sp, stack_index));
1163 return kArm64WordSize;
1164}
1165
1166size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1167 FPRegister reg = FPRegister(reg_id, kDRegSize);
1168 __ Ldr(reg, MemOperand(sp, stack_index));
1169 return kArm64WordSize;
1170}
1171
Alexandre Rames5319def2014-10-23 10:03:10 +01001172void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001173 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001174}
1175
1176void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001177 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001178}
1179
Alexandre Rames67555f72014-11-18 10:55:16 +00001180void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001181 if (constant->IsIntConstant()) {
1182 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1183 } else if (constant->IsLongConstant()) {
1184 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1185 } else if (constant->IsNullConstant()) {
1186 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001187 } else if (constant->IsFloatConstant()) {
1188 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1189 } else {
1190 DCHECK(constant->IsDoubleConstant());
1191 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1192 }
1193}
1194
Alexandre Rames3e69f162014-12-10 10:36:50 +00001195
1196static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1197 DCHECK(constant.IsConstant());
1198 HConstant* cst = constant.GetConstant();
1199 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001200 // Null is mapped to a core W register, which we associate with kPrimInt.
1201 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001202 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1203 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1204 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1205}
1206
Calin Juravlee460d1d2015-09-29 04:52:17 +01001207void CodeGeneratorARM64::MoveLocation(Location destination,
1208 Location source,
1209 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001210 if (source.Equals(destination)) {
1211 return;
1212 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001213
1214 // A valid move can always be inferred from the destination and source
1215 // locations. When moving from and to a register, the argument type can be
1216 // used to generate 32bit instead of 64bit moves. In debug mode we also
1217 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001218 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001219
1220 if (destination.IsRegister() || destination.IsFpuRegister()) {
1221 if (unspecified_type) {
1222 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1223 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001224 (src_cst != nullptr && (src_cst->IsIntConstant()
1225 || src_cst->IsFloatConstant()
1226 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001227 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001228 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001229 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001230 // If the source is a double stack slot or a 64bit constant, a 64bit
1231 // type is appropriate. Else the source is a register, and since the
1232 // type has not been specified, we chose a 64bit type to force a 64bit
1233 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001234 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001235 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001236 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001237 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1238 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1239 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001240 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1241 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1242 __ Ldr(dst, StackOperandFrom(source));
1243 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001244 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001245 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001246 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001247 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001248 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001249 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001250 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001251 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1252 ? Primitive::kPrimLong
1253 : Primitive::kPrimInt;
1254 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1255 }
1256 } else {
1257 DCHECK(source.IsFpuRegister());
1258 if (destination.IsRegister()) {
1259 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1260 ? Primitive::kPrimDouble
1261 : Primitive::kPrimFloat;
1262 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1263 } else {
1264 DCHECK(destination.IsFpuRegister());
1265 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001266 }
1267 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001268 } else { // The destination is not a register. It must be a stack slot.
1269 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1270 if (source.IsRegister() || source.IsFpuRegister()) {
1271 if (unspecified_type) {
1272 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001273 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001274 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001275 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001276 }
1277 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001278 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1279 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1280 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001281 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001282 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1283 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001284 UseScratchRegisterScope temps(GetVIXLAssembler());
1285 HConstant* src_cst = source.GetConstant();
1286 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001287 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001288 temp = temps.AcquireW();
1289 } else if (src_cst->IsLongConstant()) {
1290 temp = temps.AcquireX();
1291 } else if (src_cst->IsFloatConstant()) {
1292 temp = temps.AcquireS();
1293 } else {
1294 DCHECK(src_cst->IsDoubleConstant());
1295 temp = temps.AcquireD();
1296 }
1297 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001298 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001299 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001300 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001301 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001302 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001303 // There is generally less pressure on FP registers.
1304 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001305 __ Ldr(temp, StackOperandFrom(source));
1306 __ Str(temp, StackOperandFrom(destination));
1307 }
1308 }
1309}
1310
1311void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001312 CPURegister dst,
1313 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001314 switch (type) {
1315 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001316 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001317 break;
1318 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001319 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001320 break;
1321 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001322 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001323 break;
1324 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001325 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001326 break;
1327 case Primitive::kPrimInt:
1328 case Primitive::kPrimNot:
1329 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001330 case Primitive::kPrimFloat:
1331 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001332 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001333 __ Ldr(dst, src);
1334 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001335 case Primitive::kPrimVoid:
1336 LOG(FATAL) << "Unreachable type " << type;
1337 }
1338}
1339
Calin Juravle77520bc2015-01-12 18:45:46 +00001340void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001341 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001342 const MemOperand& src,
1343 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001344 MacroAssembler* masm = GetVIXLAssembler();
1345 BlockPoolsScope block_pools(masm);
1346 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001347 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001348 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001349
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001350 DCHECK(!src.IsPreIndex());
1351 DCHECK(!src.IsPostIndex());
1352
1353 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001354 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001355 MemOperand base = MemOperand(temp_base);
1356 switch (type) {
1357 case Primitive::kPrimBoolean:
1358 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001359 if (needs_null_check) {
1360 MaybeRecordImplicitNullCheck(instruction);
1361 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001362 break;
1363 case Primitive::kPrimByte:
1364 __ Ldarb(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 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1369 break;
1370 case Primitive::kPrimChar:
1371 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001372 if (needs_null_check) {
1373 MaybeRecordImplicitNullCheck(instruction);
1374 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001375 break;
1376 case Primitive::kPrimShort:
1377 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001378 if (needs_null_check) {
1379 MaybeRecordImplicitNullCheck(instruction);
1380 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001381 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1382 break;
1383 case Primitive::kPrimInt:
1384 case Primitive::kPrimNot:
1385 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001386 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001387 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001388 if (needs_null_check) {
1389 MaybeRecordImplicitNullCheck(instruction);
1390 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001391 break;
1392 case Primitive::kPrimFloat:
1393 case Primitive::kPrimDouble: {
1394 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001395 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001396
1397 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1398 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001399 if (needs_null_check) {
1400 MaybeRecordImplicitNullCheck(instruction);
1401 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001402 __ Fmov(FPRegister(dst), temp);
1403 break;
1404 }
1405 case Primitive::kPrimVoid:
1406 LOG(FATAL) << "Unreachable type " << type;
1407 }
1408}
1409
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001410void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001411 CPURegister src,
1412 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001413 switch (type) {
1414 case Primitive::kPrimBoolean:
1415 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001416 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001417 break;
1418 case Primitive::kPrimChar:
1419 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001420 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001421 break;
1422 case Primitive::kPrimInt:
1423 case Primitive::kPrimNot:
1424 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001425 case Primitive::kPrimFloat:
1426 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001427 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001428 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001429 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001430 case Primitive::kPrimVoid:
1431 LOG(FATAL) << "Unreachable type " << type;
1432 }
1433}
1434
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001435void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1436 CPURegister src,
1437 const MemOperand& dst) {
1438 UseScratchRegisterScope temps(GetVIXLAssembler());
1439 Register temp_base = temps.AcquireX();
1440
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001441 DCHECK(!dst.IsPreIndex());
1442 DCHECK(!dst.IsPostIndex());
1443
1444 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001445 Operand op = OperandFromMemOperand(dst);
1446 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001447 MemOperand base = MemOperand(temp_base);
1448 switch (type) {
1449 case Primitive::kPrimBoolean:
1450 case Primitive::kPrimByte:
1451 __ Stlrb(Register(src), base);
1452 break;
1453 case Primitive::kPrimChar:
1454 case Primitive::kPrimShort:
1455 __ Stlrh(Register(src), base);
1456 break;
1457 case Primitive::kPrimInt:
1458 case Primitive::kPrimNot:
1459 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001460 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001461 __ Stlr(Register(src), base);
1462 break;
1463 case Primitive::kPrimFloat:
1464 case Primitive::kPrimDouble: {
1465 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001466 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001467
1468 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1469 __ Fmov(temp, FPRegister(src));
1470 __ Stlr(temp, base);
1471 break;
1472 }
1473 case Primitive::kPrimVoid:
1474 LOG(FATAL) << "Unreachable type " << type;
1475 }
1476}
1477
Calin Juravle175dc732015-08-25 15:42:32 +01001478void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1479 HInstruction* instruction,
1480 uint32_t dex_pc,
1481 SlowPathCode* slow_path) {
1482 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1483 instruction,
1484 dex_pc,
1485 slow_path);
1486}
1487
Alexandre Rames67555f72014-11-18 10:55:16 +00001488void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1489 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001490 uint32_t dex_pc,
1491 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001492 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001493 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001494 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1495 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001496 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001497}
1498
1499void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1500 vixl::Register class_reg) {
1501 UseScratchRegisterScope temps(GetVIXLAssembler());
1502 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001503 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1504
Serban Constantinescu02164b32014-11-13 14:05:07 +00001505 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001506 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1507 __ Add(temp, class_reg, status_offset);
1508 __ Ldar(temp, HeapOperand(temp));
1509 __ Cmp(temp, mirror::Class::kStatusInitialized);
1510 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001511 __ Bind(slow_path->GetExitLabel());
1512}
Alexandre Rames5319def2014-10-23 10:03:10 +01001513
Roland Levillain44015862016-01-22 11:47:17 +00001514void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001515 BarrierType type = BarrierAll;
1516
1517 switch (kind) {
1518 case MemBarrierKind::kAnyAny:
1519 case MemBarrierKind::kAnyStore: {
1520 type = BarrierAll;
1521 break;
1522 }
1523 case MemBarrierKind::kLoadAny: {
1524 type = BarrierReads;
1525 break;
1526 }
1527 case MemBarrierKind::kStoreStore: {
1528 type = BarrierWrites;
1529 break;
1530 }
1531 default:
1532 LOG(FATAL) << "Unexpected memory barrier " << kind;
1533 }
1534 __ Dmb(InnerShareable, type);
1535}
1536
Serban Constantinescu02164b32014-11-13 14:05:07 +00001537void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1538 HBasicBlock* successor) {
1539 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001540 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1541 if (slow_path == nullptr) {
1542 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1543 instruction->SetSlowPath(slow_path);
1544 codegen_->AddSlowPath(slow_path);
1545 if (successor != nullptr) {
1546 DCHECK(successor->IsLoopHeader());
1547 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1548 }
1549 } else {
1550 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1551 }
1552
Serban Constantinescu02164b32014-11-13 14:05:07 +00001553 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1554 Register temp = temps.AcquireW();
1555
1556 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1557 if (successor == nullptr) {
1558 __ Cbnz(temp, slow_path->GetEntryLabel());
1559 __ Bind(slow_path->GetReturnLabel());
1560 } else {
1561 __ Cbz(temp, codegen_->GetLabelOf(successor));
1562 __ B(slow_path->GetEntryLabel());
1563 // slow_path will return to GetLabelOf(successor).
1564 }
1565}
1566
Alexandre Rames5319def2014-10-23 10:03:10 +01001567InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1568 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001569 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001570 assembler_(codegen->GetAssembler()),
1571 codegen_(codegen) {}
1572
1573#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001574 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001575
1576#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1577
1578enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001579 // Using a base helps identify when we hit such breakpoints.
1580 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001581#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1582 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1583#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1584};
1585
1586#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001587 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001588 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1589 } \
1590 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1591 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1592 locations->SetOut(Location::Any()); \
1593 }
1594 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1595#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1596
1597#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001598#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001599
Alexandre Rames67555f72014-11-18 10:55:16 +00001600void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001601 DCHECK_EQ(instr->InputCount(), 2U);
1602 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1603 Primitive::Type type = instr->GetResultType();
1604 switch (type) {
1605 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001606 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001607 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001608 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001609 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001610 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001611
1612 case Primitive::kPrimFloat:
1613 case Primitive::kPrimDouble:
1614 locations->SetInAt(0, Location::RequiresFpuRegister());
1615 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001616 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001617 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001618
Alexandre Rames5319def2014-10-23 10:03:10 +01001619 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001620 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001621 }
1622}
1623
Alexandre Rames09a99962015-04-15 11:47:56 +01001624void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001625 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1626
1627 bool object_field_get_with_read_barrier =
1628 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001629 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001630 new (GetGraph()->GetArena()) LocationSummary(instruction,
1631 object_field_get_with_read_barrier ?
1632 LocationSummary::kCallOnSlowPath :
1633 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001634 locations->SetInAt(0, Location::RequiresRegister());
1635 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1636 locations->SetOut(Location::RequiresFpuRegister());
1637 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001638 // The output overlaps for an object field get when read barriers
1639 // are enabled: we do not want the load to overwrite the object's
1640 // location, as we need it to emit the read barrier.
1641 locations->SetOut(
1642 Location::RequiresRegister(),
1643 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001644 }
1645}
1646
1647void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1648 const FieldInfo& field_info) {
1649 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001650 LocationSummary* locations = instruction->GetLocations();
1651 Location base_loc = locations->InAt(0);
1652 Location out = locations->Out();
1653 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001654 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001655 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001656 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001657
Roland Levillain44015862016-01-22 11:47:17 +00001658 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1659 // Object FieldGet with Baker's read barrier case.
1660 MacroAssembler* masm = GetVIXLAssembler();
1661 UseScratchRegisterScope temps(masm);
1662 // /* HeapReference<Object> */ out = *(base + offset)
1663 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1664 Register temp = temps.AcquireW();
1665 // Note that potential implicit null checks are handled in this
1666 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1667 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1668 instruction,
1669 out,
1670 base,
1671 offset,
1672 temp,
1673 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001674 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001675 } else {
1676 // General case.
1677 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001678 // Note that a potential implicit null check is handled in this
1679 // CodeGeneratorARM64::LoadAcquire call.
1680 // NB: LoadAcquire will record the pc info if needed.
1681 codegen_->LoadAcquire(
1682 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001683 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001684 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001685 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001686 }
Roland Levillain44015862016-01-22 11:47:17 +00001687 if (field_type == Primitive::kPrimNot) {
1688 // If read barriers are enabled, emit read barriers other than
1689 // Baker's using a slow path (and also unpoison the loaded
1690 // reference, if heap poisoning is enabled).
1691 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1692 }
Roland Levillain4d027112015-07-01 15:41:14 +01001693 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001694}
1695
1696void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1697 LocationSummary* locations =
1698 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1699 locations->SetInAt(0, Location::RequiresRegister());
1700 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1701 locations->SetInAt(1, Location::RequiresFpuRegister());
1702 } else {
1703 locations->SetInAt(1, Location::RequiresRegister());
1704 }
1705}
1706
1707void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001708 const FieldInfo& field_info,
1709 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001710 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001711 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001712
1713 Register obj = InputRegisterAt(instruction, 0);
1714 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001715 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001716 Offset offset = field_info.GetFieldOffset();
1717 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001718
Roland Levillain4d027112015-07-01 15:41:14 +01001719 {
1720 // We use a block to end the scratch scope before the write barrier, thus
1721 // freeing the temporary registers so they can be used in `MarkGCCard`.
1722 UseScratchRegisterScope temps(GetVIXLAssembler());
1723
1724 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1725 DCHECK(value.IsW());
1726 Register temp = temps.AcquireW();
1727 __ Mov(temp, value.W());
1728 GetAssembler()->PoisonHeapReference(temp.W());
1729 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001730 }
Roland Levillain4d027112015-07-01 15:41:14 +01001731
1732 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001733 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1734 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001735 } else {
1736 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1737 codegen_->MaybeRecordImplicitNullCheck(instruction);
1738 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001739 }
1740
1741 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001742 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001743 }
1744}
1745
Alexandre Rames67555f72014-11-18 10:55:16 +00001746void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001747 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001748
1749 switch (type) {
1750 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001751 case Primitive::kPrimLong: {
1752 Register dst = OutputRegister(instr);
1753 Register lhs = InputRegisterAt(instr, 0);
1754 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001755 if (instr->IsAdd()) {
1756 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001757 } else if (instr->IsAnd()) {
1758 __ And(dst, lhs, rhs);
1759 } else if (instr->IsOr()) {
1760 __ Orr(dst, lhs, rhs);
1761 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001762 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001763 } else if (instr->IsRor()) {
1764 if (rhs.IsImmediate()) {
1765 uint32_t shift = rhs.immediate() & (lhs.SizeInBits() - 1);
1766 __ Ror(dst, lhs, shift);
1767 } else {
1768 // Ensure shift distance is in the same size register as the result. If
1769 // we are rotating a long and the shift comes in a w register originally,
1770 // we don't need to sxtw for use as an x since the shift distances are
1771 // all & reg_bits - 1.
1772 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1773 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001774 } else {
1775 DCHECK(instr->IsXor());
1776 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001777 }
1778 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001779 }
1780 case Primitive::kPrimFloat:
1781 case Primitive::kPrimDouble: {
1782 FPRegister dst = OutputFPRegister(instr);
1783 FPRegister lhs = InputFPRegisterAt(instr, 0);
1784 FPRegister rhs = InputFPRegisterAt(instr, 1);
1785 if (instr->IsAdd()) {
1786 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001787 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001788 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001789 } else {
1790 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001791 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001792 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001793 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001794 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001795 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001796 }
1797}
1798
Serban Constantinescu02164b32014-11-13 14:05:07 +00001799void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1800 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1801
1802 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1803 Primitive::Type type = instr->GetResultType();
1804 switch (type) {
1805 case Primitive::kPrimInt:
1806 case Primitive::kPrimLong: {
1807 locations->SetInAt(0, Location::RequiresRegister());
1808 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1809 locations->SetOut(Location::RequiresRegister());
1810 break;
1811 }
1812 default:
1813 LOG(FATAL) << "Unexpected shift type " << type;
1814 }
1815}
1816
1817void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1818 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1819
1820 Primitive::Type type = instr->GetType();
1821 switch (type) {
1822 case Primitive::kPrimInt:
1823 case Primitive::kPrimLong: {
1824 Register dst = OutputRegister(instr);
1825 Register lhs = InputRegisterAt(instr, 0);
1826 Operand rhs = InputOperandAt(instr, 1);
1827 if (rhs.IsImmediate()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001828 uint32_t shift_value = rhs.immediate() &
1829 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001830 if (instr->IsShl()) {
1831 __ Lsl(dst, lhs, shift_value);
1832 } else if (instr->IsShr()) {
1833 __ Asr(dst, lhs, shift_value);
1834 } else {
1835 __ Lsr(dst, lhs, shift_value);
1836 }
1837 } else {
1838 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1839
1840 if (instr->IsShl()) {
1841 __ Lsl(dst, lhs, rhs_reg);
1842 } else if (instr->IsShr()) {
1843 __ Asr(dst, lhs, rhs_reg);
1844 } else {
1845 __ Lsr(dst, lhs, rhs_reg);
1846 }
1847 }
1848 break;
1849 }
1850 default:
1851 LOG(FATAL) << "Unexpected shift operation type " << type;
1852 }
1853}
1854
Alexandre Rames5319def2014-10-23 10:03:10 +01001855void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001856 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001857}
1858
1859void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001860 HandleBinaryOp(instruction);
1861}
1862
1863void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1864 HandleBinaryOp(instruction);
1865}
1866
1867void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1868 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001869}
1870
Artem Serov7fc63502016-02-09 17:15:29 +00001871void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001872 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1873 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1874 locations->SetInAt(0, Location::RequiresRegister());
1875 // There is no immediate variant of negated bitwise instructions in AArch64.
1876 locations->SetInAt(1, Location::RequiresRegister());
1877 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1878}
1879
Artem Serov7fc63502016-02-09 17:15:29 +00001880void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001881 Register dst = OutputRegister(instr);
1882 Register lhs = InputRegisterAt(instr, 0);
1883 Register rhs = InputRegisterAt(instr, 1);
1884
1885 switch (instr->GetOpKind()) {
1886 case HInstruction::kAnd:
1887 __ Bic(dst, lhs, rhs);
1888 break;
1889 case HInstruction::kOr:
1890 __ Orn(dst, lhs, rhs);
1891 break;
1892 case HInstruction::kXor:
1893 __ Eon(dst, lhs, rhs);
1894 break;
1895 default:
1896 LOG(FATAL) << "Unreachable";
1897 }
1898}
1899
Alexandre Rames8626b742015-11-25 16:28:08 +00001900void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1901 HArm64DataProcWithShifterOp* instruction) {
1902 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1903 instruction->GetType() == Primitive::kPrimLong);
1904 LocationSummary* locations =
1905 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1906 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1907 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1908 } else {
1909 locations->SetInAt(0, Location::RequiresRegister());
1910 }
1911 locations->SetInAt(1, Location::RequiresRegister());
1912 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1913}
1914
1915void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1916 HArm64DataProcWithShifterOp* instruction) {
1917 Primitive::Type type = instruction->GetType();
1918 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1919 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1920 Register out = OutputRegister(instruction);
1921 Register left;
1922 if (kind != HInstruction::kNeg) {
1923 left = InputRegisterAt(instruction, 0);
1924 }
1925 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1926 // shifter operand operation, the IR generating `right_reg` (input to the type
1927 // conversion) can have a different type from the current instruction's type,
1928 // so we manually indicate the type.
1929 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001930 int64_t shift_amount = instruction->GetShiftAmount() &
1931 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001932
1933 Operand right_operand(0);
1934
1935 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1936 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1937 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1938 } else {
1939 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1940 }
1941
1942 // Logical binary operations do not support extension operations in the
1943 // operand. Note that VIXL would still manage if it was passed by generating
1944 // the extension as a separate instruction.
1945 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1946 DCHECK(!right_operand.IsExtendedRegister() ||
1947 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1948 kind != HInstruction::kNeg));
1949 switch (kind) {
1950 case HInstruction::kAdd:
1951 __ Add(out, left, right_operand);
1952 break;
1953 case HInstruction::kAnd:
1954 __ And(out, left, right_operand);
1955 break;
1956 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001957 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001958 __ Neg(out, right_operand);
1959 break;
1960 case HInstruction::kOr:
1961 __ Orr(out, left, right_operand);
1962 break;
1963 case HInstruction::kSub:
1964 __ Sub(out, left, right_operand);
1965 break;
1966 case HInstruction::kXor:
1967 __ Eor(out, left, right_operand);
1968 break;
1969 default:
1970 LOG(FATAL) << "Unexpected operation kind: " << kind;
1971 UNREACHABLE();
1972 }
1973}
1974
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001975void LocationsBuilderARM64::VisitArm64IntermediateAddress(HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001976 // The read barrier instrumentation does not support the
1977 // HArm64IntermediateAddress instruction yet.
1978 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001979 LocationSummary* locations =
1980 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1981 locations->SetInAt(0, Location::RequiresRegister());
1982 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1983 locations->SetOut(Location::RequiresRegister());
1984}
1985
1986void InstructionCodeGeneratorARM64::VisitArm64IntermediateAddress(
1987 HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001988 // The read barrier instrumentation does not support the
1989 // HArm64IntermediateAddress instruction yet.
1990 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001991 __ Add(OutputRegister(instruction),
1992 InputRegisterAt(instruction, 0),
1993 Operand(InputOperandAt(instruction, 1)));
1994}
1995
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001996void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00001997 LocationSummary* locations =
1998 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001999 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2000 if (instr->GetOpKind() == HInstruction::kSub &&
2001 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002002 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002003 // Don't allocate register for Mneg instruction.
2004 } else {
2005 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2006 Location::RequiresRegister());
2007 }
2008 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2009 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002010 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2011}
2012
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002013void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002014 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002015 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2016 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002017
2018 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2019 // This fixup should be carried out for all multiply-accumulate instructions:
2020 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2021 if (instr->GetType() == Primitive::kPrimLong &&
2022 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2023 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
2024 vixl::Instruction* prev = masm->GetCursorAddress<vixl::Instruction*>() - vixl::kInstructionSize;
2025 if (prev->IsLoadOrStore()) {
2026 // Make sure we emit only exactly one nop.
2027 vixl::CodeBufferCheckScope scope(masm,
2028 vixl::kInstructionSize,
2029 vixl::CodeBufferCheckScope::kCheck,
2030 vixl::CodeBufferCheckScope::kExactSize);
2031 __ nop();
2032 }
2033 }
2034
2035 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002036 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002037 __ Madd(res, mul_left, mul_right, accumulator);
2038 } else {
2039 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002040 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002041 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002042 __ Mneg(res, mul_left, mul_right);
2043 } else {
2044 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2045 __ Msub(res, mul_left, mul_right, accumulator);
2046 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002047 }
2048}
2049
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002050void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002051 bool object_array_get_with_read_barrier =
2052 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002053 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002054 new (GetGraph()->GetArena()) LocationSummary(instruction,
2055 object_array_get_with_read_barrier ?
2056 LocationSummary::kCallOnSlowPath :
2057 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002058 locations->SetInAt(0, Location::RequiresRegister());
2059 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002060 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2061 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2062 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002063 // The output overlaps in the case of an object array get with
2064 // read barriers enabled: we do not want the move to overwrite the
2065 // array's location, as we need it to emit the read barrier.
2066 locations->SetOut(
2067 Location::RequiresRegister(),
2068 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002069 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002070}
2071
2072void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002073 Primitive::Type type = instruction->GetType();
2074 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002075 LocationSummary* locations = instruction->GetLocations();
2076 Location index = locations->InAt(1);
2077 uint32_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Roland Levillain44015862016-01-22 11:47:17 +00002078 Location out = locations->Out();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002079
Alexandre Ramesd921d642015-04-16 15:07:16 +01002080 MacroAssembler* masm = GetVIXLAssembler();
2081 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002082 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002083 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002084
Roland Levillain44015862016-01-22 11:47:17 +00002085 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2086 // Object ArrayGet with Baker's read barrier case.
2087 Register temp = temps.AcquireW();
2088 // The read barrier instrumentation does not support the
2089 // HArm64IntermediateAddress instruction yet.
2090 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
2091 // Note that a potential implicit null check is handled in the
2092 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2093 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2094 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002095 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002096 // General case.
2097 MemOperand source = HeapOperand(obj);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002098 if (index.IsConstant()) {
Roland Levillain44015862016-01-22 11:47:17 +00002099 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2100 source = HeapOperand(obj, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002101 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002102 Register temp = temps.AcquireSameSizeAs(obj);
2103 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
2104 // The read barrier instrumentation does not support the
2105 // HArm64IntermediateAddress instruction yet.
2106 DCHECK(!kEmitCompilerReadBarrier);
2107 // We do not need to compute the intermediate address from the array: the
2108 // input instruction has done it already. See the comment in
2109 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2110 if (kIsDebugBuild) {
2111 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2112 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2113 }
2114 temp = obj;
2115 } else {
2116 __ Add(temp, obj, offset);
2117 }
2118 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2119 }
2120
2121 codegen_->Load(type, OutputCPURegister(instruction), source);
2122 codegen_->MaybeRecordImplicitNullCheck(instruction);
2123
2124 if (type == Primitive::kPrimNot) {
2125 static_assert(
2126 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2127 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2128 Location obj_loc = locations->InAt(0);
2129 if (index.IsConstant()) {
2130 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2131 } else {
2132 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2133 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002134 }
Roland Levillain4d027112015-07-01 15:41:14 +01002135 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002136}
2137
Alexandre Rames5319def2014-10-23 10:03:10 +01002138void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2139 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2140 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002141 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002142}
2143
2144void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01002145 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01002146 __ Ldr(OutputRegister(instruction),
2147 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00002148 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002149}
2150
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002151void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002152 Primitive::Type value_type = instruction->GetComponentType();
2153
2154 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2155 bool object_array_set_with_read_barrier =
2156 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002157 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2158 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002159 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2160 LocationSummary::kCallOnSlowPath :
2161 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002162 locations->SetInAt(0, Location::RequiresRegister());
2163 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002164 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002165 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002166 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002167 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002168 }
2169}
2170
2171void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2172 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002173 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002174 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002175 bool needs_write_barrier =
2176 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002177
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002178 Register array = InputRegisterAt(instruction, 0);
2179 CPURegister value = InputCPURegisterAt(instruction, 2);
2180 CPURegister source = value;
2181 Location index = locations->InAt(1);
2182 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2183 MemOperand destination = HeapOperand(array);
2184 MacroAssembler* masm = GetVIXLAssembler();
2185 BlockPoolsScope block_pools(masm);
2186
2187 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002188 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002189 if (index.IsConstant()) {
2190 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2191 destination = HeapOperand(array, offset);
2192 } else {
2193 UseScratchRegisterScope temps(masm);
2194 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002195 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002196 // The read barrier instrumentation does not support the
2197 // HArm64IntermediateAddress instruction yet.
2198 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002199 // We do not need to compute the intermediate address from the array: the
2200 // input instruction has done it already. See the comment in
2201 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2202 if (kIsDebugBuild) {
2203 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2204 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2205 }
2206 temp = array;
2207 } else {
2208 __ Add(temp, array, offset);
2209 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002210 destination = HeapOperand(temp,
2211 XRegisterFrom(index),
2212 LSL,
2213 Primitive::ComponentSizeShift(value_type));
2214 }
2215 codegen_->Store(value_type, value, destination);
2216 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002217 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002218 DCHECK(needs_write_barrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002219 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002220 vixl::Label done;
2221 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002222 {
2223 // We use a block to end the scratch scope before the write barrier, thus
2224 // freeing the temporary registers so they can be used in `MarkGCCard`.
2225 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002226 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002227 if (index.IsConstant()) {
2228 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002229 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002230 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002231 destination = HeapOperand(temp,
2232 XRegisterFrom(index),
2233 LSL,
2234 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002235 }
2236
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002237 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2238 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2239 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2240
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002241 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002242 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2243 codegen_->AddSlowPath(slow_path);
2244 if (instruction->GetValueCanBeNull()) {
2245 vixl::Label non_zero;
2246 __ Cbnz(Register(value), &non_zero);
2247 if (!index.IsConstant()) {
2248 __ Add(temp, array, offset);
2249 }
2250 __ Str(wzr, destination);
2251 codegen_->MaybeRecordImplicitNullCheck(instruction);
2252 __ B(&done);
2253 __ Bind(&non_zero);
2254 }
2255
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002256 if (kEmitCompilerReadBarrier) {
2257 // When read barriers are enabled, the type checking
2258 // instrumentation requires two read barriers:
2259 //
2260 // __ Mov(temp2, temp);
2261 // // /* HeapReference<Class> */ temp = temp->component_type_
2262 // __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002263 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002264 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2265 //
2266 // // /* HeapReference<Class> */ temp2 = value->klass_
2267 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002268 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002269 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2270 //
2271 // __ Cmp(temp, temp2);
2272 //
2273 // However, the second read barrier may trash `temp`, as it
2274 // is a temporary register, and as such would not be saved
2275 // along with live registers before calling the runtime (nor
2276 // restored afterwards). So in this case, we bail out and
2277 // delegate the work to the array set slow path.
2278 //
2279 // TODO: Extend the register allocator to support a new
2280 // "(locally) live temp" location so as to avoid always
2281 // going into the slow path when read barriers are enabled.
2282 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002283 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002284 Register temp2 = temps.AcquireSameSizeAs(array);
2285 // /* HeapReference<Class> */ temp = array->klass_
2286 __ Ldr(temp, HeapOperand(array, class_offset));
2287 codegen_->MaybeRecordImplicitNullCheck(instruction);
2288 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2289
2290 // /* HeapReference<Class> */ temp = temp->component_type_
2291 __ Ldr(temp, HeapOperand(temp, component_offset));
2292 // /* HeapReference<Class> */ temp2 = value->klass_
2293 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2294 // If heap poisoning is enabled, no need to unpoison `temp`
2295 // nor `temp2`, as we are comparing two poisoned references.
2296 __ Cmp(temp, temp2);
2297
2298 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2299 vixl::Label do_put;
2300 __ B(eq, &do_put);
2301 // If heap poisoning is enabled, the `temp` reference has
2302 // not been unpoisoned yet; unpoison it now.
2303 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2304
2305 // /* HeapReference<Class> */ temp = temp->super_class_
2306 __ Ldr(temp, HeapOperand(temp, super_offset));
2307 // If heap poisoning is enabled, no need to unpoison
2308 // `temp`, as we are comparing against null below.
2309 __ Cbnz(temp, slow_path->GetEntryLabel());
2310 __ Bind(&do_put);
2311 } else {
2312 __ B(ne, slow_path->GetEntryLabel());
2313 }
2314 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002315 }
2316 }
2317
2318 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002319 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002320 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002321 __ Mov(temp2, value.W());
2322 GetAssembler()->PoisonHeapReference(temp2);
2323 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002324 }
2325
2326 if (!index.IsConstant()) {
2327 __ Add(temp, array, offset);
2328 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002329 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002330
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002331 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002332 codegen_->MaybeRecordImplicitNullCheck(instruction);
2333 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002334 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002335
2336 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2337
2338 if (done.IsLinked()) {
2339 __ Bind(&done);
2340 }
2341
2342 if (slow_path != nullptr) {
2343 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002344 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002345 }
2346}
2347
Alexandre Rames67555f72014-11-18 10:55:16 +00002348void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002349 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2350 ? LocationSummary::kCallOnSlowPath
2351 : LocationSummary::kNoCall;
2352 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002353 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002354 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002355 if (instruction->HasUses()) {
2356 locations->SetOut(Location::SameAsFirstInput());
2357 }
2358}
2359
2360void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002361 BoundsCheckSlowPathARM64* slow_path =
2362 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002363 codegen_->AddSlowPath(slow_path);
2364
2365 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2366 __ B(slow_path->GetEntryLabel(), hs);
2367}
2368
Alexandre Rames67555f72014-11-18 10:55:16 +00002369void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2370 LocationSummary* locations =
2371 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2372 locations->SetInAt(0, Location::RequiresRegister());
2373 if (check->HasUses()) {
2374 locations->SetOut(Location::SameAsFirstInput());
2375 }
2376}
2377
2378void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2379 // We assume the class is not null.
2380 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2381 check->GetLoadClass(), check, check->GetDexPc(), true);
2382 codegen_->AddSlowPath(slow_path);
2383 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2384}
2385
Roland Levillain1a653882016-03-18 18:05:57 +00002386static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2387 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2388 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2389}
2390
2391void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2392 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2393 Location rhs_loc = instruction->GetLocations()->InAt(1);
2394 if (rhs_loc.IsConstant()) {
2395 // 0.0 is the only immediate that can be encoded directly in
2396 // an FCMP instruction.
2397 //
2398 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2399 // specify that in a floating-point comparison, positive zero
2400 // and negative zero are considered equal, so we can use the
2401 // literal 0.0 for both cases here.
2402 //
2403 // Note however that some methods (Float.equal, Float.compare,
2404 // Float.compareTo, Double.equal, Double.compare,
2405 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2406 // StrictMath.min) consider 0.0 to be (strictly) greater than
2407 // -0.0. So if we ever translate calls to these methods into a
2408 // HCompare instruction, we must handle the -0.0 case with
2409 // care here.
2410 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2411 __ Fcmp(lhs_reg, 0.0);
2412 } else {
2413 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2414 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002415}
2416
Serban Constantinescu02164b32014-11-13 14:05:07 +00002417void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002418 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002419 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2420 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002421 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:
Alexandre Rames5319def2014-10-23 10:03:10 +01002427 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002428 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002429 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002430 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2431 break;
2432 }
2433 case Primitive::kPrimFloat:
2434 case Primitive::kPrimDouble: {
2435 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002436 locations->SetInAt(1,
2437 IsFloatingPointZeroConstant(compare->InputAt(1))
2438 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2439 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002440 locations->SetOut(Location::RequiresRegister());
2441 break;
2442 }
2443 default:
2444 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2445 }
2446}
2447
2448void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2449 Primitive::Type in_type = compare->InputAt(0)->GetType();
2450
2451 // 0 if: left == right
2452 // 1 if: left > right
2453 // -1 if: left < right
2454 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002455 case Primitive::kPrimBoolean:
2456 case Primitive::kPrimByte:
2457 case Primitive::kPrimShort:
2458 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002459 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002460 case Primitive::kPrimLong: {
2461 Register result = OutputRegister(compare);
2462 Register left = InputRegisterAt(compare, 0);
2463 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002464 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002465 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2466 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002467 break;
2468 }
2469 case Primitive::kPrimFloat:
2470 case Primitive::kPrimDouble: {
2471 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002472 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002473 __ Cset(result, ne);
2474 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002475 break;
2476 }
2477 default:
2478 LOG(FATAL) << "Unimplemented compare type " << in_type;
2479 }
2480}
2481
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002482void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002483 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002484
2485 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2486 locations->SetInAt(0, Location::RequiresFpuRegister());
2487 locations->SetInAt(1,
2488 IsFloatingPointZeroConstant(instruction->InputAt(1))
2489 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2490 : Location::RequiresFpuRegister());
2491 } else {
2492 // Integer cases.
2493 locations->SetInAt(0, Location::RequiresRegister());
2494 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2495 }
2496
David Brazdilb3e773e2016-01-26 11:28:37 +00002497 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002498 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002499 }
2500}
2501
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002502void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002503 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002504 return;
2505 }
2506
2507 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002508 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002509 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002510
Roland Levillain7f63c522015-07-13 15:54:55 +00002511 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002512 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002513 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002514 } else {
2515 // Integer cases.
2516 Register lhs = InputRegisterAt(instruction, 0);
2517 Operand rhs = InputOperandAt(instruction, 1);
2518 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002519 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002520 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002521}
2522
2523#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2524 M(Equal) \
2525 M(NotEqual) \
2526 M(LessThan) \
2527 M(LessThanOrEqual) \
2528 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002529 M(GreaterThanOrEqual) \
2530 M(Below) \
2531 M(BelowOrEqual) \
2532 M(Above) \
2533 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002534#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002535void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2536void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002537FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002538#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002539#undef FOR_EACH_CONDITION_INSTRUCTION
2540
Zheng Xuc6667102015-05-15 16:08:45 +08002541void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2542 DCHECK(instruction->IsDiv() || instruction->IsRem());
2543
2544 LocationSummary* locations = instruction->GetLocations();
2545 Location second = locations->InAt(1);
2546 DCHECK(second.IsConstant());
2547
2548 Register out = OutputRegister(instruction);
2549 Register dividend = InputRegisterAt(instruction, 0);
2550 int64_t imm = Int64FromConstant(second.GetConstant());
2551 DCHECK(imm == 1 || imm == -1);
2552
2553 if (instruction->IsRem()) {
2554 __ Mov(out, 0);
2555 } else {
2556 if (imm == 1) {
2557 __ Mov(out, dividend);
2558 } else {
2559 __ Neg(out, dividend);
2560 }
2561 }
2562}
2563
2564void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2565 DCHECK(instruction->IsDiv() || instruction->IsRem());
2566
2567 LocationSummary* locations = instruction->GetLocations();
2568 Location second = locations->InAt(1);
2569 DCHECK(second.IsConstant());
2570
2571 Register out = OutputRegister(instruction);
2572 Register dividend = InputRegisterAt(instruction, 0);
2573 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002574 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002575 int ctz_imm = CTZ(abs_imm);
2576
2577 UseScratchRegisterScope temps(GetVIXLAssembler());
2578 Register temp = temps.AcquireSameSizeAs(out);
2579
2580 if (instruction->IsDiv()) {
2581 __ Add(temp, dividend, abs_imm - 1);
2582 __ Cmp(dividend, 0);
2583 __ Csel(out, temp, dividend, lt);
2584 if (imm > 0) {
2585 __ Asr(out, out, ctz_imm);
2586 } else {
2587 __ Neg(out, Operand(out, ASR, ctz_imm));
2588 }
2589 } else {
2590 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2591 __ Asr(temp, dividend, bits - 1);
2592 __ Lsr(temp, temp, bits - ctz_imm);
2593 __ Add(out, dividend, temp);
2594 __ And(out, out, abs_imm - 1);
2595 __ Sub(out, out, temp);
2596 }
2597}
2598
2599void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2600 DCHECK(instruction->IsDiv() || instruction->IsRem());
2601
2602 LocationSummary* locations = instruction->GetLocations();
2603 Location second = locations->InAt(1);
2604 DCHECK(second.IsConstant());
2605
2606 Register out = OutputRegister(instruction);
2607 Register dividend = InputRegisterAt(instruction, 0);
2608 int64_t imm = Int64FromConstant(second.GetConstant());
2609
2610 Primitive::Type type = instruction->GetResultType();
2611 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2612
2613 int64_t magic;
2614 int shift;
2615 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2616
2617 UseScratchRegisterScope temps(GetVIXLAssembler());
2618 Register temp = temps.AcquireSameSizeAs(out);
2619
2620 // temp = get_high(dividend * magic)
2621 __ Mov(temp, magic);
2622 if (type == Primitive::kPrimLong) {
2623 __ Smulh(temp, dividend, temp);
2624 } else {
2625 __ Smull(temp.X(), dividend, temp);
2626 __ Lsr(temp.X(), temp.X(), 32);
2627 }
2628
2629 if (imm > 0 && magic < 0) {
2630 __ Add(temp, temp, dividend);
2631 } else if (imm < 0 && magic > 0) {
2632 __ Sub(temp, temp, dividend);
2633 }
2634
2635 if (shift != 0) {
2636 __ Asr(temp, temp, shift);
2637 }
2638
2639 if (instruction->IsDiv()) {
2640 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2641 } else {
2642 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2643 // TODO: Strength reduction for msub.
2644 Register temp_imm = temps.AcquireSameSizeAs(out);
2645 __ Mov(temp_imm, imm);
2646 __ Msub(out, temp, temp_imm, dividend);
2647 }
2648}
2649
2650void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2651 DCHECK(instruction->IsDiv() || instruction->IsRem());
2652 Primitive::Type type = instruction->GetResultType();
2653 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2654
2655 LocationSummary* locations = instruction->GetLocations();
2656 Register out = OutputRegister(instruction);
2657 Location second = locations->InAt(1);
2658
2659 if (second.IsConstant()) {
2660 int64_t imm = Int64FromConstant(second.GetConstant());
2661
2662 if (imm == 0) {
2663 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2664 } else if (imm == 1 || imm == -1) {
2665 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002666 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002667 DivRemByPowerOfTwo(instruction);
2668 } else {
2669 DCHECK(imm <= -2 || imm >= 2);
2670 GenerateDivRemWithAnyConstant(instruction);
2671 }
2672 } else {
2673 Register dividend = InputRegisterAt(instruction, 0);
2674 Register divisor = InputRegisterAt(instruction, 1);
2675 if (instruction->IsDiv()) {
2676 __ Sdiv(out, dividend, divisor);
2677 } else {
2678 UseScratchRegisterScope temps(GetVIXLAssembler());
2679 Register temp = temps.AcquireSameSizeAs(out);
2680 __ Sdiv(temp, dividend, divisor);
2681 __ Msub(out, temp, divisor, dividend);
2682 }
2683 }
2684}
2685
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002686void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2687 LocationSummary* locations =
2688 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2689 switch (div->GetResultType()) {
2690 case Primitive::kPrimInt:
2691 case Primitive::kPrimLong:
2692 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002693 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002694 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2695 break;
2696
2697 case Primitive::kPrimFloat:
2698 case Primitive::kPrimDouble:
2699 locations->SetInAt(0, Location::RequiresFpuRegister());
2700 locations->SetInAt(1, Location::RequiresFpuRegister());
2701 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2702 break;
2703
2704 default:
2705 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2706 }
2707}
2708
2709void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2710 Primitive::Type type = div->GetResultType();
2711 switch (type) {
2712 case Primitive::kPrimInt:
2713 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002714 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002715 break;
2716
2717 case Primitive::kPrimFloat:
2718 case Primitive::kPrimDouble:
2719 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2720 break;
2721
2722 default:
2723 LOG(FATAL) << "Unexpected div type " << type;
2724 }
2725}
2726
Alexandre Rames67555f72014-11-18 10:55:16 +00002727void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002728 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2729 ? LocationSummary::kCallOnSlowPath
2730 : LocationSummary::kNoCall;
2731 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002732 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2733 if (instruction->HasUses()) {
2734 locations->SetOut(Location::SameAsFirstInput());
2735 }
2736}
2737
2738void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2739 SlowPathCodeARM64* slow_path =
2740 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2741 codegen_->AddSlowPath(slow_path);
2742 Location value = instruction->GetLocations()->InAt(0);
2743
Alexandre Rames3e69f162014-12-10 10:36:50 +00002744 Primitive::Type type = instruction->GetType();
2745
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002746 if (!Primitive::IsIntegralType(type)) {
2747 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002748 return;
2749 }
2750
Alexandre Rames67555f72014-11-18 10:55:16 +00002751 if (value.IsConstant()) {
2752 int64_t divisor = Int64ConstantFrom(value);
2753 if (divisor == 0) {
2754 __ B(slow_path->GetEntryLabel());
2755 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002756 // A division by a non-null constant is valid. We don't need to perform
2757 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002758 }
2759 } else {
2760 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2761 }
2762}
2763
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002764void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2765 LocationSummary* locations =
2766 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2767 locations->SetOut(Location::ConstantLocation(constant));
2768}
2769
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002770void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2771 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002772 // Will be generated at use site.
2773}
2774
Alexandre Rames5319def2014-10-23 10:03:10 +01002775void LocationsBuilderARM64::VisitExit(HExit* exit) {
2776 exit->SetLocations(nullptr);
2777}
2778
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002779void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002780}
2781
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002782void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2783 LocationSummary* locations =
2784 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2785 locations->SetOut(Location::ConstantLocation(constant));
2786}
2787
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002788void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002789 // Will be generated at use site.
2790}
2791
David Brazdilfc6a86a2015-06-26 10:33:45 +00002792void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002793 DCHECK(!successor->IsExitBlock());
2794 HBasicBlock* block = got->GetBlock();
2795 HInstruction* previous = got->GetPrevious();
2796 HLoopInformation* info = block->GetLoopInformation();
2797
David Brazdil46e2a392015-03-16 17:31:52 +00002798 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002799 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2800 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2801 return;
2802 }
2803 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2804 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2805 }
2806 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002807 __ B(codegen_->GetLabelOf(successor));
2808 }
2809}
2810
David Brazdilfc6a86a2015-06-26 10:33:45 +00002811void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2812 got->SetLocations(nullptr);
2813}
2814
2815void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2816 HandleGoto(got, got->GetSuccessor());
2817}
2818
2819void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2820 try_boundary->SetLocations(nullptr);
2821}
2822
2823void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2824 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2825 if (!successor->IsExitBlock()) {
2826 HandleGoto(try_boundary, successor);
2827 }
2828}
2829
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002830void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002831 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002832 vixl::Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002833 vixl::Label* false_target) {
2834 // FP branching requires both targets to be explicit. If either of the targets
2835 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2836 vixl::Label fallthrough_target;
2837 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002838
David Brazdil0debae72015-11-12 18:37:00 +00002839 if (true_target == nullptr && false_target == nullptr) {
2840 // Nothing to do. The code always falls through.
2841 return;
2842 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002843 // Constant condition, statically compared against "true" (integer value 1).
2844 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002845 if (true_target != nullptr) {
2846 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002847 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002848 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002849 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002850 if (false_target != nullptr) {
2851 __ B(false_target);
2852 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002853 }
David Brazdil0debae72015-11-12 18:37:00 +00002854 return;
2855 }
2856
2857 // The following code generates these patterns:
2858 // (1) true_target == nullptr && false_target != nullptr
2859 // - opposite condition true => branch to false_target
2860 // (2) true_target != nullptr && false_target == nullptr
2861 // - condition true => branch to true_target
2862 // (3) true_target != nullptr && false_target != nullptr
2863 // - condition true => branch to true_target
2864 // - branch to false_target
2865 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002866 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002867 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002868 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002869 if (true_target == nullptr) {
2870 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2871 } else {
2872 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2873 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002874 } else {
2875 // The condition instruction has not been materialized, use its inputs as
2876 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002877 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002878
David Brazdil0debae72015-11-12 18:37:00 +00002879 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002880 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002881 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002882 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002883 IfCondition opposite_condition = condition->GetOppositeCondition();
2884 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002885 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002886 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002887 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002888 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002889 // Integer cases.
2890 Register lhs = InputRegisterAt(condition, 0);
2891 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002892
2893 Condition arm64_cond;
2894 vixl::Label* non_fallthrough_target;
2895 if (true_target == nullptr) {
2896 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2897 non_fallthrough_target = false_target;
2898 } else {
2899 arm64_cond = ARM64Condition(condition->GetCondition());
2900 non_fallthrough_target = true_target;
2901 }
2902
Aart Bik086d27e2016-01-20 17:02:00 -08002903 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
2904 rhs.IsImmediate() && (rhs.immediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002905 switch (arm64_cond) {
2906 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002907 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002908 break;
2909 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002910 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002911 break;
2912 case lt:
2913 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002914 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002915 break;
2916 case ge:
2917 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002918 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002919 break;
2920 default:
2921 // Without the `static_cast` the compiler throws an error for
2922 // `-Werror=sign-promo`.
2923 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2924 }
2925 } else {
2926 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002927 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002928 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002929 }
2930 }
David Brazdil0debae72015-11-12 18:37:00 +00002931
2932 // If neither branch falls through (case 3), the conditional branch to `true_target`
2933 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2934 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002935 __ B(false_target);
2936 }
David Brazdil0debae72015-11-12 18:37:00 +00002937
2938 if (fallthrough_target.IsLinked()) {
2939 __ Bind(&fallthrough_target);
2940 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002941}
2942
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002943void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2944 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002945 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002946 locations->SetInAt(0, Location::RequiresRegister());
2947 }
2948}
2949
2950void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002951 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2952 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2953 vixl::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2954 nullptr : codegen_->GetLabelOf(true_successor);
2955 vixl::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2956 nullptr : codegen_->GetLabelOf(false_successor);
2957 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002958}
2959
2960void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2961 LocationSummary* locations = new (GetGraph()->GetArena())
2962 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002963 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002964 locations->SetInAt(0, Location::RequiresRegister());
2965 }
2966}
2967
2968void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002969 SlowPathCodeARM64* slow_path =
2970 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002971 GenerateTestAndBranch(deoptimize,
2972 /* condition_input_index */ 0,
2973 slow_path->GetEntryLabel(),
2974 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002975}
2976
David Brazdilc0b601b2016-02-08 14:20:45 +00002977enum SelectVariant {
2978 kCsel,
2979 kCselFalseConst,
2980 kCselTrueConst,
2981 kFcsel,
2982};
2983
2984static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
2985 return condition->IsCondition() &&
2986 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
2987}
2988
2989static inline bool IsRecognizedCselConstant(HInstruction* constant) {
2990 if (constant->IsConstant()) {
2991 int64_t value = Int64FromConstant(constant->AsConstant());
2992 if ((value == -1) || (value == 0) || (value == 1)) {
2993 return true;
2994 }
2995 }
2996 return false;
2997}
2998
2999static inline SelectVariant GetSelectVariant(HSelect* select) {
3000 if (Primitive::IsFloatingPointType(select->GetType())) {
3001 return kFcsel;
3002 } else if (IsRecognizedCselConstant(select->GetFalseValue())) {
3003 return kCselFalseConst;
3004 } else if (IsRecognizedCselConstant(select->GetTrueValue())) {
3005 return kCselTrueConst;
3006 } else {
3007 return kCsel;
3008 }
3009}
3010
3011static inline bool HasSwappedInputs(SelectVariant variant) {
3012 return variant == kCselTrueConst;
3013}
3014
3015static inline Condition GetConditionForSelect(HCondition* condition, SelectVariant variant) {
3016 IfCondition cond = HasSwappedInputs(variant) ? condition->GetOppositeCondition()
3017 : condition->GetCondition();
3018 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3019 : ARM64Condition(cond);
3020}
3021
David Brazdil74eb1b22015-12-14 11:44:01 +00003022void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3023 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
David Brazdilc0b601b2016-02-08 14:20:45 +00003024 switch (GetSelectVariant(select)) {
3025 case kCsel:
3026 locations->SetInAt(0, Location::RequiresRegister());
3027 locations->SetInAt(1, Location::RequiresRegister());
3028 locations->SetOut(Location::RequiresRegister());
3029 break;
3030 case kCselFalseConst:
3031 locations->SetInAt(0, Location::ConstantLocation(select->InputAt(0)->AsConstant()));
3032 locations->SetInAt(1, Location::RequiresRegister());
3033 locations->SetOut(Location::RequiresRegister());
3034 break;
3035 case kCselTrueConst:
3036 locations->SetInAt(0, Location::RequiresRegister());
3037 locations->SetInAt(1, Location::ConstantLocation(select->InputAt(1)->AsConstant()));
3038 locations->SetOut(Location::RequiresRegister());
3039 break;
3040 case kFcsel:
3041 locations->SetInAt(0, Location::RequiresFpuRegister());
3042 locations->SetInAt(1, Location::RequiresFpuRegister());
3043 locations->SetOut(Location::RequiresFpuRegister());
3044 break;
David Brazdil74eb1b22015-12-14 11:44:01 +00003045 }
3046 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3047 locations->SetInAt(2, Location::RequiresRegister());
3048 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003049}
3050
3051void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003052 HInstruction* cond = select->GetCondition();
3053 SelectVariant variant = GetSelectVariant(select);
3054 Condition csel_cond;
3055
3056 if (IsBooleanValueOrMaterializedCondition(cond)) {
3057 if (cond->IsCondition() && cond->GetNext() == select) {
3058 // Condition codes set from previous instruction.
3059 csel_cond = GetConditionForSelect(cond->AsCondition(), variant);
3060 } else {
3061 __ Cmp(InputRegisterAt(select, 2), 0);
3062 csel_cond = HasSwappedInputs(variant) ? eq : ne;
3063 }
3064 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003065 GenerateFcmp(cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003066 csel_cond = GetConditionForSelect(cond->AsCondition(), variant);
3067 } else {
3068 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
3069 csel_cond = GetConditionForSelect(cond->AsCondition(), variant);
3070 }
3071
3072 switch (variant) {
3073 case kCsel:
3074 case kCselFalseConst:
3075 __ Csel(OutputRegister(select),
3076 InputRegisterAt(select, 1),
3077 InputOperandAt(select, 0),
3078 csel_cond);
3079 break;
3080 case kCselTrueConst:
3081 __ Csel(OutputRegister(select),
3082 InputRegisterAt(select, 0),
3083 InputOperandAt(select, 1),
3084 csel_cond);
3085 break;
3086 case kFcsel:
3087 __ Fcsel(OutputFPRegister(select),
3088 InputFPRegisterAt(select, 1),
3089 InputFPRegisterAt(select, 0),
3090 csel_cond);
3091 break;
3092 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003093}
3094
David Srbecky0cf44932015-12-09 14:09:59 +00003095void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3096 new (GetGraph()->GetArena()) LocationSummary(info);
3097}
3098
David Srbeckyd28f4a02016-03-14 17:14:24 +00003099void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3100 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003101}
3102
3103void CodeGeneratorARM64::GenerateNop() {
3104 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003105}
3106
Alexandre Rames5319def2014-10-23 10:03:10 +01003107void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003108 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003109}
3110
3111void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003112 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003113}
3114
3115void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003116 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003117}
3118
3119void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003120 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003121}
3122
Roland Levillain44015862016-01-22 11:47:17 +00003123static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3124 return kEmitCompilerReadBarrier &&
3125 (kUseBakerReadBarrier ||
3126 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3127 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3128 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3129}
3130
Alexandre Rames67555f72014-11-18 10:55:16 +00003131void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003132 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003133 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3134 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003135 case TypeCheckKind::kExactCheck:
3136 case TypeCheckKind::kAbstractClassCheck:
3137 case TypeCheckKind::kClassHierarchyCheck:
3138 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003139 call_kind =
3140 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003141 break;
3142 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003143 case TypeCheckKind::kUnresolvedCheck:
3144 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003145 call_kind = LocationSummary::kCallOnSlowPath;
3146 break;
3147 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003148
Alexandre Rames67555f72014-11-18 10:55:16 +00003149 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003150 locations->SetInAt(0, Location::RequiresRegister());
3151 locations->SetInAt(1, Location::RequiresRegister());
3152 // The "out" register is used as a temporary, so it overlaps with the inputs.
3153 // Note that TypeCheckSlowPathARM64 uses this register too.
3154 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3155 // When read barriers are enabled, we need a temporary register for
3156 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003157 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003158 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003159 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003160}
3161
3162void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003163 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003164 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003165 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003166 Register obj = InputRegisterAt(instruction, 0);
3167 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003168 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003169 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003170 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3171 locations->GetTemp(0) :
3172 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003173 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3174 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3175 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3176 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003177
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003178 vixl::Label done, zero;
3179 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003180
3181 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003182 // Avoid null check if we know `obj` is not null.
3183 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003184 __ Cbz(obj, &zero);
3185 }
3186
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003187 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003188 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003189
Roland Levillain44015862016-01-22 11:47:17 +00003190 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003191 case TypeCheckKind::kExactCheck: {
3192 __ Cmp(out, cls);
3193 __ Cset(out, eq);
3194 if (zero.IsLinked()) {
3195 __ B(&done);
3196 }
3197 break;
3198 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003199
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003200 case TypeCheckKind::kAbstractClassCheck: {
3201 // If the class is abstract, we eagerly fetch the super class of the
3202 // object to avoid doing a comparison we know will fail.
3203 vixl::Label loop, success;
3204 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003205 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003206 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003207 // If `out` is null, we use it for the result, and jump to `done`.
3208 __ Cbz(out, &done);
3209 __ Cmp(out, cls);
3210 __ B(ne, &loop);
3211 __ Mov(out, 1);
3212 if (zero.IsLinked()) {
3213 __ B(&done);
3214 }
3215 break;
3216 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003217
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003218 case TypeCheckKind::kClassHierarchyCheck: {
3219 // Walk over the class hierarchy to find a match.
3220 vixl::Label loop, success;
3221 __ Bind(&loop);
3222 __ Cmp(out, cls);
3223 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003224 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003225 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003226 __ Cbnz(out, &loop);
3227 // If `out` is null, we use it for the result, and jump to `done`.
3228 __ B(&done);
3229 __ Bind(&success);
3230 __ Mov(out, 1);
3231 if (zero.IsLinked()) {
3232 __ B(&done);
3233 }
3234 break;
3235 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003236
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003237 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003238 // Do an exact check.
3239 vixl::Label exact_check;
3240 __ Cmp(out, cls);
3241 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003242 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003243 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003244 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003245 // If `out` is null, we use it for the result, and jump to `done`.
3246 __ Cbz(out, &done);
3247 __ Ldrh(out, HeapOperand(out, primitive_offset));
3248 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3249 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003250 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003251 __ Mov(out, 1);
3252 __ B(&done);
3253 break;
3254 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003255
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003256 case TypeCheckKind::kArrayCheck: {
3257 __ Cmp(out, cls);
3258 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003259 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3260 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003261 codegen_->AddSlowPath(slow_path);
3262 __ B(ne, slow_path->GetEntryLabel());
3263 __ Mov(out, 1);
3264 if (zero.IsLinked()) {
3265 __ B(&done);
3266 }
3267 break;
3268 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003269
Calin Juravle98893e12015-10-02 21:05:03 +01003270 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003271 case TypeCheckKind::kInterfaceCheck: {
3272 // Note that we indeed only call on slow path, but we always go
3273 // into the slow path for the unresolved and interface check
3274 // cases.
3275 //
3276 // We cannot directly call the InstanceofNonTrivial runtime
3277 // entry point without resorting to a type checking slow path
3278 // here (i.e. by calling InvokeRuntime directly), as it would
3279 // require to assign fixed registers for the inputs of this
3280 // HInstanceOf instruction (following the runtime calling
3281 // convention), which might be cluttered by the potential first
3282 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003283 //
3284 // TODO: Introduce a new runtime entry point taking the object
3285 // to test (instead of its class) as argument, and let it deal
3286 // with the read barrier issues. This will let us refactor this
3287 // case of the `switch` code as it was previously (with a direct
3288 // call to the runtime not using a type checking slow path).
3289 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003290 DCHECK(locations->OnlyCallsOnSlowPath());
3291 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3292 /* is_fatal */ false);
3293 codegen_->AddSlowPath(slow_path);
3294 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003295 if (zero.IsLinked()) {
3296 __ B(&done);
3297 }
3298 break;
3299 }
3300 }
3301
3302 if (zero.IsLinked()) {
3303 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003304 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003305 }
3306
3307 if (done.IsLinked()) {
3308 __ Bind(&done);
3309 }
3310
3311 if (slow_path != nullptr) {
3312 __ Bind(slow_path->GetExitLabel());
3313 }
3314}
3315
3316void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3317 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3318 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3319
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003320 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3321 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003322 case TypeCheckKind::kExactCheck:
3323 case TypeCheckKind::kAbstractClassCheck:
3324 case TypeCheckKind::kClassHierarchyCheck:
3325 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003326 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3327 LocationSummary::kCallOnSlowPath :
3328 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003329 break;
3330 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003331 case TypeCheckKind::kUnresolvedCheck:
3332 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003333 call_kind = LocationSummary::kCallOnSlowPath;
3334 break;
3335 }
3336
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003337 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3338 locations->SetInAt(0, Location::RequiresRegister());
3339 locations->SetInAt(1, Location::RequiresRegister());
3340 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3341 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003342 // When read barriers are enabled, we need an additional temporary
3343 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003344 if (TypeCheckNeedsATemporary(type_check_kind)) {
3345 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003346 }
3347}
3348
3349void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003350 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003351 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003352 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003353 Register obj = InputRegisterAt(instruction, 0);
3354 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003355 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003356 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3357 locations->GetTemp(1) :
3358 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003359 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003360 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3361 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3362 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3363 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003364
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003365 bool is_type_check_slow_path_fatal =
3366 (type_check_kind == TypeCheckKind::kExactCheck ||
3367 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3368 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3369 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3370 !instruction->CanThrowIntoCatchBlock();
3371 SlowPathCodeARM64* type_check_slow_path =
3372 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3373 is_type_check_slow_path_fatal);
3374 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003375
3376 vixl::Label done;
3377 // Avoid null check if we know obj is not null.
3378 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003379 __ Cbz(obj, &done);
3380 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003381
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003382 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003383 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003384
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003385 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003386 case TypeCheckKind::kExactCheck:
3387 case TypeCheckKind::kArrayCheck: {
3388 __ Cmp(temp, cls);
3389 // Jump to slow path for throwing the exception or doing a
3390 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003391 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003392 break;
3393 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003394
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003395 case TypeCheckKind::kAbstractClassCheck: {
3396 // If the class is abstract, we eagerly fetch the super class of the
3397 // object to avoid doing a comparison we know will fail.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003398 vixl::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003399 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003400 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003401 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003402
3403 // If the class reference currently in `temp` is not null, jump
3404 // to the `compare_classes` label to compare it with the checked
3405 // class.
3406 __ Cbnz(temp, &compare_classes);
3407 // Otherwise, jump to the slow path to throw the exception.
3408 //
3409 // But before, move back the object's class into `temp` before
3410 // going into the slow path, as it has been overwritten in the
3411 // meantime.
3412 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003413 GenerateReferenceLoadTwoRegisters(
3414 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003415 __ B(type_check_slow_path->GetEntryLabel());
3416
3417 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003418 __ Cmp(temp, cls);
3419 __ B(ne, &loop);
3420 break;
3421 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003422
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003423 case TypeCheckKind::kClassHierarchyCheck: {
3424 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003425 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003426 __ Bind(&loop);
3427 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003428 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003429
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003430 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003431 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003432
3433 // If the class reference currently in `temp` is not null, jump
3434 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003435 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003436 // Otherwise, jump to the slow path to throw the exception.
3437 //
3438 // But before, move back the object's class into `temp` before
3439 // going into the slow path, as it has been overwritten in the
3440 // meantime.
3441 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003442 GenerateReferenceLoadTwoRegisters(
3443 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003444 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003445 break;
3446 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003447
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003448 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003449 // Do an exact check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003450 vixl::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003451 __ Cmp(temp, cls);
3452 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003453
3454 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003455 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003456 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003457
3458 // If the component type is not null (i.e. the object is indeed
3459 // an array), jump to label `check_non_primitive_component_type`
3460 // to further check that this component type is not a primitive
3461 // type.
3462 __ Cbnz(temp, &check_non_primitive_component_type);
3463 // Otherwise, jump to the slow path to throw the exception.
3464 //
3465 // But before, move back the object's class into `temp` before
3466 // going into the slow path, as it has been overwritten in the
3467 // meantime.
3468 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003469 GenerateReferenceLoadTwoRegisters(
3470 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003471 __ B(type_check_slow_path->GetEntryLabel());
3472
3473 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003474 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3475 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003476 __ Cbz(temp, &done);
3477 // Same comment as above regarding `temp` and the slow path.
3478 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003479 GenerateReferenceLoadTwoRegisters(
3480 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003481 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003482 break;
3483 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003484
Calin Juravle98893e12015-10-02 21:05:03 +01003485 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003486 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003487 // We always go into the type check slow path for the unresolved
3488 // and interface check cases.
3489 //
3490 // We cannot directly call the CheckCast runtime entry point
3491 // without resorting to a type checking slow path here (i.e. by
3492 // calling InvokeRuntime directly), as it would require to
3493 // assign fixed registers for the inputs of this HInstanceOf
3494 // instruction (following the runtime calling convention), which
3495 // might be cluttered by the potential first read barrier
3496 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003497 //
3498 // TODO: Introduce a new runtime entry point taking the object
3499 // to test (instead of its class) as argument, and let it deal
3500 // with the read barrier issues. This will let us refactor this
3501 // case of the `switch` code as it was previously (with a direct
3502 // call to the runtime not using a type checking slow path).
3503 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003504 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003505 break;
3506 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003507 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003508
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003509 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003510}
3511
Alexandre Rames5319def2014-10-23 10:03:10 +01003512void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3513 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3514 locations->SetOut(Location::ConstantLocation(constant));
3515}
3516
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003517void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003518 // Will be generated at use site.
3519}
3520
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003521void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3522 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3523 locations->SetOut(Location::ConstantLocation(constant));
3524}
3525
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003526void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003527 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003528}
3529
Calin Juravle175dc732015-08-25 15:42:32 +01003530void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3531 // The trampoline uses the same calling convention as dex calling conventions,
3532 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3533 // the method_idx.
3534 HandleInvoke(invoke);
3535}
3536
3537void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3538 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3539}
3540
Alexandre Rames5319def2014-10-23 10:03:10 +01003541void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003542 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003543 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003544}
3545
Alexandre Rames67555f72014-11-18 10:55:16 +00003546void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3547 HandleInvoke(invoke);
3548}
3549
3550void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3551 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003552 LocationSummary* locations = invoke->GetLocations();
3553 Register temp = XRegisterFrom(locations->GetTemp(0));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003554 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3555 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003556 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003557 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003558 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003559
3560 // The register ip1 is required to be used for the hidden argument in
3561 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003562 MacroAssembler* masm = GetVIXLAssembler();
3563 UseScratchRegisterScope scratch_scope(masm);
3564 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003565 scratch_scope.Exclude(ip1);
3566 __ Mov(ip1, invoke->GetDexMethodIndex());
3567
Alexandre Rames67555f72014-11-18 10:55:16 +00003568 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003569 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003570 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003571 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003572 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003573 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003574 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003575 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003576 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003577 // Instead of simply (possibly) unpoisoning `temp` here, we should
3578 // emit a read barrier for the previous class reference load.
3579 // However this is not required in practice, as this is an
3580 // intermediate/temporary reference and because the current
3581 // concurrent copying collector keeps the from-space memory
3582 // intact/accessible until the end of the marking phase (the
3583 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003584 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00003585 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003586 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003587 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003588 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003589 // lr();
3590 __ Blr(lr);
3591 DCHECK(!codegen_->IsLeafMethod());
3592 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3593}
3594
3595void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003596 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3597 if (intrinsic.TryDispatch(invoke)) {
3598 return;
3599 }
3600
Alexandre Rames67555f72014-11-18 10:55:16 +00003601 HandleInvoke(invoke);
3602}
3603
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003604void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003605 // Explicit clinit checks triggered by static invokes must have been pruned by
3606 // art::PrepareForRegisterAllocation.
3607 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003608
Andreas Gampe878d58c2015-01-15 23:24:00 -08003609 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3610 if (intrinsic.TryDispatch(invoke)) {
3611 return;
3612 }
3613
Alexandre Rames67555f72014-11-18 10:55:16 +00003614 HandleInvoke(invoke);
3615}
3616
Andreas Gampe878d58c2015-01-15 23:24:00 -08003617static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3618 if (invoke->GetLocations()->Intrinsified()) {
3619 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3620 intrinsic.Dispatch(invoke);
3621 return true;
3622 }
3623 return false;
3624}
3625
Vladimir Markodc151b22015-10-15 18:02:30 +01003626HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3627 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3628 MethodReference target_method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003629 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003630 return desired_dispatch_info;
3631}
3632
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003633void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003634 // For better instruction scheduling we load the direct code pointer before the method pointer.
3635 bool direct_code_loaded = false;
3636 switch (invoke->GetCodePtrLocation()) {
3637 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3638 // LR = code address from literal pool with link-time patch.
3639 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3640 direct_code_loaded = true;
3641 break;
3642 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3643 // LR = invoke->GetDirectCodePtr();
3644 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3645 direct_code_loaded = true;
3646 break;
3647 default:
3648 break;
3649 }
3650
Andreas Gampe878d58c2015-01-15 23:24:00 -08003651 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003652 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3653 switch (invoke->GetMethodLoadKind()) {
3654 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3655 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003656 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003657 break;
3658 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003659 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003660 break;
3661 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3662 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003663 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003664 break;
3665 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3666 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003667 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003668 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3669 break;
3670 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3671 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003672 const DexFile& dex_file = *invoke->GetTargetMethod().dex_file;
3673 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
3674 vixl::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003675 {
3676 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003677 __ Bind(adrp_label);
3678 __ adrp(XRegisterFrom(temp), /* offset placeholder */ 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003679 }
Vladimir Marko58155012015-08-19 12:49:41 +00003680 // Add LDR with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003681 vixl::Label* ldr_label =
3682 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Alexandre Rames6dc01742015-11-12 14:44:19 +00003683 {
3684 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003685 __ Bind(ldr_label);
3686 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), /* offset placeholder */ 0));
Alexandre Rames6dc01742015-11-12 14:44:19 +00003687 }
Vladimir Marko58155012015-08-19 12:49:41 +00003688 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003689 }
Vladimir Marko58155012015-08-19 12:49:41 +00003690 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003691 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003692 Register reg = XRegisterFrom(temp);
3693 Register method_reg;
3694 if (current_method.IsRegister()) {
3695 method_reg = XRegisterFrom(current_method);
3696 } else {
3697 DCHECK(invoke->GetLocations()->Intrinsified());
3698 DCHECK(!current_method.IsValid());
3699 method_reg = reg;
3700 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3701 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003702
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003703 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003704 __ Ldr(reg.X(),
3705 MemOperand(method_reg.X(),
3706 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003707 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003708 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3709 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003710 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3711 break;
3712 }
3713 }
3714
3715 switch (invoke->GetCodePtrLocation()) {
3716 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3717 __ Bl(&frame_entry_label_);
3718 break;
3719 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3720 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3721 vixl::Label* label = &relative_call_patches_.back().label;
Alexandre Rames6dc01742015-11-12 14:44:19 +00003722 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3723 __ Bind(label);
3724 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003725 break;
3726 }
3727 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3728 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3729 // LR prepared above for better instruction scheduling.
3730 DCHECK(direct_code_loaded);
3731 // lr()
3732 __ Blr(lr);
3733 break;
3734 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3735 // LR = callee_method->entry_point_from_quick_compiled_code_;
3736 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003737 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003738 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3739 // lr()
3740 __ Blr(lr);
3741 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003742 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003743
Andreas Gampe878d58c2015-01-15 23:24:00 -08003744 DCHECK(!IsLeafMethod());
3745}
3746
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003747void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003748 // Use the calling convention instead of the location of the receiver, as
3749 // intrinsics may have put the receiver in a different register. In the intrinsics
3750 // slow path, the arguments have been moved to the right place, so here we are
3751 // guaranteed that the receiver is the first register of the calling convention.
3752 InvokeDexCallingConvention calling_convention;
3753 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003754 Register temp = XRegisterFrom(temp_in);
3755 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3756 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3757 Offset class_offset = mirror::Object::ClassOffset();
3758 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3759
3760 BlockPoolsScope block_pools(GetVIXLAssembler());
3761
3762 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003763 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003764 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003765 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003766 // Instead of simply (possibly) unpoisoning `temp` here, we should
3767 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003768 // intermediate/temporary reference and because the current
3769 // concurrent copying collector keeps the from-space memory
3770 // intact/accessible until the end of the marking phase (the
3771 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003772 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3773 // temp = temp->GetMethodAt(method_offset);
3774 __ Ldr(temp, MemOperand(temp, method_offset));
3775 // lr = temp->GetEntryPoint();
3776 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3777 // lr();
3778 __ Blr(lr);
3779}
3780
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003781vixl::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(const DexFile& dex_file,
3782 uint32_t string_index,
3783 vixl::Label* adrp_label) {
3784 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3785}
3786
3787vixl::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
3788 uint32_t element_offset,
3789 vixl::Label* adrp_label) {
3790 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3791}
3792
3793vixl::Label* CodeGeneratorARM64::NewPcRelativePatch(const DexFile& dex_file,
3794 uint32_t offset_or_index,
3795 vixl::Label* adrp_label,
3796 ArenaDeque<PcRelativePatchInfo>* patches) {
3797 // Add a patch entry and return the label.
3798 patches->emplace_back(dex_file, offset_or_index);
3799 PcRelativePatchInfo* info = &patches->back();
3800 vixl::Label* label = &info->label;
3801 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3802 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3803 return label;
3804}
3805
3806vixl::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
3807 const DexFile& dex_file, uint32_t string_index) {
3808 return boot_image_string_patches_.GetOrCreate(
3809 StringReference(&dex_file, string_index),
3810 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3811}
3812
3813vixl::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(uint64_t address) {
3814 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3815 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3816 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3817}
3818
3819vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(uint64_t address) {
3820 return DeduplicateUint64Literal(address);
3821}
3822
Vladimir Marko58155012015-08-19 12:49:41 +00003823void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3824 DCHECK(linker_patches->empty());
3825 size_t size =
3826 method_patches_.size() +
3827 call_patches_.size() +
3828 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003829 pc_relative_dex_cache_patches_.size() +
3830 boot_image_string_patches_.size() +
3831 pc_relative_string_patches_.size() +
3832 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003833 linker_patches->reserve(size);
3834 for (const auto& entry : method_patches_) {
3835 const MethodReference& target_method = entry.first;
3836 vixl::Literal<uint64_t>* literal = entry.second;
3837 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
3838 target_method.dex_file,
3839 target_method.dex_method_index));
3840 }
3841 for (const auto& entry : call_patches_) {
3842 const MethodReference& target_method = entry.first;
3843 vixl::Literal<uint64_t>* literal = entry.second;
3844 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
3845 target_method.dex_file,
3846 target_method.dex_method_index));
3847 }
3848 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003849 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003850 info.target_method.dex_file,
3851 info.target_method.dex_method_index));
3852 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003853 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003854 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003855 &info.target_dex_file,
Alexandre Rames6dc01742015-11-12 14:44:19 +00003856 info.pc_insn_label->location(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003857 info.offset_or_index));
3858 }
3859 for (const auto& entry : boot_image_string_patches_) {
3860 const StringReference& target_string = entry.first;
3861 vixl::Literal<uint32_t>* literal = entry.second;
3862 linker_patches->push_back(LinkerPatch::StringPatch(literal->offset(),
3863 target_string.dex_file,
3864 target_string.string_index));
3865 }
3866 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
3867 linker_patches->push_back(LinkerPatch::RelativeStringPatch(info.label.location(),
3868 &info.target_dex_file,
3869 info.pc_insn_label->location(),
3870 info.offset_or_index));
3871 }
3872 for (const auto& entry : boot_image_address_patches_) {
3873 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
3874 vixl::Literal<uint32_t>* literal = entry.second;
3875 linker_patches->push_back(LinkerPatch::RecordPosition(literal->offset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003876 }
3877}
3878
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003879vixl::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
3880 Uint32ToLiteralMap* map) {
3881 return map->GetOrCreate(
3882 value,
3883 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3884}
3885
Vladimir Marko58155012015-08-19 12:49:41 +00003886vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003887 return uint64_literals_.GetOrCreate(
3888 value,
3889 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003890}
3891
3892vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
3893 MethodReference target_method,
3894 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003895 return map->GetOrCreate(
3896 target_method,
3897 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003898}
3899
3900vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
3901 MethodReference target_method) {
3902 return DeduplicateMethodLiteral(target_method, &method_patches_);
3903}
3904
3905vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
3906 MethodReference target_method) {
3907 return DeduplicateMethodLiteral(target_method, &call_patches_);
3908}
3909
3910
Andreas Gampe878d58c2015-01-15 23:24:00 -08003911void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003912 // Explicit clinit checks triggered by static invokes must have been pruned by
3913 // art::PrepareForRegisterAllocation.
3914 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003915
Andreas Gampe878d58c2015-01-15 23:24:00 -08003916 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3917 return;
3918 }
3919
Alexandre Ramesd921d642015-04-16 15:07:16 +01003920 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003921 LocationSummary* locations = invoke->GetLocations();
3922 codegen_->GenerateStaticOrDirectCall(
3923 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003924 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003925}
3926
3927void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003928 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3929 return;
3930 }
3931
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003932 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003933 DCHECK(!codegen_->IsLeafMethod());
3934 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3935}
3936
Alexandre Rames67555f72014-11-18 10:55:16 +00003937void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003938 InvokeRuntimeCallingConvention calling_convention;
3939 CodeGenerator::CreateLoadClassLocationSummary(
3940 cls,
3941 LocationFrom(calling_convention.GetRegisterAt(0)),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003942 LocationFrom(vixl::x0),
3943 /* code_generator_supports_read_barrier */ true);
Alexandre Rames67555f72014-11-18 10:55:16 +00003944}
3945
3946void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003947 if (cls->NeedsAccessCheck()) {
3948 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3949 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3950 cls,
3951 cls->GetDexPc(),
3952 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003953 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003954 return;
3955 }
3956
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003957 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01003958 Register out = OutputRegister(cls);
3959 Register current_method = InputRegisterAt(cls, 0);
3960 if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003961 DCHECK(!cls->CanCallRuntime());
3962 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain44015862016-01-22 11:47:17 +00003963 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3964 GenerateGcRootFieldLoad(
3965 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Alexandre Rames67555f72014-11-18 10:55:16 +00003966 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003967 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003968 // /* GcRoot<mirror::Class>[] */ out =
3969 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Vladimir Marko05792b92015-08-03 11:56:49 +01003970 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00003971 // /* GcRoot<mirror::Class> */ out = out[type_index]
3972 GenerateGcRootFieldLoad(
3973 cls, out_loc, out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003974
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003975 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3976 DCHECK(cls->CanCallRuntime());
3977 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3978 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3979 codegen_->AddSlowPath(slow_path);
3980 if (!cls->IsInDexCache()) {
3981 __ Cbz(out, slow_path->GetEntryLabel());
3982 }
3983 if (cls->MustGenerateClinitCheck()) {
3984 GenerateClassInitializationCheck(slow_path, out);
3985 } else {
3986 __ Bind(slow_path->GetExitLabel());
3987 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003988 }
3989 }
3990}
3991
David Brazdilcb1c0552015-08-04 16:22:25 +01003992static MemOperand GetExceptionTlsAddress() {
3993 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3994}
3995
Alexandre Rames67555f72014-11-18 10:55:16 +00003996void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3997 LocationSummary* locations =
3998 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3999 locations->SetOut(Location::RequiresRegister());
4000}
4001
4002void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004003 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4004}
4005
4006void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4007 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4008}
4009
4010void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4011 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004012}
4013
David Brazdil60328912016-04-04 17:47:42 +00004014void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
4015 load->SetLocations(nullptr);
4016}
4017
4018void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
4019 // Nothing to do, this is driven by the code generator.
4020}
4021
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004022HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4023 HLoadString::LoadKind desired_string_load_kind) {
4024 if (kEmitCompilerReadBarrier) {
4025 switch (desired_string_load_kind) {
4026 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4027 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4028 case HLoadString::LoadKind::kBootImageAddress:
4029 // TODO: Implement for read barrier.
4030 return HLoadString::LoadKind::kDexCacheViaMethod;
4031 default:
4032 break;
4033 }
4034 }
4035 switch (desired_string_load_kind) {
4036 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4037 DCHECK(!GetCompilerOptions().GetCompilePic());
4038 break;
4039 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4040 DCHECK(GetCompilerOptions().GetCompilePic());
4041 break;
4042 case HLoadString::LoadKind::kBootImageAddress:
4043 break;
4044 case HLoadString::LoadKind::kDexCacheAddress:
4045 DCHECK(Runtime::Current()->UseJit());
4046 break;
4047 case HLoadString::LoadKind::kDexCachePcRelative:
4048 DCHECK(!Runtime::Current()->UseJit());
4049 break;
4050 case HLoadString::LoadKind::kDexCacheViaMethod:
4051 break;
4052 }
4053 return desired_string_load_kind;
4054}
4055
Alexandre Rames67555f72014-11-18 10:55:16 +00004056void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004057 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004058 ? LocationSummary::kCallOnSlowPath
4059 : LocationSummary::kNoCall;
4060 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004061 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
4062 locations->SetInAt(0, Location::RequiresRegister());
4063 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004064 locations->SetOut(Location::RequiresRegister());
4065}
4066
4067void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004068 Location out_loc = load->GetLocations()->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00004069 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004070
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004071 switch (load->GetLoadKind()) {
4072 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4073 DCHECK(!kEmitCompilerReadBarrier);
4074 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4075 load->GetStringIndex()));
4076 return; // No dex cache slow path.
4077 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4078 DCHECK(!kEmitCompilerReadBarrier);
4079 // Add ADRP with its PC-relative String patch.
4080 const DexFile& dex_file = load->GetDexFile();
4081 uint32_t string_index = load->GetStringIndex();
4082 vixl::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
4083 {
4084 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4085 __ Bind(adrp_label);
4086 __ adrp(out.X(), /* offset placeholder */ 0);
4087 }
4088 // Add ADD with its PC-relative String patch.
4089 vixl::Label* add_label =
4090 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4091 {
4092 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4093 __ Bind(add_label);
4094 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
4095 }
4096 return; // No dex cache slow path.
4097 }
4098 case HLoadString::LoadKind::kBootImageAddress: {
4099 DCHECK(!kEmitCompilerReadBarrier);
4100 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4101 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4102 return; // No dex cache slow path.
4103 }
4104 case HLoadString::LoadKind::kDexCacheAddress: {
4105 DCHECK_NE(load->GetAddress(), 0u);
4106 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4107 // that gives a 16KiB range. To try and reduce the number of literals if we load
4108 // multiple strings, simply split the dex cache address to a 16KiB aligned base
4109 // loaded from a literal and the remaining offset embedded in the load.
4110 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
4111 DCHECK_ALIGNED(load->GetAddress(), 4u);
4112 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4113 uint64_t base_address = load->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4114 uint32_t offset = load->GetAddress() & MaxInt<uint64_t>(offset_bits);
4115 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4116 GenerateGcRootFieldLoad(load, out_loc, out.X(), offset);
4117 break;
4118 }
4119 case HLoadString::LoadKind::kDexCachePcRelative: {
4120 // Add ADRP with its PC-relative DexCache access patch.
4121 const DexFile& dex_file = load->GetDexFile();
4122 uint32_t element_offset = load->GetDexCacheElementOffset();
4123 vixl::Label* adrp_label = codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
4124 {
4125 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4126 __ Bind(adrp_label);
4127 __ adrp(out.X(), /* offset placeholder */ 0);
4128 }
4129 // Add LDR with its PC-relative DexCache access patch.
4130 vixl::Label* ldr_label =
4131 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4132 GenerateGcRootFieldLoad(load, out_loc, out.X(), /* offset placeholder */ 0, ldr_label);
4133 break;
4134 }
4135 case HLoadString::LoadKind::kDexCacheViaMethod: {
4136 Register current_method = InputRegisterAt(load, 0);
4137 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4138 GenerateGcRootFieldLoad(
4139 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4140 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
4141 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
4142 // /* GcRoot<mirror::String> */ out = out[string_index]
4143 GenerateGcRootFieldLoad(
4144 load, out_loc, out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex()));
4145 break;
4146 }
4147 default:
4148 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
4149 UNREACHABLE();
4150 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004151
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004152 if (!load->IsInDexCache()) {
4153 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
4154 codegen_->AddSlowPath(slow_path);
4155 __ Cbz(out, slow_path->GetEntryLabel());
4156 __ Bind(slow_path->GetExitLabel());
4157 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004158}
4159
David Brazdil60328912016-04-04 17:47:42 +00004160void LocationsBuilderARM64::VisitLocal(HLocal* local) {
4161 local->SetLocations(nullptr);
4162}
4163
4164void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
4165 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
4166}
4167
Alexandre Rames5319def2014-10-23 10:03:10 +01004168void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4169 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4170 locations->SetOut(Location::ConstantLocation(constant));
4171}
4172
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004173void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004174 // Will be generated at use site.
4175}
4176
Alexandre Rames67555f72014-11-18 10:55:16 +00004177void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4178 LocationSummary* locations =
4179 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4180 InvokeRuntimeCallingConvention calling_convention;
4181 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4182}
4183
4184void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4185 codegen_->InvokeRuntime(instruction->IsEnter()
4186 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4187 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004188 instruction->GetDexPc(),
4189 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004190 if (instruction->IsEnter()) {
4191 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4192 } else {
4193 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4194 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004195}
4196
Alexandre Rames42d641b2014-10-27 14:00:51 +00004197void LocationsBuilderARM64::VisitMul(HMul* mul) {
4198 LocationSummary* locations =
4199 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4200 switch (mul->GetResultType()) {
4201 case Primitive::kPrimInt:
4202 case Primitive::kPrimLong:
4203 locations->SetInAt(0, Location::RequiresRegister());
4204 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004205 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004206 break;
4207
4208 case Primitive::kPrimFloat:
4209 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004210 locations->SetInAt(0, Location::RequiresFpuRegister());
4211 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004212 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004213 break;
4214
4215 default:
4216 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4217 }
4218}
4219
4220void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4221 switch (mul->GetResultType()) {
4222 case Primitive::kPrimInt:
4223 case Primitive::kPrimLong:
4224 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4225 break;
4226
4227 case Primitive::kPrimFloat:
4228 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004229 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004230 break;
4231
4232 default:
4233 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4234 }
4235}
4236
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004237void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4238 LocationSummary* locations =
4239 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4240 switch (neg->GetResultType()) {
4241 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004242 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004243 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004244 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004245 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004246
4247 case Primitive::kPrimFloat:
4248 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004249 locations->SetInAt(0, Location::RequiresFpuRegister());
4250 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004251 break;
4252
4253 default:
4254 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4255 }
4256}
4257
4258void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4259 switch (neg->GetResultType()) {
4260 case Primitive::kPrimInt:
4261 case Primitive::kPrimLong:
4262 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4263 break;
4264
4265 case Primitive::kPrimFloat:
4266 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004267 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004268 break;
4269
4270 default:
4271 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4272 }
4273}
4274
4275void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4276 LocationSummary* locations =
4277 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4278 InvokeRuntimeCallingConvention calling_convention;
4279 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004280 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004281 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004282 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004283}
4284
4285void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4286 LocationSummary* locations = instruction->GetLocations();
4287 InvokeRuntimeCallingConvention calling_convention;
4288 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4289 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004290 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004291 // Note: if heap poisoning is enabled, the entry point takes cares
4292 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004293 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4294 instruction,
4295 instruction->GetDexPc(),
4296 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004297 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004298}
4299
Alexandre Rames5319def2014-10-23 10:03:10 +01004300void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4301 LocationSummary* locations =
4302 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4303 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004304 if (instruction->IsStringAlloc()) {
4305 locations->AddTemp(LocationFrom(kArtMethodRegister));
4306 } else {
4307 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4308 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4309 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004310 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4311}
4312
4313void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004314 // Note: if heap poisoning is enabled, the entry point takes cares
4315 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004316 if (instruction->IsStringAlloc()) {
4317 // String is allocated through StringFactory. Call NewEmptyString entry point.
4318 Location temp = instruction->GetLocations()->GetTemp(0);
4319 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
4320 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4321 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4322 __ Blr(lr);
4323 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4324 } else {
4325 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4326 instruction,
4327 instruction->GetDexPc(),
4328 nullptr);
4329 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4330 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004331}
4332
4333void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4334 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004335 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004336 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004337}
4338
4339void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004340 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004341 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004342 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004343 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004344 break;
4345
4346 default:
4347 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4348 }
4349}
4350
David Brazdil66d126e2015-04-03 16:02:44 +01004351void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4352 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4353 locations->SetInAt(0, Location::RequiresRegister());
4354 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4355}
4356
4357void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01004358 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
4359}
4360
Alexandre Rames5319def2014-10-23 10:03:10 +01004361void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004362 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4363 ? LocationSummary::kCallOnSlowPath
4364 : LocationSummary::kNoCall;
4365 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004366 locations->SetInAt(0, Location::RequiresRegister());
4367 if (instruction->HasUses()) {
4368 locations->SetOut(Location::SameAsFirstInput());
4369 }
4370}
4371
Calin Juravle2ae48182016-03-16 14:05:09 +00004372void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4373 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004374 return;
4375 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004376
Alexandre Ramesd921d642015-04-16 15:07:16 +01004377 BlockPoolsScope block_pools(GetVIXLAssembler());
4378 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004379 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004380 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004381}
4382
Calin Juravle2ae48182016-03-16 14:05:09 +00004383void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004384 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004385 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004386
4387 LocationSummary* locations = instruction->GetLocations();
4388 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004389
4390 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004391}
4392
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004393void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004394 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004395}
4396
Alexandre Rames67555f72014-11-18 10:55:16 +00004397void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4398 HandleBinaryOp(instruction);
4399}
4400
4401void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4402 HandleBinaryOp(instruction);
4403}
4404
Alexandre Rames3e69f162014-12-10 10:36:50 +00004405void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4406 LOG(FATAL) << "Unreachable";
4407}
4408
4409void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4410 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4411}
4412
Alexandre Rames5319def2014-10-23 10:03:10 +01004413void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4414 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4415 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4416 if (location.IsStackSlot()) {
4417 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4418 } else if (location.IsDoubleStackSlot()) {
4419 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4420 }
4421 locations->SetOut(location);
4422}
4423
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004424void InstructionCodeGeneratorARM64::VisitParameterValue(
4425 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004426 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004427}
4428
4429void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4430 LocationSummary* locations =
4431 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004432 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004433}
4434
4435void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4436 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4437 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004438}
4439
4440void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4441 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4442 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4443 locations->SetInAt(i, Location::Any());
4444 }
4445 locations->SetOut(Location::Any());
4446}
4447
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004448void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004449 LOG(FATAL) << "Unreachable";
4450}
4451
Serban Constantinescu02164b32014-11-13 14:05:07 +00004452void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004453 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004454 LocationSummary::CallKind call_kind =
4455 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004456 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4457
4458 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004459 case Primitive::kPrimInt:
4460 case Primitive::kPrimLong:
4461 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004462 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004463 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4464 break;
4465
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004466 case Primitive::kPrimFloat:
4467 case Primitive::kPrimDouble: {
4468 InvokeRuntimeCallingConvention calling_convention;
4469 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4470 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4471 locations->SetOut(calling_convention.GetReturnLocation(type));
4472
4473 break;
4474 }
4475
Serban Constantinescu02164b32014-11-13 14:05:07 +00004476 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004477 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004478 }
4479}
4480
4481void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4482 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004483
Serban Constantinescu02164b32014-11-13 14:05:07 +00004484 switch (type) {
4485 case Primitive::kPrimInt:
4486 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004487 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004488 break;
4489 }
4490
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004491 case Primitive::kPrimFloat:
4492 case Primitive::kPrimDouble: {
4493 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4494 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004495 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004496 if (type == Primitive::kPrimFloat) {
4497 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4498 } else {
4499 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4500 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004501 break;
4502 }
4503
Serban Constantinescu02164b32014-11-13 14:05:07 +00004504 default:
4505 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004506 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004507 }
4508}
4509
Calin Juravle27df7582015-04-17 19:12:31 +01004510void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4511 memory_barrier->SetLocations(nullptr);
4512}
4513
4514void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004515 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004516}
4517
Alexandre Rames5319def2014-10-23 10:03:10 +01004518void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4519 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4520 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004521 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004522}
4523
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004524void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004525 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004526}
4527
4528void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4529 instruction->SetLocations(nullptr);
4530}
4531
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004532void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004533 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004534}
4535
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004536void LocationsBuilderARM64::VisitRor(HRor* ror) {
4537 HandleBinaryOp(ror);
4538}
4539
4540void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4541 HandleBinaryOp(ror);
4542}
4543
Serban Constantinescu02164b32014-11-13 14:05:07 +00004544void LocationsBuilderARM64::VisitShl(HShl* shl) {
4545 HandleShift(shl);
4546}
4547
4548void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4549 HandleShift(shl);
4550}
4551
4552void LocationsBuilderARM64::VisitShr(HShr* shr) {
4553 HandleShift(shr);
4554}
4555
4556void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4557 HandleShift(shr);
4558}
4559
David Brazdil60328912016-04-04 17:47:42 +00004560void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
4561 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
4562 Primitive::Type field_type = store->InputAt(1)->GetType();
4563 switch (field_type) {
4564 case Primitive::kPrimNot:
4565 case Primitive::kPrimBoolean:
4566 case Primitive::kPrimByte:
4567 case Primitive::kPrimChar:
4568 case Primitive::kPrimShort:
4569 case Primitive::kPrimInt:
4570 case Primitive::kPrimFloat:
4571 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
4572 break;
4573
4574 case Primitive::kPrimLong:
4575 case Primitive::kPrimDouble:
4576 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
4577 break;
4578
4579 default:
4580 LOG(FATAL) << "Unimplemented local type " << field_type;
4581 UNREACHABLE();
4582 }
4583}
4584
4585void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
4586}
4587
Alexandre Rames5319def2014-10-23 10:03:10 +01004588void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004589 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004590}
4591
4592void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004593 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004594}
4595
Alexandre Rames67555f72014-11-18 10:55:16 +00004596void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004597 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004598}
4599
4600void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004601 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004602}
4603
4604void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004605 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004606}
4607
Alexandre Rames67555f72014-11-18 10:55:16 +00004608void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004609 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004610}
4611
Calin Juravlee460d1d2015-09-29 04:52:17 +01004612void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4613 HUnresolvedInstanceFieldGet* instruction) {
4614 FieldAccessCallingConventionARM64 calling_convention;
4615 codegen_->CreateUnresolvedFieldLocationSummary(
4616 instruction, instruction->GetFieldType(), calling_convention);
4617}
4618
4619void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4620 HUnresolvedInstanceFieldGet* instruction) {
4621 FieldAccessCallingConventionARM64 calling_convention;
4622 codegen_->GenerateUnresolvedFieldAccess(instruction,
4623 instruction->GetFieldType(),
4624 instruction->GetFieldIndex(),
4625 instruction->GetDexPc(),
4626 calling_convention);
4627}
4628
4629void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4630 HUnresolvedInstanceFieldSet* instruction) {
4631 FieldAccessCallingConventionARM64 calling_convention;
4632 codegen_->CreateUnresolvedFieldLocationSummary(
4633 instruction, instruction->GetFieldType(), calling_convention);
4634}
4635
4636void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4637 HUnresolvedInstanceFieldSet* instruction) {
4638 FieldAccessCallingConventionARM64 calling_convention;
4639 codegen_->GenerateUnresolvedFieldAccess(instruction,
4640 instruction->GetFieldType(),
4641 instruction->GetFieldIndex(),
4642 instruction->GetDexPc(),
4643 calling_convention);
4644}
4645
4646void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4647 HUnresolvedStaticFieldGet* instruction) {
4648 FieldAccessCallingConventionARM64 calling_convention;
4649 codegen_->CreateUnresolvedFieldLocationSummary(
4650 instruction, instruction->GetFieldType(), calling_convention);
4651}
4652
4653void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4654 HUnresolvedStaticFieldGet* instruction) {
4655 FieldAccessCallingConventionARM64 calling_convention;
4656 codegen_->GenerateUnresolvedFieldAccess(instruction,
4657 instruction->GetFieldType(),
4658 instruction->GetFieldIndex(),
4659 instruction->GetDexPc(),
4660 calling_convention);
4661}
4662
4663void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4664 HUnresolvedStaticFieldSet* instruction) {
4665 FieldAccessCallingConventionARM64 calling_convention;
4666 codegen_->CreateUnresolvedFieldLocationSummary(
4667 instruction, instruction->GetFieldType(), calling_convention);
4668}
4669
4670void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4671 HUnresolvedStaticFieldSet* instruction) {
4672 FieldAccessCallingConventionARM64 calling_convention;
4673 codegen_->GenerateUnresolvedFieldAccess(instruction,
4674 instruction->GetFieldType(),
4675 instruction->GetFieldIndex(),
4676 instruction->GetDexPc(),
4677 calling_convention);
4678}
4679
Alexandre Rames5319def2014-10-23 10:03:10 +01004680void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4681 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4682}
4683
4684void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004685 HBasicBlock* block = instruction->GetBlock();
4686 if (block->GetLoopInformation() != nullptr) {
4687 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4688 // The back edge will generate the suspend check.
4689 return;
4690 }
4691 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4692 // The goto will generate the suspend check.
4693 return;
4694 }
4695 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004696}
4697
Alexandre Rames67555f72014-11-18 10:55:16 +00004698void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4699 LocationSummary* locations =
4700 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4701 InvokeRuntimeCallingConvention calling_convention;
4702 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4703}
4704
4705void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4706 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004707 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004708 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004709}
4710
4711void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4712 LocationSummary* locations =
4713 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4714 Primitive::Type input_type = conversion->GetInputType();
4715 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004716 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004717 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4718 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4719 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4720 }
4721
Alexandre Rames542361f2015-01-29 16:57:31 +00004722 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004723 locations->SetInAt(0, Location::RequiresFpuRegister());
4724 } else {
4725 locations->SetInAt(0, Location::RequiresRegister());
4726 }
4727
Alexandre Rames542361f2015-01-29 16:57:31 +00004728 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004729 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4730 } else {
4731 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4732 }
4733}
4734
4735void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4736 Primitive::Type result_type = conversion->GetResultType();
4737 Primitive::Type input_type = conversion->GetInputType();
4738
4739 DCHECK_NE(input_type, result_type);
4740
Alexandre Rames542361f2015-01-29 16:57:31 +00004741 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004742 int result_size = Primitive::ComponentSize(result_type);
4743 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004744 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004745 Register output = OutputRegister(conversion);
4746 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004747 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004748 // 'int' values are used directly as W registers, discarding the top
4749 // bits, so we don't need to sign-extend and can just perform a move.
4750 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4751 // top 32 bits of the target register. We theoretically could leave those
4752 // bits unchanged, but we would have to make sure that no code uses a
4753 // 32bit input value as a 64bit value assuming that the top 32 bits are
4754 // zero.
4755 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004756 } else if (result_type == Primitive::kPrimChar ||
4757 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4758 __ Ubfx(output,
4759 output.IsX() ? source.X() : source.W(),
4760 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004761 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004762 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004763 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004764 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004765 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004766 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004767 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4768 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004769 } else if (Primitive::IsFloatingPointType(result_type) &&
4770 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004771 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4772 } else {
4773 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4774 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004775 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004776}
Alexandre Rames67555f72014-11-18 10:55:16 +00004777
Serban Constantinescu02164b32014-11-13 14:05:07 +00004778void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4779 HandleShift(ushr);
4780}
4781
4782void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4783 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004784}
4785
4786void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4787 HandleBinaryOp(instruction);
4788}
4789
4790void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4791 HandleBinaryOp(instruction);
4792}
4793
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004794void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004795 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004796 LOG(FATAL) << "Unreachable";
4797}
4798
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004799void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004800 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004801 LOG(FATAL) << "Unreachable";
4802}
4803
Mark Mendellfe57faa2015-09-18 09:26:15 -04004804// Simple implementation of packed switch - generate cascaded compare/jumps.
4805void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4806 LocationSummary* locations =
4807 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4808 locations->SetInAt(0, Location::RequiresRegister());
4809}
4810
4811void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4812 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004813 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004814 Register value_reg = InputRegisterAt(switch_instr, 0);
4815 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4816
Zheng Xu3927c8b2015-11-18 17:46:25 +08004817 // Roughly set 16 as max average assemblies generated per HIR in a graph.
4818 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * vixl::kInstructionSize;
4819 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4820 // make sure we don't emit it if the target may run out of range.
4821 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4822 // ranges and emit the tables only as required.
4823 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004824
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004825 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004826 // Current instruction id is an upper bound of the number of HIRs in the graph.
4827 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4828 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004829 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4830 Register temp = temps.AcquireW();
4831 __ Subs(temp, value_reg, Operand(lower_bound));
4832
Zheng Xu3927c8b2015-11-18 17:46:25 +08004833 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004834 // Jump to successors[0] if value == lower_bound.
4835 __ B(eq, codegen_->GetLabelOf(successors[0]));
4836 int32_t last_index = 0;
4837 for (; num_entries - last_index > 2; last_index += 2) {
4838 __ Subs(temp, temp, Operand(2));
4839 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4840 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4841 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4842 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4843 }
4844 if (num_entries - last_index == 2) {
4845 // The last missing case_value.
4846 __ Cmp(temp, Operand(1));
4847 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004848 }
4849
4850 // And the default for any other value.
4851 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4852 __ B(codegen_->GetLabelOf(default_block));
4853 }
4854 } else {
4855 JumpTableARM64* jump_table = new (GetGraph()->GetArena()) JumpTableARM64(switch_instr);
4856 codegen_->AddJumpTable(jump_table);
4857
4858 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4859
4860 // Below instructions should use at most one blocked register. Since there are two blocked
4861 // registers, we are free to block one.
4862 Register temp_w = temps.AcquireW();
4863 Register index;
4864 // Remove the bias.
4865 if (lower_bound != 0) {
4866 index = temp_w;
4867 __ Sub(index, value_reg, Operand(lower_bound));
4868 } else {
4869 index = value_reg;
4870 }
4871
4872 // Jump to default block if index is out of the range.
4873 __ Cmp(index, Operand(num_entries));
4874 __ B(hs, codegen_->GetLabelOf(default_block));
4875
4876 // In current VIXL implementation, it won't require any blocked registers to encode the
4877 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4878 // register pressure.
4879 Register table_base = temps.AcquireX();
4880 // Load jump offset from the table.
4881 __ Adr(table_base, jump_table->GetTableStartLabel());
4882 Register jump_offset = temp_w;
4883 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4884
4885 // Jump to target block by branching to table_base(pc related) + offset.
4886 Register target_address = table_base;
4887 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4888 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004889 }
4890}
4891
Roland Levillain44015862016-01-22 11:47:17 +00004892void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4893 Location out,
4894 uint32_t offset,
4895 Location maybe_temp) {
4896 Primitive::Type type = Primitive::kPrimNot;
4897 Register out_reg = RegisterFrom(out, type);
4898 if (kEmitCompilerReadBarrier) {
4899 Register temp_reg = RegisterFrom(maybe_temp, type);
4900 if (kUseBakerReadBarrier) {
4901 // Load with fast path based Baker's read barrier.
4902 // /* HeapReference<Object> */ out = *(out + offset)
4903 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4904 out,
4905 out_reg,
4906 offset,
4907 temp_reg,
4908 /* needs_null_check */ false,
4909 /* use_load_acquire */ false);
4910 } else {
4911 // Load with slow path based read barrier.
4912 // Save the value of `out` into `maybe_temp` before overwriting it
4913 // in the following move operation, as we will need it for the
4914 // read barrier below.
4915 __ Mov(temp_reg, out_reg);
4916 // /* HeapReference<Object> */ out = *(out + offset)
4917 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4918 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4919 }
4920 } else {
4921 // Plain load with no read barrier.
4922 // /* HeapReference<Object> */ out = *(out + offset)
4923 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4924 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4925 }
4926}
4927
4928void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
4929 Location out,
4930 Location obj,
4931 uint32_t offset,
4932 Location maybe_temp) {
4933 Primitive::Type type = Primitive::kPrimNot;
4934 Register out_reg = RegisterFrom(out, type);
4935 Register obj_reg = RegisterFrom(obj, type);
4936 if (kEmitCompilerReadBarrier) {
4937 if (kUseBakerReadBarrier) {
4938 // Load with fast path based Baker's read barrier.
4939 Register temp_reg = RegisterFrom(maybe_temp, type);
4940 // /* HeapReference<Object> */ out = *(obj + offset)
4941 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4942 out,
4943 obj_reg,
4944 offset,
4945 temp_reg,
4946 /* needs_null_check */ false,
4947 /* use_load_acquire */ false);
4948 } else {
4949 // Load with slow path based read barrier.
4950 // /* HeapReference<Object> */ out = *(obj + offset)
4951 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
4952 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4953 }
4954 } else {
4955 // Plain load with no read barrier.
4956 // /* HeapReference<Object> */ out = *(obj + offset)
4957 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
4958 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4959 }
4960}
4961
4962void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
4963 Location root,
4964 vixl::Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004965 uint32_t offset,
4966 vixl::Label* fixup_label) {
Roland Levillain44015862016-01-22 11:47:17 +00004967 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
4968 if (kEmitCompilerReadBarrier) {
4969 if (kUseBakerReadBarrier) {
4970 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4971 // Baker's read barrier are used:
4972 //
4973 // root = obj.field;
4974 // if (Thread::Current()->GetIsGcMarking()) {
4975 // root = ReadBarrier::Mark(root)
4976 // }
4977
4978 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004979 if (fixup_label == nullptr) {
4980 __ Ldr(root_reg, MemOperand(obj, offset));
4981 } else {
4982 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4983 __ Bind(fixup_label);
4984 __ ldr(root_reg, MemOperand(obj, offset));
4985 }
Roland Levillain44015862016-01-22 11:47:17 +00004986 static_assert(
4987 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
4988 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
4989 "have different sizes.");
4990 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
4991 "art::mirror::CompressedReference<mirror::Object> and int32_t "
4992 "have different sizes.");
4993
4994 // Slow path used to mark the GC root `root`.
4995 SlowPathCodeARM64* slow_path =
4996 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root, root);
4997 codegen_->AddSlowPath(slow_path);
4998
4999 MacroAssembler* masm = GetVIXLAssembler();
5000 UseScratchRegisterScope temps(masm);
5001 Register temp = temps.AcquireW();
5002 // temp = Thread::Current()->GetIsGcMarking()
5003 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64WordSize>().Int32Value()));
5004 __ Cbnz(temp, slow_path->GetEntryLabel());
5005 __ Bind(slow_path->GetExitLabel());
5006 } else {
5007 // GC root loaded through a slow path for read barriers other
5008 // than Baker's.
5009 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005010 if (fixup_label == nullptr) {
5011 __ Add(root_reg.X(), obj.X(), offset);
5012 } else {
5013 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
5014 __ Bind(fixup_label);
5015 __ add(root_reg.X(), obj.X(), offset);
5016 }
Roland Levillain44015862016-01-22 11:47:17 +00005017 // /* mirror::Object* */ root = root->Read()
5018 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5019 }
5020 } else {
5021 // Plain GC root load with no read barrier.
5022 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005023 if (fixup_label == nullptr) {
5024 __ Ldr(root_reg, MemOperand(obj, offset));
5025 } else {
5026 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
5027 __ Bind(fixup_label);
5028 __ ldr(root_reg, MemOperand(obj, offset));
5029 }
Roland Levillain44015862016-01-22 11:47:17 +00005030 // Note that GC roots are not affected by heap poisoning, thus we
5031 // do not have to unpoison `root_reg` here.
5032 }
5033}
5034
5035void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5036 Location ref,
5037 vixl::Register obj,
5038 uint32_t offset,
5039 Register temp,
5040 bool needs_null_check,
5041 bool use_load_acquire) {
5042 DCHECK(kEmitCompilerReadBarrier);
5043 DCHECK(kUseBakerReadBarrier);
5044
5045 // /* HeapReference<Object> */ ref = *(obj + offset)
5046 Location no_index = Location::NoLocation();
5047 GenerateReferenceLoadWithBakerReadBarrier(
5048 instruction, ref, obj, offset, no_index, temp, needs_null_check, use_load_acquire);
5049}
5050
5051void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5052 Location ref,
5053 vixl::Register obj,
5054 uint32_t data_offset,
5055 Location index,
5056 Register temp,
5057 bool needs_null_check) {
5058 DCHECK(kEmitCompilerReadBarrier);
5059 DCHECK(kUseBakerReadBarrier);
5060
5061 // Array cells are never volatile variables, therefore array loads
5062 // never use Load-Acquire instructions on ARM64.
5063 const bool use_load_acquire = false;
5064
5065 // /* HeapReference<Object> */ ref =
5066 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5067 GenerateReferenceLoadWithBakerReadBarrier(
5068 instruction, ref, obj, data_offset, index, temp, needs_null_check, use_load_acquire);
5069}
5070
5071void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5072 Location ref,
5073 vixl::Register obj,
5074 uint32_t offset,
5075 Location index,
5076 Register temp,
5077 bool needs_null_check,
5078 bool use_load_acquire) {
5079 DCHECK(kEmitCompilerReadBarrier);
5080 DCHECK(kUseBakerReadBarrier);
5081 // If `index` is a valid location, then we are emitting an array
5082 // load, so we shouldn't be using a Load Acquire instruction.
5083 // In other words: `index.IsValid()` => `!use_load_acquire`.
5084 DCHECK(!index.IsValid() || !use_load_acquire);
5085
5086 MacroAssembler* masm = GetVIXLAssembler();
5087 UseScratchRegisterScope temps(masm);
5088
5089 // In slow path based read barriers, the read barrier call is
5090 // inserted after the original load. However, in fast path based
5091 // Baker's read barriers, we need to perform the load of
5092 // mirror::Object::monitor_ *before* the original reference load.
5093 // This load-load ordering is required by the read barrier.
5094 // The fast path/slow path (for Baker's algorithm) should look like:
5095 //
5096 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5097 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5098 // HeapReference<Object> ref = *src; // Original reference load.
5099 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5100 // if (is_gray) {
5101 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5102 // }
5103 //
5104 // Note: the original implementation in ReadBarrier::Barrier is
5105 // slightly more complex as it performs additional checks that we do
5106 // not do here for performance reasons.
5107
5108 Primitive::Type type = Primitive::kPrimNot;
5109 Register ref_reg = RegisterFrom(ref, type);
5110 DCHECK(obj.IsW());
5111 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5112
5113 // /* int32_t */ monitor = obj->monitor_
5114 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5115 if (needs_null_check) {
5116 MaybeRecordImplicitNullCheck(instruction);
5117 }
5118 // /* LockWord */ lock_word = LockWord(monitor)
5119 static_assert(sizeof(LockWord) == sizeof(int32_t),
5120 "art::LockWord and int32_t have different sizes.");
5121 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
5122 __ Lsr(temp, temp, LockWord::kReadBarrierStateShift);
5123 __ And(temp, temp, Operand(LockWord::kReadBarrierStateMask));
5124 static_assert(
5125 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
5126 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
5127
5128 // Introduce a dependency on the high bits of rb_state, which shall
5129 // be all zeroes, to prevent load-load reordering, and without using
5130 // a memory barrier (which would be more expensive).
5131 // temp2 = rb_state & ~LockWord::kReadBarrierStateMask = 0
5132 Register temp2 = temps.AcquireW();
5133 __ Bic(temp2, temp, Operand(LockWord::kReadBarrierStateMask));
5134 // obj is unchanged by this operation, but its value now depends on
5135 // temp2, which depends on temp.
5136 __ Add(obj, obj, Operand(temp2));
5137 temps.Release(temp2);
5138
5139 // The actual reference load.
5140 if (index.IsValid()) {
5141 static_assert(
5142 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5143 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005144 // /* HeapReference<Object> */ ref =
5145 // *(obj + offset + index * sizeof(HeapReference<Object>))
Roland Levillainca0bf032016-02-09 12:49:18 +00005146 const size_t shift_amount = Primitive::ComponentSizeShift(type);
Roland Levillain44015862016-01-22 11:47:17 +00005147 if (index.IsConstant()) {
Roland Levillainca0bf032016-02-09 12:49:18 +00005148 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << shift_amount);
5149 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillain44015862016-01-22 11:47:17 +00005150 } else {
Roland Levillainca0bf032016-02-09 12:49:18 +00005151 temp2 = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00005152 __ Add(temp2, obj, offset);
Roland Levillainca0bf032016-02-09 12:49:18 +00005153 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, shift_amount));
5154 temps.Release(temp2);
Roland Levillain44015862016-01-22 11:47:17 +00005155 }
Roland Levillain44015862016-01-22 11:47:17 +00005156 } else {
5157 // /* HeapReference<Object> */ ref = *(obj + offset)
5158 MemOperand field = HeapOperand(obj, offset);
5159 if (use_load_acquire) {
5160 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5161 } else {
5162 Load(type, ref_reg, field);
5163 }
5164 }
5165
5166 // Object* ref = ref_addr->AsMirrorPtr()
5167 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5168
5169 // Slow path used to mark the object `ref` when it is gray.
5170 SlowPathCodeARM64* slow_path =
5171 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref, ref);
5172 AddSlowPath(slow_path);
5173
5174 // if (rb_state == ReadBarrier::gray_ptr_)
5175 // ref = ReadBarrier::Mark(ref);
5176 __ Cmp(temp, ReadBarrier::gray_ptr_);
5177 __ B(eq, slow_path->GetEntryLabel());
5178 __ Bind(slow_path->GetExitLabel());
5179}
5180
5181void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5182 Location out,
5183 Location ref,
5184 Location obj,
5185 uint32_t offset,
5186 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005187 DCHECK(kEmitCompilerReadBarrier);
5188
Roland Levillain44015862016-01-22 11:47:17 +00005189 // Insert a slow path based read barrier *after* the reference load.
5190 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005191 // If heap poisoning is enabled, the unpoisoning of the loaded
5192 // reference will be carried out by the runtime within the slow
5193 // path.
5194 //
5195 // Note that `ref` currently does not get unpoisoned (when heap
5196 // poisoning is enabled), which is alright as the `ref` argument is
5197 // not used by the artReadBarrierSlow entry point.
5198 //
5199 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5200 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5201 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5202 AddSlowPath(slow_path);
5203
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005204 __ B(slow_path->GetEntryLabel());
5205 __ Bind(slow_path->GetExitLabel());
5206}
5207
Roland Levillain44015862016-01-22 11:47:17 +00005208void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5209 Location out,
5210 Location ref,
5211 Location obj,
5212 uint32_t offset,
5213 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005214 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005215 // Baker's read barriers shall be handled by the fast path
5216 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5217 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005218 // If heap poisoning is enabled, unpoisoning will be taken care of
5219 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005220 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005221 } else if (kPoisonHeapReferences) {
5222 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5223 }
5224}
5225
Roland Levillain44015862016-01-22 11:47:17 +00005226void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5227 Location out,
5228 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005229 DCHECK(kEmitCompilerReadBarrier);
5230
Roland Levillain44015862016-01-22 11:47:17 +00005231 // Insert a slow path based read barrier *after* the GC root load.
5232 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005233 // Note that GC roots are not affected by heap poisoning, so we do
5234 // not need to do anything special for this here.
5235 SlowPathCodeARM64* slow_path =
5236 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5237 AddSlowPath(slow_path);
5238
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005239 __ B(slow_path->GetEntryLabel());
5240 __ Bind(slow_path->GetExitLabel());
5241}
5242
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005243void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5244 LocationSummary* locations =
5245 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5246 locations->SetInAt(0, Location::RequiresRegister());
5247 locations->SetOut(Location::RequiresRegister());
5248}
5249
5250void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5251 LocationSummary* locations = instruction->GetLocations();
5252 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005253 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005254 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5255 instruction->GetIndex(), kArm64PointerSize).SizeValue();
5256 } else {
5257 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
5258 instruction->GetIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
5259 }
5260 __ Ldr(XRegisterFrom(locations->Out()),
5261 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
5262}
5263
5264
5265
Alexandre Rames67555f72014-11-18 10:55:16 +00005266#undef __
5267#undef QUICK_ENTRY_POINT
5268
Alexandre Rames5319def2014-10-23 10:03:10 +01005269} // namespace arm64
5270} // namespace art