blob: a13800dc874528b3d40f53fb1fae64b99b6d28cb [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 +0100101Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000102 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100103}
104
Alexandre Rames67555f72014-11-18 10:55:16 +0000105#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
106#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100107
Alexandre Rames5319def2014-10-23 10:03:10 +0100108class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
109 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000110 BoundsCheckSlowPathARM64(HBoundsCheck* instruction,
111 Location index_location,
112 Location length_location)
113 : instruction_(instruction),
114 index_location_(index_location),
115 length_location_(length_location) {}
116
Alexandre Rames5319def2014-10-23 10:03:10 +0100117
Alexandre Rames67555f72014-11-18 10:55:16 +0000118 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000119 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100120 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000121 // We're moving two locations to locations that could overlap, so we need a parallel
122 // move resolver.
123 InvokeRuntimeCallingConvention calling_convention;
124 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100125 index_location_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
126 length_location_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000127 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000128 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800129 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100130 }
131
132 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000133 HBoundsCheck* const instruction_;
134 const Location index_location_;
135 const Location length_location_;
136
Alexandre Rames5319def2014-10-23 10:03:10 +0100137 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
138};
139
Alexandre Rames67555f72014-11-18 10:55:16 +0000140class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
141 public:
142 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
143
144 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
145 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
146 __ Bind(GetEntryLabel());
147 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000148 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800149 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000150 }
151
152 private:
153 HDivZeroCheck* const instruction_;
154 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
155};
156
157class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
158 public:
159 LoadClassSlowPathARM64(HLoadClass* cls,
160 HInstruction* at,
161 uint32_t dex_pc,
162 bool do_clinit)
163 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
164 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
165 }
166
167 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
168 LocationSummary* locations = at_->GetLocations();
169 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
170
171 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000172 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000173
174 InvokeRuntimeCallingConvention calling_convention;
175 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
176 arm64_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1).W());
177 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
178 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000179 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800180 if (do_clinit_) {
181 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t, mirror::ArtMethod*>();
182 } else {
183 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t, mirror::ArtMethod*>();
184 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000185
186 // Move the class to the desired location.
187 Location out = locations->Out();
188 if (out.IsValid()) {
189 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
190 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000191 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000192 }
193
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000194 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000195 __ B(GetExitLabel());
196 }
197
198 private:
199 // The class this slow path will load.
200 HLoadClass* const cls_;
201
202 // The instruction where this slow path is happening.
203 // (Might be the load class or an initialization check).
204 HInstruction* const at_;
205
206 // The dex PC of `at_`.
207 const uint32_t dex_pc_;
208
209 // Whether to initialize the class.
210 const bool do_clinit_;
211
212 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
213};
214
215class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
216 public:
217 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
218
219 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
220 LocationSummary* locations = instruction_->GetLocations();
221 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
222 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
223
224 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000225 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000226
227 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800228 arm64_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1).W());
229 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000230 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000231 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800232 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000233 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000234 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000235
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000236 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000237 __ B(GetExitLabel());
238 }
239
240 private:
241 HLoadString* const instruction_;
242
243 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
244};
245
Alexandre Rames5319def2014-10-23 10:03:10 +0100246class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
247 public:
248 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
249
Alexandre Rames67555f72014-11-18 10:55:16 +0000250 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
251 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100252 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000253 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000254 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800255 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100256 }
257
258 private:
259 HNullCheck* const instruction_;
260
261 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
262};
263
264class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
265 public:
266 explicit SuspendCheckSlowPathARM64(HSuspendCheck* instruction,
267 HBasicBlock* successor)
268 : instruction_(instruction), successor_(successor) {}
269
Alexandre Rames67555f72014-11-18 10:55:16 +0000270 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
271 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100272 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000273 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000274 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000275 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800276 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000277 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000278 if (successor_ == nullptr) {
279 __ B(GetReturnLabel());
280 } else {
281 __ B(arm64_codegen->GetLabelOf(successor_));
282 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100283 }
284
285 vixl::Label* GetReturnLabel() {
286 DCHECK(successor_ == nullptr);
287 return &return_label_;
288 }
289
Alexandre Rames5319def2014-10-23 10:03:10 +0100290 private:
291 HSuspendCheck* const instruction_;
292 // If not null, the block to branch to after the suspend check.
293 HBasicBlock* const successor_;
294
295 // If `successor_` is null, the label to branch to after the suspend check.
296 vixl::Label return_label_;
297
298 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
299};
300
Alexandre Rames67555f72014-11-18 10:55:16 +0000301class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
302 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000303 TypeCheckSlowPathARM64(HInstruction* instruction,
304 Location class_to_check,
305 Location object_class,
306 uint32_t dex_pc)
307 : instruction_(instruction),
308 class_to_check_(class_to_check),
309 object_class_(object_class),
310 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000311
312 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000313 LocationSummary* locations = instruction_->GetLocations();
314 DCHECK(instruction_->IsCheckCast()
315 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
316 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
317
Alexandre Rames67555f72014-11-18 10:55:16 +0000318 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000319 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000320
321 // We're moving two locations to locations that could overlap, so we need a parallel
322 // move resolver.
323 InvokeRuntimeCallingConvention calling_convention;
324 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100325 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
326 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000327
328 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000329 arm64_codegen->InvokeRuntime(
330 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000331 Primitive::Type ret_type = instruction_->GetType();
332 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
333 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800334 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
335 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000336 } else {
337 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000338 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800339 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000340 }
341
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000342 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000343 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000344 }
345
346 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000347 HInstruction* const instruction_;
348 const Location class_to_check_;
349 const Location object_class_;
350 uint32_t dex_pc_;
351
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
353};
354
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700355class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
356 public:
357 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
358 : instruction_(instruction) {}
359
360 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
361 __ Bind(GetEntryLabel());
362 SaveLiveRegisters(codegen, instruction_->GetLocations());
363 DCHECK(instruction_->IsDeoptimize());
364 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
365 uint32_t dex_pc = deoptimize->GetDexPc();
366 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
367 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
368 }
369
370 private:
371 HInstruction* const instruction_;
372 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
373};
374
Alexandre Rames5319def2014-10-23 10:03:10 +0100375#undef __
376
377Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
378 Location next_location;
379 if (type == Primitive::kPrimVoid) {
380 LOG(FATAL) << "Unreachable type " << type;
381 }
382
Alexandre Rames542361f2015-01-29 16:57:31 +0000383 if (Primitive::IsFloatingPointType(type) &&
384 (fp_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000385 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(fp_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000386 } else if (!Primitive::IsFloatingPointType(type) &&
387 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000388 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
389 } else {
390 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000391 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
392 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100393 }
394
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000395 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000396 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100397 return next_location;
398}
399
Serban Constantinescu579885a2015-02-22 20:51:33 +0000400CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
401 const Arm64InstructionSetFeatures& isa_features,
402 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100403 : CodeGenerator(graph,
404 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000405 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000406 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000407 callee_saved_core_registers.list(),
408 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000409 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100410 block_labels_(nullptr),
411 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000412 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000413 move_resolver_(graph->GetArena(), this),
414 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000415 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000416 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000417}
Alexandre Rames5319def2014-10-23 10:03:10 +0100418
Alexandre Rames67555f72014-11-18 10:55:16 +0000419#undef __
420#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100421
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000422void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
423 // Ensure we emit the literal pool.
424 __ FinalizeCode();
425 CodeGenerator::Finalize(allocator);
426}
427
Alexandre Rames3e69f162014-12-10 10:36:50 +0000428void ParallelMoveResolverARM64::EmitMove(size_t index) {
429 MoveOperands* move = moves_.Get(index);
430 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
431}
432
433void ParallelMoveResolverARM64::EmitSwap(size_t index) {
434 MoveOperands* move = moves_.Get(index);
435 codegen_->SwapLocations(move->GetDestination(), move->GetSource());
436}
437
438void ParallelMoveResolverARM64::RestoreScratch(int reg) {
439 __ Pop(Register(VIXLRegCodeFromART(reg), kXRegSize));
440}
441
442void ParallelMoveResolverARM64::SpillScratch(int reg) {
443 __ Push(Register(VIXLRegCodeFromART(reg), kXRegSize));
444}
445
Alexandre Rames5319def2014-10-23 10:03:10 +0100446void CodeGeneratorARM64::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000447 __ Bind(&frame_entry_label_);
448
Serban Constantinescu02164b32014-11-13 14:05:07 +0000449 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
450 if (do_overflow_check) {
451 UseScratchRegisterScope temps(GetVIXLAssembler());
452 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000453 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000454 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000455 __ Ldr(wzr, MemOperand(temp, 0));
456 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000457 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100458
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000459 if (!HasEmptyFrame()) {
460 int frame_size = GetFrameSize();
461 // Stack layout:
462 // sp[frame_size - 8] : lr.
463 // ... : other preserved core registers.
464 // ... : other preserved fp registers.
465 // ... : reserved frame space.
466 // sp[0] : current method.
467 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100468 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800469 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
470 frame_size - GetCoreSpillSize());
471 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
472 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000473 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100474}
475
476void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100477 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000478 if (!HasEmptyFrame()) {
479 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800480 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
481 frame_size - FrameEntrySpillSize());
482 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
483 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000484 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100485 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000486 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100487 __ Ret();
488 GetAssembler()->cfi().RestoreState();
489 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100490}
491
492void CodeGeneratorARM64::Bind(HBasicBlock* block) {
493 __ Bind(GetLabelOf(block));
494}
495
Alexandre Rames5319def2014-10-23 10:03:10 +0100496void CodeGeneratorARM64::Move(HInstruction* instruction,
497 Location location,
498 HInstruction* move_for) {
499 LocationSummary* locations = instruction->GetLocations();
500 if (locations != nullptr && locations->Out().Equals(location)) {
501 return;
502 }
503
504 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000505 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100506
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000507 if (instruction->IsIntConstant()
508 || instruction->IsLongConstant()
509 || instruction->IsNullConstant()) {
510 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100511 if (location.IsRegister()) {
512 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000513 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100514 (instruction->IsLongConstant() && dst.Is64Bits()));
515 __ Mov(dst, value);
516 } else {
517 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000518 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000519 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
520 ? temps.AcquireW()
521 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100522 __ Mov(temp, value);
523 __ Str(temp, StackOperandFrom(location));
524 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000525 } else if (instruction->IsTemporary()) {
526 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000527 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100528 } else if (instruction->IsLoadLocal()) {
529 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000530 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000531 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000532 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000533 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100534 }
535
536 } else {
537 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000538 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100539 }
540}
541
Alexandre Rames5319def2014-10-23 10:03:10 +0100542Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
543 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000544
Alexandre Rames5319def2014-10-23 10:03:10 +0100545 switch (type) {
546 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000547 case Primitive::kPrimInt:
548 case Primitive::kPrimFloat:
549 return Location::StackSlot(GetStackSlot(load->GetLocal()));
550
551 case Primitive::kPrimLong:
552 case Primitive::kPrimDouble:
553 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
554
Alexandre Rames5319def2014-10-23 10:03:10 +0100555 case Primitive::kPrimBoolean:
556 case Primitive::kPrimByte:
557 case Primitive::kPrimChar:
558 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100559 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100560 LOG(FATAL) << "Unexpected type " << type;
561 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000562
Alexandre Rames5319def2014-10-23 10:03:10 +0100563 LOG(FATAL) << "Unreachable";
564 return Location::NoLocation();
565}
566
567void CodeGeneratorARM64::MarkGCCard(Register object, Register value) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000568 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100569 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000570 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100571 vixl::Label done;
572 __ Cbz(value, &done);
573 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
574 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000575 __ Strb(card, MemOperand(card, temp.X()));
Alexandre Rames5319def2014-10-23 10:03:10 +0100576 __ Bind(&done);
577}
578
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000579void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
580 // Blocked core registers:
581 // lr : Runtime reserved.
582 // tr : Runtime reserved.
583 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
584 // ip1 : VIXL core temp.
585 // ip0 : VIXL core temp.
586 //
587 // Blocked fp registers:
588 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100589 CPURegList reserved_core_registers = vixl_reserved_core_registers;
590 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100591 while (!reserved_core_registers.IsEmpty()) {
592 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
593 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000594
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000595 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800596 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000597 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
598 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000599
600 if (is_baseline) {
601 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
602 while (!reserved_core_baseline_registers.IsEmpty()) {
603 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
604 }
605
606 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
607 while (!reserved_fp_baseline_registers.IsEmpty()) {
608 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
609 }
610 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100611}
612
613Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
614 if (type == Primitive::kPrimVoid) {
615 LOG(FATAL) << "Unreachable type " << type;
616 }
617
Alexandre Rames542361f2015-01-29 16:57:31 +0000618 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000619 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
620 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100621 return Location::FpuRegisterLocation(reg);
622 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000623 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
624 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100625 return Location::RegisterLocation(reg);
626 }
627}
628
Alexandre Rames3e69f162014-12-10 10:36:50 +0000629size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
630 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
631 __ Str(reg, MemOperand(sp, stack_index));
632 return kArm64WordSize;
633}
634
635size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
636 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
637 __ Ldr(reg, MemOperand(sp, stack_index));
638 return kArm64WordSize;
639}
640
641size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
642 FPRegister reg = FPRegister(reg_id, kDRegSize);
643 __ Str(reg, MemOperand(sp, stack_index));
644 return kArm64WordSize;
645}
646
647size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
648 FPRegister reg = FPRegister(reg_id, kDRegSize);
649 __ Ldr(reg, MemOperand(sp, stack_index));
650 return kArm64WordSize;
651}
652
Alexandre Rames5319def2014-10-23 10:03:10 +0100653void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
654 stream << Arm64ManagedRegister::FromXRegister(XRegister(reg));
655}
656
657void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
658 stream << Arm64ManagedRegister::FromDRegister(DRegister(reg));
659}
660
Alexandre Rames67555f72014-11-18 10:55:16 +0000661void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000662 if (constant->IsIntConstant()) {
663 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
664 } else if (constant->IsLongConstant()) {
665 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
666 } else if (constant->IsNullConstant()) {
667 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000668 } else if (constant->IsFloatConstant()) {
669 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
670 } else {
671 DCHECK(constant->IsDoubleConstant());
672 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
673 }
674}
675
Alexandre Rames3e69f162014-12-10 10:36:50 +0000676
677static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
678 DCHECK(constant.IsConstant());
679 HConstant* cst = constant.GetConstant();
680 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000681 // Null is mapped to a core W register, which we associate with kPrimInt.
682 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000683 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
684 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
685 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
686}
687
688void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000689 if (source.Equals(destination)) {
690 return;
691 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000692
693 // A valid move can always be inferred from the destination and source
694 // locations. When moving from and to a register, the argument type can be
695 // used to generate 32bit instead of 64bit moves. In debug mode we also
696 // checks the coherency of the locations and the type.
697 bool unspecified_type = (type == Primitive::kPrimVoid);
698
699 if (destination.IsRegister() || destination.IsFpuRegister()) {
700 if (unspecified_type) {
701 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
702 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000703 (src_cst != nullptr && (src_cst->IsIntConstant()
704 || src_cst->IsFloatConstant()
705 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000706 // For stack slots and 32bit constants, a 64bit type is appropriate.
707 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000708 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000709 // If the source is a double stack slot or a 64bit constant, a 64bit
710 // type is appropriate. Else the source is a register, and since the
711 // type has not been specified, we chose a 64bit type to force a 64bit
712 // move.
713 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000714 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000715 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000716 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
717 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000718 CPURegister dst = CPURegisterFrom(destination, type);
719 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
720 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
721 __ Ldr(dst, StackOperandFrom(source));
722 } else if (source.IsConstant()) {
723 DCHECK(CoherentConstantAndType(source, type));
724 MoveConstant(dst, source.GetConstant());
725 } else {
726 if (destination.IsRegister()) {
727 __ Mov(Register(dst), RegisterFrom(source, type));
728 } else {
729 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
730 }
731 }
732
733 } else { // The destination is not a register. It must be a stack slot.
734 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
735 if (source.IsRegister() || source.IsFpuRegister()) {
736 if (unspecified_type) {
737 if (source.IsRegister()) {
738 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
739 } else {
740 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
741 }
742 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000743 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
744 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000745 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
746 } else if (source.IsConstant()) {
747 DCHECK(unspecified_type || CoherentConstantAndType(source, type));
748 UseScratchRegisterScope temps(GetVIXLAssembler());
749 HConstant* src_cst = source.GetConstant();
750 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000751 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000752 temp = temps.AcquireW();
753 } else if (src_cst->IsLongConstant()) {
754 temp = temps.AcquireX();
755 } else if (src_cst->IsFloatConstant()) {
756 temp = temps.AcquireS();
757 } else {
758 DCHECK(src_cst->IsDoubleConstant());
759 temp = temps.AcquireD();
760 }
761 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000762 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000763 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000764 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000765 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000766 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000767 // There is generally less pressure on FP registers.
768 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000769 __ Ldr(temp, StackOperandFrom(source));
770 __ Str(temp, StackOperandFrom(destination));
771 }
772 }
773}
774
Alexandre Rames3e69f162014-12-10 10:36:50 +0000775void CodeGeneratorARM64::SwapLocations(Location loc1, Location loc2) {
776 DCHECK(!loc1.IsConstant());
777 DCHECK(!loc2.IsConstant());
778
779 if (loc1.Equals(loc2)) {
780 return;
781 }
782
783 UseScratchRegisterScope temps(GetAssembler()->vixl_masm_);
784
785 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
786 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
787 bool is_fp_reg1 = loc1.IsFpuRegister();
788 bool is_fp_reg2 = loc2.IsFpuRegister();
789
790 if (loc2.IsRegister() && loc1.IsRegister()) {
791 Register r1 = XRegisterFrom(loc1);
792 Register r2 = XRegisterFrom(loc2);
793 Register tmp = temps.AcquireSameSizeAs(r1);
794 __ Mov(tmp, r2);
795 __ Mov(r2, r1);
796 __ Mov(r1, tmp);
797 } else if (is_fp_reg2 && is_fp_reg1) {
798 FPRegister r1 = DRegisterFrom(loc1);
799 FPRegister r2 = DRegisterFrom(loc2);
800 FPRegister tmp = temps.AcquireSameSizeAs(r1);
801 __ Fmov(tmp, r2);
802 __ Fmov(r2, r1);
803 __ Fmov(r1, tmp);
804 } else if (is_slot1 != is_slot2) {
805 MemOperand mem = StackOperandFrom(is_slot1 ? loc1 : loc2);
806 Location reg_loc = is_slot1 ? loc2 : loc1;
807 CPURegister reg, tmp;
808 if (reg_loc.IsFpuRegister()) {
809 reg = DRegisterFrom(reg_loc);
810 tmp = temps.AcquireD();
811 } else {
812 reg = XRegisterFrom(reg_loc);
813 tmp = temps.AcquireX();
814 }
815 __ Ldr(tmp, mem);
816 __ Str(reg, mem);
817 if (reg_loc.IsFpuRegister()) {
818 __ Fmov(FPRegister(reg), FPRegister(tmp));
819 } else {
820 __ Mov(Register(reg), Register(tmp));
821 }
822 } else if (is_slot1 && is_slot2) {
823 MemOperand mem1 = StackOperandFrom(loc1);
824 MemOperand mem2 = StackOperandFrom(loc2);
825 Register tmp1 = loc1.IsStackSlot() ? temps.AcquireW() : temps.AcquireX();
826 Register tmp2 = temps.AcquireSameSizeAs(tmp1);
827 __ Ldr(tmp1, mem1);
828 __ Ldr(tmp2, mem2);
829 __ Str(tmp1, mem2);
830 __ Str(tmp2, mem1);
831 } else {
832 LOG(FATAL) << "Unimplemented";
833 }
834}
835
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000836void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000837 CPURegister dst,
838 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000839 switch (type) {
840 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000841 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000842 break;
843 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000844 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000845 break;
846 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000847 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000848 break;
849 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000850 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000851 break;
852 case Primitive::kPrimInt:
853 case Primitive::kPrimNot:
854 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000855 case Primitive::kPrimFloat:
856 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000857 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000858 __ Ldr(dst, src);
859 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000860 case Primitive::kPrimVoid:
861 LOG(FATAL) << "Unreachable type " << type;
862 }
863}
864
Calin Juravle77520bc2015-01-12 18:45:46 +0000865void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000866 CPURegister dst,
867 const MemOperand& src) {
868 UseScratchRegisterScope temps(GetVIXLAssembler());
869 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000870 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000871
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000872 DCHECK(!src.IsPreIndex());
873 DCHECK(!src.IsPostIndex());
874
875 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800876 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000877 MemOperand base = MemOperand(temp_base);
878 switch (type) {
879 case Primitive::kPrimBoolean:
880 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000881 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000882 break;
883 case Primitive::kPrimByte:
884 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000885 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000886 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
887 break;
888 case Primitive::kPrimChar:
889 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000890 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000891 break;
892 case Primitive::kPrimShort:
893 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000894 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000895 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
896 break;
897 case Primitive::kPrimInt:
898 case Primitive::kPrimNot:
899 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000900 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000901 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000902 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000903 break;
904 case Primitive::kPrimFloat:
905 case Primitive::kPrimDouble: {
906 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000907 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000908
909 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
910 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000911 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000912 __ Fmov(FPRegister(dst), temp);
913 break;
914 }
915 case Primitive::kPrimVoid:
916 LOG(FATAL) << "Unreachable type " << type;
917 }
918}
919
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000920void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000921 CPURegister src,
922 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000923 switch (type) {
924 case Primitive::kPrimBoolean:
925 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000926 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000927 break;
928 case Primitive::kPrimChar:
929 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000930 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000931 break;
932 case Primitive::kPrimInt:
933 case Primitive::kPrimNot:
934 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000935 case Primitive::kPrimFloat:
936 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000937 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000938 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000939 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000940 case Primitive::kPrimVoid:
941 LOG(FATAL) << "Unreachable type " << type;
942 }
943}
944
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000945void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
946 CPURegister src,
947 const MemOperand& dst) {
948 UseScratchRegisterScope temps(GetVIXLAssembler());
949 Register temp_base = temps.AcquireX();
950
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000951 DCHECK(!dst.IsPreIndex());
952 DCHECK(!dst.IsPostIndex());
953
954 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800955 Operand op = OperandFromMemOperand(dst);
956 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000957 MemOperand base = MemOperand(temp_base);
958 switch (type) {
959 case Primitive::kPrimBoolean:
960 case Primitive::kPrimByte:
961 __ Stlrb(Register(src), base);
962 break;
963 case Primitive::kPrimChar:
964 case Primitive::kPrimShort:
965 __ Stlrh(Register(src), base);
966 break;
967 case Primitive::kPrimInt:
968 case Primitive::kPrimNot:
969 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000970 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000971 __ Stlr(Register(src), base);
972 break;
973 case Primitive::kPrimFloat:
974 case Primitive::kPrimDouble: {
975 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000976 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000977
978 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
979 __ Fmov(temp, FPRegister(src));
980 __ Stlr(temp, base);
981 break;
982 }
983 case Primitive::kPrimVoid:
984 LOG(FATAL) << "Unreachable type " << type;
985 }
986}
987
Alexandre Rames67555f72014-11-18 10:55:16 +0000988void CodeGeneratorARM64::LoadCurrentMethod(vixl::Register current_method) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000989 DCHECK(RequiresCurrentMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +0000990 DCHECK(current_method.IsW());
991 __ Ldr(current_method, MemOperand(sp, kCurrentMethodStackOffset));
992}
993
994void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
995 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000996 uint32_t dex_pc,
997 SlowPathCode* slow_path) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000998 __ Ldr(lr, MemOperand(tr, entry_point_offset));
999 __ Blr(lr);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001000 if (instruction != nullptr) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001001 RecordPcInfo(instruction, dex_pc, slow_path);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001002 DCHECK(instruction->IsSuspendCheck()
1003 || instruction->IsBoundsCheck()
1004 || instruction->IsNullCheck()
1005 || instruction->IsDivZeroCheck()
1006 || !IsLeafMethod());
1007 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001008}
1009
1010void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1011 vixl::Register class_reg) {
1012 UseScratchRegisterScope temps(GetVIXLAssembler());
1013 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001014 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001015 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001016
Serban Constantinescu02164b32014-11-13 14:05:07 +00001017 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001018 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001019 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1020 __ Add(temp, class_reg, status_offset);
1021 __ Ldar(temp, HeapOperand(temp));
1022 __ Cmp(temp, mirror::Class::kStatusInitialized);
1023 __ B(lt, slow_path->GetEntryLabel());
1024 } else {
1025 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1026 __ Cmp(temp, mirror::Class::kStatusInitialized);
1027 __ B(lt, slow_path->GetEntryLabel());
1028 __ Dmb(InnerShareable, BarrierReads);
1029 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001030 __ Bind(slow_path->GetExitLabel());
1031}
Alexandre Rames5319def2014-10-23 10:03:10 +01001032
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001033void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1034 BarrierType type = BarrierAll;
1035
1036 switch (kind) {
1037 case MemBarrierKind::kAnyAny:
1038 case MemBarrierKind::kAnyStore: {
1039 type = BarrierAll;
1040 break;
1041 }
1042 case MemBarrierKind::kLoadAny: {
1043 type = BarrierReads;
1044 break;
1045 }
1046 case MemBarrierKind::kStoreStore: {
1047 type = BarrierWrites;
1048 break;
1049 }
1050 default:
1051 LOG(FATAL) << "Unexpected memory barrier " << kind;
1052 }
1053 __ Dmb(InnerShareable, type);
1054}
1055
Serban Constantinescu02164b32014-11-13 14:05:07 +00001056void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1057 HBasicBlock* successor) {
1058 SuspendCheckSlowPathARM64* slow_path =
1059 new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1060 codegen_->AddSlowPath(slow_path);
1061 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1062 Register temp = temps.AcquireW();
1063
1064 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1065 if (successor == nullptr) {
1066 __ Cbnz(temp, slow_path->GetEntryLabel());
1067 __ Bind(slow_path->GetReturnLabel());
1068 } else {
1069 __ Cbz(temp, codegen_->GetLabelOf(successor));
1070 __ B(slow_path->GetEntryLabel());
1071 // slow_path will return to GetLabelOf(successor).
1072 }
1073}
1074
Alexandre Rames5319def2014-10-23 10:03:10 +01001075InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1076 CodeGeneratorARM64* codegen)
1077 : HGraphVisitor(graph),
1078 assembler_(codegen->GetAssembler()),
1079 codegen_(codegen) {}
1080
1081#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001082 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001083
1084#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1085
1086enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001087 // Using a base helps identify when we hit such breakpoints.
1088 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001089#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1090 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1091#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1092};
1093
1094#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1095 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001096 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001097 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1098 } \
1099 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1100 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1101 locations->SetOut(Location::Any()); \
1102 }
1103 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1104#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1105
1106#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001107#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001108
Alexandre Rames67555f72014-11-18 10:55:16 +00001109void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001110 DCHECK_EQ(instr->InputCount(), 2U);
1111 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1112 Primitive::Type type = instr->GetResultType();
1113 switch (type) {
1114 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001115 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001116 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001117 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001118 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001119 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001120
1121 case Primitive::kPrimFloat:
1122 case Primitive::kPrimDouble:
1123 locations->SetInAt(0, Location::RequiresFpuRegister());
1124 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001125 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001126 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001127
Alexandre Rames5319def2014-10-23 10:03:10 +01001128 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001129 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001130 }
1131}
1132
Alexandre Rames09a99962015-04-15 11:47:56 +01001133void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1134 LocationSummary* locations =
1135 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1136 locations->SetInAt(0, Location::RequiresRegister());
1137 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1138 locations->SetOut(Location::RequiresFpuRegister());
1139 } else {
1140 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1141 }
1142}
1143
1144void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1145 const FieldInfo& field_info) {
1146 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1147
1148 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1149 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1150
1151 if (field_info.IsVolatile()) {
1152 if (use_acquire_release) {
1153 // NB: LoadAcquire will record the pc info if needed.
1154 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1155 } else {
1156 codegen_->Load(field_info.GetFieldType(), OutputCPURegister(instruction), field);
1157 codegen_->MaybeRecordImplicitNullCheck(instruction);
1158 // For IRIW sequential consistency kLoadAny is not sufficient.
1159 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1160 }
1161 } else {
1162 codegen_->Load(field_info.GetFieldType(), OutputCPURegister(instruction), field);
1163 codegen_->MaybeRecordImplicitNullCheck(instruction);
1164 }
1165}
1166
1167void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1168 LocationSummary* locations =
1169 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1170 locations->SetInAt(0, Location::RequiresRegister());
1171 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1172 locations->SetInAt(1, Location::RequiresFpuRegister());
1173 } else {
1174 locations->SetInAt(1, Location::RequiresRegister());
1175 }
1176}
1177
1178void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
1179 const FieldInfo& field_info) {
1180 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
1181
1182 Register obj = InputRegisterAt(instruction, 0);
1183 CPURegister value = InputCPURegisterAt(instruction, 1);
1184 Offset offset = field_info.GetFieldOffset();
1185 Primitive::Type field_type = field_info.GetFieldType();
1186 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1187
1188 if (field_info.IsVolatile()) {
1189 if (use_acquire_release) {
1190 codegen_->StoreRelease(field_type, value, HeapOperand(obj, offset));
1191 codegen_->MaybeRecordImplicitNullCheck(instruction);
1192 } else {
1193 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1194 codegen_->Store(field_type, value, HeapOperand(obj, offset));
1195 codegen_->MaybeRecordImplicitNullCheck(instruction);
1196 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1197 }
1198 } else {
1199 codegen_->Store(field_type, value, HeapOperand(obj, offset));
1200 codegen_->MaybeRecordImplicitNullCheck(instruction);
1201 }
1202
1203 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
1204 codegen_->MarkGCCard(obj, Register(value));
1205 }
1206}
1207
Alexandre Rames67555f72014-11-18 10:55:16 +00001208void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001209 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001210
1211 switch (type) {
1212 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001213 case Primitive::kPrimLong: {
1214 Register dst = OutputRegister(instr);
1215 Register lhs = InputRegisterAt(instr, 0);
1216 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001217 if (instr->IsAdd()) {
1218 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001219 } else if (instr->IsAnd()) {
1220 __ And(dst, lhs, rhs);
1221 } else if (instr->IsOr()) {
1222 __ Orr(dst, lhs, rhs);
1223 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001224 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001225 } else {
1226 DCHECK(instr->IsXor());
1227 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001228 }
1229 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001230 }
1231 case Primitive::kPrimFloat:
1232 case Primitive::kPrimDouble: {
1233 FPRegister dst = OutputFPRegister(instr);
1234 FPRegister lhs = InputFPRegisterAt(instr, 0);
1235 FPRegister rhs = InputFPRegisterAt(instr, 1);
1236 if (instr->IsAdd()) {
1237 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001238 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001239 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001240 } else {
1241 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001242 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001243 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001244 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001245 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001246 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001247 }
1248}
1249
Serban Constantinescu02164b32014-11-13 14:05:07 +00001250void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1251 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1252
1253 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1254 Primitive::Type type = instr->GetResultType();
1255 switch (type) {
1256 case Primitive::kPrimInt:
1257 case Primitive::kPrimLong: {
1258 locations->SetInAt(0, Location::RequiresRegister());
1259 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1260 locations->SetOut(Location::RequiresRegister());
1261 break;
1262 }
1263 default:
1264 LOG(FATAL) << "Unexpected shift type " << type;
1265 }
1266}
1267
1268void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1269 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1270
1271 Primitive::Type type = instr->GetType();
1272 switch (type) {
1273 case Primitive::kPrimInt:
1274 case Primitive::kPrimLong: {
1275 Register dst = OutputRegister(instr);
1276 Register lhs = InputRegisterAt(instr, 0);
1277 Operand rhs = InputOperandAt(instr, 1);
1278 if (rhs.IsImmediate()) {
1279 uint32_t shift_value = (type == Primitive::kPrimInt)
1280 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1281 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1282 if (instr->IsShl()) {
1283 __ Lsl(dst, lhs, shift_value);
1284 } else if (instr->IsShr()) {
1285 __ Asr(dst, lhs, shift_value);
1286 } else {
1287 __ Lsr(dst, lhs, shift_value);
1288 }
1289 } else {
1290 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1291
1292 if (instr->IsShl()) {
1293 __ Lsl(dst, lhs, rhs_reg);
1294 } else if (instr->IsShr()) {
1295 __ Asr(dst, lhs, rhs_reg);
1296 } else {
1297 __ Lsr(dst, lhs, rhs_reg);
1298 }
1299 }
1300 break;
1301 }
1302 default:
1303 LOG(FATAL) << "Unexpected shift operation type " << type;
1304 }
1305}
1306
Alexandre Rames5319def2014-10-23 10:03:10 +01001307void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001308 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001309}
1310
1311void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001312 HandleBinaryOp(instruction);
1313}
1314
1315void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1316 HandleBinaryOp(instruction);
1317}
1318
1319void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1320 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001321}
1322
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001323void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1324 LocationSummary* locations =
1325 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1326 locations->SetInAt(0, Location::RequiresRegister());
1327 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001328 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1329 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1330 } else {
1331 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1332 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001333}
1334
1335void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1336 LocationSummary* locations = instruction->GetLocations();
1337 Primitive::Type type = instruction->GetType();
1338 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001339 Location index = locations->InAt(1);
1340 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001341 MemOperand source = HeapOperand(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00001342 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001343
1344 if (index.IsConstant()) {
1345 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001346 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001347 } else {
1348 Register temp = temps.AcquireSameSizeAs(obj);
1349 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
1350 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001351 source = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001352 }
1353
Alexandre Rames67555f72014-11-18 10:55:16 +00001354 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001355 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001356}
1357
Alexandre Rames5319def2014-10-23 10:03:10 +01001358void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1359 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1360 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001361 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001362}
1363
1364void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
1365 __ Ldr(OutputRegister(instruction),
1366 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001367 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001368}
1369
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001370void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
1371 Primitive::Type value_type = instruction->GetComponentType();
1372 bool is_object = value_type == Primitive::kPrimNot;
1373 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1374 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1375 if (is_object) {
1376 InvokeRuntimeCallingConvention calling_convention;
1377 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1378 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1379 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1380 } else {
1381 locations->SetInAt(0, Location::RequiresRegister());
1382 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001383 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1384 locations->SetInAt(2, Location::RequiresFpuRegister());
1385 } else {
1386 locations->SetInAt(2, Location::RequiresRegister());
1387 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001388 }
1389}
1390
1391void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1392 Primitive::Type value_type = instruction->GetComponentType();
1393 if (value_type == Primitive::kPrimNot) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001394 codegen_->InvokeRuntime(
1395 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001396 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001397 } else {
1398 LocationSummary* locations = instruction->GetLocations();
1399 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001400 CPURegister value = InputCPURegisterAt(instruction, 2);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001401 Location index = locations->InAt(1);
1402 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001403 MemOperand destination = HeapOperand(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00001404 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001405
1406 if (index.IsConstant()) {
1407 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001408 destination = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001409 } else {
1410 Register temp = temps.AcquireSameSizeAs(obj);
1411 Register index_reg = InputRegisterAt(instruction, 1);
1412 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(value_type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001413 destination = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001414 }
1415
1416 codegen_->Store(value_type, value, destination);
Calin Juravle77520bc2015-01-12 18:45:46 +00001417 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001418 }
1419}
1420
Alexandre Rames67555f72014-11-18 10:55:16 +00001421void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1422 LocationSummary* locations =
1423 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1424 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001425 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001426 if (instruction->HasUses()) {
1427 locations->SetOut(Location::SameAsFirstInput());
1428 }
1429}
1430
1431void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001432 LocationSummary* locations = instruction->GetLocations();
1433 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1434 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001435 codegen_->AddSlowPath(slow_path);
1436
1437 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1438 __ B(slow_path->GetEntryLabel(), hs);
1439}
1440
1441void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1442 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1443 instruction, LocationSummary::kCallOnSlowPath);
1444 locations->SetInAt(0, Location::RequiresRegister());
1445 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001446 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001447}
1448
1449void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001450 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001451 Register obj = InputRegisterAt(instruction, 0);;
1452 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001453 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001454
Alexandre Rames3e69f162014-12-10 10:36:50 +00001455 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1456 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001457 codegen_->AddSlowPath(slow_path);
1458
1459 // TODO: avoid this check if we know obj is not null.
1460 __ Cbz(obj, slow_path->GetExitLabel());
1461 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001462 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
1463 __ Cmp(obj_cls, cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00001464 __ B(ne, slow_path->GetEntryLabel());
1465 __ Bind(slow_path->GetExitLabel());
1466}
1467
1468void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1469 LocationSummary* locations =
1470 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1471 locations->SetInAt(0, Location::RequiresRegister());
1472 if (check->HasUses()) {
1473 locations->SetOut(Location::SameAsFirstInput());
1474 }
1475}
1476
1477void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1478 // We assume the class is not null.
1479 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1480 check->GetLoadClass(), check, check->GetDexPc(), true);
1481 codegen_->AddSlowPath(slow_path);
1482 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1483}
1484
Serban Constantinescu02164b32014-11-13 14:05:07 +00001485void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001486 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001487 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1488 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001489 switch (in_type) {
1490 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001491 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001492 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001493 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1494 break;
1495 }
1496 case Primitive::kPrimFloat:
1497 case Primitive::kPrimDouble: {
1498 locations->SetInAt(0, Location::RequiresFpuRegister());
Alexandre Rames93415462015-02-17 15:08:20 +00001499 HInstruction* right = compare->InputAt(1);
1500 if ((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1501 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0))) {
1502 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1503 } else {
1504 locations->SetInAt(1, Location::RequiresFpuRegister());
1505 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001506 locations->SetOut(Location::RequiresRegister());
1507 break;
1508 }
1509 default:
1510 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1511 }
1512}
1513
1514void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1515 Primitive::Type in_type = compare->InputAt(0)->GetType();
1516
1517 // 0 if: left == right
1518 // 1 if: left > right
1519 // -1 if: left < right
1520 switch (in_type) {
1521 case Primitive::kPrimLong: {
1522 Register result = OutputRegister(compare);
1523 Register left = InputRegisterAt(compare, 0);
1524 Operand right = InputOperandAt(compare, 1);
1525
1526 __ Cmp(left, right);
1527 __ Cset(result, ne);
1528 __ Cneg(result, result, lt);
1529 break;
1530 }
1531 case Primitive::kPrimFloat:
1532 case Primitive::kPrimDouble: {
1533 Register result = OutputRegister(compare);
1534 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001535 if (compare->GetLocations()->InAt(1).IsConstant()) {
1536 if (kIsDebugBuild) {
1537 HInstruction* right = compare->GetLocations()->InAt(1).GetConstant();
1538 DCHECK((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1539 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0)));
1540 }
1541 // 0.0 is the only immediate that can be encoded directly in a FCMP instruction.
1542 __ Fcmp(left, 0.0);
1543 } else {
1544 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1545 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001546 if (compare->IsGtBias()) {
1547 __ Cset(result, ne);
1548 } else {
1549 __ Csetm(result, ne);
1550 }
1551 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001552 break;
1553 }
1554 default:
1555 LOG(FATAL) << "Unimplemented compare type " << in_type;
1556 }
1557}
1558
1559void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1560 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1561 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001562 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames5319def2014-10-23 10:03:10 +01001563 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001564 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001565 }
1566}
1567
1568void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1569 if (!instruction->NeedsMaterialization()) {
1570 return;
1571 }
1572
1573 LocationSummary* locations = instruction->GetLocations();
1574 Register lhs = InputRegisterAt(instruction, 0);
1575 Operand rhs = InputOperandAt(instruction, 1);
1576 Register res = RegisterFrom(locations->Out(), instruction->GetType());
1577 Condition cond = ARM64Condition(instruction->GetCondition());
1578
1579 __ Cmp(lhs, rhs);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001580 __ Cset(res, cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001581}
1582
1583#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1584 M(Equal) \
1585 M(NotEqual) \
1586 M(LessThan) \
1587 M(LessThanOrEqual) \
1588 M(GreaterThan) \
1589 M(GreaterThanOrEqual)
1590#define DEFINE_CONDITION_VISITORS(Name) \
1591void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1592void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1593FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001594#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001595#undef FOR_EACH_CONDITION_INSTRUCTION
1596
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001597void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1598 LocationSummary* locations =
1599 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1600 switch (div->GetResultType()) {
1601 case Primitive::kPrimInt:
1602 case Primitive::kPrimLong:
1603 locations->SetInAt(0, Location::RequiresRegister());
1604 locations->SetInAt(1, Location::RequiresRegister());
1605 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1606 break;
1607
1608 case Primitive::kPrimFloat:
1609 case Primitive::kPrimDouble:
1610 locations->SetInAt(0, Location::RequiresFpuRegister());
1611 locations->SetInAt(1, Location::RequiresFpuRegister());
1612 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1613 break;
1614
1615 default:
1616 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1617 }
1618}
1619
1620void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1621 Primitive::Type type = div->GetResultType();
1622 switch (type) {
1623 case Primitive::kPrimInt:
1624 case Primitive::kPrimLong:
1625 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
1626 break;
1627
1628 case Primitive::kPrimFloat:
1629 case Primitive::kPrimDouble:
1630 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1631 break;
1632
1633 default:
1634 LOG(FATAL) << "Unexpected div type " << type;
1635 }
1636}
1637
Alexandre Rames67555f72014-11-18 10:55:16 +00001638void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1639 LocationSummary* locations =
1640 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1641 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1642 if (instruction->HasUses()) {
1643 locations->SetOut(Location::SameAsFirstInput());
1644 }
1645}
1646
1647void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1648 SlowPathCodeARM64* slow_path =
1649 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1650 codegen_->AddSlowPath(slow_path);
1651 Location value = instruction->GetLocations()->InAt(0);
1652
Alexandre Rames3e69f162014-12-10 10:36:50 +00001653 Primitive::Type type = instruction->GetType();
1654
1655 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
1656 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
1657 return;
1658 }
1659
Alexandre Rames67555f72014-11-18 10:55:16 +00001660 if (value.IsConstant()) {
1661 int64_t divisor = Int64ConstantFrom(value);
1662 if (divisor == 0) {
1663 __ B(slow_path->GetEntryLabel());
1664 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001665 // A division by a non-null constant is valid. We don't need to perform
1666 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00001667 }
1668 } else {
1669 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
1670 }
1671}
1672
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001673void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1674 LocationSummary* locations =
1675 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1676 locations->SetOut(Location::ConstantLocation(constant));
1677}
1678
1679void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1680 UNUSED(constant);
1681 // Will be generated at use site.
1682}
1683
Alexandre Rames5319def2014-10-23 10:03:10 +01001684void LocationsBuilderARM64::VisitExit(HExit* exit) {
1685 exit->SetLocations(nullptr);
1686}
1687
1688void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001689 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01001690}
1691
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001692void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
1693 LocationSummary* locations =
1694 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1695 locations->SetOut(Location::ConstantLocation(constant));
1696}
1697
1698void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
1699 UNUSED(constant);
1700 // Will be generated at use site.
1701}
1702
Alexandre Rames5319def2014-10-23 10:03:10 +01001703void LocationsBuilderARM64::VisitGoto(HGoto* got) {
1704 got->SetLocations(nullptr);
1705}
1706
1707void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
1708 HBasicBlock* successor = got->GetSuccessor();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001709 DCHECK(!successor->IsExitBlock());
1710 HBasicBlock* block = got->GetBlock();
1711 HInstruction* previous = got->GetPrevious();
1712 HLoopInformation* info = block->GetLoopInformation();
1713
David Brazdil46e2a392015-03-16 17:31:52 +00001714 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001715 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1716 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1717 return;
1718 }
1719 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1720 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1721 }
1722 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001723 __ B(codegen_->GetLabelOf(successor));
1724 }
1725}
1726
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001727void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
1728 vixl::Label* true_target,
1729 vixl::Label* false_target,
1730 vixl::Label* always_true_target) {
1731 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001732 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01001733
Serban Constantinescu02164b32014-11-13 14:05:07 +00001734 if (cond->IsIntConstant()) {
1735 int32_t cond_value = cond->AsIntConstant()->GetValue();
1736 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001737 if (always_true_target != nullptr) {
1738 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001739 }
1740 return;
1741 } else {
1742 DCHECK_EQ(cond_value, 0);
1743 }
1744 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001745 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001746 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001747 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001748 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001749 } else {
1750 // The condition instruction has not been materialized, use its inputs as
1751 // the comparison and its condition as the branch condition.
1752 Register lhs = InputRegisterAt(condition, 0);
1753 Operand rhs = InputOperandAt(condition, 1);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001754 Condition arm64_cond = ARM64Condition(condition->GetCondition());
Alexandre Rames4388dcc2015-02-03 10:28:33 +00001755 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
1756 switch (arm64_cond) {
1757 case eq:
1758 __ Cbz(lhs, true_target);
1759 break;
1760 case ne:
1761 __ Cbnz(lhs, true_target);
1762 break;
1763 case lt:
1764 // Test the sign bit and branch accordingly.
1765 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
1766 break;
1767 case ge:
1768 // Test the sign bit and branch accordingly.
1769 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
1770 break;
1771 default:
1772 // Without the `static_cast` the compiler throws an error for
1773 // `-Werror=sign-promo`.
1774 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001775 }
1776 } else {
1777 __ Cmp(lhs, rhs);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001778 __ B(arm64_cond, true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001779 }
1780 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001781 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001782 __ B(false_target);
1783 }
1784}
1785
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001786void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
1787 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1788 HInstruction* cond = if_instr->InputAt(0);
1789 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1790 locations->SetInAt(0, Location::RequiresRegister());
1791 }
1792}
1793
1794void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
1795 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1796 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1797 vixl::Label* always_true_target = true_target;
1798 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1799 if_instr->IfTrueSuccessor())) {
1800 always_true_target = nullptr;
1801 }
1802 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1803 if_instr->IfFalseSuccessor())) {
1804 false_target = nullptr;
1805 }
1806 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1807}
1808
1809void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
1810 LocationSummary* locations = new (GetGraph()->GetArena())
1811 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1812 HInstruction* cond = deoptimize->InputAt(0);
1813 DCHECK(cond->IsCondition());
1814 if (cond->AsCondition()->NeedsMaterialization()) {
1815 locations->SetInAt(0, Location::RequiresRegister());
1816 }
1817}
1818
1819void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
1820 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
1821 DeoptimizationSlowPathARM64(deoptimize);
1822 codegen_->AddSlowPath(slow_path);
1823 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
1824 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1825}
1826
Alexandre Rames5319def2014-10-23 10:03:10 +01001827void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001828 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001829}
1830
1831void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001832 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01001833}
1834
1835void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001836 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001837}
1838
1839void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001840 HandleFieldSet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01001841}
1842
Alexandre Rames67555f72014-11-18 10:55:16 +00001843void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
1844 LocationSummary::CallKind call_kind =
1845 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
1846 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1847 locations->SetInAt(0, Location::RequiresRegister());
1848 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001849 // The output does overlap inputs.
1850 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00001851}
1852
1853void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
1854 LocationSummary* locations = instruction->GetLocations();
1855 Register obj = InputRegisterAt(instruction, 0);;
1856 Register cls = InputRegisterAt(instruction, 1);;
1857 Register out = OutputRegister(instruction);
1858
1859 vixl::Label done;
1860
1861 // Return 0 if `obj` is null.
1862 // TODO: Avoid this check if we know `obj` is not null.
1863 __ Mov(out, 0);
1864 __ Cbz(obj, &done);
1865
1866 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00001867 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Alexandre Rames67555f72014-11-18 10:55:16 +00001868 __ Cmp(out, cls);
1869 if (instruction->IsClassFinal()) {
1870 // Classes must be equal for the instanceof to succeed.
1871 __ Cset(out, eq);
1872 } else {
1873 // If the classes are not equal, we go into a slow path.
1874 DCHECK(locations->OnlyCallsOnSlowPath());
1875 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00001876 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1877 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001878 codegen_->AddSlowPath(slow_path);
1879 __ B(ne, slow_path->GetEntryLabel());
1880 __ Mov(out, 1);
1881 __ Bind(slow_path->GetExitLabel());
1882 }
1883
1884 __ Bind(&done);
1885}
1886
Alexandre Rames5319def2014-10-23 10:03:10 +01001887void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
1888 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
1889 locations->SetOut(Location::ConstantLocation(constant));
1890}
1891
1892void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
1893 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001894 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01001895}
1896
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001897void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
1898 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
1899 locations->SetOut(Location::ConstantLocation(constant));
1900}
1901
1902void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
1903 // Will be generated at use site.
1904 UNUSED(constant);
1905}
1906
Alexandre Rames5319def2014-10-23 10:03:10 +01001907void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
1908 LocationSummary* locations =
1909 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
1910 locations->AddTemp(LocationFrom(x0));
1911
1912 InvokeDexCallingConventionVisitor calling_convention_visitor;
1913 for (size_t i = 0; i < invoke->InputCount(); i++) {
1914 HInstruction* input = invoke->InputAt(i);
1915 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1916 }
1917
1918 Primitive::Type return_type = invoke->GetType();
1919 if (return_type != Primitive::kPrimVoid) {
1920 locations->SetOut(calling_convention_visitor.GetReturnLocation(return_type));
1921 }
1922}
1923
Alexandre Rames67555f72014-11-18 10:55:16 +00001924void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1925 HandleInvoke(invoke);
1926}
1927
1928void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1929 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1930 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
1931 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1932 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1933 Location receiver = invoke->GetLocations()->InAt(0);
1934 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001935 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00001936
1937 // The register ip1 is required to be used for the hidden argument in
1938 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
1939 UseScratchRegisterScope scratch_scope(GetVIXLAssembler());
1940 scratch_scope.Exclude(ip1);
1941 __ Mov(ip1, invoke->GetDexMethodIndex());
1942
1943 // temp = object->GetClass();
1944 if (receiver.IsStackSlot()) {
1945 __ Ldr(temp, StackOperandFrom(receiver));
1946 __ Ldr(temp, HeapOperand(temp, class_offset));
1947 } else {
1948 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
1949 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001950 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00001951 // temp = temp->GetImtEntryAt(method_offset);
1952 __ Ldr(temp, HeapOperand(temp, method_offset));
1953 // lr = temp->GetEntryPoint();
1954 __ Ldr(lr, HeapOperand(temp, entry_point));
1955 // lr();
1956 __ Blr(lr);
1957 DCHECK(!codegen_->IsLeafMethod());
1958 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1959}
1960
1961void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001962 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1963 if (intrinsic.TryDispatch(invoke)) {
1964 return;
1965 }
1966
Alexandre Rames67555f72014-11-18 10:55:16 +00001967 HandleInvoke(invoke);
1968}
1969
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001970void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001971 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1972 if (intrinsic.TryDispatch(invoke)) {
1973 return;
1974 }
1975
Alexandre Rames67555f72014-11-18 10:55:16 +00001976 HandleInvoke(invoke);
1977}
1978
Andreas Gampe878d58c2015-01-15 23:24:00 -08001979static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
1980 if (invoke->GetLocations()->Intrinsified()) {
1981 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
1982 intrinsic.Dispatch(invoke);
1983 return true;
1984 }
1985 return false;
1986}
1987
1988void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Register temp) {
1989 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
1990 DCHECK(temp.Is(kArtMethodRegister));
Alexandre Rames5319def2014-10-23 10:03:10 +01001991 size_t index_in_cache = mirror::Array::DataOffset(kHeapRefSize).SizeValue() +
Andreas Gampe878d58c2015-01-15 23:24:00 -08001992 invoke->GetDexMethodIndex() * kHeapRefSize;
Alexandre Rames5319def2014-10-23 10:03:10 +01001993
1994 // TODO: Implement all kinds of calls:
1995 // 1) boot -> boot
1996 // 2) app -> boot
1997 // 3) app -> app
1998 //
1999 // Currently we implement the app -> app logic, which looks up in the resolve cache.
2000
Nicolas Geoffray0a299b92015-01-29 11:39:44 +00002001 // temp = method;
2002 LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002003 if (!invoke->IsRecursive()) {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002004 // temp = temp->dex_cache_resolved_methods_;
2005 __ Ldr(temp, HeapOperand(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset()));
2006 // temp = temp[index_in_cache];
2007 __ Ldr(temp, HeapOperand(temp, index_in_cache));
2008 // lr = temp->entry_point_from_quick_compiled_code_;
2009 __ Ldr(lr, HeapOperand(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2010 kArm64WordSize)));
2011 // lr();
2012 __ Blr(lr);
2013 } else {
2014 __ Bl(&frame_entry_label_);
2015 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002016
Andreas Gampe878d58c2015-01-15 23:24:00 -08002017 DCHECK(!IsLeafMethod());
2018}
2019
2020void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2021 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2022 return;
2023 }
2024
2025 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
2026 codegen_->GenerateStaticOrDirectCall(invoke, temp);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002027 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002028}
2029
2030void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002031 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2032 return;
2033 }
2034
Alexandre Rames5319def2014-10-23 10:03:10 +01002035 LocationSummary* locations = invoke->GetLocations();
2036 Location receiver = locations->InAt(0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002037 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002038 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
2039 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
2040 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00002041 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002042
2043 // temp = object->GetClass();
2044 if (receiver.IsStackSlot()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002045 __ Ldr(temp, MemOperand(sp, receiver.GetStackIndex()));
2046 __ Ldr(temp, HeapOperand(temp, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002047 } else {
2048 DCHECK(receiver.IsRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002049 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002050 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002051 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames5319def2014-10-23 10:03:10 +01002052 // temp = temp->GetMethodAt(method_offset);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002053 __ Ldr(temp, HeapOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002054 // lr = temp->GetEntryPoint();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002055 __ Ldr(lr, HeapOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002056 // lr();
2057 __ Blr(lr);
2058 DCHECK(!codegen_->IsLeafMethod());
2059 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2060}
2061
Alexandre Rames67555f72014-11-18 10:55:16 +00002062void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2063 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2064 : LocationSummary::kNoCall;
2065 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
2066 locations->SetOut(Location::RequiresRegister());
2067}
2068
2069void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2070 Register out = OutputRegister(cls);
2071 if (cls->IsReferrersClass()) {
2072 DCHECK(!cls->CanCallRuntime());
2073 DCHECK(!cls->MustGenerateClinitCheck());
2074 codegen_->LoadCurrentMethod(out);
2075 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
2076 } else {
2077 DCHECK(cls->CanCallRuntime());
2078 codegen_->LoadCurrentMethod(out);
2079 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DexCacheResolvedTypesOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002080 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002081
2082 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2083 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2084 codegen_->AddSlowPath(slow_path);
2085 __ Cbz(out, slow_path->GetEntryLabel());
2086 if (cls->MustGenerateClinitCheck()) {
2087 GenerateClassInitializationCheck(slow_path, out);
2088 } else {
2089 __ Bind(slow_path->GetExitLabel());
2090 }
2091 }
2092}
2093
2094void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2095 LocationSummary* locations =
2096 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2097 locations->SetOut(Location::RequiresRegister());
2098}
2099
2100void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
2101 MemOperand exception = MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2102 __ Ldr(OutputRegister(instruction), exception);
2103 __ Str(wzr, exception);
2104}
2105
Alexandre Rames5319def2014-10-23 10:03:10 +01002106void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2107 load->SetLocations(nullptr);
2108}
2109
2110void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2111 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002112 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002113}
2114
Alexandre Rames67555f72014-11-18 10:55:16 +00002115void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2116 LocationSummary* locations =
2117 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2118 locations->SetOut(Location::RequiresRegister());
2119}
2120
2121void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2122 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2123 codegen_->AddSlowPath(slow_path);
2124
2125 Register out = OutputRegister(load);
2126 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08002127 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
2128 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002129 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002130 __ Cbz(out, slow_path->GetEntryLabel());
2131 __ Bind(slow_path->GetExitLabel());
2132}
2133
Alexandre Rames5319def2014-10-23 10:03:10 +01002134void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2135 local->SetLocations(nullptr);
2136}
2137
2138void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2139 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2140}
2141
2142void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2143 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2144 locations->SetOut(Location::ConstantLocation(constant));
2145}
2146
2147void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2148 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002149 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002150}
2151
Alexandre Rames67555f72014-11-18 10:55:16 +00002152void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2153 LocationSummary* locations =
2154 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2155 InvokeRuntimeCallingConvention calling_convention;
2156 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2157}
2158
2159void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2160 codegen_->InvokeRuntime(instruction->IsEnter()
2161 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2162 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002163 instruction->GetDexPc(),
2164 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002165 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002166}
2167
Alexandre Rames42d641b2014-10-27 14:00:51 +00002168void LocationsBuilderARM64::VisitMul(HMul* mul) {
2169 LocationSummary* locations =
2170 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2171 switch (mul->GetResultType()) {
2172 case Primitive::kPrimInt:
2173 case Primitive::kPrimLong:
2174 locations->SetInAt(0, Location::RequiresRegister());
2175 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002176 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002177 break;
2178
2179 case Primitive::kPrimFloat:
2180 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002181 locations->SetInAt(0, Location::RequiresFpuRegister());
2182 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002183 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002184 break;
2185
2186 default:
2187 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2188 }
2189}
2190
2191void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2192 switch (mul->GetResultType()) {
2193 case Primitive::kPrimInt:
2194 case Primitive::kPrimLong:
2195 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2196 break;
2197
2198 case Primitive::kPrimFloat:
2199 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002200 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002201 break;
2202
2203 default:
2204 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2205 }
2206}
2207
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002208void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2209 LocationSummary* locations =
2210 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2211 switch (neg->GetResultType()) {
2212 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002213 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002214 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002215 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002216 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002217
2218 case Primitive::kPrimFloat:
2219 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002220 locations->SetInAt(0, Location::RequiresFpuRegister());
2221 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002222 break;
2223
2224 default:
2225 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2226 }
2227}
2228
2229void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2230 switch (neg->GetResultType()) {
2231 case Primitive::kPrimInt:
2232 case Primitive::kPrimLong:
2233 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2234 break;
2235
2236 case Primitive::kPrimFloat:
2237 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002238 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002239 break;
2240
2241 default:
2242 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2243 }
2244}
2245
2246void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2247 LocationSummary* locations =
2248 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2249 InvokeRuntimeCallingConvention calling_convention;
2250 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002251 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002252 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002253 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2254 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2255 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002256}
2257
2258void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2259 LocationSummary* locations = instruction->GetLocations();
2260 InvokeRuntimeCallingConvention calling_convention;
2261 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2262 DCHECK(type_index.Is(w0));
2263 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002264 DCHECK(current_method.Is(w2));
Alexandre Rames67555f72014-11-18 10:55:16 +00002265 codegen_->LoadCurrentMethod(current_method);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002266 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002267 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002268 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2269 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002270 instruction->GetDexPc(),
2271 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002272 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2273 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002274}
2275
Alexandre Rames5319def2014-10-23 10:03:10 +01002276void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2277 LocationSummary* locations =
2278 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2279 InvokeRuntimeCallingConvention calling_convention;
2280 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2281 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2282 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002283 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002284}
2285
2286void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2287 LocationSummary* locations = instruction->GetLocations();
2288 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2289 DCHECK(type_index.Is(w0));
2290 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
2291 DCHECK(current_method.Is(w1));
Alexandre Rames67555f72014-11-18 10:55:16 +00002292 codegen_->LoadCurrentMethod(current_method);
Alexandre Rames5319def2014-10-23 10:03:10 +01002293 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002294 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002295 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2296 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002297 instruction->GetDexPc(),
2298 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002299 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002300}
2301
2302void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2303 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002304 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002305 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002306}
2307
2308void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002309 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002310 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002311 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002312 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002313 break;
2314
2315 default:
2316 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2317 }
2318}
2319
David Brazdil66d126e2015-04-03 16:02:44 +01002320void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2321 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2322 locations->SetInAt(0, Location::RequiresRegister());
2323 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2324}
2325
2326void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002327 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2328}
2329
Alexandre Rames5319def2014-10-23 10:03:10 +01002330void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2331 LocationSummary* locations =
2332 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2333 locations->SetInAt(0, Location::RequiresRegister());
2334 if (instruction->HasUses()) {
2335 locations->SetOut(Location::SameAsFirstInput());
2336 }
2337}
2338
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002339void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002340 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2341 return;
2342 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002343 Location obj = instruction->GetLocations()->InAt(0);
2344
2345 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2346 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2347}
2348
2349void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002350 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2351 codegen_->AddSlowPath(slow_path);
2352
2353 LocationSummary* locations = instruction->GetLocations();
2354 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002355
2356 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002357}
2358
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002359void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2360 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2361 GenerateImplicitNullCheck(instruction);
2362 } else {
2363 GenerateExplicitNullCheck(instruction);
2364 }
2365}
2366
Alexandre Rames67555f72014-11-18 10:55:16 +00002367void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2368 HandleBinaryOp(instruction);
2369}
2370
2371void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2372 HandleBinaryOp(instruction);
2373}
2374
Alexandre Rames3e69f162014-12-10 10:36:50 +00002375void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2376 LOG(FATAL) << "Unreachable";
2377}
2378
2379void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2380 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2381}
2382
Alexandre Rames5319def2014-10-23 10:03:10 +01002383void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2384 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2385 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2386 if (location.IsStackSlot()) {
2387 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2388 } else if (location.IsDoubleStackSlot()) {
2389 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2390 }
2391 locations->SetOut(location);
2392}
2393
2394void InstructionCodeGeneratorARM64::VisitParameterValue(HParameterValue* instruction) {
2395 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002396 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002397}
2398
2399void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2400 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2401 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2402 locations->SetInAt(i, Location::Any());
2403 }
2404 locations->SetOut(Location::Any());
2405}
2406
2407void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002408 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002409 LOG(FATAL) << "Unreachable";
2410}
2411
Serban Constantinescu02164b32014-11-13 14:05:07 +00002412void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002413 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002414 LocationSummary::CallKind call_kind =
2415 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002416 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2417
2418 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002419 case Primitive::kPrimInt:
2420 case Primitive::kPrimLong:
2421 locations->SetInAt(0, Location::RequiresRegister());
2422 locations->SetInAt(1, Location::RequiresRegister());
2423 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2424 break;
2425
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002426 case Primitive::kPrimFloat:
2427 case Primitive::kPrimDouble: {
2428 InvokeRuntimeCallingConvention calling_convention;
2429 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2430 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2431 locations->SetOut(calling_convention.GetReturnLocation(type));
2432
2433 break;
2434 }
2435
Serban Constantinescu02164b32014-11-13 14:05:07 +00002436 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002437 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002438 }
2439}
2440
2441void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2442 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002443
Serban Constantinescu02164b32014-11-13 14:05:07 +00002444 switch (type) {
2445 case Primitive::kPrimInt:
2446 case Primitive::kPrimLong: {
2447 UseScratchRegisterScope temps(GetVIXLAssembler());
2448 Register dividend = InputRegisterAt(rem, 0);
2449 Register divisor = InputRegisterAt(rem, 1);
2450 Register output = OutputRegister(rem);
2451 Register temp = temps.AcquireSameSizeAs(output);
2452
2453 __ Sdiv(temp, dividend, divisor);
2454 __ Msub(output, temp, divisor, dividend);
2455 break;
2456 }
2457
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002458 case Primitive::kPrimFloat:
2459 case Primitive::kPrimDouble: {
2460 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2461 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002462 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002463 break;
2464 }
2465
Serban Constantinescu02164b32014-11-13 14:05:07 +00002466 default:
2467 LOG(FATAL) << "Unexpected rem type " << type;
2468 }
2469}
2470
Alexandre Rames5319def2014-10-23 10:03:10 +01002471void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2472 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2473 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002474 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002475}
2476
2477void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002478 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002479 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002480}
2481
2482void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2483 instruction->SetLocations(nullptr);
2484}
2485
2486void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002487 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002488 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002489}
2490
Serban Constantinescu02164b32014-11-13 14:05:07 +00002491void LocationsBuilderARM64::VisitShl(HShl* shl) {
2492 HandleShift(shl);
2493}
2494
2495void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2496 HandleShift(shl);
2497}
2498
2499void LocationsBuilderARM64::VisitShr(HShr* shr) {
2500 HandleShift(shr);
2501}
2502
2503void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2504 HandleShift(shr);
2505}
2506
Alexandre Rames5319def2014-10-23 10:03:10 +01002507void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2508 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2509 Primitive::Type field_type = store->InputAt(1)->GetType();
2510 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002511 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002512 case Primitive::kPrimBoolean:
2513 case Primitive::kPrimByte:
2514 case Primitive::kPrimChar:
2515 case Primitive::kPrimShort:
2516 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002517 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002518 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2519 break;
2520
2521 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002522 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002523 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2524 break;
2525
2526 default:
2527 LOG(FATAL) << "Unimplemented local type " << field_type;
2528 }
2529}
2530
2531void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002532 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002533}
2534
2535void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002536 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002537}
2538
2539void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002540 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002541}
2542
Alexandre Rames67555f72014-11-18 10:55:16 +00002543void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002544 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002545}
2546
2547void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002548 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00002549}
2550
2551void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002552 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002553}
2554
Alexandre Rames67555f72014-11-18 10:55:16 +00002555void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002556 HandleFieldSet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002557}
2558
2559void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2560 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2561}
2562
2563void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002564 HBasicBlock* block = instruction->GetBlock();
2565 if (block->GetLoopInformation() != nullptr) {
2566 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2567 // The back edge will generate the suspend check.
2568 return;
2569 }
2570 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2571 // The goto will generate the suspend check.
2572 return;
2573 }
2574 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01002575}
2576
2577void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
2578 temp->SetLocations(nullptr);
2579}
2580
2581void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
2582 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002583 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01002584}
2585
Alexandre Rames67555f72014-11-18 10:55:16 +00002586void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
2587 LocationSummary* locations =
2588 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2589 InvokeRuntimeCallingConvention calling_convention;
2590 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2591}
2592
2593void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
2594 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002595 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002596 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002597}
2598
2599void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
2600 LocationSummary* locations =
2601 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2602 Primitive::Type input_type = conversion->GetInputType();
2603 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002604 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00002605 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
2606 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
2607 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
2608 }
2609
Alexandre Rames542361f2015-01-29 16:57:31 +00002610 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002611 locations->SetInAt(0, Location::RequiresFpuRegister());
2612 } else {
2613 locations->SetInAt(0, Location::RequiresRegister());
2614 }
2615
Alexandre Rames542361f2015-01-29 16:57:31 +00002616 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002617 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2618 } else {
2619 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2620 }
2621}
2622
2623void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
2624 Primitive::Type result_type = conversion->GetResultType();
2625 Primitive::Type input_type = conversion->GetInputType();
2626
2627 DCHECK_NE(input_type, result_type);
2628
Alexandre Rames542361f2015-01-29 16:57:31 +00002629 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002630 int result_size = Primitive::ComponentSize(result_type);
2631 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002632 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002633 Register output = OutputRegister(conversion);
2634 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002635 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
2636 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
2637 } else if ((result_type == Primitive::kPrimChar) ||
2638 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
2639 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002640 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002641 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002642 }
Alexandre Rames542361f2015-01-29 16:57:31 +00002643 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002644 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002645 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002646 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
2647 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002648 } else if (Primitive::IsFloatingPointType(result_type) &&
2649 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002650 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
2651 } else {
2652 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
2653 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00002654 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002655}
Alexandre Rames67555f72014-11-18 10:55:16 +00002656
Serban Constantinescu02164b32014-11-13 14:05:07 +00002657void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
2658 HandleShift(ushr);
2659}
2660
2661void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
2662 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00002663}
2664
2665void LocationsBuilderARM64::VisitXor(HXor* instruction) {
2666 HandleBinaryOp(instruction);
2667}
2668
2669void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
2670 HandleBinaryOp(instruction);
2671}
2672
Calin Juravleb1498f62015-02-16 13:13:29 +00002673void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
2674 // Nothing to do, this should be removed during prepare for register allocator.
2675 UNUSED(instruction);
2676 LOG(FATAL) << "Unreachable";
2677}
2678
2679void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
2680 // Nothing to do, this should be removed during prepare for register allocator.
2681 UNUSED(instruction);
2682 LOG(FATAL) << "Unreachable";
2683}
2684
Alexandre Rames67555f72014-11-18 10:55:16 +00002685#undef __
2686#undef QUICK_ENTRY_POINT
2687
Alexandre Rames5319def2014-10-23 10:03:10 +01002688} // namespace arm64
2689} // namespace art