blob: 44bd399127000fbac3484d1b48a33212803cff65 [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"
Andreas Gampe878d58c2015-01-15 23:24:00 -080020#include "common_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010021#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080022#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080024#include "intrinsics.h"
25#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010026#include "mirror/array-inl.h"
27#include "mirror/art_method.h"
28#include "mirror/class.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000029#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010030#include "thread.h"
31#include "utils/arm64/assembler_arm64.h"
32#include "utils/assembler.h"
33#include "utils/stack_checks.h"
34
35
36using namespace vixl; // NOLINT(build/namespaces)
37
38#ifdef __
39#error "ARM64 Codegen VIXL macro-assembler macro already defined."
40#endif
41
Alexandre Rames5319def2014-10-23 10:03:10 +010042namespace art {
43
44namespace arm64 {
45
Andreas Gampe878d58c2015-01-15 23:24:00 -080046using helpers::CPURegisterFrom;
47using helpers::DRegisterFrom;
48using helpers::FPRegisterFrom;
49using helpers::HeapOperand;
50using helpers::HeapOperandFrom;
51using helpers::InputCPURegisterAt;
52using helpers::InputFPRegisterAt;
53using helpers::InputRegisterAt;
54using helpers::InputOperandAt;
55using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080056using helpers::LocationFrom;
57using helpers::OperandFromMemOperand;
58using helpers::OutputCPURegister;
59using helpers::OutputFPRegister;
60using helpers::OutputRegister;
61using helpers::RegisterFrom;
62using helpers::StackOperandFrom;
63using helpers::VIXLRegCodeFromART;
64using helpers::WRegisterFrom;
65using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000066using helpers::ARM64EncodableConstantOrRegister;
Andreas Gampe878d58c2015-01-15 23:24:00 -080067
Alexandre Rames5319def2014-10-23 10:03:10 +010068static constexpr size_t kHeapRefSize = sizeof(mirror::HeapReference<mirror::Object>);
69static constexpr int kCurrentMethodStackOffset = 0;
70
Alexandre Rames5319def2014-10-23 10:03:10 +010071inline Condition ARM64Condition(IfCondition cond) {
72 switch (cond) {
73 case kCondEQ: return eq;
74 case kCondNE: return ne;
75 case kCondLT: return lt;
76 case kCondLE: return le;
77 case kCondGT: return gt;
78 case kCondGE: return ge;
79 default:
80 LOG(FATAL) << "Unknown if condition";
81 }
82 return nv; // Unreachable.
83}
84
Alexandre Ramesa89086e2014-11-07 17:13:25 +000085Location ARM64ReturnLocation(Primitive::Type return_type) {
86 DCHECK_NE(return_type, Primitive::kPrimVoid);
87 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
88 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
89 // but we use the exact registers for clarity.
90 if (return_type == Primitive::kPrimFloat) {
91 return LocationFrom(s0);
92 } else if (return_type == Primitive::kPrimDouble) {
93 return LocationFrom(d0);
94 } else if (return_type == Primitive::kPrimLong) {
95 return LocationFrom(x0);
96 } else {
97 return LocationFrom(w0);
98 }
99}
100
Alexandre Rames5319def2014-10-23 10:03:10 +0100101static const Register kRuntimeParameterCoreRegisters[] = { x0, x1, x2, x3, x4, x5, x6, x7 };
102static constexpr size_t kRuntimeParameterCoreRegistersLength =
103 arraysize(kRuntimeParameterCoreRegisters);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000104static const FPRegister kRuntimeParameterFpuRegisters[] = { d0, d1, d2, d3, d4, d5, d6, d7 };
105static constexpr size_t kRuntimeParameterFpuRegistersLength =
106 arraysize(kRuntimeParameterCoreRegisters);
Alexandre Rames5319def2014-10-23 10:03:10 +0100107
108class InvokeRuntimeCallingConvention : public CallingConvention<Register, FPRegister> {
109 public:
110 static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
111
112 InvokeRuntimeCallingConvention()
113 : CallingConvention(kRuntimeParameterCoreRegisters,
114 kRuntimeParameterCoreRegistersLength,
115 kRuntimeParameterFpuRegisters,
116 kRuntimeParameterFpuRegistersLength) {}
117
118 Location GetReturnLocation(Primitive::Type return_type);
119
120 private:
121 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
122};
123
124Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000125 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100126}
127
Alexandre Rames67555f72014-11-18 10:55:16 +0000128#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
129#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100130
Alexandre Rames5319def2014-10-23 10:03:10 +0100131class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
132 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000133 BoundsCheckSlowPathARM64(HBoundsCheck* instruction,
134 Location index_location,
135 Location length_location)
136 : instruction_(instruction),
137 index_location_(index_location),
138 length_location_(length_location) {}
139
Alexandre Rames5319def2014-10-23 10:03:10 +0100140
Alexandre Rames67555f72014-11-18 10:55:16 +0000141 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000142 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100143 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000144 // We're moving two locations to locations that could overlap, so we need a parallel
145 // move resolver.
146 InvokeRuntimeCallingConvention calling_convention;
147 codegen->EmitParallelMoves(
148 index_location_, LocationFrom(calling_convention.GetRegisterAt(0)),
149 length_location_, LocationFrom(calling_convention.GetRegisterAt(1)));
150 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000151 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800152 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100153 }
154
155 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000156 HBoundsCheck* const instruction_;
157 const Location index_location_;
158 const Location length_location_;
159
Alexandre Rames5319def2014-10-23 10:03:10 +0100160 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
161};
162
Alexandre Rames67555f72014-11-18 10:55:16 +0000163class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
164 public:
165 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
166
167 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
168 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
169 __ Bind(GetEntryLabel());
170 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000171 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800172 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000173 }
174
175 private:
176 HDivZeroCheck* const instruction_;
177 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
178};
179
180class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
181 public:
182 LoadClassSlowPathARM64(HLoadClass* cls,
183 HInstruction* at,
184 uint32_t dex_pc,
185 bool do_clinit)
186 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
187 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
188 }
189
190 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
191 LocationSummary* locations = at_->GetLocations();
192 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
193
194 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000195 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000196
197 InvokeRuntimeCallingConvention calling_convention;
198 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
199 arm64_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1).W());
200 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
201 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000202 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800203 if (do_clinit_) {
204 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t, mirror::ArtMethod*>();
205 } else {
206 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t, mirror::ArtMethod*>();
207 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000208
209 // Move the class to the desired location.
210 Location out = locations->Out();
211 if (out.IsValid()) {
212 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
213 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000214 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000215 }
216
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000217 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000218 __ B(GetExitLabel());
219 }
220
221 private:
222 // The class this slow path will load.
223 HLoadClass* const cls_;
224
225 // The instruction where this slow path is happening.
226 // (Might be the load class or an initialization check).
227 HInstruction* const at_;
228
229 // The dex PC of `at_`.
230 const uint32_t dex_pc_;
231
232 // Whether to initialize the class.
233 const bool do_clinit_;
234
235 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
236};
237
238class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
239 public:
240 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
241
242 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
243 LocationSummary* locations = instruction_->GetLocations();
244 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
245 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
246
247 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000248 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000249
250 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800251 arm64_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1).W());
252 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000253 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000254 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800255 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000256 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000257 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000258
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000259 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000260 __ B(GetExitLabel());
261 }
262
263 private:
264 HLoadString* const instruction_;
265
266 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
267};
268
Alexandre Rames5319def2014-10-23 10:03:10 +0100269class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
270 public:
271 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
272
Alexandre Rames67555f72014-11-18 10:55:16 +0000273 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
274 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100275 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000276 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000277 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800278 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100279 }
280
281 private:
282 HNullCheck* const instruction_;
283
284 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
285};
286
287class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
288 public:
289 explicit SuspendCheckSlowPathARM64(HSuspendCheck* instruction,
290 HBasicBlock* successor)
291 : instruction_(instruction), successor_(successor) {}
292
Alexandre Rames67555f72014-11-18 10:55:16 +0000293 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
294 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100295 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000296 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000297 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000298 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800299 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000300 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000301 if (successor_ == nullptr) {
302 __ B(GetReturnLabel());
303 } else {
304 __ B(arm64_codegen->GetLabelOf(successor_));
305 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100306 }
307
308 vixl::Label* GetReturnLabel() {
309 DCHECK(successor_ == nullptr);
310 return &return_label_;
311 }
312
Alexandre Rames5319def2014-10-23 10:03:10 +0100313 private:
314 HSuspendCheck* const instruction_;
315 // If not null, the block to branch to after the suspend check.
316 HBasicBlock* const successor_;
317
318 // If `successor_` is null, the label to branch to after the suspend check.
319 vixl::Label return_label_;
320
321 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
322};
323
Alexandre Rames67555f72014-11-18 10:55:16 +0000324class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
325 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000326 TypeCheckSlowPathARM64(HInstruction* instruction,
327 Location class_to_check,
328 Location object_class,
329 uint32_t dex_pc)
330 : instruction_(instruction),
331 class_to_check_(class_to_check),
332 object_class_(object_class),
333 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000334
335 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000336 LocationSummary* locations = instruction_->GetLocations();
337 DCHECK(instruction_->IsCheckCast()
338 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
339 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
340
Alexandre Rames67555f72014-11-18 10:55:16 +0000341 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000342 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000343
344 // We're moving two locations to locations that could overlap, so we need a parallel
345 // move resolver.
346 InvokeRuntimeCallingConvention calling_convention;
347 codegen->EmitParallelMoves(
348 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)),
349 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)));
350
351 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000352 arm64_codegen->InvokeRuntime(
353 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000354 Primitive::Type ret_type = instruction_->GetType();
355 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
356 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800357 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
358 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000359 } else {
360 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000361 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800362 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000363 }
364
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000365 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000366 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000367 }
368
369 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000370 HInstruction* const instruction_;
371 const Location class_to_check_;
372 const Location object_class_;
373 uint32_t dex_pc_;
374
Alexandre Rames67555f72014-11-18 10:55:16 +0000375 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
376};
377
Mingyao Yange295e6e2015-03-07 06:37:59 -0800378class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
379 public:
380 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
381 : instruction_(instruction) {}
382
383 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
384 __ Bind(GetEntryLabel());
385 SaveLiveRegisters(codegen, instruction_->GetLocations());
386 DCHECK(instruction_->IsDeoptimize());
387 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
388 uint32_t dex_pc = deoptimize->GetDexPc();
389 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
390 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
391 }
392
393 private:
394 HInstruction* const instruction_;
395 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
396};
397
Alexandre Rames5319def2014-10-23 10:03:10 +0100398#undef __
399
400Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
401 Location next_location;
402 if (type == Primitive::kPrimVoid) {
403 LOG(FATAL) << "Unreachable type " << type;
404 }
405
Alexandre Rames542361f2015-01-29 16:57:31 +0000406 if (Primitive::IsFloatingPointType(type) &&
407 (fp_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000408 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(fp_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000409 } else if (!Primitive::IsFloatingPointType(type) &&
410 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000411 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
412 } else {
413 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000414 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
415 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100416 }
417
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000418 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000419 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100420 return next_location;
421}
422
Serban Constantinescu579885a2015-02-22 20:51:33 +0000423CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
424 const Arm64InstructionSetFeatures& isa_features,
425 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100426 : CodeGenerator(graph,
427 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000428 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000429 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000430 callee_saved_core_registers.list(),
431 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000432 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100433 block_labels_(nullptr),
434 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000435 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000436 move_resolver_(graph->GetArena(), this),
437 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000438 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000439 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000440}
Alexandre Rames5319def2014-10-23 10:03:10 +0100441
Alexandre Rames67555f72014-11-18 10:55:16 +0000442#undef __
443#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100444
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000445void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
446 // Ensure we emit the literal pool.
447 __ FinalizeCode();
448 CodeGenerator::Finalize(allocator);
449}
450
Alexandre Rames3e69f162014-12-10 10:36:50 +0000451void ParallelMoveResolverARM64::EmitMove(size_t index) {
452 MoveOperands* move = moves_.Get(index);
453 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
454}
455
456void ParallelMoveResolverARM64::EmitSwap(size_t index) {
457 MoveOperands* move = moves_.Get(index);
458 codegen_->SwapLocations(move->GetDestination(), move->GetSource());
459}
460
461void ParallelMoveResolverARM64::RestoreScratch(int reg) {
462 __ Pop(Register(VIXLRegCodeFromART(reg), kXRegSize));
463}
464
465void ParallelMoveResolverARM64::SpillScratch(int reg) {
466 __ Push(Register(VIXLRegCodeFromART(reg), kXRegSize));
467}
468
Alexandre Rames5319def2014-10-23 10:03:10 +0100469void CodeGeneratorARM64::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000470 __ Bind(&frame_entry_label_);
471
Serban Constantinescu02164b32014-11-13 14:05:07 +0000472 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
473 if (do_overflow_check) {
474 UseScratchRegisterScope temps(GetVIXLAssembler());
475 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000476 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000477 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000478 __ Ldr(wzr, MemOperand(temp, 0));
479 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000480 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100481
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000482 if (!HasEmptyFrame()) {
483 int frame_size = GetFrameSize();
484 // Stack layout:
485 // sp[frame_size - 8] : lr.
486 // ... : other preserved core registers.
487 // ... : other preserved fp registers.
488 // ... : reserved frame space.
489 // sp[0] : current method.
490 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
491 __ PokeCPURegList(GetFramePreservedCoreRegisters(), frame_size - GetCoreSpillSize());
492 __ PokeCPURegList(GetFramePreservedFPRegisters(), frame_size - FrameEntrySpillSize());
493 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100494}
495
496void CodeGeneratorARM64::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000497 if (!HasEmptyFrame()) {
498 int frame_size = GetFrameSize();
499 __ PeekCPURegList(GetFramePreservedFPRegisters(), frame_size - FrameEntrySpillSize());
500 __ PeekCPURegList(GetFramePreservedCoreRegisters(), frame_size - GetCoreSpillSize());
501 __ Drop(frame_size);
502 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100503}
504
505void CodeGeneratorARM64::Bind(HBasicBlock* block) {
506 __ Bind(GetLabelOf(block));
507}
508
Alexandre Rames5319def2014-10-23 10:03:10 +0100509void CodeGeneratorARM64::Move(HInstruction* instruction,
510 Location location,
511 HInstruction* move_for) {
512 LocationSummary* locations = instruction->GetLocations();
513 if (locations != nullptr && locations->Out().Equals(location)) {
514 return;
515 }
516
517 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000518 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100519
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000520 if (instruction->IsIntConstant()
521 || instruction->IsLongConstant()
522 || instruction->IsNullConstant()) {
523 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100524 if (location.IsRegister()) {
525 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000526 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100527 (instruction->IsLongConstant() && dst.Is64Bits()));
528 __ Mov(dst, value);
529 } else {
530 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000531 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000532 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
533 ? temps.AcquireW()
534 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100535 __ Mov(temp, value);
536 __ Str(temp, StackOperandFrom(location));
537 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000538 } else if (instruction->IsTemporary()) {
539 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000540 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100541 } else if (instruction->IsLoadLocal()) {
542 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000543 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000544 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000545 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000546 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100547 }
548
549 } else {
550 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000551 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100552 }
553}
554
Alexandre Rames5319def2014-10-23 10:03:10 +0100555Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
556 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000557
Alexandre Rames5319def2014-10-23 10:03:10 +0100558 switch (type) {
559 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000560 case Primitive::kPrimInt:
561 case Primitive::kPrimFloat:
562 return Location::StackSlot(GetStackSlot(load->GetLocal()));
563
564 case Primitive::kPrimLong:
565 case Primitive::kPrimDouble:
566 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
567
Alexandre Rames5319def2014-10-23 10:03:10 +0100568 case Primitive::kPrimBoolean:
569 case Primitive::kPrimByte:
570 case Primitive::kPrimChar:
571 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100572 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100573 LOG(FATAL) << "Unexpected type " << type;
574 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000575
Alexandre Rames5319def2014-10-23 10:03:10 +0100576 LOG(FATAL) << "Unreachable";
577 return Location::NoLocation();
578}
579
580void CodeGeneratorARM64::MarkGCCard(Register object, Register value) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000581 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100582 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000583 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100584 vixl::Label done;
585 __ Cbz(value, &done);
586 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
587 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000588 __ Strb(card, MemOperand(card, temp.X()));
Alexandre Rames5319def2014-10-23 10:03:10 +0100589 __ Bind(&done);
590}
591
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000592void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
593 // Blocked core registers:
594 // lr : Runtime reserved.
595 // tr : Runtime reserved.
596 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
597 // ip1 : VIXL core temp.
598 // ip0 : VIXL core temp.
599 //
600 // Blocked fp registers:
601 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100602 CPURegList reserved_core_registers = vixl_reserved_core_registers;
603 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100604 while (!reserved_core_registers.IsEmpty()) {
605 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
606 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000607
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000608 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800609 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000610 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
611 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000612
613 if (is_baseline) {
614 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
615 while (!reserved_core_baseline_registers.IsEmpty()) {
616 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
617 }
618
619 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
620 while (!reserved_fp_baseline_registers.IsEmpty()) {
621 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
622 }
623 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100624}
625
626Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
627 if (type == Primitive::kPrimVoid) {
628 LOG(FATAL) << "Unreachable type " << type;
629 }
630
Alexandre Rames542361f2015-01-29 16:57:31 +0000631 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000632 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
633 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100634 return Location::FpuRegisterLocation(reg);
635 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000636 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
637 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100638 return Location::RegisterLocation(reg);
639 }
640}
641
Alexandre Rames3e69f162014-12-10 10:36:50 +0000642size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
643 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
644 __ Str(reg, MemOperand(sp, stack_index));
645 return kArm64WordSize;
646}
647
648size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
649 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
650 __ Ldr(reg, MemOperand(sp, stack_index));
651 return kArm64WordSize;
652}
653
654size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
655 FPRegister reg = FPRegister(reg_id, kDRegSize);
656 __ Str(reg, MemOperand(sp, stack_index));
657 return kArm64WordSize;
658}
659
660size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
661 FPRegister reg = FPRegister(reg_id, kDRegSize);
662 __ Ldr(reg, MemOperand(sp, stack_index));
663 return kArm64WordSize;
664}
665
Alexandre Rames5319def2014-10-23 10:03:10 +0100666void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
667 stream << Arm64ManagedRegister::FromXRegister(XRegister(reg));
668}
669
670void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
671 stream << Arm64ManagedRegister::FromDRegister(DRegister(reg));
672}
673
Alexandre Rames67555f72014-11-18 10:55:16 +0000674void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000675 if (constant->IsIntConstant()) {
676 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
677 } else if (constant->IsLongConstant()) {
678 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
679 } else if (constant->IsNullConstant()) {
680 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000681 } else if (constant->IsFloatConstant()) {
682 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
683 } else {
684 DCHECK(constant->IsDoubleConstant());
685 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
686 }
687}
688
Alexandre Rames3e69f162014-12-10 10:36:50 +0000689
690static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
691 DCHECK(constant.IsConstant());
692 HConstant* cst = constant.GetConstant();
693 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000694 // Null is mapped to a core W register, which we associate with kPrimInt.
695 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000696 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
697 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
698 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
699}
700
701void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000702 if (source.Equals(destination)) {
703 return;
704 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000705
706 // A valid move can always be inferred from the destination and source
707 // locations. When moving from and to a register, the argument type can be
708 // used to generate 32bit instead of 64bit moves. In debug mode we also
709 // checks the coherency of the locations and the type.
710 bool unspecified_type = (type == Primitive::kPrimVoid);
711
712 if (destination.IsRegister() || destination.IsFpuRegister()) {
713 if (unspecified_type) {
714 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
715 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000716 (src_cst != nullptr && (src_cst->IsIntConstant()
717 || src_cst->IsFloatConstant()
718 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000719 // For stack slots and 32bit constants, a 64bit type is appropriate.
720 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000721 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000722 // If the source is a double stack slot or a 64bit constant, a 64bit
723 // type is appropriate. Else the source is a register, and since the
724 // type has not been specified, we chose a 64bit type to force a 64bit
725 // move.
726 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000727 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000728 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000729 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
730 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000731 CPURegister dst = CPURegisterFrom(destination, type);
732 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
733 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
734 __ Ldr(dst, StackOperandFrom(source));
735 } else if (source.IsConstant()) {
736 DCHECK(CoherentConstantAndType(source, type));
737 MoveConstant(dst, source.GetConstant());
738 } else {
739 if (destination.IsRegister()) {
740 __ Mov(Register(dst), RegisterFrom(source, type));
741 } else {
742 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
743 }
744 }
745
746 } else { // The destination is not a register. It must be a stack slot.
747 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
748 if (source.IsRegister() || source.IsFpuRegister()) {
749 if (unspecified_type) {
750 if (source.IsRegister()) {
751 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
752 } else {
753 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
754 }
755 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000756 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
757 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000758 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
759 } else if (source.IsConstant()) {
760 DCHECK(unspecified_type || CoherentConstantAndType(source, type));
761 UseScratchRegisterScope temps(GetVIXLAssembler());
762 HConstant* src_cst = source.GetConstant();
763 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000764 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000765 temp = temps.AcquireW();
766 } else if (src_cst->IsLongConstant()) {
767 temp = temps.AcquireX();
768 } else if (src_cst->IsFloatConstant()) {
769 temp = temps.AcquireS();
770 } else {
771 DCHECK(src_cst->IsDoubleConstant());
772 temp = temps.AcquireD();
773 }
774 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000775 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000776 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000777 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000778 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000779 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000780 // There is generally less pressure on FP registers.
781 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000782 __ Ldr(temp, StackOperandFrom(source));
783 __ Str(temp, StackOperandFrom(destination));
784 }
785 }
786}
787
Alexandre Rames3e69f162014-12-10 10:36:50 +0000788void CodeGeneratorARM64::SwapLocations(Location loc1, Location loc2) {
789 DCHECK(!loc1.IsConstant());
790 DCHECK(!loc2.IsConstant());
791
792 if (loc1.Equals(loc2)) {
793 return;
794 }
795
796 UseScratchRegisterScope temps(GetAssembler()->vixl_masm_);
797
798 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
799 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
800 bool is_fp_reg1 = loc1.IsFpuRegister();
801 bool is_fp_reg2 = loc2.IsFpuRegister();
802
803 if (loc2.IsRegister() && loc1.IsRegister()) {
804 Register r1 = XRegisterFrom(loc1);
805 Register r2 = XRegisterFrom(loc2);
806 Register tmp = temps.AcquireSameSizeAs(r1);
807 __ Mov(tmp, r2);
808 __ Mov(r2, r1);
809 __ Mov(r1, tmp);
810 } else if (is_fp_reg2 && is_fp_reg1) {
811 FPRegister r1 = DRegisterFrom(loc1);
812 FPRegister r2 = DRegisterFrom(loc2);
813 FPRegister tmp = temps.AcquireSameSizeAs(r1);
814 __ Fmov(tmp, r2);
815 __ Fmov(r2, r1);
816 __ Fmov(r1, tmp);
817 } else if (is_slot1 != is_slot2) {
818 MemOperand mem = StackOperandFrom(is_slot1 ? loc1 : loc2);
819 Location reg_loc = is_slot1 ? loc2 : loc1;
820 CPURegister reg, tmp;
821 if (reg_loc.IsFpuRegister()) {
822 reg = DRegisterFrom(reg_loc);
823 tmp = temps.AcquireD();
824 } else {
825 reg = XRegisterFrom(reg_loc);
826 tmp = temps.AcquireX();
827 }
828 __ Ldr(tmp, mem);
829 __ Str(reg, mem);
830 if (reg_loc.IsFpuRegister()) {
831 __ Fmov(FPRegister(reg), FPRegister(tmp));
832 } else {
833 __ Mov(Register(reg), Register(tmp));
834 }
835 } else if (is_slot1 && is_slot2) {
836 MemOperand mem1 = StackOperandFrom(loc1);
837 MemOperand mem2 = StackOperandFrom(loc2);
838 Register tmp1 = loc1.IsStackSlot() ? temps.AcquireW() : temps.AcquireX();
839 Register tmp2 = temps.AcquireSameSizeAs(tmp1);
840 __ Ldr(tmp1, mem1);
841 __ Ldr(tmp2, mem2);
842 __ Str(tmp1, mem2);
843 __ Str(tmp2, mem1);
844 } else {
845 LOG(FATAL) << "Unimplemented";
846 }
847}
848
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000849void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000850 CPURegister dst,
851 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000852 switch (type) {
853 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000854 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000855 break;
856 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000857 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000858 break;
859 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000860 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000861 break;
862 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000863 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000864 break;
865 case Primitive::kPrimInt:
866 case Primitive::kPrimNot:
867 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000868 case Primitive::kPrimFloat:
869 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000870 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000871 __ Ldr(dst, src);
872 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000873 case Primitive::kPrimVoid:
874 LOG(FATAL) << "Unreachable type " << type;
875 }
876}
877
Calin Juravle77520bc2015-01-12 18:45:46 +0000878void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000879 CPURegister dst,
880 const MemOperand& src) {
881 UseScratchRegisterScope temps(GetVIXLAssembler());
882 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000883 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000884
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000885 DCHECK(!src.IsPreIndex());
886 DCHECK(!src.IsPostIndex());
887
888 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800889 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000890 MemOperand base = MemOperand(temp_base);
891 switch (type) {
892 case Primitive::kPrimBoolean:
893 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000894 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000895 break;
896 case Primitive::kPrimByte:
897 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000898 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000899 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
900 break;
901 case Primitive::kPrimChar:
902 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000903 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000904 break;
905 case Primitive::kPrimShort:
906 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000907 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000908 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
909 break;
910 case Primitive::kPrimInt:
911 case Primitive::kPrimNot:
912 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000913 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000914 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000915 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000916 break;
917 case Primitive::kPrimFloat:
918 case Primitive::kPrimDouble: {
919 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000920 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000921
922 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
923 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000924 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000925 __ Fmov(FPRegister(dst), temp);
926 break;
927 }
928 case Primitive::kPrimVoid:
929 LOG(FATAL) << "Unreachable type " << type;
930 }
931}
932
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000933void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000934 CPURegister src,
935 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000936 switch (type) {
937 case Primitive::kPrimBoolean:
938 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000939 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000940 break;
941 case Primitive::kPrimChar:
942 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000943 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000944 break;
945 case Primitive::kPrimInt:
946 case Primitive::kPrimNot:
947 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000948 case Primitive::kPrimFloat:
949 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000950 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000951 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000952 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000953 case Primitive::kPrimVoid:
954 LOG(FATAL) << "Unreachable type " << type;
955 }
956}
957
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000958void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
959 CPURegister src,
960 const MemOperand& dst) {
961 UseScratchRegisterScope temps(GetVIXLAssembler());
962 Register temp_base = temps.AcquireX();
963
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000964 DCHECK(!dst.IsPreIndex());
965 DCHECK(!dst.IsPostIndex());
966
967 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800968 Operand op = OperandFromMemOperand(dst);
969 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000970 MemOperand base = MemOperand(temp_base);
971 switch (type) {
972 case Primitive::kPrimBoolean:
973 case Primitive::kPrimByte:
974 __ Stlrb(Register(src), base);
975 break;
976 case Primitive::kPrimChar:
977 case Primitive::kPrimShort:
978 __ Stlrh(Register(src), base);
979 break;
980 case Primitive::kPrimInt:
981 case Primitive::kPrimNot:
982 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000983 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000984 __ Stlr(Register(src), base);
985 break;
986 case Primitive::kPrimFloat:
987 case Primitive::kPrimDouble: {
988 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000989 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000990
991 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
992 __ Fmov(temp, FPRegister(src));
993 __ Stlr(temp, base);
994 break;
995 }
996 case Primitive::kPrimVoid:
997 LOG(FATAL) << "Unreachable type " << type;
998 }
999}
1000
Alexandre Rames67555f72014-11-18 10:55:16 +00001001void CodeGeneratorARM64::LoadCurrentMethod(vixl::Register current_method) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001002 DCHECK(RequiresCurrentMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +00001003 DCHECK(current_method.IsW());
1004 __ Ldr(current_method, MemOperand(sp, kCurrentMethodStackOffset));
1005}
1006
1007void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1008 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001009 uint32_t dex_pc,
1010 SlowPathCode* slow_path) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001011 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1012 __ Blr(lr);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001013 if (instruction != nullptr) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001014 RecordPcInfo(instruction, dex_pc, slow_path);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001015 DCHECK(instruction->IsSuspendCheck()
1016 || instruction->IsBoundsCheck()
1017 || instruction->IsNullCheck()
1018 || instruction->IsDivZeroCheck()
1019 || !IsLeafMethod());
1020 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001021}
1022
1023void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1024 vixl::Register class_reg) {
1025 UseScratchRegisterScope temps(GetVIXLAssembler());
1026 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001027 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001028 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001029
Serban Constantinescu02164b32014-11-13 14:05:07 +00001030 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001031 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001032 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1033 __ Add(temp, class_reg, status_offset);
1034 __ Ldar(temp, HeapOperand(temp));
1035 __ Cmp(temp, mirror::Class::kStatusInitialized);
1036 __ B(lt, slow_path->GetEntryLabel());
1037 } else {
1038 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1039 __ Cmp(temp, mirror::Class::kStatusInitialized);
1040 __ B(lt, slow_path->GetEntryLabel());
1041 __ Dmb(InnerShareable, BarrierReads);
1042 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001043 __ Bind(slow_path->GetExitLabel());
1044}
Alexandre Rames5319def2014-10-23 10:03:10 +01001045
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001046void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1047 BarrierType type = BarrierAll;
1048
1049 switch (kind) {
1050 case MemBarrierKind::kAnyAny:
1051 case MemBarrierKind::kAnyStore: {
1052 type = BarrierAll;
1053 break;
1054 }
1055 case MemBarrierKind::kLoadAny: {
1056 type = BarrierReads;
1057 break;
1058 }
1059 case MemBarrierKind::kStoreStore: {
1060 type = BarrierWrites;
1061 break;
1062 }
1063 default:
1064 LOG(FATAL) << "Unexpected memory barrier " << kind;
1065 }
1066 __ Dmb(InnerShareable, type);
1067}
1068
Serban Constantinescu02164b32014-11-13 14:05:07 +00001069void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1070 HBasicBlock* successor) {
1071 SuspendCheckSlowPathARM64* slow_path =
1072 new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1073 codegen_->AddSlowPath(slow_path);
1074 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1075 Register temp = temps.AcquireW();
1076
1077 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1078 if (successor == nullptr) {
1079 __ Cbnz(temp, slow_path->GetEntryLabel());
1080 __ Bind(slow_path->GetReturnLabel());
1081 } else {
1082 __ Cbz(temp, codegen_->GetLabelOf(successor));
1083 __ B(slow_path->GetEntryLabel());
1084 // slow_path will return to GetLabelOf(successor).
1085 }
1086}
1087
Alexandre Rames5319def2014-10-23 10:03:10 +01001088InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1089 CodeGeneratorARM64* codegen)
1090 : HGraphVisitor(graph),
1091 assembler_(codegen->GetAssembler()),
1092 codegen_(codegen) {}
1093
1094#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001095 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001096
1097#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1098
1099enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001100 // Using a base helps identify when we hit such breakpoints.
1101 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001102#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1103 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1104#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1105};
1106
1107#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1108 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001109 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001110 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1111 } \
1112 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1113 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1114 locations->SetOut(Location::Any()); \
1115 }
1116 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1117#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1118
1119#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001120#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001121
Alexandre Rames67555f72014-11-18 10:55:16 +00001122void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001123 DCHECK_EQ(instr->InputCount(), 2U);
1124 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1125 Primitive::Type type = instr->GetResultType();
1126 switch (type) {
1127 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001128 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001129 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001130 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001131 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001132 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001133
1134 case Primitive::kPrimFloat:
1135 case Primitive::kPrimDouble:
1136 locations->SetInAt(0, Location::RequiresFpuRegister());
1137 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001138 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001139 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001140
Alexandre Rames5319def2014-10-23 10:03:10 +01001141 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001142 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001143 }
1144}
1145
Alexandre Rames67555f72014-11-18 10:55:16 +00001146void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001147 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001148
1149 switch (type) {
1150 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001151 case Primitive::kPrimLong: {
1152 Register dst = OutputRegister(instr);
1153 Register lhs = InputRegisterAt(instr, 0);
1154 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001155 if (instr->IsAdd()) {
1156 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001157 } else if (instr->IsAnd()) {
1158 __ And(dst, lhs, rhs);
1159 } else if (instr->IsOr()) {
1160 __ Orr(dst, lhs, rhs);
1161 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001162 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001163 } else {
1164 DCHECK(instr->IsXor());
1165 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001166 }
1167 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001168 }
1169 case Primitive::kPrimFloat:
1170 case Primitive::kPrimDouble: {
1171 FPRegister dst = OutputFPRegister(instr);
1172 FPRegister lhs = InputFPRegisterAt(instr, 0);
1173 FPRegister rhs = InputFPRegisterAt(instr, 1);
1174 if (instr->IsAdd()) {
1175 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001176 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001177 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001178 } else {
1179 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001180 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001181 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001182 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001183 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001184 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001185 }
1186}
1187
Serban Constantinescu02164b32014-11-13 14:05:07 +00001188void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1189 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1190
1191 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1192 Primitive::Type type = instr->GetResultType();
1193 switch (type) {
1194 case Primitive::kPrimInt:
1195 case Primitive::kPrimLong: {
1196 locations->SetInAt(0, Location::RequiresRegister());
1197 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1198 locations->SetOut(Location::RequiresRegister());
1199 break;
1200 }
1201 default:
1202 LOG(FATAL) << "Unexpected shift type " << type;
1203 }
1204}
1205
1206void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1207 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1208
1209 Primitive::Type type = instr->GetType();
1210 switch (type) {
1211 case Primitive::kPrimInt:
1212 case Primitive::kPrimLong: {
1213 Register dst = OutputRegister(instr);
1214 Register lhs = InputRegisterAt(instr, 0);
1215 Operand rhs = InputOperandAt(instr, 1);
1216 if (rhs.IsImmediate()) {
1217 uint32_t shift_value = (type == Primitive::kPrimInt)
1218 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1219 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1220 if (instr->IsShl()) {
1221 __ Lsl(dst, lhs, shift_value);
1222 } else if (instr->IsShr()) {
1223 __ Asr(dst, lhs, shift_value);
1224 } else {
1225 __ Lsr(dst, lhs, shift_value);
1226 }
1227 } else {
1228 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1229
1230 if (instr->IsShl()) {
1231 __ Lsl(dst, lhs, rhs_reg);
1232 } else if (instr->IsShr()) {
1233 __ Asr(dst, lhs, rhs_reg);
1234 } else {
1235 __ Lsr(dst, lhs, rhs_reg);
1236 }
1237 }
1238 break;
1239 }
1240 default:
1241 LOG(FATAL) << "Unexpected shift operation type " << type;
1242 }
1243}
1244
Alexandre Rames5319def2014-10-23 10:03:10 +01001245void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001246 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001247}
1248
1249void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001250 HandleBinaryOp(instruction);
1251}
1252
1253void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1254 HandleBinaryOp(instruction);
1255}
1256
1257void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1258 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001259}
1260
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001261void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1262 LocationSummary* locations =
1263 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1264 locations->SetInAt(0, Location::RequiresRegister());
1265 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1266 locations->SetOut(Location::RequiresRegister());
1267}
1268
1269void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1270 LocationSummary* locations = instruction->GetLocations();
1271 Primitive::Type type = instruction->GetType();
1272 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001273 Location index = locations->InAt(1);
1274 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001275 MemOperand source = HeapOperand(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00001276 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001277
1278 if (index.IsConstant()) {
1279 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001280 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001281 } else {
1282 Register temp = temps.AcquireSameSizeAs(obj);
1283 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
1284 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001285 source = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001286 }
1287
Alexandre Rames67555f72014-11-18 10:55:16 +00001288 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001289 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001290}
1291
Alexandre Rames5319def2014-10-23 10:03:10 +01001292void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1293 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1294 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001295 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001296}
1297
1298void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
1299 __ Ldr(OutputRegister(instruction),
1300 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001301 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001302}
1303
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001304void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
1305 Primitive::Type value_type = instruction->GetComponentType();
1306 bool is_object = value_type == Primitive::kPrimNot;
1307 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1308 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1309 if (is_object) {
1310 InvokeRuntimeCallingConvention calling_convention;
1311 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1312 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1313 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1314 } else {
1315 locations->SetInAt(0, Location::RequiresRegister());
1316 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1317 locations->SetInAt(2, Location::RequiresRegister());
1318 }
1319}
1320
1321void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1322 Primitive::Type value_type = instruction->GetComponentType();
1323 if (value_type == Primitive::kPrimNot) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001324 codegen_->InvokeRuntime(
1325 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001326 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001327 } else {
1328 LocationSummary* locations = instruction->GetLocations();
1329 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001330 CPURegister value = InputCPURegisterAt(instruction, 2);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001331 Location index = locations->InAt(1);
1332 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001333 MemOperand destination = HeapOperand(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00001334 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001335
1336 if (index.IsConstant()) {
1337 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001338 destination = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001339 } else {
1340 Register temp = temps.AcquireSameSizeAs(obj);
1341 Register index_reg = InputRegisterAt(instruction, 1);
1342 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(value_type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001343 destination = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001344 }
1345
1346 codegen_->Store(value_type, value, destination);
Calin Juravle77520bc2015-01-12 18:45:46 +00001347 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001348 }
1349}
1350
Alexandre Rames67555f72014-11-18 10:55:16 +00001351void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1352 LocationSummary* locations =
1353 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1354 locations->SetInAt(0, Location::RequiresRegister());
1355 locations->SetInAt(1, Location::RequiresRegister());
1356 if (instruction->HasUses()) {
1357 locations->SetOut(Location::SameAsFirstInput());
1358 }
1359}
1360
1361void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001362 LocationSummary* locations = instruction->GetLocations();
1363 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1364 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001365 codegen_->AddSlowPath(slow_path);
1366
1367 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1368 __ B(slow_path->GetEntryLabel(), hs);
1369}
1370
1371void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1372 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1373 instruction, LocationSummary::kCallOnSlowPath);
1374 locations->SetInAt(0, Location::RequiresRegister());
1375 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001376 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001377}
1378
1379void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001380 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001381 Register obj = InputRegisterAt(instruction, 0);;
1382 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001383 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001384
Alexandre Rames3e69f162014-12-10 10:36:50 +00001385 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1386 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001387 codegen_->AddSlowPath(slow_path);
1388
1389 // TODO: avoid this check if we know obj is not null.
1390 __ Cbz(obj, slow_path->GetExitLabel());
1391 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001392 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
1393 __ Cmp(obj_cls, cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00001394 __ B(ne, slow_path->GetEntryLabel());
1395 __ Bind(slow_path->GetExitLabel());
1396}
1397
1398void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1399 LocationSummary* locations =
1400 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1401 locations->SetInAt(0, Location::RequiresRegister());
1402 if (check->HasUses()) {
1403 locations->SetOut(Location::SameAsFirstInput());
1404 }
1405}
1406
1407void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1408 // We assume the class is not null.
1409 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1410 check->GetLoadClass(), check, check->GetDexPc(), true);
1411 codegen_->AddSlowPath(slow_path);
1412 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1413}
1414
Serban Constantinescu02164b32014-11-13 14:05:07 +00001415void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001416 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001417 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1418 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001419 switch (in_type) {
1420 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001421 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001422 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001423 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1424 break;
1425 }
1426 case Primitive::kPrimFloat:
1427 case Primitive::kPrimDouble: {
1428 locations->SetInAt(0, Location::RequiresFpuRegister());
Alexandre Rames93415462015-02-17 15:08:20 +00001429 HInstruction* right = compare->InputAt(1);
1430 if ((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1431 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0))) {
1432 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1433 } else {
1434 locations->SetInAt(1, Location::RequiresFpuRegister());
1435 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001436 locations->SetOut(Location::RequiresRegister());
1437 break;
1438 }
1439 default:
1440 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1441 }
1442}
1443
1444void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1445 Primitive::Type in_type = compare->InputAt(0)->GetType();
1446
1447 // 0 if: left == right
1448 // 1 if: left > right
1449 // -1 if: left < right
1450 switch (in_type) {
1451 case Primitive::kPrimLong: {
1452 Register result = OutputRegister(compare);
1453 Register left = InputRegisterAt(compare, 0);
1454 Operand right = InputOperandAt(compare, 1);
1455
1456 __ Cmp(left, right);
1457 __ Cset(result, ne);
1458 __ Cneg(result, result, lt);
1459 break;
1460 }
1461 case Primitive::kPrimFloat:
1462 case Primitive::kPrimDouble: {
1463 Register result = OutputRegister(compare);
1464 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001465 if (compare->GetLocations()->InAt(1).IsConstant()) {
1466 if (kIsDebugBuild) {
1467 HInstruction* right = compare->GetLocations()->InAt(1).GetConstant();
1468 DCHECK((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1469 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0)));
1470 }
1471 // 0.0 is the only immediate that can be encoded directly in a FCMP instruction.
1472 __ Fcmp(left, 0.0);
1473 } else {
1474 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1475 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001476 if (compare->IsGtBias()) {
1477 __ Cset(result, ne);
1478 } else {
1479 __ Csetm(result, ne);
1480 }
1481 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001482 break;
1483 }
1484 default:
1485 LOG(FATAL) << "Unimplemented compare type " << in_type;
1486 }
1487}
1488
1489void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1490 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1491 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001492 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames5319def2014-10-23 10:03:10 +01001493 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001494 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001495 }
1496}
1497
1498void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1499 if (!instruction->NeedsMaterialization()) {
1500 return;
1501 }
1502
1503 LocationSummary* locations = instruction->GetLocations();
1504 Register lhs = InputRegisterAt(instruction, 0);
1505 Operand rhs = InputOperandAt(instruction, 1);
1506 Register res = RegisterFrom(locations->Out(), instruction->GetType());
1507 Condition cond = ARM64Condition(instruction->GetCondition());
1508
1509 __ Cmp(lhs, rhs);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001510 __ Cset(res, cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001511}
1512
1513#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1514 M(Equal) \
1515 M(NotEqual) \
1516 M(LessThan) \
1517 M(LessThanOrEqual) \
1518 M(GreaterThan) \
1519 M(GreaterThanOrEqual)
1520#define DEFINE_CONDITION_VISITORS(Name) \
1521void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1522void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1523FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001524#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001525#undef FOR_EACH_CONDITION_INSTRUCTION
1526
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001527void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1528 LocationSummary* locations =
1529 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1530 switch (div->GetResultType()) {
1531 case Primitive::kPrimInt:
1532 case Primitive::kPrimLong:
1533 locations->SetInAt(0, Location::RequiresRegister());
1534 locations->SetInAt(1, Location::RequiresRegister());
1535 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1536 break;
1537
1538 case Primitive::kPrimFloat:
1539 case Primitive::kPrimDouble:
1540 locations->SetInAt(0, Location::RequiresFpuRegister());
1541 locations->SetInAt(1, Location::RequiresFpuRegister());
1542 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1543 break;
1544
1545 default:
1546 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1547 }
1548}
1549
1550void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1551 Primitive::Type type = div->GetResultType();
1552 switch (type) {
1553 case Primitive::kPrimInt:
1554 case Primitive::kPrimLong:
1555 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
1556 break;
1557
1558 case Primitive::kPrimFloat:
1559 case Primitive::kPrimDouble:
1560 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1561 break;
1562
1563 default:
1564 LOG(FATAL) << "Unexpected div type " << type;
1565 }
1566}
1567
Alexandre Rames67555f72014-11-18 10:55:16 +00001568void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1569 LocationSummary* locations =
1570 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1571 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1572 if (instruction->HasUses()) {
1573 locations->SetOut(Location::SameAsFirstInput());
1574 }
1575}
1576
1577void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1578 SlowPathCodeARM64* slow_path =
1579 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1580 codegen_->AddSlowPath(slow_path);
1581 Location value = instruction->GetLocations()->InAt(0);
1582
Alexandre Rames3e69f162014-12-10 10:36:50 +00001583 Primitive::Type type = instruction->GetType();
1584
1585 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
1586 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
1587 return;
1588 }
1589
Alexandre Rames67555f72014-11-18 10:55:16 +00001590 if (value.IsConstant()) {
1591 int64_t divisor = Int64ConstantFrom(value);
1592 if (divisor == 0) {
1593 __ B(slow_path->GetEntryLabel());
1594 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001595 // A division by a non-null constant is valid. We don't need to perform
1596 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00001597 }
1598 } else {
1599 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
1600 }
1601}
1602
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001603void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1604 LocationSummary* locations =
1605 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1606 locations->SetOut(Location::ConstantLocation(constant));
1607}
1608
1609void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1610 UNUSED(constant);
1611 // Will be generated at use site.
1612}
1613
Alexandre Rames5319def2014-10-23 10:03:10 +01001614void LocationsBuilderARM64::VisitExit(HExit* exit) {
1615 exit->SetLocations(nullptr);
1616}
1617
1618void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001619 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01001620}
1621
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001622void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
1623 LocationSummary* locations =
1624 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1625 locations->SetOut(Location::ConstantLocation(constant));
1626}
1627
1628void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
1629 UNUSED(constant);
1630 // Will be generated at use site.
1631}
1632
Alexandre Rames5319def2014-10-23 10:03:10 +01001633void LocationsBuilderARM64::VisitGoto(HGoto* got) {
1634 got->SetLocations(nullptr);
1635}
1636
1637void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
1638 HBasicBlock* successor = got->GetSuccessor();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001639 DCHECK(!successor->IsExitBlock());
1640 HBasicBlock* block = got->GetBlock();
1641 HInstruction* previous = got->GetPrevious();
1642 HLoopInformation* info = block->GetLoopInformation();
1643
1644 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
1645 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1646 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1647 return;
1648 }
1649 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1650 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1651 }
1652 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001653 __ B(codegen_->GetLabelOf(successor));
1654 }
1655}
1656
Mingyao Yange295e6e2015-03-07 06:37:59 -08001657void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
1658 vixl::Label* true_target,
1659 vixl::Label* false_target,
1660 vixl::Label* always_true_target) {
1661 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001662 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01001663
Serban Constantinescu02164b32014-11-13 14:05:07 +00001664 if (cond->IsIntConstant()) {
1665 int32_t cond_value = cond->AsIntConstant()->GetValue();
1666 if (cond_value == 1) {
Mingyao Yange295e6e2015-03-07 06:37:59 -08001667 if (always_true_target != nullptr) {
1668 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001669 }
1670 return;
1671 } else {
1672 DCHECK_EQ(cond_value, 0);
1673 }
1674 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001675 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yange295e6e2015-03-07 06:37:59 -08001676 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001677 DCHECK(cond_val.IsRegister());
Mingyao Yange295e6e2015-03-07 06:37:59 -08001678 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001679 } else {
1680 // The condition instruction has not been materialized, use its inputs as
1681 // the comparison and its condition as the branch condition.
1682 Register lhs = InputRegisterAt(condition, 0);
1683 Operand rhs = InputOperandAt(condition, 1);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001684 Condition arm64_cond = ARM64Condition(condition->GetCondition());
1685 if ((arm64_cond == eq || arm64_cond == ne) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
1686 if (arm64_cond == eq) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001687 __ Cbz(lhs, true_target);
1688 } else {
1689 __ Cbnz(lhs, true_target);
1690 }
1691 } else {
1692 __ Cmp(lhs, rhs);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001693 __ B(arm64_cond, true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001694 }
1695 }
Mingyao Yange295e6e2015-03-07 06:37:59 -08001696 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001697 __ B(false_target);
1698 }
1699}
1700
Mingyao Yange295e6e2015-03-07 06:37:59 -08001701void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
1702 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1703 HInstruction* cond = if_instr->InputAt(0);
1704 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1705 locations->SetInAt(0, Location::RequiresRegister());
1706 }
1707}
1708
1709void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
1710 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1711 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1712 vixl::Label* always_true_target = true_target;
1713 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1714 if_instr->IfTrueSuccessor())) {
1715 always_true_target = nullptr;
1716 }
1717 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1718 if_instr->IfFalseSuccessor())) {
1719 false_target = nullptr;
1720 }
1721 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1722}
1723
1724void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
1725 LocationSummary* locations = new (GetGraph()->GetArena())
1726 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1727 HInstruction* cond = deoptimize->InputAt(0);
1728 DCHECK(cond->IsCondition());
1729 if (cond->AsCondition()->NeedsMaterialization()) {
1730 locations->SetInAt(0, Location::RequiresRegister());
1731 }
1732}
1733
1734void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
1735 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
1736 DeoptimizationSlowPathARM64(deoptimize);
1737 codegen_->AddSlowPath(slow_path);
1738 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
1739 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1740}
1741
Alexandre Rames5319def2014-10-23 10:03:10 +01001742void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001743 LocationSummary* locations =
1744 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames5319def2014-10-23 10:03:10 +01001745 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001746 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001747}
1748
1749void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001750 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), instruction->GetFieldOffset());
Serban Constantinescu579885a2015-02-22 20:51:33 +00001751 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001752
1753 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00001754 if (use_acquire_release) {
Calin Juravle77520bc2015-01-12 18:45:46 +00001755 // NB: LoadAcquire will record the pc info if needed.
1756 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001757 } else {
1758 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
Calin Juravle77520bc2015-01-12 18:45:46 +00001759 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001760 // For IRIW sequential consistency kLoadAny is not sufficient.
1761 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1762 }
1763 } else {
1764 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
Calin Juravle77520bc2015-01-12 18:45:46 +00001765 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001766 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001767}
1768
1769void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001770 LocationSummary* locations =
1771 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames5319def2014-10-23 10:03:10 +01001772 locations->SetInAt(0, Location::RequiresRegister());
1773 locations->SetInAt(1, Location::RequiresRegister());
1774}
1775
1776void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001777 Register obj = InputRegisterAt(instruction, 0);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001778 CPURegister value = InputCPURegisterAt(instruction, 1);
1779 Offset offset = instruction->GetFieldOffset();
1780 Primitive::Type field_type = instruction->GetFieldType();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001781 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001782
1783 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00001784 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001785 codegen_->StoreRelease(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001786 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001787 } else {
1788 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1789 codegen_->Store(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001790 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001791 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1792 }
1793 } else {
1794 codegen_->Store(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001795 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001796 }
1797
1798 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001799 codegen_->MarkGCCard(obj, Register(value));
Alexandre Rames5319def2014-10-23 10:03:10 +01001800 }
1801}
1802
Alexandre Rames67555f72014-11-18 10:55:16 +00001803void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
1804 LocationSummary::CallKind call_kind =
1805 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
1806 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1807 locations->SetInAt(0, Location::RequiresRegister());
1808 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001809 // The output does overlap inputs.
1810 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00001811}
1812
1813void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
1814 LocationSummary* locations = instruction->GetLocations();
1815 Register obj = InputRegisterAt(instruction, 0);;
1816 Register cls = InputRegisterAt(instruction, 1);;
1817 Register out = OutputRegister(instruction);
1818
1819 vixl::Label done;
1820
1821 // Return 0 if `obj` is null.
1822 // TODO: Avoid this check if we know `obj` is not null.
1823 __ Mov(out, 0);
1824 __ Cbz(obj, &done);
1825
1826 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00001827 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Alexandre Rames67555f72014-11-18 10:55:16 +00001828 __ Cmp(out, cls);
1829 if (instruction->IsClassFinal()) {
1830 // Classes must be equal for the instanceof to succeed.
1831 __ Cset(out, eq);
1832 } else {
1833 // If the classes are not equal, we go into a slow path.
1834 DCHECK(locations->OnlyCallsOnSlowPath());
1835 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00001836 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1837 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001838 codegen_->AddSlowPath(slow_path);
1839 __ B(ne, slow_path->GetEntryLabel());
1840 __ Mov(out, 1);
1841 __ Bind(slow_path->GetExitLabel());
1842 }
1843
1844 __ Bind(&done);
1845}
1846
Alexandre Rames5319def2014-10-23 10:03:10 +01001847void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
1848 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
1849 locations->SetOut(Location::ConstantLocation(constant));
1850}
1851
1852void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
1853 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001854 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01001855}
1856
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001857void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
1858 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
1859 locations->SetOut(Location::ConstantLocation(constant));
1860}
1861
1862void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
1863 // Will be generated at use site.
1864 UNUSED(constant);
1865}
1866
Alexandre Rames5319def2014-10-23 10:03:10 +01001867void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
1868 LocationSummary* locations =
1869 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
1870 locations->AddTemp(LocationFrom(x0));
1871
1872 InvokeDexCallingConventionVisitor calling_convention_visitor;
1873 for (size_t i = 0; i < invoke->InputCount(); i++) {
1874 HInstruction* input = invoke->InputAt(i);
1875 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1876 }
1877
1878 Primitive::Type return_type = invoke->GetType();
1879 if (return_type != Primitive::kPrimVoid) {
1880 locations->SetOut(calling_convention_visitor.GetReturnLocation(return_type));
1881 }
1882}
1883
Alexandre Rames67555f72014-11-18 10:55:16 +00001884void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1885 HandleInvoke(invoke);
1886}
1887
1888void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1889 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1890 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
1891 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1892 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1893 Location receiver = invoke->GetLocations()->InAt(0);
1894 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001895 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00001896
1897 // The register ip1 is required to be used for the hidden argument in
1898 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
1899 UseScratchRegisterScope scratch_scope(GetVIXLAssembler());
1900 scratch_scope.Exclude(ip1);
1901 __ Mov(ip1, invoke->GetDexMethodIndex());
1902
1903 // temp = object->GetClass();
1904 if (receiver.IsStackSlot()) {
1905 __ Ldr(temp, StackOperandFrom(receiver));
1906 __ Ldr(temp, HeapOperand(temp, class_offset));
1907 } else {
1908 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
1909 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001910 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00001911 // temp = temp->GetImtEntryAt(method_offset);
1912 __ Ldr(temp, HeapOperand(temp, method_offset));
1913 // lr = temp->GetEntryPoint();
1914 __ Ldr(lr, HeapOperand(temp, entry_point));
1915 // lr();
1916 __ Blr(lr);
1917 DCHECK(!codegen_->IsLeafMethod());
1918 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1919}
1920
1921void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001922 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1923 if (intrinsic.TryDispatch(invoke)) {
1924 return;
1925 }
1926
Alexandre Rames67555f72014-11-18 10:55:16 +00001927 HandleInvoke(invoke);
1928}
1929
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001930void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001931 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1932 if (intrinsic.TryDispatch(invoke)) {
1933 return;
1934 }
1935
Alexandre Rames67555f72014-11-18 10:55:16 +00001936 HandleInvoke(invoke);
1937}
1938
Andreas Gampe878d58c2015-01-15 23:24:00 -08001939static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
1940 if (invoke->GetLocations()->Intrinsified()) {
1941 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
1942 intrinsic.Dispatch(invoke);
1943 return true;
1944 }
1945 return false;
1946}
1947
1948void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Register temp) {
1949 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
1950 DCHECK(temp.Is(kArtMethodRegister));
Alexandre Rames5319def2014-10-23 10:03:10 +01001951 size_t index_in_cache = mirror::Array::DataOffset(kHeapRefSize).SizeValue() +
Andreas Gampe878d58c2015-01-15 23:24:00 -08001952 invoke->GetDexMethodIndex() * kHeapRefSize;
Alexandre Rames5319def2014-10-23 10:03:10 +01001953
1954 // TODO: Implement all kinds of calls:
1955 // 1) boot -> boot
1956 // 2) app -> boot
1957 // 3) app -> app
1958 //
1959 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1960
Nicolas Geoffray0a299b92015-01-29 11:39:44 +00001961 // temp = method;
1962 LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001963 if (!invoke->IsRecursive()) {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001964 // temp = temp->dex_cache_resolved_methods_;
1965 __ Ldr(temp, HeapOperand(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset()));
1966 // temp = temp[index_in_cache];
1967 __ Ldr(temp, HeapOperand(temp, index_in_cache));
1968 // lr = temp->entry_point_from_quick_compiled_code_;
1969 __ Ldr(lr, HeapOperand(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
1970 kArm64WordSize)));
1971 // lr();
1972 __ Blr(lr);
1973 } else {
1974 __ Bl(&frame_entry_label_);
1975 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001976
Andreas Gampe878d58c2015-01-15 23:24:00 -08001977 DCHECK(!IsLeafMethod());
1978}
1979
1980void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
1981 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1982 return;
1983 }
1984
1985 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
1986 codegen_->GenerateStaticOrDirectCall(invoke, temp);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001987 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01001988}
1989
1990void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001991 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1992 return;
1993 }
1994
Alexandre Rames5319def2014-10-23 10:03:10 +01001995 LocationSummary* locations = invoke->GetLocations();
1996 Location receiver = locations->InAt(0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001997 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01001998 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1999 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
2000 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00002001 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002002
2003 // temp = object->GetClass();
2004 if (receiver.IsStackSlot()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002005 __ Ldr(temp, MemOperand(sp, receiver.GetStackIndex()));
2006 __ Ldr(temp, HeapOperand(temp, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002007 } else {
2008 DCHECK(receiver.IsRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002009 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002010 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002011 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames5319def2014-10-23 10:03:10 +01002012 // temp = temp->GetMethodAt(method_offset);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002013 __ Ldr(temp, HeapOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002014 // lr = temp->GetEntryPoint();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002015 __ Ldr(lr, HeapOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002016 // lr();
2017 __ Blr(lr);
2018 DCHECK(!codegen_->IsLeafMethod());
2019 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2020}
2021
Alexandre Rames67555f72014-11-18 10:55:16 +00002022void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2023 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2024 : LocationSummary::kNoCall;
2025 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
2026 locations->SetOut(Location::RequiresRegister());
2027}
2028
2029void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2030 Register out = OutputRegister(cls);
2031 if (cls->IsReferrersClass()) {
2032 DCHECK(!cls->CanCallRuntime());
2033 DCHECK(!cls->MustGenerateClinitCheck());
2034 codegen_->LoadCurrentMethod(out);
2035 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
2036 } else {
2037 DCHECK(cls->CanCallRuntime());
2038 codegen_->LoadCurrentMethod(out);
2039 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DexCacheResolvedTypesOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002040 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002041
2042 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2043 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2044 codegen_->AddSlowPath(slow_path);
2045 __ Cbz(out, slow_path->GetEntryLabel());
2046 if (cls->MustGenerateClinitCheck()) {
2047 GenerateClassInitializationCheck(slow_path, out);
2048 } else {
2049 __ Bind(slow_path->GetExitLabel());
2050 }
2051 }
2052}
2053
2054void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2055 LocationSummary* locations =
2056 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2057 locations->SetOut(Location::RequiresRegister());
2058}
2059
2060void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
2061 MemOperand exception = MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2062 __ Ldr(OutputRegister(instruction), exception);
2063 __ Str(wzr, exception);
2064}
2065
Alexandre Rames5319def2014-10-23 10:03:10 +01002066void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2067 load->SetLocations(nullptr);
2068}
2069
2070void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2071 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002072 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002073}
2074
Alexandre Rames67555f72014-11-18 10:55:16 +00002075void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2076 LocationSummary* locations =
2077 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2078 locations->SetOut(Location::RequiresRegister());
2079}
2080
2081void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2082 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2083 codegen_->AddSlowPath(slow_path);
2084
2085 Register out = OutputRegister(load);
2086 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08002087 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
2088 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002089 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002090 __ Cbz(out, slow_path->GetEntryLabel());
2091 __ Bind(slow_path->GetExitLabel());
2092}
2093
Alexandre Rames5319def2014-10-23 10:03:10 +01002094void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2095 local->SetLocations(nullptr);
2096}
2097
2098void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2099 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2100}
2101
2102void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2103 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2104 locations->SetOut(Location::ConstantLocation(constant));
2105}
2106
2107void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2108 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002109 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002110}
2111
Alexandre Rames67555f72014-11-18 10:55:16 +00002112void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2113 LocationSummary* locations =
2114 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2115 InvokeRuntimeCallingConvention calling_convention;
2116 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2117}
2118
2119void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2120 codegen_->InvokeRuntime(instruction->IsEnter()
2121 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2122 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002123 instruction->GetDexPc(),
2124 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002125 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002126}
2127
Alexandre Rames42d641b2014-10-27 14:00:51 +00002128void LocationsBuilderARM64::VisitMul(HMul* mul) {
2129 LocationSummary* locations =
2130 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2131 switch (mul->GetResultType()) {
2132 case Primitive::kPrimInt:
2133 case Primitive::kPrimLong:
2134 locations->SetInAt(0, Location::RequiresRegister());
2135 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002136 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002137 break;
2138
2139 case Primitive::kPrimFloat:
2140 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002141 locations->SetInAt(0, Location::RequiresFpuRegister());
2142 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002143 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002144 break;
2145
2146 default:
2147 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2148 }
2149}
2150
2151void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2152 switch (mul->GetResultType()) {
2153 case Primitive::kPrimInt:
2154 case Primitive::kPrimLong:
2155 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2156 break;
2157
2158 case Primitive::kPrimFloat:
2159 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002160 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002161 break;
2162
2163 default:
2164 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2165 }
2166}
2167
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002168void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2169 LocationSummary* locations =
2170 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2171 switch (neg->GetResultType()) {
2172 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002173 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002174 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002175 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002176 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002177
2178 case Primitive::kPrimFloat:
2179 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002180 locations->SetInAt(0, Location::RequiresFpuRegister());
2181 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002182 break;
2183
2184 default:
2185 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2186 }
2187}
2188
2189void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2190 switch (neg->GetResultType()) {
2191 case Primitive::kPrimInt:
2192 case Primitive::kPrimLong:
2193 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2194 break;
2195
2196 case Primitive::kPrimFloat:
2197 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002198 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002199 break;
2200
2201 default:
2202 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2203 }
2204}
2205
2206void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2207 LocationSummary* locations =
2208 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2209 InvokeRuntimeCallingConvention calling_convention;
2210 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002211 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002212 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002213 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2214 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2215 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002216}
2217
2218void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2219 LocationSummary* locations = instruction->GetLocations();
2220 InvokeRuntimeCallingConvention calling_convention;
2221 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2222 DCHECK(type_index.Is(w0));
2223 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002224 DCHECK(current_method.Is(w2));
Alexandre Rames67555f72014-11-18 10:55:16 +00002225 codegen_->LoadCurrentMethod(current_method);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002226 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002227 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002228 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2229 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002230 instruction->GetDexPc(),
2231 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002232 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2233 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002234}
2235
Alexandre Rames5319def2014-10-23 10:03:10 +01002236void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2237 LocationSummary* locations =
2238 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2239 InvokeRuntimeCallingConvention calling_convention;
2240 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2241 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2242 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002243 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002244}
2245
2246void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2247 LocationSummary* locations = instruction->GetLocations();
2248 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2249 DCHECK(type_index.Is(w0));
2250 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
2251 DCHECK(current_method.Is(w1));
Alexandre Rames67555f72014-11-18 10:55:16 +00002252 codegen_->LoadCurrentMethod(current_method);
Alexandre Rames5319def2014-10-23 10:03:10 +01002253 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002254 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002255 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2256 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002257 instruction->GetDexPc(),
2258 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002259 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002260}
2261
2262void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2263 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002264 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002265 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002266}
2267
2268void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002269 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002270 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002271 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002272 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002273 break;
2274
2275 default:
2276 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2277 }
2278}
2279
2280void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2281 LocationSummary* locations =
2282 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2283 locations->SetInAt(0, Location::RequiresRegister());
2284 if (instruction->HasUses()) {
2285 locations->SetOut(Location::SameAsFirstInput());
2286 }
2287}
2288
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002289void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002290 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2291 return;
2292 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002293 Location obj = instruction->GetLocations()->InAt(0);
2294
2295 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2296 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2297}
2298
2299void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002300 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2301 codegen_->AddSlowPath(slow_path);
2302
2303 LocationSummary* locations = instruction->GetLocations();
2304 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002305
2306 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002307}
2308
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002309void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2310 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2311 GenerateImplicitNullCheck(instruction);
2312 } else {
2313 GenerateExplicitNullCheck(instruction);
2314 }
2315}
2316
Alexandre Rames67555f72014-11-18 10:55:16 +00002317void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2318 HandleBinaryOp(instruction);
2319}
2320
2321void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2322 HandleBinaryOp(instruction);
2323}
2324
Alexandre Rames3e69f162014-12-10 10:36:50 +00002325void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2326 LOG(FATAL) << "Unreachable";
2327}
2328
2329void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2330 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2331}
2332
Alexandre Rames5319def2014-10-23 10:03:10 +01002333void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2334 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2335 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2336 if (location.IsStackSlot()) {
2337 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2338 } else if (location.IsDoubleStackSlot()) {
2339 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2340 }
2341 locations->SetOut(location);
2342}
2343
2344void InstructionCodeGeneratorARM64::VisitParameterValue(HParameterValue* instruction) {
2345 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002346 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002347}
2348
2349void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2350 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2351 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2352 locations->SetInAt(i, Location::Any());
2353 }
2354 locations->SetOut(Location::Any());
2355}
2356
2357void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002358 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002359 LOG(FATAL) << "Unreachable";
2360}
2361
Serban Constantinescu02164b32014-11-13 14:05:07 +00002362void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002363 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002364 LocationSummary::CallKind call_kind =
2365 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002366 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2367
2368 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002369 case Primitive::kPrimInt:
2370 case Primitive::kPrimLong:
2371 locations->SetInAt(0, Location::RequiresRegister());
2372 locations->SetInAt(1, Location::RequiresRegister());
2373 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2374 break;
2375
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002376 case Primitive::kPrimFloat:
2377 case Primitive::kPrimDouble: {
2378 InvokeRuntimeCallingConvention calling_convention;
2379 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2380 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2381 locations->SetOut(calling_convention.GetReturnLocation(type));
2382
2383 break;
2384 }
2385
Serban Constantinescu02164b32014-11-13 14:05:07 +00002386 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002387 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002388 }
2389}
2390
2391void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2392 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002393
Serban Constantinescu02164b32014-11-13 14:05:07 +00002394 switch (type) {
2395 case Primitive::kPrimInt:
2396 case Primitive::kPrimLong: {
2397 UseScratchRegisterScope temps(GetVIXLAssembler());
2398 Register dividend = InputRegisterAt(rem, 0);
2399 Register divisor = InputRegisterAt(rem, 1);
2400 Register output = OutputRegister(rem);
2401 Register temp = temps.AcquireSameSizeAs(output);
2402
2403 __ Sdiv(temp, dividend, divisor);
2404 __ Msub(output, temp, divisor, dividend);
2405 break;
2406 }
2407
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002408 case Primitive::kPrimFloat:
2409 case Primitive::kPrimDouble: {
2410 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2411 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002412 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002413 break;
2414 }
2415
Serban Constantinescu02164b32014-11-13 14:05:07 +00002416 default:
2417 LOG(FATAL) << "Unexpected rem type " << type;
2418 }
2419}
2420
Alexandre Rames5319def2014-10-23 10:03:10 +01002421void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2422 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2423 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002424 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002425}
2426
2427void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002428 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002429 codegen_->GenerateFrameExit();
Alexandre Rames3e69f162014-12-10 10:36:50 +00002430 __ Ret();
Alexandre Rames5319def2014-10-23 10:03:10 +01002431}
2432
2433void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2434 instruction->SetLocations(nullptr);
2435}
2436
2437void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002438 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002439 codegen_->GenerateFrameExit();
Alexandre Rames3e69f162014-12-10 10:36:50 +00002440 __ Ret();
Alexandre Rames5319def2014-10-23 10:03:10 +01002441}
2442
Serban Constantinescu02164b32014-11-13 14:05:07 +00002443void LocationsBuilderARM64::VisitShl(HShl* shl) {
2444 HandleShift(shl);
2445}
2446
2447void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2448 HandleShift(shl);
2449}
2450
2451void LocationsBuilderARM64::VisitShr(HShr* shr) {
2452 HandleShift(shr);
2453}
2454
2455void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2456 HandleShift(shr);
2457}
2458
Alexandre Rames5319def2014-10-23 10:03:10 +01002459void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2460 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2461 Primitive::Type field_type = store->InputAt(1)->GetType();
2462 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002463 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002464 case Primitive::kPrimBoolean:
2465 case Primitive::kPrimByte:
2466 case Primitive::kPrimChar:
2467 case Primitive::kPrimShort:
2468 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002469 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002470 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2471 break;
2472
2473 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002474 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002475 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2476 break;
2477
2478 default:
2479 LOG(FATAL) << "Unimplemented local type " << field_type;
2480 }
2481}
2482
2483void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002484 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002485}
2486
2487void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002488 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002489}
2490
2491void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002492 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002493}
2494
Alexandre Rames67555f72014-11-18 10:55:16 +00002495void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2496 LocationSummary* locations =
2497 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2498 locations->SetInAt(0, Location::RequiresRegister());
2499 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2500}
2501
2502void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002503 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), instruction->GetFieldOffset());
Serban Constantinescu579885a2015-02-22 20:51:33 +00002504 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002505
2506 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00002507 if (use_acquire_release) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002508 // NB: LoadAcquire will record the pc info if needed.
2509 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002510 } else {
2511 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
2512 // For IRIW sequential consistency kLoadAny is not sufficient.
2513 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2514 }
2515 } else {
2516 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
2517 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002518}
2519
2520void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002521 LocationSummary* locations =
2522 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2523 locations->SetInAt(0, Location::RequiresRegister());
2524 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01002525}
2526
Alexandre Rames67555f72014-11-18 10:55:16 +00002527void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002528 Register cls = InputRegisterAt(instruction, 0);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002529 CPURegister value = InputCPURegisterAt(instruction, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002530 Offset offset = instruction->GetFieldOffset();
Alexandre Rames67555f72014-11-18 10:55:16 +00002531 Primitive::Type field_type = instruction->GetFieldType();
Serban Constantinescu579885a2015-02-22 20:51:33 +00002532 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Alexandre Rames5319def2014-10-23 10:03:10 +01002533
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002534 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00002535 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002536 codegen_->StoreRelease(field_type, value, HeapOperand(cls, offset));
2537 } else {
2538 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2539 codegen_->Store(field_type, value, HeapOperand(cls, offset));
2540 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2541 }
2542 } else {
2543 codegen_->Store(field_type, value, HeapOperand(cls, offset));
2544 }
2545
2546 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002547 codegen_->MarkGCCard(cls, Register(value));
2548 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002549}
2550
2551void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2552 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2553}
2554
2555void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002556 HBasicBlock* block = instruction->GetBlock();
2557 if (block->GetLoopInformation() != nullptr) {
2558 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2559 // The back edge will generate the suspend check.
2560 return;
2561 }
2562 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2563 // The goto will generate the suspend check.
2564 return;
2565 }
2566 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01002567}
2568
2569void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
2570 temp->SetLocations(nullptr);
2571}
2572
2573void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
2574 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002575 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01002576}
2577
Alexandre Rames67555f72014-11-18 10:55:16 +00002578void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
2579 LocationSummary* locations =
2580 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2581 InvokeRuntimeCallingConvention calling_convention;
2582 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2583}
2584
2585void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
2586 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002587 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002588 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002589}
2590
2591void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
2592 LocationSummary* locations =
2593 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2594 Primitive::Type input_type = conversion->GetInputType();
2595 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002596 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00002597 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
2598 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
2599 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
2600 }
2601
Alexandre Rames542361f2015-01-29 16:57:31 +00002602 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002603 locations->SetInAt(0, Location::RequiresFpuRegister());
2604 } else {
2605 locations->SetInAt(0, Location::RequiresRegister());
2606 }
2607
Alexandre Rames542361f2015-01-29 16:57:31 +00002608 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002609 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2610 } else {
2611 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2612 }
2613}
2614
2615void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
2616 Primitive::Type result_type = conversion->GetResultType();
2617 Primitive::Type input_type = conversion->GetInputType();
2618
2619 DCHECK_NE(input_type, result_type);
2620
Alexandre Rames542361f2015-01-29 16:57:31 +00002621 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002622 int result_size = Primitive::ComponentSize(result_type);
2623 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002624 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002625 Register output = OutputRegister(conversion);
2626 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002627 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
2628 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
2629 } else if ((result_type == Primitive::kPrimChar) ||
2630 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
2631 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002632 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002633 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002634 }
Alexandre Rames542361f2015-01-29 16:57:31 +00002635 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002636 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002637 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002638 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
2639 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002640 } else if (Primitive::IsFloatingPointType(result_type) &&
2641 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002642 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
2643 } else {
2644 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
2645 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00002646 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002647}
Alexandre Rames67555f72014-11-18 10:55:16 +00002648
Serban Constantinescu02164b32014-11-13 14:05:07 +00002649void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
2650 HandleShift(ushr);
2651}
2652
2653void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
2654 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00002655}
2656
2657void LocationsBuilderARM64::VisitXor(HXor* instruction) {
2658 HandleBinaryOp(instruction);
2659}
2660
2661void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
2662 HandleBinaryOp(instruction);
2663}
2664
Calin Juravleb1498f62015-02-16 13:13:29 +00002665void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
2666 // Nothing to do, this should be removed during prepare for register allocator.
2667 UNUSED(instruction);
2668 LOG(FATAL) << "Unreachable";
2669}
2670
2671void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
2672 // Nothing to do, this should be removed during prepare for register allocator.
2673 UNUSED(instruction);
2674 LOG(FATAL) << "Unreachable";
2675}
2676
Alexandre Rames67555f72014-11-18 10:55:16 +00002677#undef __
2678#undef QUICK_ENTRY_POINT
2679
Alexandre Rames5319def2014-10-23 10:03:10 +01002680} // namespace arm64
2681} // namespace art