blob: 4e33ee15da7de679eb0c2fa5f4614731370d3cb0 [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
45namespace arm64 {
46
Andreas Gampe878d58c2015-01-15 23:24:00 -080047using helpers::CPURegisterFrom;
48using helpers::DRegisterFrom;
49using helpers::FPRegisterFrom;
50using helpers::HeapOperand;
51using helpers::HeapOperandFrom;
52using helpers::InputCPURegisterAt;
53using helpers::InputFPRegisterAt;
54using helpers::InputRegisterAt;
55using helpers::InputOperandAt;
56using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080057using helpers::LocationFrom;
58using helpers::OperandFromMemOperand;
59using helpers::OutputCPURegister;
60using helpers::OutputFPRegister;
61using helpers::OutputRegister;
62using helpers::RegisterFrom;
63using helpers::StackOperandFrom;
64using helpers::VIXLRegCodeFromART;
65using helpers::WRegisterFrom;
66using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000067using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080068using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069
Alexandre Rames5319def2014-10-23 10:03:10 +010070static constexpr int kCurrentMethodStackOffset = 0;
71
Alexandre Rames5319def2014-10-23 10:03:10 +010072inline Condition ARM64Condition(IfCondition cond) {
73 switch (cond) {
74 case kCondEQ: return eq;
75 case kCondNE: return ne;
76 case kCondLT: return lt;
77 case kCondLE: return le;
78 case kCondGT: return gt;
79 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070080 case kCondB: return lo;
81 case kCondBE: return ls;
82 case kCondA: return hi;
83 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010084 }
Roland Levillain7f63c522015-07-13 15:54:55 +000085 LOG(FATAL) << "Unreachable";
86 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010087}
88
Alexandre Ramesa89086e2014-11-07 17:13:25 +000089Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000090 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
91 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
92 // but we use the exact registers for clarity.
93 if (return_type == Primitive::kPrimFloat) {
94 return LocationFrom(s0);
95 } else if (return_type == Primitive::kPrimDouble) {
96 return LocationFrom(d0);
97 } else if (return_type == Primitive::kPrimLong) {
98 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +010099 } else if (return_type == Primitive::kPrimVoid) {
100 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000101 } else {
102 return LocationFrom(w0);
103 }
104}
105
Alexandre Rames5319def2014-10-23 10:03:10 +0100106Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000107 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100108}
109
Alexandre Rames67555f72014-11-18 10:55:16 +0000110#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
111#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100112
Zheng Xuda403092015-04-24 17:35:39 +0800113// Calculate memory accessing operand for save/restore live registers.
114static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
115 RegisterSet* register_set,
116 int64_t spill_offset,
117 bool is_save) {
118 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
119 codegen->GetNumberOfCoreRegisters(),
120 register_set->GetFloatingPointRegisters(),
121 codegen->GetNumberOfFloatingPointRegisters()));
122
123 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
124 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000125 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
126 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
Zheng Xuda403092015-04-24 17:35:39 +0800127
128 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
129 UseScratchRegisterScope temps(masm);
130
131 Register base = masm->StackPointer();
132 int64_t core_spill_size = core_list.TotalSizeInBytes();
133 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
134 int64_t reg_size = kXRegSizeInBytes;
135 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
136 uint32_t ls_access_size = WhichPowerOf2(reg_size);
137 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
138 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
139 // If the offset does not fit in the instruction's immediate field, use an alternate register
140 // to compute the base address(float point registers spill base address).
141 Register new_base = temps.AcquireSameSizeAs(base);
142 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
143 base = new_base;
144 spill_offset = -core_spill_size;
145 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
146 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
147 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
148 }
149
150 if (is_save) {
151 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
152 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
153 } else {
154 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
155 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
156 }
157}
158
159void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
160 RegisterSet* register_set = locations->GetLiveRegisters();
161 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
162 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
163 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
164 // If the register holds an object, update the stack mask.
165 if (locations->RegisterContainsObject(i)) {
166 locations->SetStackBit(stack_offset / kVRegSize);
167 }
168 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
169 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
170 saved_core_stack_offsets_[i] = stack_offset;
171 stack_offset += kXRegSizeInBytes;
172 }
173 }
174
175 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
176 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
177 register_set->ContainsFloatingPointRegister(i)) {
178 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
179 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
180 saved_fpu_stack_offsets_[i] = stack_offset;
181 stack_offset += kDRegSizeInBytes;
182 }
183 }
184
185 SaveRestoreLiveRegistersHelper(codegen, register_set,
186 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
187}
188
189void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
190 RegisterSet* register_set = locations->GetLiveRegisters();
191 SaveRestoreLiveRegistersHelper(codegen, register_set,
192 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
193}
194
Alexandre Rames5319def2014-10-23 10:03:10 +0100195class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
196 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100197 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100198
Alexandre Rames67555f72014-11-18 10:55:16 +0000199 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100200 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000201 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100202
Alexandre Rames5319def2014-10-23 10:03:10 +0100203 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000204 if (instruction_->CanThrowIntoCatchBlock()) {
205 // Live registers will be restored in the catch block if caught.
206 SaveLiveRegisters(codegen, instruction_->GetLocations());
207 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000208 // We're moving two locations to locations that could overlap, so we need a parallel
209 // move resolver.
210 InvokeRuntimeCallingConvention calling_convention;
211 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100212 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
213 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000214 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000215 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800216 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100217 }
218
Alexandre Rames8158f282015-08-07 10:26:17 +0100219 bool IsFatal() const OVERRIDE { return true; }
220
Alexandre Rames9931f312015-06-19 14:47:01 +0100221 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
222
Alexandre Rames5319def2014-10-23 10:03:10 +0100223 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000224 HBoundsCheck* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000225
Alexandre Rames5319def2014-10-23 10:03:10 +0100226 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
227};
228
Alexandre Rames67555f72014-11-18 10:55:16 +0000229class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
230 public:
231 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
232
233 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
234 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
235 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000236 if (instruction_->CanThrowIntoCatchBlock()) {
237 // Live registers will be restored in the catch block if caught.
238 SaveLiveRegisters(codegen, instruction_->GetLocations());
239 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000240 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000241 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800242 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000243 }
244
Alexandre Rames8158f282015-08-07 10:26:17 +0100245 bool IsFatal() const OVERRIDE { return true; }
246
Alexandre Rames9931f312015-06-19 14:47:01 +0100247 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
248
Alexandre Rames67555f72014-11-18 10:55:16 +0000249 private:
250 HDivZeroCheck* const instruction_;
251 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
252};
253
254class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
255 public:
256 LoadClassSlowPathARM64(HLoadClass* cls,
257 HInstruction* at,
258 uint32_t dex_pc,
259 bool do_clinit)
260 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
261 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
262 }
263
264 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
265 LocationSummary* locations = at_->GetLocations();
266 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
267
268 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000269 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000270
271 InvokeRuntimeCallingConvention calling_convention;
272 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000273 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
274 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000275 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800276 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100277 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800278 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100279 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800280 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000281
282 // Move the class to the desired location.
283 Location out = locations->Out();
284 if (out.IsValid()) {
285 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
286 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000287 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000288 }
289
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000290 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000291 __ B(GetExitLabel());
292 }
293
Alexandre Rames9931f312015-06-19 14:47:01 +0100294 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
295
Alexandre Rames67555f72014-11-18 10:55:16 +0000296 private:
297 // The class this slow path will load.
298 HLoadClass* const cls_;
299
300 // The instruction where this slow path is happening.
301 // (Might be the load class or an initialization check).
302 HInstruction* const at_;
303
304 // The dex PC of `at_`.
305 const uint32_t dex_pc_;
306
307 // Whether to initialize the class.
308 const bool do_clinit_;
309
310 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
311};
312
313class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
314 public:
315 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
316
317 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
318 LocationSummary* locations = instruction_->GetLocations();
319 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
320 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
321
322 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000323 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000324
325 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800326 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000327 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000328 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100329 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000330 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000331 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000332
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000333 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000334 __ B(GetExitLabel());
335 }
336
Alexandre Rames9931f312015-06-19 14:47:01 +0100337 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
338
Alexandre Rames67555f72014-11-18 10:55:16 +0000339 private:
340 HLoadString* const instruction_;
341
342 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
343};
344
Alexandre Rames5319def2014-10-23 10:03:10 +0100345class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
346 public:
347 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
348
Alexandre Rames67555f72014-11-18 10:55:16 +0000349 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
350 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100351 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000352 if (instruction_->CanThrowIntoCatchBlock()) {
353 // Live registers will be restored in the catch block if caught.
354 SaveLiveRegisters(codegen, instruction_->GetLocations());
355 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000357 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800358 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100359 }
360
Alexandre Rames8158f282015-08-07 10:26:17 +0100361 bool IsFatal() const OVERRIDE { return true; }
362
Alexandre Rames9931f312015-06-19 14:47:01 +0100363 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
364
Alexandre Rames5319def2014-10-23 10:03:10 +0100365 private:
366 HNullCheck* const instruction_;
367
368 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
369};
370
371class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
372 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100373 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100374 : instruction_(instruction), successor_(successor) {}
375
Alexandre Rames67555f72014-11-18 10:55:16 +0000376 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
377 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100378 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000379 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000380 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000381 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800382 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000383 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000384 if (successor_ == nullptr) {
385 __ B(GetReturnLabel());
386 } else {
387 __ B(arm64_codegen->GetLabelOf(successor_));
388 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100389 }
390
391 vixl::Label* GetReturnLabel() {
392 DCHECK(successor_ == nullptr);
393 return &return_label_;
394 }
395
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100396 HBasicBlock* GetSuccessor() const {
397 return successor_;
398 }
399
Alexandre Rames9931f312015-06-19 14:47:01 +0100400 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
401
Alexandre Rames5319def2014-10-23 10:03:10 +0100402 private:
403 HSuspendCheck* const instruction_;
404 // If not null, the block to branch to after the suspend check.
405 HBasicBlock* const successor_;
406
407 // If `successor_` is null, the label to branch to after the suspend check.
408 vixl::Label return_label_;
409
410 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
411};
412
Alexandre Rames67555f72014-11-18 10:55:16 +0000413class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
414 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000415 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
416 : instruction_(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000417
418 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000419 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100420 Location class_to_check = locations->InAt(1);
421 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
422 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000423 DCHECK(instruction_->IsCheckCast()
424 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
425 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100426 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000427
Alexandre Rames67555f72014-11-18 10:55:16 +0000428 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000429
430 if (instruction_->IsCheckCast()) {
431 // The codegen for the instruction overwrites `temp`, so put it back in place.
432 Register obj = InputRegisterAt(instruction_, 0);
433 Register temp = WRegisterFrom(locations->GetTemp(0));
434 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
435 __ Ldr(temp, HeapOperand(obj, class_offset));
436 arm64_codegen->GetAssembler()->MaybeUnpoisonHeapReference(temp);
437 }
438
439 if (!is_fatal_) {
440 SaveLiveRegisters(codegen, locations);
441 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000442
443 // We're moving two locations to locations that could overlap, so we need a parallel
444 // move resolver.
445 InvokeRuntimeCallingConvention calling_convention;
446 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100447 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
448 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000449
450 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000451 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100452 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000453 Primitive::Type ret_type = instruction_->GetType();
454 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
455 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800456 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
457 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000458 } else {
459 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100460 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800461 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000462 }
463
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000464 if (!is_fatal_) {
465 RestoreLiveRegisters(codegen, locations);
466 __ B(GetExitLabel());
467 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000468 }
469
Alexandre Rames9931f312015-06-19 14:47:01 +0100470 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000471 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100472
Alexandre Rames67555f72014-11-18 10:55:16 +0000473 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000474 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000475 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000476
Alexandre Rames67555f72014-11-18 10:55:16 +0000477 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
478};
479
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700480class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
481 public:
482 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100483 : instruction_(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700484
485 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
486 __ Bind(GetEntryLabel());
487 SaveLiveRegisters(codegen, instruction_->GetLocations());
488 DCHECK(instruction_->IsDeoptimize());
489 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
490 uint32_t dex_pc = deoptimize->GetDexPc();
491 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
492 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
493 }
494
Alexandre Rames9931f312015-06-19 14:47:01 +0100495 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
496
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700497 private:
498 HInstruction* const instruction_;
499 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
500};
501
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100502class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
503 public:
504 explicit ArraySetSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
505
506 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
507 LocationSummary* locations = instruction_->GetLocations();
508 __ Bind(GetEntryLabel());
509 SaveLiveRegisters(codegen, locations);
510
511 InvokeRuntimeCallingConvention calling_convention;
512 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
513 parallel_move.AddMove(
514 locations->InAt(0),
515 LocationFrom(calling_convention.GetRegisterAt(0)),
516 Primitive::kPrimNot,
517 nullptr);
518 parallel_move.AddMove(
519 locations->InAt(1),
520 LocationFrom(calling_convention.GetRegisterAt(1)),
521 Primitive::kPrimInt,
522 nullptr);
523 parallel_move.AddMove(
524 locations->InAt(2),
525 LocationFrom(calling_convention.GetRegisterAt(2)),
526 Primitive::kPrimNot,
527 nullptr);
528 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
529
530 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
531 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
532 instruction_,
533 instruction_->GetDexPc(),
534 this);
535 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
536 RestoreLiveRegisters(codegen, locations);
537 __ B(GetExitLabel());
538 }
539
540 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
541
542 private:
543 HInstruction* const instruction_;
544
545 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
546};
547
Alexandre Rames5319def2014-10-23 10:03:10 +0100548#undef __
549
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100550Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100551 Location next_location;
552 if (type == Primitive::kPrimVoid) {
553 LOG(FATAL) << "Unreachable type " << type;
554 }
555
Alexandre Rames542361f2015-01-29 16:57:31 +0000556 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100557 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
558 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000559 } else if (!Primitive::IsFloatingPointType(type) &&
560 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000561 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
562 } else {
563 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000564 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
565 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100566 }
567
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000568 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000569 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100570 return next_location;
571}
572
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100573Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100574 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100575}
576
Serban Constantinescu579885a2015-02-22 20:51:33 +0000577CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
578 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100579 const CompilerOptions& compiler_options,
580 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100581 : CodeGenerator(graph,
582 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000583 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000584 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000585 callee_saved_core_registers.list(),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000586 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100587 compiler_options,
588 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100589 block_labels_(nullptr),
590 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000591 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000592 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000593 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100594 uint64_literals_(std::less<uint64_t>(),
595 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
596 method_patches_(MethodReferenceComparator(),
597 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
598 call_patches_(MethodReferenceComparator(),
599 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
600 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
601 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000602 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000603 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000604}
Alexandre Rames5319def2014-10-23 10:03:10 +0100605
Alexandre Rames67555f72014-11-18 10:55:16 +0000606#undef __
607#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100608
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000609void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
610 // Ensure we emit the literal pool.
611 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000612
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000613 CodeGenerator::Finalize(allocator);
614}
615
Zheng Xuad4450e2015-04-17 18:48:56 +0800616void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
617 // Note: There are 6 kinds of moves:
618 // 1. constant -> GPR/FPR (non-cycle)
619 // 2. constant -> stack (non-cycle)
620 // 3. GPR/FPR -> GPR/FPR
621 // 4. GPR/FPR -> stack
622 // 5. stack -> GPR/FPR
623 // 6. stack -> stack (non-cycle)
624 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
625 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
626 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
627 // dependency.
628 vixl_temps_.Open(GetVIXLAssembler());
629}
630
631void ParallelMoveResolverARM64::FinishEmitNativeCode() {
632 vixl_temps_.Close();
633}
634
635Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
636 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
637 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
638 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
639 Location scratch = GetScratchLocation(kind);
640 if (!scratch.Equals(Location::NoLocation())) {
641 return scratch;
642 }
643 // Allocate from VIXL temp registers.
644 if (kind == Location::kRegister) {
645 scratch = LocationFrom(vixl_temps_.AcquireX());
646 } else {
647 DCHECK(kind == Location::kFpuRegister);
648 scratch = LocationFrom(vixl_temps_.AcquireD());
649 }
650 AddScratchLocation(scratch);
651 return scratch;
652}
653
654void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
655 if (loc.IsRegister()) {
656 vixl_temps_.Release(XRegisterFrom(loc));
657 } else {
658 DCHECK(loc.IsFpuRegister());
659 vixl_temps_.Release(DRegisterFrom(loc));
660 }
661 RemoveScratchLocation(loc);
662}
663
Alexandre Rames3e69f162014-12-10 10:36:50 +0000664void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100665 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100666 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000667}
668
Alexandre Rames5319def2014-10-23 10:03:10 +0100669void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100670 MacroAssembler* masm = GetVIXLAssembler();
671 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000672 __ Bind(&frame_entry_label_);
673
Serban Constantinescu02164b32014-11-13 14:05:07 +0000674 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
675 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100676 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000677 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000678 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000679 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000680 __ Ldr(wzr, MemOperand(temp, 0));
681 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000682 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100683
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000684 if (!HasEmptyFrame()) {
685 int frame_size = GetFrameSize();
686 // Stack layout:
687 // sp[frame_size - 8] : lr.
688 // ... : other preserved core registers.
689 // ... : other preserved fp registers.
690 // ... : reserved frame space.
691 // sp[0] : current method.
692 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100693 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800694 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
695 frame_size - GetCoreSpillSize());
696 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
697 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000698 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100699}
700
701void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100702 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100703 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000704 if (!HasEmptyFrame()) {
705 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800706 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
707 frame_size - FrameEntrySpillSize());
708 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
709 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000710 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100711 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000712 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100713 __ Ret();
714 GetAssembler()->cfi().RestoreState();
715 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100716}
717
Zheng Xuda403092015-04-24 17:35:39 +0800718vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
719 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
720 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
721 core_spill_mask_);
722}
723
724vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
725 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
726 GetNumberOfFloatingPointRegisters()));
727 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
728 fpu_spill_mask_);
729}
730
Alexandre Rames5319def2014-10-23 10:03:10 +0100731void CodeGeneratorARM64::Bind(HBasicBlock* block) {
732 __ Bind(GetLabelOf(block));
733}
734
Alexandre Rames5319def2014-10-23 10:03:10 +0100735void CodeGeneratorARM64::Move(HInstruction* instruction,
736 Location location,
737 HInstruction* move_for) {
738 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100739 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000740 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100741
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100742 if (instruction->IsFakeString()) {
743 // The fake string is an alias for null.
744 DCHECK(IsBaseline());
745 instruction = locations->Out().GetConstant();
746 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
747 }
748
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100749 if (instruction->IsCurrentMethod()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100750 MoveLocation(location,
751 Location::DoubleStackSlot(kCurrentMethodStackOffset),
752 Primitive::kPrimVoid);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100753 } else if (locations != nullptr && locations->Out().Equals(location)) {
754 return;
755 } else if (instruction->IsIntConstant()
756 || instruction->IsLongConstant()
757 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000758 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100759 if (location.IsRegister()) {
760 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000761 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100762 (instruction->IsLongConstant() && dst.Is64Bits()));
763 __ Mov(dst, value);
764 } else {
765 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000766 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000767 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
768 ? temps.AcquireW()
769 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100770 __ Mov(temp, value);
771 __ Str(temp, StackOperandFrom(location));
772 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000773 } else if (instruction->IsTemporary()) {
774 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000775 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100776 } else if (instruction->IsLoadLocal()) {
777 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000778 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000779 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000780 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000781 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100782 }
783
784 } else {
785 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000786 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100787 }
788}
789
Calin Juravle175dc732015-08-25 15:42:32 +0100790void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
791 DCHECK(location.IsRegister());
792 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
793}
794
Calin Juravlee460d1d2015-09-29 04:52:17 +0100795void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
796 if (location.IsRegister()) {
797 locations->AddTemp(location);
798 } else {
799 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
800 }
801}
802
Alexandre Rames5319def2014-10-23 10:03:10 +0100803Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
804 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000805
Alexandre Rames5319def2014-10-23 10:03:10 +0100806 switch (type) {
807 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000808 case Primitive::kPrimInt:
809 case Primitive::kPrimFloat:
810 return Location::StackSlot(GetStackSlot(load->GetLocal()));
811
812 case Primitive::kPrimLong:
813 case Primitive::kPrimDouble:
814 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
815
Alexandre Rames5319def2014-10-23 10:03:10 +0100816 case Primitive::kPrimBoolean:
817 case Primitive::kPrimByte:
818 case Primitive::kPrimChar:
819 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100820 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100821 LOG(FATAL) << "Unexpected type " << type;
822 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000823
Alexandre Rames5319def2014-10-23 10:03:10 +0100824 LOG(FATAL) << "Unreachable";
825 return Location::NoLocation();
826}
827
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100828void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000829 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100830 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000831 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100832 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100833 if (value_can_be_null) {
834 __ Cbz(value, &done);
835 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100836 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
837 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000838 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100839 if (value_can_be_null) {
840 __ Bind(&done);
841 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100842}
843
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000844void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
845 // Blocked core registers:
846 // lr : Runtime reserved.
847 // tr : Runtime reserved.
848 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
849 // ip1 : VIXL core temp.
850 // ip0 : VIXL core temp.
851 //
852 // Blocked fp registers:
853 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100854 CPURegList reserved_core_registers = vixl_reserved_core_registers;
855 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100856 while (!reserved_core_registers.IsEmpty()) {
857 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
858 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000859
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000860 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800861 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000862 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
863 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000864
865 if (is_baseline) {
866 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
867 while (!reserved_core_baseline_registers.IsEmpty()) {
868 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
869 }
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100870 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000871
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100872 if (is_baseline || GetGraph()->IsDebuggable()) {
873 // Stubs do not save callee-save floating point registers. If the graph
874 // is debuggable, we need to deal with these registers differently. For
875 // now, just block them.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000876 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
877 while (!reserved_fp_baseline_registers.IsEmpty()) {
878 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
879 }
880 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100881}
882
883Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
884 if (type == Primitive::kPrimVoid) {
885 LOG(FATAL) << "Unreachable type " << type;
886 }
887
Alexandre Rames542361f2015-01-29 16:57:31 +0000888 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000889 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
890 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100891 return Location::FpuRegisterLocation(reg);
892 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000893 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
894 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100895 return Location::RegisterLocation(reg);
896 }
897}
898
Alexandre Rames3e69f162014-12-10 10:36:50 +0000899size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
900 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
901 __ Str(reg, MemOperand(sp, stack_index));
902 return kArm64WordSize;
903}
904
905size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
906 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
907 __ Ldr(reg, MemOperand(sp, stack_index));
908 return kArm64WordSize;
909}
910
911size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
912 FPRegister reg = FPRegister(reg_id, kDRegSize);
913 __ Str(reg, MemOperand(sp, stack_index));
914 return kArm64WordSize;
915}
916
917size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
918 FPRegister reg = FPRegister(reg_id, kDRegSize);
919 __ Ldr(reg, MemOperand(sp, stack_index));
920 return kArm64WordSize;
921}
922
Alexandre Rames5319def2014-10-23 10:03:10 +0100923void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100924 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100925}
926
927void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100928 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100929}
930
Alexandre Rames67555f72014-11-18 10:55:16 +0000931void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000932 if (constant->IsIntConstant()) {
933 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
934 } else if (constant->IsLongConstant()) {
935 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
936 } else if (constant->IsNullConstant()) {
937 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000938 } else if (constant->IsFloatConstant()) {
939 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
940 } else {
941 DCHECK(constant->IsDoubleConstant());
942 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
943 }
944}
945
Alexandre Rames3e69f162014-12-10 10:36:50 +0000946
947static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
948 DCHECK(constant.IsConstant());
949 HConstant* cst = constant.GetConstant();
950 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000951 // Null is mapped to a core W register, which we associate with kPrimInt.
952 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000953 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
954 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
955 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
956}
957
Calin Juravlee460d1d2015-09-29 04:52:17 +0100958void CodeGeneratorARM64::MoveLocation(Location destination,
959 Location source,
960 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000961 if (source.Equals(destination)) {
962 return;
963 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000964
965 // A valid move can always be inferred from the destination and source
966 // locations. When moving from and to a register, the argument type can be
967 // used to generate 32bit instead of 64bit moves. In debug mode we also
968 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100969 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000970
971 if (destination.IsRegister() || destination.IsFpuRegister()) {
972 if (unspecified_type) {
973 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
974 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000975 (src_cst != nullptr && (src_cst->IsIntConstant()
976 || src_cst->IsFloatConstant()
977 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000978 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100979 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000980 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000981 // If the source is a double stack slot or a 64bit constant, a 64bit
982 // type is appropriate. Else the source is a register, and since the
983 // type has not been specified, we chose a 64bit type to force a 64bit
984 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100985 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000986 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000987 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100988 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
989 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
990 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000991 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
992 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
993 __ Ldr(dst, StackOperandFrom(source));
994 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100995 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000996 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100997 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000998 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100999 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001000 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001001 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001002 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1003 ? Primitive::kPrimLong
1004 : Primitive::kPrimInt;
1005 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1006 }
1007 } else {
1008 DCHECK(source.IsFpuRegister());
1009 if (destination.IsRegister()) {
1010 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1011 ? Primitive::kPrimDouble
1012 : Primitive::kPrimFloat;
1013 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1014 } else {
1015 DCHECK(destination.IsFpuRegister());
1016 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001017 }
1018 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001019 } else { // The destination is not a register. It must be a stack slot.
1020 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1021 if (source.IsRegister() || source.IsFpuRegister()) {
1022 if (unspecified_type) {
1023 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001024 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001025 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001026 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001027 }
1028 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001029 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1030 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1031 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001032 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001033 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1034 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001035 UseScratchRegisterScope temps(GetVIXLAssembler());
1036 HConstant* src_cst = source.GetConstant();
1037 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001038 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001039 temp = temps.AcquireW();
1040 } else if (src_cst->IsLongConstant()) {
1041 temp = temps.AcquireX();
1042 } else if (src_cst->IsFloatConstant()) {
1043 temp = temps.AcquireS();
1044 } else {
1045 DCHECK(src_cst->IsDoubleConstant());
1046 temp = temps.AcquireD();
1047 }
1048 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001049 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001050 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001051 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001052 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001053 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001054 // There is generally less pressure on FP registers.
1055 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001056 __ Ldr(temp, StackOperandFrom(source));
1057 __ Str(temp, StackOperandFrom(destination));
1058 }
1059 }
1060}
1061
1062void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001063 CPURegister dst,
1064 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001065 switch (type) {
1066 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001067 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001068 break;
1069 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001070 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001071 break;
1072 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001073 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001074 break;
1075 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001076 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001077 break;
1078 case Primitive::kPrimInt:
1079 case Primitive::kPrimNot:
1080 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001081 case Primitive::kPrimFloat:
1082 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001083 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001084 __ Ldr(dst, src);
1085 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001086 case Primitive::kPrimVoid:
1087 LOG(FATAL) << "Unreachable type " << type;
1088 }
1089}
1090
Calin Juravle77520bc2015-01-12 18:45:46 +00001091void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001092 CPURegister dst,
1093 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001094 MacroAssembler* masm = GetVIXLAssembler();
1095 BlockPoolsScope block_pools(masm);
1096 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001097 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001098 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001099
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001100 DCHECK(!src.IsPreIndex());
1101 DCHECK(!src.IsPostIndex());
1102
1103 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001104 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001105 MemOperand base = MemOperand(temp_base);
1106 switch (type) {
1107 case Primitive::kPrimBoolean:
1108 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001109 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001110 break;
1111 case Primitive::kPrimByte:
1112 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001113 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001114 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1115 break;
1116 case Primitive::kPrimChar:
1117 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001118 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001119 break;
1120 case Primitive::kPrimShort:
1121 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001122 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001123 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1124 break;
1125 case Primitive::kPrimInt:
1126 case Primitive::kPrimNot:
1127 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001128 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001129 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001130 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001131 break;
1132 case Primitive::kPrimFloat:
1133 case Primitive::kPrimDouble: {
1134 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001135 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001136
1137 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1138 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001139 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001140 __ Fmov(FPRegister(dst), temp);
1141 break;
1142 }
1143 case Primitive::kPrimVoid:
1144 LOG(FATAL) << "Unreachable type " << type;
1145 }
1146}
1147
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001148void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001149 CPURegister src,
1150 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001151 switch (type) {
1152 case Primitive::kPrimBoolean:
1153 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001154 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001155 break;
1156 case Primitive::kPrimChar:
1157 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001158 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001159 break;
1160 case Primitive::kPrimInt:
1161 case Primitive::kPrimNot:
1162 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001163 case Primitive::kPrimFloat:
1164 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001165 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001166 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001167 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001168 case Primitive::kPrimVoid:
1169 LOG(FATAL) << "Unreachable type " << type;
1170 }
1171}
1172
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001173void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1174 CPURegister src,
1175 const MemOperand& dst) {
1176 UseScratchRegisterScope temps(GetVIXLAssembler());
1177 Register temp_base = temps.AcquireX();
1178
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001179 DCHECK(!dst.IsPreIndex());
1180 DCHECK(!dst.IsPostIndex());
1181
1182 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001183 Operand op = OperandFromMemOperand(dst);
1184 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001185 MemOperand base = MemOperand(temp_base);
1186 switch (type) {
1187 case Primitive::kPrimBoolean:
1188 case Primitive::kPrimByte:
1189 __ Stlrb(Register(src), base);
1190 break;
1191 case Primitive::kPrimChar:
1192 case Primitive::kPrimShort:
1193 __ Stlrh(Register(src), base);
1194 break;
1195 case Primitive::kPrimInt:
1196 case Primitive::kPrimNot:
1197 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001198 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001199 __ Stlr(Register(src), base);
1200 break;
1201 case Primitive::kPrimFloat:
1202 case Primitive::kPrimDouble: {
1203 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001204 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001205
1206 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1207 __ Fmov(temp, FPRegister(src));
1208 __ Stlr(temp, base);
1209 break;
1210 }
1211 case Primitive::kPrimVoid:
1212 LOG(FATAL) << "Unreachable type " << type;
1213 }
1214}
1215
Calin Juravle175dc732015-08-25 15:42:32 +01001216void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1217 HInstruction* instruction,
1218 uint32_t dex_pc,
1219 SlowPathCode* slow_path) {
1220 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1221 instruction,
1222 dex_pc,
1223 slow_path);
1224}
1225
Alexandre Rames67555f72014-11-18 10:55:16 +00001226void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1227 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001228 uint32_t dex_pc,
1229 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001230 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001231 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001232 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1233 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001234 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001235}
1236
1237void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1238 vixl::Register class_reg) {
1239 UseScratchRegisterScope temps(GetVIXLAssembler());
1240 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001241 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001242 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001243
Serban Constantinescu02164b32014-11-13 14:05:07 +00001244 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001245 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001246 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1247 __ Add(temp, class_reg, status_offset);
1248 __ Ldar(temp, HeapOperand(temp));
1249 __ Cmp(temp, mirror::Class::kStatusInitialized);
1250 __ B(lt, slow_path->GetEntryLabel());
1251 } else {
1252 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1253 __ Cmp(temp, mirror::Class::kStatusInitialized);
1254 __ B(lt, slow_path->GetEntryLabel());
1255 __ Dmb(InnerShareable, BarrierReads);
1256 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001257 __ Bind(slow_path->GetExitLabel());
1258}
Alexandre Rames5319def2014-10-23 10:03:10 +01001259
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001260void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1261 BarrierType type = BarrierAll;
1262
1263 switch (kind) {
1264 case MemBarrierKind::kAnyAny:
1265 case MemBarrierKind::kAnyStore: {
1266 type = BarrierAll;
1267 break;
1268 }
1269 case MemBarrierKind::kLoadAny: {
1270 type = BarrierReads;
1271 break;
1272 }
1273 case MemBarrierKind::kStoreStore: {
1274 type = BarrierWrites;
1275 break;
1276 }
1277 default:
1278 LOG(FATAL) << "Unexpected memory barrier " << kind;
1279 }
1280 __ Dmb(InnerShareable, type);
1281}
1282
Serban Constantinescu02164b32014-11-13 14:05:07 +00001283void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1284 HBasicBlock* successor) {
1285 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001286 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1287 if (slow_path == nullptr) {
1288 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1289 instruction->SetSlowPath(slow_path);
1290 codegen_->AddSlowPath(slow_path);
1291 if (successor != nullptr) {
1292 DCHECK(successor->IsLoopHeader());
1293 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1294 }
1295 } else {
1296 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1297 }
1298
Serban Constantinescu02164b32014-11-13 14:05:07 +00001299 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1300 Register temp = temps.AcquireW();
1301
1302 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1303 if (successor == nullptr) {
1304 __ Cbnz(temp, slow_path->GetEntryLabel());
1305 __ Bind(slow_path->GetReturnLabel());
1306 } else {
1307 __ Cbz(temp, codegen_->GetLabelOf(successor));
1308 __ B(slow_path->GetEntryLabel());
1309 // slow_path will return to GetLabelOf(successor).
1310 }
1311}
1312
Alexandre Rames5319def2014-10-23 10:03:10 +01001313InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1314 CodeGeneratorARM64* codegen)
1315 : HGraphVisitor(graph),
1316 assembler_(codegen->GetAssembler()),
1317 codegen_(codegen) {}
1318
1319#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001320 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001321
1322#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1323
1324enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001325 // Using a base helps identify when we hit such breakpoints.
1326 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001327#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1328 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1329#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1330};
1331
1332#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1333 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001334 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001335 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1336 } \
1337 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1338 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1339 locations->SetOut(Location::Any()); \
1340 }
1341 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1342#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1343
1344#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001345#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001346
Alexandre Rames67555f72014-11-18 10:55:16 +00001347void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001348 DCHECK_EQ(instr->InputCount(), 2U);
1349 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1350 Primitive::Type type = instr->GetResultType();
1351 switch (type) {
1352 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001353 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001354 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001355 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001356 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001357 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001358
1359 case Primitive::kPrimFloat:
1360 case Primitive::kPrimDouble:
1361 locations->SetInAt(0, Location::RequiresFpuRegister());
1362 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001363 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001364 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001365
Alexandre Rames5319def2014-10-23 10:03:10 +01001366 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001367 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001368 }
1369}
1370
Alexandre Rames09a99962015-04-15 11:47:56 +01001371void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1372 LocationSummary* locations =
1373 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1374 locations->SetInAt(0, Location::RequiresRegister());
1375 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1376 locations->SetOut(Location::RequiresFpuRegister());
1377 } else {
1378 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1379 }
1380}
1381
1382void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1383 const FieldInfo& field_info) {
1384 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001385 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001386 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001387
1388 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1389 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1390
1391 if (field_info.IsVolatile()) {
1392 if (use_acquire_release) {
1393 // NB: LoadAcquire will record the pc info if needed.
1394 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1395 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001396 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001397 codegen_->MaybeRecordImplicitNullCheck(instruction);
1398 // For IRIW sequential consistency kLoadAny is not sufficient.
1399 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1400 }
1401 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001402 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001403 codegen_->MaybeRecordImplicitNullCheck(instruction);
1404 }
Roland Levillain4d027112015-07-01 15:41:14 +01001405
1406 if (field_type == Primitive::kPrimNot) {
1407 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1408 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001409}
1410
1411void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1412 LocationSummary* locations =
1413 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1414 locations->SetInAt(0, Location::RequiresRegister());
1415 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1416 locations->SetInAt(1, Location::RequiresFpuRegister());
1417 } else {
1418 locations->SetInAt(1, Location::RequiresRegister());
1419 }
1420}
1421
1422void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001423 const FieldInfo& field_info,
1424 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001425 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001426 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001427
1428 Register obj = InputRegisterAt(instruction, 0);
1429 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001430 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001431 Offset offset = field_info.GetFieldOffset();
1432 Primitive::Type field_type = field_info.GetFieldType();
1433 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1434
Roland Levillain4d027112015-07-01 15:41:14 +01001435 {
1436 // We use a block to end the scratch scope before the write barrier, thus
1437 // freeing the temporary registers so they can be used in `MarkGCCard`.
1438 UseScratchRegisterScope temps(GetVIXLAssembler());
1439
1440 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1441 DCHECK(value.IsW());
1442 Register temp = temps.AcquireW();
1443 __ Mov(temp, value.W());
1444 GetAssembler()->PoisonHeapReference(temp.W());
1445 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001446 }
Roland Levillain4d027112015-07-01 15:41:14 +01001447
1448 if (field_info.IsVolatile()) {
1449 if (use_acquire_release) {
1450 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1451 codegen_->MaybeRecordImplicitNullCheck(instruction);
1452 } else {
1453 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1454 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1455 codegen_->MaybeRecordImplicitNullCheck(instruction);
1456 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1457 }
1458 } else {
1459 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1460 codegen_->MaybeRecordImplicitNullCheck(instruction);
1461 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001462 }
1463
1464 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001465 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001466 }
1467}
1468
Alexandre Rames67555f72014-11-18 10:55:16 +00001469void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001470 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001471
1472 switch (type) {
1473 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001474 case Primitive::kPrimLong: {
1475 Register dst = OutputRegister(instr);
1476 Register lhs = InputRegisterAt(instr, 0);
1477 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001478 if (instr->IsAdd()) {
1479 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001480 } else if (instr->IsAnd()) {
1481 __ And(dst, lhs, rhs);
1482 } else if (instr->IsOr()) {
1483 __ Orr(dst, lhs, rhs);
1484 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001485 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001486 } else {
1487 DCHECK(instr->IsXor());
1488 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001489 }
1490 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001491 }
1492 case Primitive::kPrimFloat:
1493 case Primitive::kPrimDouble: {
1494 FPRegister dst = OutputFPRegister(instr);
1495 FPRegister lhs = InputFPRegisterAt(instr, 0);
1496 FPRegister rhs = InputFPRegisterAt(instr, 1);
1497 if (instr->IsAdd()) {
1498 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001499 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001500 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001501 } else {
1502 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001503 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001504 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001505 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001506 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001507 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001508 }
1509}
1510
Serban Constantinescu02164b32014-11-13 14:05:07 +00001511void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1512 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1513
1514 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1515 Primitive::Type type = instr->GetResultType();
1516 switch (type) {
1517 case Primitive::kPrimInt:
1518 case Primitive::kPrimLong: {
1519 locations->SetInAt(0, Location::RequiresRegister());
1520 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1521 locations->SetOut(Location::RequiresRegister());
1522 break;
1523 }
1524 default:
1525 LOG(FATAL) << "Unexpected shift type " << type;
1526 }
1527}
1528
1529void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1530 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1531
1532 Primitive::Type type = instr->GetType();
1533 switch (type) {
1534 case Primitive::kPrimInt:
1535 case Primitive::kPrimLong: {
1536 Register dst = OutputRegister(instr);
1537 Register lhs = InputRegisterAt(instr, 0);
1538 Operand rhs = InputOperandAt(instr, 1);
1539 if (rhs.IsImmediate()) {
1540 uint32_t shift_value = (type == Primitive::kPrimInt)
1541 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1542 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1543 if (instr->IsShl()) {
1544 __ Lsl(dst, lhs, shift_value);
1545 } else if (instr->IsShr()) {
1546 __ Asr(dst, lhs, shift_value);
1547 } else {
1548 __ Lsr(dst, lhs, shift_value);
1549 }
1550 } else {
1551 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1552
1553 if (instr->IsShl()) {
1554 __ Lsl(dst, lhs, rhs_reg);
1555 } else if (instr->IsShr()) {
1556 __ Asr(dst, lhs, rhs_reg);
1557 } else {
1558 __ Lsr(dst, lhs, rhs_reg);
1559 }
1560 }
1561 break;
1562 }
1563 default:
1564 LOG(FATAL) << "Unexpected shift operation type " << type;
1565 }
1566}
1567
Alexandre Rames5319def2014-10-23 10:03:10 +01001568void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001569 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001570}
1571
1572void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001573 HandleBinaryOp(instruction);
1574}
1575
1576void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1577 HandleBinaryOp(instruction);
1578}
1579
1580void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1581 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001582}
1583
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001584void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1585 LocationSummary* locations =
1586 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1587 locations->SetInAt(0, Location::RequiresRegister());
1588 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001589 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1590 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1591 } else {
1592 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1593 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001594}
1595
1596void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1597 LocationSummary* locations = instruction->GetLocations();
1598 Primitive::Type type = instruction->GetType();
1599 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001600 Location index = locations->InAt(1);
1601 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001602 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001603 MacroAssembler* masm = GetVIXLAssembler();
1604 UseScratchRegisterScope temps(masm);
1605 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001606
1607 if (index.IsConstant()) {
1608 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001609 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001610 } else {
1611 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001612 __ Add(temp, obj, offset);
1613 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001614 }
1615
Alexandre Rames67555f72014-11-18 10:55:16 +00001616 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001617 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001618
1619 if (type == Primitive::kPrimNot) {
1620 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1621 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001622}
1623
Alexandre Rames5319def2014-10-23 10:03:10 +01001624void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1625 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1626 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001627 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001628}
1629
1630void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001631 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001632 __ Ldr(OutputRegister(instruction),
1633 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001634 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001635}
1636
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001637void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001638 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1639 instruction,
1640 instruction->NeedsTypeCheck() ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
1641 locations->SetInAt(0, Location::RequiresRegister());
1642 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1643 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1644 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001645 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001646 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001647 }
1648}
1649
1650void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1651 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001652 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001653 bool may_need_runtime_call = locations->CanCall();
1654 bool needs_write_barrier =
1655 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01001656
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001657 Register array = InputRegisterAt(instruction, 0);
1658 CPURegister value = InputCPURegisterAt(instruction, 2);
1659 CPURegister source = value;
1660 Location index = locations->InAt(1);
1661 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
1662 MemOperand destination = HeapOperand(array);
1663 MacroAssembler* masm = GetVIXLAssembler();
1664 BlockPoolsScope block_pools(masm);
1665
1666 if (!needs_write_barrier) {
1667 DCHECK(!may_need_runtime_call);
1668 if (index.IsConstant()) {
1669 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1670 destination = HeapOperand(array, offset);
1671 } else {
1672 UseScratchRegisterScope temps(masm);
1673 Register temp = temps.AcquireSameSizeAs(array);
1674 __ Add(temp, array, offset);
1675 destination = HeapOperand(temp,
1676 XRegisterFrom(index),
1677 LSL,
1678 Primitive::ComponentSizeShift(value_type));
1679 }
1680 codegen_->Store(value_type, value, destination);
1681 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001682 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001683 DCHECK(needs_write_barrier);
1684 vixl::Label done;
1685 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01001686 {
1687 // We use a block to end the scratch scope before the write barrier, thus
1688 // freeing the temporary registers so they can be used in `MarkGCCard`.
1689 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001690 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01001691 if (index.IsConstant()) {
1692 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001693 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01001694 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01001695 destination = HeapOperand(temp,
1696 XRegisterFrom(index),
1697 LSL,
1698 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001699 }
1700
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001701 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1702 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1703 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1704
1705 if (may_need_runtime_call) {
1706 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
1707 codegen_->AddSlowPath(slow_path);
1708 if (instruction->GetValueCanBeNull()) {
1709 vixl::Label non_zero;
1710 __ Cbnz(Register(value), &non_zero);
1711 if (!index.IsConstant()) {
1712 __ Add(temp, array, offset);
1713 }
1714 __ Str(wzr, destination);
1715 codegen_->MaybeRecordImplicitNullCheck(instruction);
1716 __ B(&done);
1717 __ Bind(&non_zero);
1718 }
1719
1720 Register temp2 = temps.AcquireSameSizeAs(array);
1721 __ Ldr(temp, HeapOperand(array, class_offset));
1722 codegen_->MaybeRecordImplicitNullCheck(instruction);
1723 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1724 __ Ldr(temp, HeapOperand(temp, component_offset));
1725 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
1726 // No need to poison/unpoison, we're comparing two poisoned references.
1727 __ Cmp(temp, temp2);
1728 if (instruction->StaticTypeOfArrayIsObjectArray()) {
1729 vixl::Label do_put;
1730 __ B(eq, &do_put);
1731 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1732 __ Ldr(temp, HeapOperand(temp, super_offset));
1733 // No need to unpoison, we're comparing against null.
1734 __ Cbnz(temp, slow_path->GetEntryLabel());
1735 __ Bind(&do_put);
1736 } else {
1737 __ B(ne, slow_path->GetEntryLabel());
1738 }
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001739 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001740 }
1741
1742 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001743 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001744 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001745 __ Mov(temp2, value.W());
1746 GetAssembler()->PoisonHeapReference(temp2);
1747 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001748 }
1749
1750 if (!index.IsConstant()) {
1751 __ Add(temp, array, offset);
1752 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01001753 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001754
1755 if (!may_need_runtime_call) {
1756 codegen_->MaybeRecordImplicitNullCheck(instruction);
1757 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001758 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001759
1760 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
1761
1762 if (done.IsLinked()) {
1763 __ Bind(&done);
1764 }
1765
1766 if (slow_path != nullptr) {
1767 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01001768 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001769 }
1770}
1771
Alexandre Rames67555f72014-11-18 10:55:16 +00001772void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001773 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1774 ? LocationSummary::kCallOnSlowPath
1775 : LocationSummary::kNoCall;
1776 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00001777 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001778 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001779 if (instruction->HasUses()) {
1780 locations->SetOut(Location::SameAsFirstInput());
1781 }
1782}
1783
1784void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001785 BoundsCheckSlowPathARM64* slow_path =
1786 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00001787 codegen_->AddSlowPath(slow_path);
1788
1789 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1790 __ B(slow_path->GetEntryLabel(), hs);
1791}
1792
Alexandre Rames67555f72014-11-18 10:55:16 +00001793void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1794 LocationSummary* locations =
1795 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1796 locations->SetInAt(0, Location::RequiresRegister());
1797 if (check->HasUses()) {
1798 locations->SetOut(Location::SameAsFirstInput());
1799 }
1800}
1801
1802void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1803 // We assume the class is not null.
1804 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1805 check->GetLoadClass(), check, check->GetDexPc(), true);
1806 codegen_->AddSlowPath(slow_path);
1807 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1808}
1809
Roland Levillain7f63c522015-07-13 15:54:55 +00001810static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1811 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1812 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1813}
1814
Serban Constantinescu02164b32014-11-13 14:05:07 +00001815void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001816 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001817 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1818 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001819 switch (in_type) {
1820 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001821 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001822 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001823 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1824 break;
1825 }
1826 case Primitive::kPrimFloat:
1827 case Primitive::kPrimDouble: {
1828 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001829 locations->SetInAt(1,
1830 IsFloatingPointZeroConstant(compare->InputAt(1))
1831 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1832 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001833 locations->SetOut(Location::RequiresRegister());
1834 break;
1835 }
1836 default:
1837 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1838 }
1839}
1840
1841void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1842 Primitive::Type in_type = compare->InputAt(0)->GetType();
1843
1844 // 0 if: left == right
1845 // 1 if: left > right
1846 // -1 if: left < right
1847 switch (in_type) {
1848 case Primitive::kPrimLong: {
1849 Register result = OutputRegister(compare);
1850 Register left = InputRegisterAt(compare, 0);
1851 Operand right = InputOperandAt(compare, 1);
1852
1853 __ Cmp(left, right);
1854 __ Cset(result, ne);
1855 __ Cneg(result, result, lt);
1856 break;
1857 }
1858 case Primitive::kPrimFloat:
1859 case Primitive::kPrimDouble: {
1860 Register result = OutputRegister(compare);
1861 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001862 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001863 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1864 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001865 __ Fcmp(left, 0.0);
1866 } else {
1867 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1868 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001869 if (compare->IsGtBias()) {
1870 __ Cset(result, ne);
1871 } else {
1872 __ Csetm(result, ne);
1873 }
1874 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001875 break;
1876 }
1877 default:
1878 LOG(FATAL) << "Unimplemented compare type " << in_type;
1879 }
1880}
1881
1882void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1883 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001884
1885 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1886 locations->SetInAt(0, Location::RequiresFpuRegister());
1887 locations->SetInAt(1,
1888 IsFloatingPointZeroConstant(instruction->InputAt(1))
1889 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1890 : Location::RequiresFpuRegister());
1891 } else {
1892 // Integer cases.
1893 locations->SetInAt(0, Location::RequiresRegister());
1894 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1895 }
1896
Alexandre Rames5319def2014-10-23 10:03:10 +01001897 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001898 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001899 }
1900}
1901
1902void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1903 if (!instruction->NeedsMaterialization()) {
1904 return;
1905 }
1906
1907 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001908 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001909 IfCondition if_cond = instruction->GetCondition();
1910 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001911
Roland Levillain7f63c522015-07-13 15:54:55 +00001912 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1913 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1914 if (locations->InAt(1).IsConstant()) {
1915 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1916 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1917 __ Fcmp(lhs, 0.0);
1918 } else {
1919 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1920 }
1921 __ Cset(res, arm64_cond);
1922 if (instruction->IsFPConditionTrueIfNaN()) {
1923 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1924 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1925 } else if (instruction->IsFPConditionFalseIfNaN()) {
1926 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1927 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1928 }
1929 } else {
1930 // Integer cases.
1931 Register lhs = InputRegisterAt(instruction, 0);
1932 Operand rhs = InputOperandAt(instruction, 1);
1933 __ Cmp(lhs, rhs);
1934 __ Cset(res, arm64_cond);
1935 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001936}
1937
1938#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1939 M(Equal) \
1940 M(NotEqual) \
1941 M(LessThan) \
1942 M(LessThanOrEqual) \
1943 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07001944 M(GreaterThanOrEqual) \
1945 M(Below) \
1946 M(BelowOrEqual) \
1947 M(Above) \
1948 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01001949#define DEFINE_CONDITION_VISITORS(Name) \
1950void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1951void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1952FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001953#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001954#undef FOR_EACH_CONDITION_INSTRUCTION
1955
Zheng Xuc6667102015-05-15 16:08:45 +08001956void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1957 DCHECK(instruction->IsDiv() || instruction->IsRem());
1958
1959 LocationSummary* locations = instruction->GetLocations();
1960 Location second = locations->InAt(1);
1961 DCHECK(second.IsConstant());
1962
1963 Register out = OutputRegister(instruction);
1964 Register dividend = InputRegisterAt(instruction, 0);
1965 int64_t imm = Int64FromConstant(second.GetConstant());
1966 DCHECK(imm == 1 || imm == -1);
1967
1968 if (instruction->IsRem()) {
1969 __ Mov(out, 0);
1970 } else {
1971 if (imm == 1) {
1972 __ Mov(out, dividend);
1973 } else {
1974 __ Neg(out, dividend);
1975 }
1976 }
1977}
1978
1979void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1980 DCHECK(instruction->IsDiv() || instruction->IsRem());
1981
1982 LocationSummary* locations = instruction->GetLocations();
1983 Location second = locations->InAt(1);
1984 DCHECK(second.IsConstant());
1985
1986 Register out = OutputRegister(instruction);
1987 Register dividend = InputRegisterAt(instruction, 0);
1988 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001989 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001990 DCHECK(IsPowerOfTwo(abs_imm));
1991 int ctz_imm = CTZ(abs_imm);
1992
1993 UseScratchRegisterScope temps(GetVIXLAssembler());
1994 Register temp = temps.AcquireSameSizeAs(out);
1995
1996 if (instruction->IsDiv()) {
1997 __ Add(temp, dividend, abs_imm - 1);
1998 __ Cmp(dividend, 0);
1999 __ Csel(out, temp, dividend, lt);
2000 if (imm > 0) {
2001 __ Asr(out, out, ctz_imm);
2002 } else {
2003 __ Neg(out, Operand(out, ASR, ctz_imm));
2004 }
2005 } else {
2006 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2007 __ Asr(temp, dividend, bits - 1);
2008 __ Lsr(temp, temp, bits - ctz_imm);
2009 __ Add(out, dividend, temp);
2010 __ And(out, out, abs_imm - 1);
2011 __ Sub(out, out, temp);
2012 }
2013}
2014
2015void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2016 DCHECK(instruction->IsDiv() || instruction->IsRem());
2017
2018 LocationSummary* locations = instruction->GetLocations();
2019 Location second = locations->InAt(1);
2020 DCHECK(second.IsConstant());
2021
2022 Register out = OutputRegister(instruction);
2023 Register dividend = InputRegisterAt(instruction, 0);
2024 int64_t imm = Int64FromConstant(second.GetConstant());
2025
2026 Primitive::Type type = instruction->GetResultType();
2027 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2028
2029 int64_t magic;
2030 int shift;
2031 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2032
2033 UseScratchRegisterScope temps(GetVIXLAssembler());
2034 Register temp = temps.AcquireSameSizeAs(out);
2035
2036 // temp = get_high(dividend * magic)
2037 __ Mov(temp, magic);
2038 if (type == Primitive::kPrimLong) {
2039 __ Smulh(temp, dividend, temp);
2040 } else {
2041 __ Smull(temp.X(), dividend, temp);
2042 __ Lsr(temp.X(), temp.X(), 32);
2043 }
2044
2045 if (imm > 0 && magic < 0) {
2046 __ Add(temp, temp, dividend);
2047 } else if (imm < 0 && magic > 0) {
2048 __ Sub(temp, temp, dividend);
2049 }
2050
2051 if (shift != 0) {
2052 __ Asr(temp, temp, shift);
2053 }
2054
2055 if (instruction->IsDiv()) {
2056 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2057 } else {
2058 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2059 // TODO: Strength reduction for msub.
2060 Register temp_imm = temps.AcquireSameSizeAs(out);
2061 __ Mov(temp_imm, imm);
2062 __ Msub(out, temp, temp_imm, dividend);
2063 }
2064}
2065
2066void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2067 DCHECK(instruction->IsDiv() || instruction->IsRem());
2068 Primitive::Type type = instruction->GetResultType();
2069 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2070
2071 LocationSummary* locations = instruction->GetLocations();
2072 Register out = OutputRegister(instruction);
2073 Location second = locations->InAt(1);
2074
2075 if (second.IsConstant()) {
2076 int64_t imm = Int64FromConstant(second.GetConstant());
2077
2078 if (imm == 0) {
2079 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2080 } else if (imm == 1 || imm == -1) {
2081 DivRemOneOrMinusOne(instruction);
2082 } else if (IsPowerOfTwo(std::abs(imm))) {
2083 DivRemByPowerOfTwo(instruction);
2084 } else {
2085 DCHECK(imm <= -2 || imm >= 2);
2086 GenerateDivRemWithAnyConstant(instruction);
2087 }
2088 } else {
2089 Register dividend = InputRegisterAt(instruction, 0);
2090 Register divisor = InputRegisterAt(instruction, 1);
2091 if (instruction->IsDiv()) {
2092 __ Sdiv(out, dividend, divisor);
2093 } else {
2094 UseScratchRegisterScope temps(GetVIXLAssembler());
2095 Register temp = temps.AcquireSameSizeAs(out);
2096 __ Sdiv(temp, dividend, divisor);
2097 __ Msub(out, temp, divisor, dividend);
2098 }
2099 }
2100}
2101
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002102void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2103 LocationSummary* locations =
2104 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2105 switch (div->GetResultType()) {
2106 case Primitive::kPrimInt:
2107 case Primitive::kPrimLong:
2108 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002109 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002110 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2111 break;
2112
2113 case Primitive::kPrimFloat:
2114 case Primitive::kPrimDouble:
2115 locations->SetInAt(0, Location::RequiresFpuRegister());
2116 locations->SetInAt(1, Location::RequiresFpuRegister());
2117 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2118 break;
2119
2120 default:
2121 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2122 }
2123}
2124
2125void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2126 Primitive::Type type = div->GetResultType();
2127 switch (type) {
2128 case Primitive::kPrimInt:
2129 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002130 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002131 break;
2132
2133 case Primitive::kPrimFloat:
2134 case Primitive::kPrimDouble:
2135 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2136 break;
2137
2138 default:
2139 LOG(FATAL) << "Unexpected div type " << type;
2140 }
2141}
2142
Alexandre Rames67555f72014-11-18 10:55:16 +00002143void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002144 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2145 ? LocationSummary::kCallOnSlowPath
2146 : LocationSummary::kNoCall;
2147 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002148 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2149 if (instruction->HasUses()) {
2150 locations->SetOut(Location::SameAsFirstInput());
2151 }
2152}
2153
2154void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2155 SlowPathCodeARM64* slow_path =
2156 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2157 codegen_->AddSlowPath(slow_path);
2158 Location value = instruction->GetLocations()->InAt(0);
2159
Alexandre Rames3e69f162014-12-10 10:36:50 +00002160 Primitive::Type type = instruction->GetType();
2161
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002162 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2163 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002164 return;
2165 }
2166
Alexandre Rames67555f72014-11-18 10:55:16 +00002167 if (value.IsConstant()) {
2168 int64_t divisor = Int64ConstantFrom(value);
2169 if (divisor == 0) {
2170 __ B(slow_path->GetEntryLabel());
2171 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002172 // A division by a non-null constant is valid. We don't need to perform
2173 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002174 }
2175 } else {
2176 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2177 }
2178}
2179
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002180void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2181 LocationSummary* locations =
2182 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2183 locations->SetOut(Location::ConstantLocation(constant));
2184}
2185
2186void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2187 UNUSED(constant);
2188 // Will be generated at use site.
2189}
2190
Alexandre Rames5319def2014-10-23 10:03:10 +01002191void LocationsBuilderARM64::VisitExit(HExit* exit) {
2192 exit->SetLocations(nullptr);
2193}
2194
2195void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002196 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002197}
2198
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002199void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2200 LocationSummary* locations =
2201 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2202 locations->SetOut(Location::ConstantLocation(constant));
2203}
2204
2205void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2206 UNUSED(constant);
2207 // Will be generated at use site.
2208}
2209
David Brazdilfc6a86a2015-06-26 10:33:45 +00002210void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002211 DCHECK(!successor->IsExitBlock());
2212 HBasicBlock* block = got->GetBlock();
2213 HInstruction* previous = got->GetPrevious();
2214 HLoopInformation* info = block->GetLoopInformation();
2215
David Brazdil46e2a392015-03-16 17:31:52 +00002216 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002217 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2218 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2219 return;
2220 }
2221 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2222 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2223 }
2224 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002225 __ B(codegen_->GetLabelOf(successor));
2226 }
2227}
2228
David Brazdilfc6a86a2015-06-26 10:33:45 +00002229void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2230 got->SetLocations(nullptr);
2231}
2232
2233void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2234 HandleGoto(got, got->GetSuccessor());
2235}
2236
2237void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2238 try_boundary->SetLocations(nullptr);
2239}
2240
2241void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2242 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2243 if (!successor->IsExitBlock()) {
2244 HandleGoto(try_boundary, successor);
2245 }
2246}
2247
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002248void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2249 vixl::Label* true_target,
2250 vixl::Label* false_target,
2251 vixl::Label* always_true_target) {
2252 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002253 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002254
Serban Constantinescu02164b32014-11-13 14:05:07 +00002255 if (cond->IsIntConstant()) {
2256 int32_t cond_value = cond->AsIntConstant()->GetValue();
2257 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002258 if (always_true_target != nullptr) {
2259 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002260 }
2261 return;
2262 } else {
2263 DCHECK_EQ(cond_value, 0);
2264 }
2265 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002266 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002267 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002268 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002269 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002270 } else {
2271 // The condition instruction has not been materialized, use its inputs as
2272 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002273 Primitive::Type type =
2274 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2275
2276 if (Primitive::IsFloatingPointType(type)) {
2277 // FP compares don't like null false_targets.
2278 if (false_target == nullptr) {
2279 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002280 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002281 FPRegister lhs = InputFPRegisterAt(condition, 0);
2282 if (condition->GetLocations()->InAt(1).IsConstant()) {
2283 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2284 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2285 __ Fcmp(lhs, 0.0);
2286 } else {
2287 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2288 }
2289 if (condition->IsFPConditionTrueIfNaN()) {
2290 __ B(vs, true_target); // VS for unordered.
2291 } else if (condition->IsFPConditionFalseIfNaN()) {
2292 __ B(vs, false_target); // VS for unordered.
2293 }
2294 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002295 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002296 // Integer cases.
2297 Register lhs = InputRegisterAt(condition, 0);
2298 Operand rhs = InputOperandAt(condition, 1);
2299 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2300 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2301 switch (arm64_cond) {
2302 case eq:
2303 __ Cbz(lhs, true_target);
2304 break;
2305 case ne:
2306 __ Cbnz(lhs, true_target);
2307 break;
2308 case lt:
2309 // Test the sign bit and branch accordingly.
2310 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2311 break;
2312 case ge:
2313 // Test the sign bit and branch accordingly.
2314 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2315 break;
2316 default:
2317 // Without the `static_cast` the compiler throws an error for
2318 // `-Werror=sign-promo`.
2319 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2320 }
2321 } else {
2322 __ Cmp(lhs, rhs);
2323 __ B(arm64_cond, true_target);
2324 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002325 }
2326 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002327 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002328 __ B(false_target);
2329 }
2330}
2331
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002332void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2333 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2334 HInstruction* cond = if_instr->InputAt(0);
2335 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2336 locations->SetInAt(0, Location::RequiresRegister());
2337 }
2338}
2339
2340void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2341 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2342 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2343 vixl::Label* always_true_target = true_target;
2344 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2345 if_instr->IfTrueSuccessor())) {
2346 always_true_target = nullptr;
2347 }
2348 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2349 if_instr->IfFalseSuccessor())) {
2350 false_target = nullptr;
2351 }
2352 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2353}
2354
2355void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2356 LocationSummary* locations = new (GetGraph()->GetArena())
2357 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2358 HInstruction* cond = deoptimize->InputAt(0);
2359 DCHECK(cond->IsCondition());
2360 if (cond->AsCondition()->NeedsMaterialization()) {
2361 locations->SetInAt(0, Location::RequiresRegister());
2362 }
2363}
2364
2365void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2366 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2367 DeoptimizationSlowPathARM64(deoptimize);
2368 codegen_->AddSlowPath(slow_path);
2369 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2370 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2371}
2372
Alexandre Rames5319def2014-10-23 10:03:10 +01002373void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002374 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002375}
2376
2377void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002378 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002379}
2380
2381void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002382 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002383}
2384
2385void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002386 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002387}
2388
Alexandre Rames67555f72014-11-18 10:55:16 +00002389void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002390 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2391 switch (instruction->GetTypeCheckKind()) {
2392 case TypeCheckKind::kExactCheck:
2393 case TypeCheckKind::kAbstractClassCheck:
2394 case TypeCheckKind::kClassHierarchyCheck:
2395 case TypeCheckKind::kArrayObjectCheck:
2396 call_kind = LocationSummary::kNoCall;
2397 break;
Calin Juravle98893e12015-10-02 21:05:03 +01002398 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002399 case TypeCheckKind::kInterfaceCheck:
2400 call_kind = LocationSummary::kCall;
2401 break;
2402 case TypeCheckKind::kArrayCheck:
2403 call_kind = LocationSummary::kCallOnSlowPath;
2404 break;
2405 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002407 if (call_kind != LocationSummary::kCall) {
2408 locations->SetInAt(0, Location::RequiresRegister());
2409 locations->SetInAt(1, Location::RequiresRegister());
2410 // The out register is used as a temporary, so it overlaps with the inputs.
2411 // Note that TypeCheckSlowPathARM64 uses this register too.
2412 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2413 } else {
2414 InvokeRuntimeCallingConvention calling_convention;
2415 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2416 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2417 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
2418 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002419}
2420
2421void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2422 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002423 Register obj = InputRegisterAt(instruction, 0);
2424 Register cls = InputRegisterAt(instruction, 1);
Alexandre Rames67555f72014-11-18 10:55:16 +00002425 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002426 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2427 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2428 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2429 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002430
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002431 vixl::Label done, zero;
2432 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002433
2434 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002435 // Avoid null check if we know `obj` is not null.
2436 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002437 __ Cbz(obj, &zero);
2438 }
2439
Calin Juravle98893e12015-10-02 21:05:03 +01002440 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002441 // This is safe, as the register is caller-save, and the object must be in another
2442 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01002443 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
2444 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002445 ? obj
2446 : out;
2447 __ Ldr(target, HeapOperand(obj.W(), class_offset));
2448 GetAssembler()->MaybeUnpoisonHeapReference(target);
2449
2450 switch (instruction->GetTypeCheckKind()) {
2451 case TypeCheckKind::kExactCheck: {
2452 __ Cmp(out, cls);
2453 __ Cset(out, eq);
2454 if (zero.IsLinked()) {
2455 __ B(&done);
2456 }
2457 break;
2458 }
2459 case TypeCheckKind::kAbstractClassCheck: {
2460 // If the class is abstract, we eagerly fetch the super class of the
2461 // object to avoid doing a comparison we know will fail.
2462 vixl::Label loop, success;
2463 __ Bind(&loop);
2464 __ Ldr(out, HeapOperand(out, super_offset));
2465 GetAssembler()->MaybeUnpoisonHeapReference(out);
2466 // If `out` is null, we use it for the result, and jump to `done`.
2467 __ Cbz(out, &done);
2468 __ Cmp(out, cls);
2469 __ B(ne, &loop);
2470 __ Mov(out, 1);
2471 if (zero.IsLinked()) {
2472 __ B(&done);
2473 }
2474 break;
2475 }
2476 case TypeCheckKind::kClassHierarchyCheck: {
2477 // Walk over the class hierarchy to find a match.
2478 vixl::Label loop, success;
2479 __ Bind(&loop);
2480 __ Cmp(out, cls);
2481 __ B(eq, &success);
2482 __ Ldr(out, HeapOperand(out, super_offset));
2483 GetAssembler()->MaybeUnpoisonHeapReference(out);
2484 __ Cbnz(out, &loop);
2485 // If `out` is null, we use it for the result, and jump to `done`.
2486 __ B(&done);
2487 __ Bind(&success);
2488 __ Mov(out, 1);
2489 if (zero.IsLinked()) {
2490 __ B(&done);
2491 }
2492 break;
2493 }
2494 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002495 // Do an exact check.
2496 vixl::Label exact_check;
2497 __ Cmp(out, cls);
2498 __ B(eq, &exact_check);
2499 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002500 __ Ldr(out, HeapOperand(out, component_offset));
2501 GetAssembler()->MaybeUnpoisonHeapReference(out);
2502 // If `out` is null, we use it for the result, and jump to `done`.
2503 __ Cbz(out, &done);
2504 __ Ldrh(out, HeapOperand(out, primitive_offset));
2505 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2506 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002507 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002508 __ Mov(out, 1);
2509 __ B(&done);
2510 break;
2511 }
2512 case TypeCheckKind::kArrayCheck: {
2513 __ Cmp(out, cls);
2514 DCHECK(locations->OnlyCallsOnSlowPath());
2515 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2516 instruction, /* is_fatal */ false);
2517 codegen_->AddSlowPath(slow_path);
2518 __ B(ne, slow_path->GetEntryLabel());
2519 __ Mov(out, 1);
2520 if (zero.IsLinked()) {
2521 __ B(&done);
2522 }
2523 break;
2524 }
Calin Juravle98893e12015-10-02 21:05:03 +01002525 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002526 case TypeCheckKind::kInterfaceCheck:
2527 default: {
2528 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
2529 instruction,
2530 instruction->GetDexPc(),
2531 nullptr);
2532 if (zero.IsLinked()) {
2533 __ B(&done);
2534 }
2535 break;
2536 }
2537 }
2538
2539 if (zero.IsLinked()) {
2540 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002541 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002542 }
2543
2544 if (done.IsLinked()) {
2545 __ Bind(&done);
2546 }
2547
2548 if (slow_path != nullptr) {
2549 __ Bind(slow_path->GetExitLabel());
2550 }
2551}
2552
2553void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
2554 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2555 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2556
2557 switch (instruction->GetTypeCheckKind()) {
2558 case TypeCheckKind::kExactCheck:
2559 case TypeCheckKind::kAbstractClassCheck:
2560 case TypeCheckKind::kClassHierarchyCheck:
2561 case TypeCheckKind::kArrayObjectCheck:
2562 call_kind = throws_into_catch
2563 ? LocationSummary::kCallOnSlowPath
2564 : LocationSummary::kNoCall;
2565 break;
Calin Juravle98893e12015-10-02 21:05:03 +01002566 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002567 case TypeCheckKind::kInterfaceCheck:
2568 call_kind = LocationSummary::kCall;
2569 break;
2570 case TypeCheckKind::kArrayCheck:
2571 call_kind = LocationSummary::kCallOnSlowPath;
2572 break;
2573 }
2574
2575 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2576 instruction, call_kind);
2577 if (call_kind != LocationSummary::kCall) {
2578 locations->SetInAt(0, Location::RequiresRegister());
2579 locations->SetInAt(1, Location::RequiresRegister());
2580 // Note that TypeCheckSlowPathARM64 uses this register too.
2581 locations->AddTemp(Location::RequiresRegister());
2582 } else {
2583 InvokeRuntimeCallingConvention calling_convention;
2584 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2585 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2586 }
2587}
2588
2589void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
2590 LocationSummary* locations = instruction->GetLocations();
2591 Register obj = InputRegisterAt(instruction, 0);
2592 Register cls = InputRegisterAt(instruction, 1);
2593 Register temp;
2594 if (!locations->WillCall()) {
2595 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
2596 }
2597
2598 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2599 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2600 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2601 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2602 SlowPathCodeARM64* slow_path = nullptr;
2603
2604 if (!locations->WillCall()) {
2605 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2606 instruction, !locations->CanCall());
2607 codegen_->AddSlowPath(slow_path);
2608 }
2609
2610 vixl::Label done;
2611 // Avoid null check if we know obj is not null.
2612 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002613 __ Cbz(obj, &done);
2614 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002615
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002616 if (locations->WillCall()) {
2617 __ Ldr(obj, HeapOperand(obj, class_offset));
2618 GetAssembler()->MaybeUnpoisonHeapReference(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00002619 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002620 __ Ldr(temp, HeapOperand(obj, class_offset));
2621 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002622 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002623
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002624 switch (instruction->GetTypeCheckKind()) {
2625 case TypeCheckKind::kExactCheck:
2626 case TypeCheckKind::kArrayCheck: {
2627 __ Cmp(temp, cls);
2628 // Jump to slow path for throwing the exception or doing a
2629 // more involved array check.
2630 __ B(ne, slow_path->GetEntryLabel());
2631 break;
2632 }
2633 case TypeCheckKind::kAbstractClassCheck: {
2634 // If the class is abstract, we eagerly fetch the super class of the
2635 // object to avoid doing a comparison we know will fail.
2636 vixl::Label loop;
2637 __ Bind(&loop);
2638 __ Ldr(temp, HeapOperand(temp, super_offset));
2639 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2640 // Jump to the slow path to throw the exception.
2641 __ Cbz(temp, slow_path->GetEntryLabel());
2642 __ Cmp(temp, cls);
2643 __ B(ne, &loop);
2644 break;
2645 }
2646 case TypeCheckKind::kClassHierarchyCheck: {
2647 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002648 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002649 __ Bind(&loop);
2650 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002651 __ B(eq, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002652 __ Ldr(temp, HeapOperand(temp, super_offset));
2653 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2654 __ Cbnz(temp, &loop);
2655 // Jump to the slow path to throw the exception.
2656 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002657 break;
2658 }
2659 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002660 // Do an exact check.
2661 __ Cmp(temp, cls);
2662 __ B(eq, &done);
2663 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002664 __ Ldr(temp, HeapOperand(temp, component_offset));
2665 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2666 __ Cbz(temp, slow_path->GetEntryLabel());
2667 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
2668 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2669 __ Cbnz(temp, slow_path->GetEntryLabel());
2670 break;
2671 }
Calin Juravle98893e12015-10-02 21:05:03 +01002672 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002673 case TypeCheckKind::kInterfaceCheck:
2674 default:
2675 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
2676 instruction,
2677 instruction->GetDexPc(),
2678 nullptr);
2679 break;
2680 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002681 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002682
2683 if (slow_path != nullptr) {
2684 __ Bind(slow_path->GetExitLabel());
2685 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002686}
2687
Alexandre Rames5319def2014-10-23 10:03:10 +01002688void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2689 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2690 locations->SetOut(Location::ConstantLocation(constant));
2691}
2692
2693void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2694 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002695 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002696}
2697
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002698void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2699 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2700 locations->SetOut(Location::ConstantLocation(constant));
2701}
2702
2703void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2704 // Will be generated at use site.
2705 UNUSED(constant);
2706}
2707
Calin Juravle175dc732015-08-25 15:42:32 +01002708void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2709 // The trampoline uses the same calling convention as dex calling conventions,
2710 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2711 // the method_idx.
2712 HandleInvoke(invoke);
2713}
2714
2715void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2716 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2717}
2718
Alexandre Rames5319def2014-10-23 10:03:10 +01002719void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002720 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002721 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002722}
2723
Alexandre Rames67555f72014-11-18 10:55:16 +00002724void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2725 HandleInvoke(invoke);
2726}
2727
2728void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2729 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002730 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2731 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2732 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002733 Location receiver = invoke->GetLocations()->InAt(0);
2734 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002735 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002736
2737 // The register ip1 is required to be used for the hidden argument in
2738 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002739 MacroAssembler* masm = GetVIXLAssembler();
2740 UseScratchRegisterScope scratch_scope(masm);
2741 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002742 scratch_scope.Exclude(ip1);
2743 __ Mov(ip1, invoke->GetDexMethodIndex());
2744
2745 // temp = object->GetClass();
2746 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002747 __ Ldr(temp.W(), StackOperandFrom(receiver));
2748 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002749 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002750 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002751 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002752 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002753 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002754 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002755 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002756 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002757 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002758 // lr();
2759 __ Blr(lr);
2760 DCHECK(!codegen_->IsLeafMethod());
2761 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2762}
2763
2764void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002765 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2766 if (intrinsic.TryDispatch(invoke)) {
2767 return;
2768 }
2769
Alexandre Rames67555f72014-11-18 10:55:16 +00002770 HandleInvoke(invoke);
2771}
2772
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002773void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002774 // When we do not run baseline, explicit clinit checks triggered by static
2775 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2776 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002777
Andreas Gampe878d58c2015-01-15 23:24:00 -08002778 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2779 if (intrinsic.TryDispatch(invoke)) {
2780 return;
2781 }
2782
Alexandre Rames67555f72014-11-18 10:55:16 +00002783 HandleInvoke(invoke);
2784}
2785
Andreas Gampe878d58c2015-01-15 23:24:00 -08002786static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2787 if (invoke->GetLocations()->Intrinsified()) {
2788 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2789 intrinsic.Dispatch(invoke);
2790 return true;
2791 }
2792 return false;
2793}
2794
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002795void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00002796 // For better instruction scheduling we load the direct code pointer before the method pointer.
2797 bool direct_code_loaded = false;
2798 switch (invoke->GetCodePtrLocation()) {
2799 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2800 // LR = code address from literal pool with link-time patch.
2801 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
2802 direct_code_loaded = true;
2803 break;
2804 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2805 // LR = invoke->GetDirectCodePtr();
2806 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
2807 direct_code_loaded = true;
2808 break;
2809 default:
2810 break;
2811 }
2812
Andreas Gampe878d58c2015-01-15 23:24:00 -08002813 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00002814 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2815 switch (invoke->GetMethodLoadKind()) {
2816 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2817 // temp = thread->string_init_entrypoint
2818 __ Ldr(XRegisterFrom(temp).X(), MemOperand(tr, invoke->GetStringInitOffset()));
2819 break;
2820 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2821 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2822 break;
2823 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2824 // Load method address from literal pool.
2825 __ Ldr(XRegisterFrom(temp).X(), DeduplicateUint64Literal(invoke->GetMethodAddress()));
2826 break;
2827 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2828 // Load method address from literal pool with a link-time patch.
2829 __ Ldr(XRegisterFrom(temp).X(),
2830 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
2831 break;
2832 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
2833 // Add ADRP with its PC-relative DexCache access patch.
2834 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2835 invoke->GetDexCacheArrayOffset());
2836 vixl::Label* pc_insn_label = &pc_rel_dex_cache_patches_.back().label;
2837 {
2838 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
2839 __ adrp(XRegisterFrom(temp).X(), 0);
2840 }
2841 __ Bind(pc_insn_label); // Bind after ADRP.
2842 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2843 // Add LDR with its PC-relative DexCache access patch.
2844 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2845 invoke->GetDexCacheArrayOffset());
2846 __ Ldr(XRegisterFrom(temp).X(), MemOperand(XRegisterFrom(temp).X(), 0));
2847 __ Bind(&pc_rel_dex_cache_patches_.back().label); // Bind after LDR.
2848 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2849 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01002850 }
Vladimir Marko58155012015-08-19 12:49:41 +00002851 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2852 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2853 Register reg = XRegisterFrom(temp);
2854 Register method_reg;
2855 if (current_method.IsRegister()) {
2856 method_reg = XRegisterFrom(current_method);
2857 } else {
2858 DCHECK(invoke->GetLocations()->Intrinsified());
2859 DCHECK(!current_method.IsValid());
2860 method_reg = reg;
2861 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2862 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00002863
Vladimir Marko58155012015-08-19 12:49:41 +00002864 // temp = current_method->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002865 __ Ldr(reg.X(),
2866 MemOperand(method_reg.X(),
2867 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00002868 // temp = temp[index_in_cache];
2869 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2870 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
2871 break;
2872 }
2873 }
2874
2875 switch (invoke->GetCodePtrLocation()) {
2876 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2877 __ Bl(&frame_entry_label_);
2878 break;
2879 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
2880 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
2881 vixl::Label* label = &relative_call_patches_.back().label;
2882 __ Bl(label); // Arbitrarily branch to the instruction after BL, override at link time.
2883 __ Bind(label); // Bind after BL.
2884 break;
2885 }
2886 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2887 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2888 // LR prepared above for better instruction scheduling.
2889 DCHECK(direct_code_loaded);
2890 // lr()
2891 __ Blr(lr);
2892 break;
2893 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2894 // LR = callee_method->entry_point_from_quick_compiled_code_;
2895 __ Ldr(lr, MemOperand(
2896 XRegisterFrom(callee_method).X(),
2897 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
2898 // lr()
2899 __ Blr(lr);
2900 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002901 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002902
Andreas Gampe878d58c2015-01-15 23:24:00 -08002903 DCHECK(!IsLeafMethod());
2904}
2905
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002906void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
2907 LocationSummary* locations = invoke->GetLocations();
2908 Location receiver = locations->InAt(0);
2909 Register temp = XRegisterFrom(temp_in);
2910 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2911 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
2912 Offset class_offset = mirror::Object::ClassOffset();
2913 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
2914
2915 BlockPoolsScope block_pools(GetVIXLAssembler());
2916
2917 DCHECK(receiver.IsRegister());
2918 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
2919 MaybeRecordImplicitNullCheck(invoke);
2920 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
2921 // temp = temp->GetMethodAt(method_offset);
2922 __ Ldr(temp, MemOperand(temp, method_offset));
2923 // lr = temp->GetEntryPoint();
2924 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
2925 // lr();
2926 __ Blr(lr);
2927}
2928
Vladimir Marko58155012015-08-19 12:49:41 +00002929void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
2930 DCHECK(linker_patches->empty());
2931 size_t size =
2932 method_patches_.size() +
2933 call_patches_.size() +
2934 relative_call_patches_.size() +
2935 pc_rel_dex_cache_patches_.size();
2936 linker_patches->reserve(size);
2937 for (const auto& entry : method_patches_) {
2938 const MethodReference& target_method = entry.first;
2939 vixl::Literal<uint64_t>* literal = entry.second;
2940 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
2941 target_method.dex_file,
2942 target_method.dex_method_index));
2943 }
2944 for (const auto& entry : call_patches_) {
2945 const MethodReference& target_method = entry.first;
2946 vixl::Literal<uint64_t>* literal = entry.second;
2947 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
2948 target_method.dex_file,
2949 target_method.dex_method_index));
2950 }
2951 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
2952 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location() - 4u,
2953 info.target_method.dex_file,
2954 info.target_method.dex_method_index));
2955 }
2956 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
2957 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location() - 4u,
2958 &info.target_dex_file,
2959 info.pc_insn_label->location() - 4u,
2960 info.element_offset));
2961 }
2962}
2963
2964vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
2965 // Look up the literal for value.
2966 auto lb = uint64_literals_.lower_bound(value);
2967 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
2968 return lb->second;
2969 }
2970 // We don't have a literal for this value, insert a new one.
2971 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
2972 uint64_literals_.PutBefore(lb, value, literal);
2973 return literal;
2974}
2975
2976vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
2977 MethodReference target_method,
2978 MethodToLiteralMap* map) {
2979 // Look up the literal for target_method.
2980 auto lb = map->lower_bound(target_method);
2981 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
2982 return lb->second;
2983 }
2984 // We don't have a literal for this method yet, insert a new one.
2985 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
2986 map->PutBefore(lb, target_method, literal);
2987 return literal;
2988}
2989
2990vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
2991 MethodReference target_method) {
2992 return DeduplicateMethodLiteral(target_method, &method_patches_);
2993}
2994
2995vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
2996 MethodReference target_method) {
2997 return DeduplicateMethodLiteral(target_method, &call_patches_);
2998}
2999
3000
Andreas Gampe878d58c2015-01-15 23:24:00 -08003001void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003002 // When we do not run baseline, explicit clinit checks triggered by static
3003 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3004 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003005
Andreas Gampe878d58c2015-01-15 23:24:00 -08003006 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3007 return;
3008 }
3009
Alexandre Ramesd921d642015-04-16 15:07:16 +01003010 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003011 LocationSummary* locations = invoke->GetLocations();
3012 codegen_->GenerateStaticOrDirectCall(
3013 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003014 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003015}
3016
3017void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003018 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3019 return;
3020 }
3021
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003022 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003023 DCHECK(!codegen_->IsLeafMethod());
3024 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3025}
3026
Alexandre Rames67555f72014-11-18 10:55:16 +00003027void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003028 InvokeRuntimeCallingConvention calling_convention;
3029 CodeGenerator::CreateLoadClassLocationSummary(
3030 cls,
3031 LocationFrom(calling_convention.GetRegisterAt(0)),
3032 LocationFrom(vixl::x0));
Alexandre Rames67555f72014-11-18 10:55:16 +00003033}
3034
3035void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003036 if (cls->NeedsAccessCheck()) {
3037 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3038 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3039 cls,
3040 cls->GetDexPc(),
3041 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01003042 return;
3043 }
3044
3045 Register out = OutputRegister(cls);
3046 Register current_method = InputRegisterAt(cls, 0);
3047 if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003048 DCHECK(!cls->CanCallRuntime());
3049 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07003050 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003051 } else {
3052 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01003053 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
3054 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
3055 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
3056 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003057
3058 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3059 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3060 codegen_->AddSlowPath(slow_path);
3061 __ Cbz(out, slow_path->GetEntryLabel());
3062 if (cls->MustGenerateClinitCheck()) {
3063 GenerateClassInitializationCheck(slow_path, out);
3064 } else {
3065 __ Bind(slow_path->GetExitLabel());
3066 }
3067 }
3068}
3069
David Brazdilcb1c0552015-08-04 16:22:25 +01003070static MemOperand GetExceptionTlsAddress() {
3071 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3072}
3073
Alexandre Rames67555f72014-11-18 10:55:16 +00003074void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3075 LocationSummary* locations =
3076 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3077 locations->SetOut(Location::RequiresRegister());
3078}
3079
3080void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003081 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3082}
3083
3084void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3085 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3086}
3087
3088void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3089 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003090}
3091
Alexandre Rames5319def2014-10-23 10:03:10 +01003092void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3093 load->SetLocations(nullptr);
3094}
3095
3096void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
3097 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003098 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01003099}
3100
Alexandre Rames67555f72014-11-18 10:55:16 +00003101void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
3102 LocationSummary* locations =
3103 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003104 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003105 locations->SetOut(Location::RequiresRegister());
3106}
3107
3108void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
3109 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3110 codegen_->AddSlowPath(slow_path);
3111
3112 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003113 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003114 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003115 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
3116 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3117 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003118 __ Cbz(out, slow_path->GetEntryLabel());
3119 __ Bind(slow_path->GetExitLabel());
3120}
3121
Alexandre Rames5319def2014-10-23 10:03:10 +01003122void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3123 local->SetLocations(nullptr);
3124}
3125
3126void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3127 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3128}
3129
3130void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3131 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3132 locations->SetOut(Location::ConstantLocation(constant));
3133}
3134
3135void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
3136 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003137 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01003138}
3139
Alexandre Rames67555f72014-11-18 10:55:16 +00003140void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3141 LocationSummary* locations =
3142 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3143 InvokeRuntimeCallingConvention calling_convention;
3144 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3145}
3146
3147void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3148 codegen_->InvokeRuntime(instruction->IsEnter()
3149 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3150 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003151 instruction->GetDexPc(),
3152 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003153 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003154}
3155
Alexandre Rames42d641b2014-10-27 14:00:51 +00003156void LocationsBuilderARM64::VisitMul(HMul* mul) {
3157 LocationSummary* locations =
3158 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3159 switch (mul->GetResultType()) {
3160 case Primitive::kPrimInt:
3161 case Primitive::kPrimLong:
3162 locations->SetInAt(0, Location::RequiresRegister());
3163 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003164 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003165 break;
3166
3167 case Primitive::kPrimFloat:
3168 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003169 locations->SetInAt(0, Location::RequiresFpuRegister());
3170 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003171 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003172 break;
3173
3174 default:
3175 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3176 }
3177}
3178
3179void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3180 switch (mul->GetResultType()) {
3181 case Primitive::kPrimInt:
3182 case Primitive::kPrimLong:
3183 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3184 break;
3185
3186 case Primitive::kPrimFloat:
3187 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003188 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003189 break;
3190
3191 default:
3192 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3193 }
3194}
3195
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003196void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3197 LocationSummary* locations =
3198 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3199 switch (neg->GetResultType()) {
3200 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003201 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003202 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003203 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003204 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003205
3206 case Primitive::kPrimFloat:
3207 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003208 locations->SetInAt(0, Location::RequiresFpuRegister());
3209 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003210 break;
3211
3212 default:
3213 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3214 }
3215}
3216
3217void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3218 switch (neg->GetResultType()) {
3219 case Primitive::kPrimInt:
3220 case Primitive::kPrimLong:
3221 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3222 break;
3223
3224 case Primitive::kPrimFloat:
3225 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003226 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003227 break;
3228
3229 default:
3230 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3231 }
3232}
3233
3234void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
3235 LocationSummary* locations =
3236 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3237 InvokeRuntimeCallingConvention calling_convention;
3238 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003239 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003240 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003241 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003242 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07003243 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003244}
3245
3246void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
3247 LocationSummary* locations = instruction->GetLocations();
3248 InvokeRuntimeCallingConvention calling_convention;
3249 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3250 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003251 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003252 // Note: if heap poisoning is enabled, the entry point takes cares
3253 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003254 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3255 instruction,
3256 instruction->GetDexPc(),
3257 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003258 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003259}
3260
Alexandre Rames5319def2014-10-23 10:03:10 +01003261void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
3262 LocationSummary* locations =
3263 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3264 InvokeRuntimeCallingConvention calling_convention;
3265 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003266 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01003267 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003268 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003269}
3270
3271void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
3272 LocationSummary* locations = instruction->GetLocations();
3273 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3274 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003275 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003276 // Note: if heap poisoning is enabled, the entry point takes cares
3277 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003278 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3279 instruction,
3280 instruction->GetDexPc(),
3281 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003282 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003283}
3284
3285void LocationsBuilderARM64::VisitNot(HNot* instruction) {
3286 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00003287 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003288 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003289}
3290
3291void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003292 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003293 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003294 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01003295 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003296 break;
3297
3298 default:
3299 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3300 }
3301}
3302
David Brazdil66d126e2015-04-03 16:02:44 +01003303void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
3304 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3305 locations->SetInAt(0, Location::RequiresRegister());
3306 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3307}
3308
3309void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01003310 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
3311}
3312
Alexandre Rames5319def2014-10-23 10:03:10 +01003313void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003314 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3315 ? LocationSummary::kCallOnSlowPath
3316 : LocationSummary::kNoCall;
3317 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01003318 locations->SetInAt(0, Location::RequiresRegister());
3319 if (instruction->HasUses()) {
3320 locations->SetOut(Location::SameAsFirstInput());
3321 }
3322}
3323
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003324void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003325 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3326 return;
3327 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003328
Alexandre Ramesd921d642015-04-16 15:07:16 +01003329 BlockPoolsScope block_pools(GetVIXLAssembler());
3330 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003331 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
3332 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3333}
3334
3335void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003336 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
3337 codegen_->AddSlowPath(slow_path);
3338
3339 LocationSummary* locations = instruction->GetLocations();
3340 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003341
3342 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01003343}
3344
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003345void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003346 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003347 GenerateImplicitNullCheck(instruction);
3348 } else {
3349 GenerateExplicitNullCheck(instruction);
3350 }
3351}
3352
Alexandre Rames67555f72014-11-18 10:55:16 +00003353void LocationsBuilderARM64::VisitOr(HOr* instruction) {
3354 HandleBinaryOp(instruction);
3355}
3356
3357void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
3358 HandleBinaryOp(instruction);
3359}
3360
Alexandre Rames3e69f162014-12-10 10:36:50 +00003361void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3362 LOG(FATAL) << "Unreachable";
3363}
3364
3365void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
3366 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3367}
3368
Alexandre Rames5319def2014-10-23 10:03:10 +01003369void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
3370 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3371 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3372 if (location.IsStackSlot()) {
3373 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3374 } else if (location.IsDoubleStackSlot()) {
3375 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3376 }
3377 locations->SetOut(location);
3378}
3379
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003380void InstructionCodeGeneratorARM64::VisitParameterValue(
3381 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003382 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003383}
3384
3385void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
3386 LocationSummary* locations =
3387 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003388 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003389}
3390
3391void InstructionCodeGeneratorARM64::VisitCurrentMethod(
3392 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3393 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01003394}
3395
3396void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
3397 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3398 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3399 locations->SetInAt(i, Location::Any());
3400 }
3401 locations->SetOut(Location::Any());
3402}
3403
3404void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003405 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003406 LOG(FATAL) << "Unreachable";
3407}
3408
Serban Constantinescu02164b32014-11-13 14:05:07 +00003409void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003410 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00003411 LocationSummary::CallKind call_kind =
3412 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003413 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3414
3415 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003416 case Primitive::kPrimInt:
3417 case Primitive::kPrimLong:
3418 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003419 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003420 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3421 break;
3422
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003423 case Primitive::kPrimFloat:
3424 case Primitive::kPrimDouble: {
3425 InvokeRuntimeCallingConvention calling_convention;
3426 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3427 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3428 locations->SetOut(calling_convention.GetReturnLocation(type));
3429
3430 break;
3431 }
3432
Serban Constantinescu02164b32014-11-13 14:05:07 +00003433 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003434 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00003435 }
3436}
3437
3438void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
3439 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003440
Serban Constantinescu02164b32014-11-13 14:05:07 +00003441 switch (type) {
3442 case Primitive::kPrimInt:
3443 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08003444 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003445 break;
3446 }
3447
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003448 case Primitive::kPrimFloat:
3449 case Primitive::kPrimDouble: {
3450 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3451 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003452 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003453 break;
3454 }
3455
Serban Constantinescu02164b32014-11-13 14:05:07 +00003456 default:
3457 LOG(FATAL) << "Unexpected rem type " << type;
3458 }
3459}
3460
Calin Juravle27df7582015-04-17 19:12:31 +01003461void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3462 memory_barrier->SetLocations(nullptr);
3463}
3464
3465void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3466 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3467}
3468
Alexandre Rames5319def2014-10-23 10:03:10 +01003469void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
3470 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3471 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003472 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01003473}
3474
3475void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003476 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003477 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003478}
3479
3480void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
3481 instruction->SetLocations(nullptr);
3482}
3483
3484void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003485 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003486 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003487}
3488
Serban Constantinescu02164b32014-11-13 14:05:07 +00003489void LocationsBuilderARM64::VisitShl(HShl* shl) {
3490 HandleShift(shl);
3491}
3492
3493void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
3494 HandleShift(shl);
3495}
3496
3497void LocationsBuilderARM64::VisitShr(HShr* shr) {
3498 HandleShift(shr);
3499}
3500
3501void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
3502 HandleShift(shr);
3503}
3504
Alexandre Rames5319def2014-10-23 10:03:10 +01003505void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
3506 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3507 Primitive::Type field_type = store->InputAt(1)->GetType();
3508 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003509 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01003510 case Primitive::kPrimBoolean:
3511 case Primitive::kPrimByte:
3512 case Primitive::kPrimChar:
3513 case Primitive::kPrimShort:
3514 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003515 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01003516 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3517 break;
3518
3519 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003520 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01003521 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3522 break;
3523
3524 default:
3525 LOG(FATAL) << "Unimplemented local type " << field_type;
3526 }
3527}
3528
3529void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003530 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01003531}
3532
3533void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003534 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003535}
3536
3537void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003538 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003539}
3540
Alexandre Rames67555f72014-11-18 10:55:16 +00003541void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003542 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003543}
3544
3545void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003546 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00003547}
3548
3549void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003550 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003551}
3552
Alexandre Rames67555f72014-11-18 10:55:16 +00003553void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003554 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003555}
3556
Calin Juravlee460d1d2015-09-29 04:52:17 +01003557void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
3558 HUnresolvedInstanceFieldGet* instruction) {
3559 FieldAccessCallingConventionARM64 calling_convention;
3560 codegen_->CreateUnresolvedFieldLocationSummary(
3561 instruction, instruction->GetFieldType(), calling_convention);
3562}
3563
3564void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
3565 HUnresolvedInstanceFieldGet* instruction) {
3566 FieldAccessCallingConventionARM64 calling_convention;
3567 codegen_->GenerateUnresolvedFieldAccess(instruction,
3568 instruction->GetFieldType(),
3569 instruction->GetFieldIndex(),
3570 instruction->GetDexPc(),
3571 calling_convention);
3572}
3573
3574void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
3575 HUnresolvedInstanceFieldSet* instruction) {
3576 FieldAccessCallingConventionARM64 calling_convention;
3577 codegen_->CreateUnresolvedFieldLocationSummary(
3578 instruction, instruction->GetFieldType(), calling_convention);
3579}
3580
3581void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
3582 HUnresolvedInstanceFieldSet* instruction) {
3583 FieldAccessCallingConventionARM64 calling_convention;
3584 codegen_->GenerateUnresolvedFieldAccess(instruction,
3585 instruction->GetFieldType(),
3586 instruction->GetFieldIndex(),
3587 instruction->GetDexPc(),
3588 calling_convention);
3589}
3590
3591void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
3592 HUnresolvedStaticFieldGet* instruction) {
3593 FieldAccessCallingConventionARM64 calling_convention;
3594 codegen_->CreateUnresolvedFieldLocationSummary(
3595 instruction, instruction->GetFieldType(), calling_convention);
3596}
3597
3598void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
3599 HUnresolvedStaticFieldGet* instruction) {
3600 FieldAccessCallingConventionARM64 calling_convention;
3601 codegen_->GenerateUnresolvedFieldAccess(instruction,
3602 instruction->GetFieldType(),
3603 instruction->GetFieldIndex(),
3604 instruction->GetDexPc(),
3605 calling_convention);
3606}
3607
3608void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
3609 HUnresolvedStaticFieldSet* instruction) {
3610 FieldAccessCallingConventionARM64 calling_convention;
3611 codegen_->CreateUnresolvedFieldLocationSummary(
3612 instruction, instruction->GetFieldType(), calling_convention);
3613}
3614
3615void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
3616 HUnresolvedStaticFieldSet* instruction) {
3617 FieldAccessCallingConventionARM64 calling_convention;
3618 codegen_->GenerateUnresolvedFieldAccess(instruction,
3619 instruction->GetFieldType(),
3620 instruction->GetFieldIndex(),
3621 instruction->GetDexPc(),
3622 calling_convention);
3623}
3624
Alexandre Rames5319def2014-10-23 10:03:10 +01003625void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3626 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3627}
3628
3629void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003630 HBasicBlock* block = instruction->GetBlock();
3631 if (block->GetLoopInformation() != nullptr) {
3632 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3633 // The back edge will generate the suspend check.
3634 return;
3635 }
3636 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3637 // The goto will generate the suspend check.
3638 return;
3639 }
3640 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003641}
3642
3643void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3644 temp->SetLocations(nullptr);
3645}
3646
3647void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3648 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003649 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003650}
3651
Alexandre Rames67555f72014-11-18 10:55:16 +00003652void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3653 LocationSummary* locations =
3654 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3655 InvokeRuntimeCallingConvention calling_convention;
3656 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3657}
3658
3659void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3660 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003661 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003662 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003663}
3664
3665void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3666 LocationSummary* locations =
3667 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3668 Primitive::Type input_type = conversion->GetInputType();
3669 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003670 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003671 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3672 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3673 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3674 }
3675
Alexandre Rames542361f2015-01-29 16:57:31 +00003676 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003677 locations->SetInAt(0, Location::RequiresFpuRegister());
3678 } else {
3679 locations->SetInAt(0, Location::RequiresRegister());
3680 }
3681
Alexandre Rames542361f2015-01-29 16:57:31 +00003682 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003683 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3684 } else {
3685 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3686 }
3687}
3688
3689void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3690 Primitive::Type result_type = conversion->GetResultType();
3691 Primitive::Type input_type = conversion->GetInputType();
3692
3693 DCHECK_NE(input_type, result_type);
3694
Alexandre Rames542361f2015-01-29 16:57:31 +00003695 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003696 int result_size = Primitive::ComponentSize(result_type);
3697 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003698 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003699 Register output = OutputRegister(conversion);
3700 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003701 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3702 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01003703 } else if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
3704 // 'int' values are used directly as W registers, discarding the top
3705 // bits, so we don't need to sign-extend and can just perform a move.
3706 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
3707 // top 32 bits of the target register. We theoretically could leave those
3708 // bits unchanged, but we would have to make sure that no code uses a
3709 // 32bit input value as a 64bit value assuming that the top 32 bits are
3710 // zero.
3711 __ Mov(output.W(), source.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00003712 } else if ((result_type == Primitive::kPrimChar) ||
3713 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3714 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003715 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003716 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003717 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003718 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003719 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003720 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003721 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3722 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003723 } else if (Primitive::IsFloatingPointType(result_type) &&
3724 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003725 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3726 } else {
3727 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3728 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003729 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003730}
Alexandre Rames67555f72014-11-18 10:55:16 +00003731
Serban Constantinescu02164b32014-11-13 14:05:07 +00003732void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3733 HandleShift(ushr);
3734}
3735
3736void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3737 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003738}
3739
3740void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3741 HandleBinaryOp(instruction);
3742}
3743
3744void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3745 HandleBinaryOp(instruction);
3746}
3747
Calin Juravleb1498f62015-02-16 13:13:29 +00003748void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3749 // Nothing to do, this should be removed during prepare for register allocator.
3750 UNUSED(instruction);
3751 LOG(FATAL) << "Unreachable";
3752}
3753
3754void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3755 // Nothing to do, this should be removed during prepare for register allocator.
3756 UNUSED(instruction);
3757 LOG(FATAL) << "Unreachable";
3758}
3759
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003760void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3761 DCHECK(codegen_->IsBaseline());
3762 LocationSummary* locations =
3763 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3764 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3765}
3766
3767void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3768 DCHECK(codegen_->IsBaseline());
3769 // Will be generated at use site.
3770}
3771
Mark Mendellfe57faa2015-09-18 09:26:15 -04003772// Simple implementation of packed switch - generate cascaded compare/jumps.
3773void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3774 LocationSummary* locations =
3775 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
3776 locations->SetInAt(0, Location::RequiresRegister());
3777}
3778
3779void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3780 int32_t lower_bound = switch_instr->GetStartValue();
3781 int32_t num_entries = switch_instr->GetNumEntries();
3782 Register value_reg = InputRegisterAt(switch_instr, 0);
3783 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
3784
3785 // Create a series of compare/jumps.
3786 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
3787 for (int32_t i = 0; i < num_entries; i++) {
3788 int32_t case_value = lower_bound + i;
Vladimir Markoec7802a2015-10-01 20:57:57 +01003789 vixl::Label* succ = codegen_->GetLabelOf(successors[i]);
Mark Mendellfe57faa2015-09-18 09:26:15 -04003790 if (case_value == 0) {
3791 __ Cbz(value_reg, succ);
3792 } else {
3793 __ Cmp(value_reg, vixl::Operand(case_value));
3794 __ B(eq, succ);
3795 }
3796 }
3797
3798 // And the default for any other value.
3799 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
3800 __ B(codegen_->GetLabelOf(default_block));
3801 }
3802}
3803
Alexandre Rames67555f72014-11-18 10:55:16 +00003804#undef __
3805#undef QUICK_ENTRY_POINT
3806
Alexandre Rames5319def2014-10-23 10:03:10 +01003807} // namespace arm64
3808} // namespace art