blob: 03d344555f9c985a93e26317db27f83000accf94 [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(
125 index_location_, LocationFrom(calling_convention.GetRegisterAt(0)),
126 length_location_, LocationFrom(calling_convention.GetRegisterAt(1)));
127 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(
325 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)),
326 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)));
327
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
Alexandre Rames5319def2014-10-23 10:03:10 +0100355#undef __
356
357Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
358 Location next_location;
359 if (type == Primitive::kPrimVoid) {
360 LOG(FATAL) << "Unreachable type " << type;
361 }
362
Alexandre Rames542361f2015-01-29 16:57:31 +0000363 if (Primitive::IsFloatingPointType(type) &&
364 (fp_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000365 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(fp_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000366 } else if (!Primitive::IsFloatingPointType(type) &&
367 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000368 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
369 } else {
370 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000371 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
372 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100373 }
374
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000375 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000376 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100377 return next_location;
378}
379
Serban Constantinescu579885a2015-02-22 20:51:33 +0000380CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
381 const Arm64InstructionSetFeatures& isa_features,
382 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100383 : CodeGenerator(graph,
384 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000385 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000386 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000387 callee_saved_core_registers.list(),
388 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000389 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100390 block_labels_(nullptr),
391 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000392 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000393 move_resolver_(graph->GetArena(), this),
394 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000395 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000396 AddAllocatedRegister(LocationFrom(lr));
Andreas Gampe75fda572015-04-01 00:36:42 -0700397
398 // Workaround for valgrind undefined recommended_checkpoint_.
399 // This won't do anything, as the literal pool is empty, but initialize the field.
400 GetVIXLAssembler()->EmitLiteralPool(LiteralPool::EmitOption::kNoBranchRequired);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000401}
Alexandre Rames5319def2014-10-23 10:03:10 +0100402
Alexandre Rames67555f72014-11-18 10:55:16 +0000403#undef __
404#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100405
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000406void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
407 // Ensure we emit the literal pool.
408 __ FinalizeCode();
409 CodeGenerator::Finalize(allocator);
410}
411
Alexandre Rames3e69f162014-12-10 10:36:50 +0000412void ParallelMoveResolverARM64::EmitMove(size_t index) {
413 MoveOperands* move = moves_.Get(index);
414 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
415}
416
417void ParallelMoveResolverARM64::EmitSwap(size_t index) {
418 MoveOperands* move = moves_.Get(index);
419 codegen_->SwapLocations(move->GetDestination(), move->GetSource());
420}
421
422void ParallelMoveResolverARM64::RestoreScratch(int reg) {
423 __ Pop(Register(VIXLRegCodeFromART(reg), kXRegSize));
424}
425
426void ParallelMoveResolverARM64::SpillScratch(int reg) {
427 __ Push(Register(VIXLRegCodeFromART(reg), kXRegSize));
428}
429
Alexandre Rames5319def2014-10-23 10:03:10 +0100430void CodeGeneratorARM64::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000431 __ Bind(&frame_entry_label_);
432
Serban Constantinescu02164b32014-11-13 14:05:07 +0000433 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
434 if (do_overflow_check) {
435 UseScratchRegisterScope temps(GetVIXLAssembler());
436 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000437 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000438 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000439 __ Ldr(wzr, MemOperand(temp, 0));
440 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000441 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100442
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000443 if (!HasEmptyFrame()) {
444 int frame_size = GetFrameSize();
445 // Stack layout:
446 // sp[frame_size - 8] : lr.
447 // ... : other preserved core registers.
448 // ... : other preserved fp registers.
449 // ... : reserved frame space.
450 // sp[0] : current method.
451 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
452 __ PokeCPURegList(GetFramePreservedCoreRegisters(), frame_size - GetCoreSpillSize());
453 __ PokeCPURegList(GetFramePreservedFPRegisters(), frame_size - FrameEntrySpillSize());
454 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100455}
456
457void CodeGeneratorARM64::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000458 if (!HasEmptyFrame()) {
459 int frame_size = GetFrameSize();
460 __ PeekCPURegList(GetFramePreservedFPRegisters(), frame_size - FrameEntrySpillSize());
461 __ PeekCPURegList(GetFramePreservedCoreRegisters(), frame_size - GetCoreSpillSize());
462 __ Drop(frame_size);
463 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100464}
465
466void CodeGeneratorARM64::Bind(HBasicBlock* block) {
467 __ Bind(GetLabelOf(block));
468}
469
Alexandre Rames5319def2014-10-23 10:03:10 +0100470void CodeGeneratorARM64::Move(HInstruction* instruction,
471 Location location,
472 HInstruction* move_for) {
473 LocationSummary* locations = instruction->GetLocations();
474 if (locations != nullptr && locations->Out().Equals(location)) {
475 return;
476 }
477
478 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000479 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100480
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000481 if (instruction->IsIntConstant()
482 || instruction->IsLongConstant()
483 || instruction->IsNullConstant()) {
484 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100485 if (location.IsRegister()) {
486 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000487 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100488 (instruction->IsLongConstant() && dst.Is64Bits()));
489 __ Mov(dst, value);
490 } else {
491 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000492 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000493 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
494 ? temps.AcquireW()
495 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100496 __ Mov(temp, value);
497 __ Str(temp, StackOperandFrom(location));
498 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000499 } else if (instruction->IsTemporary()) {
500 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000501 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100502 } else if (instruction->IsLoadLocal()) {
503 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000504 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000505 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000506 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000507 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100508 }
509
510 } else {
511 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000512 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100513 }
514}
515
Alexandre Rames5319def2014-10-23 10:03:10 +0100516Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
517 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000518
Alexandre Rames5319def2014-10-23 10:03:10 +0100519 switch (type) {
520 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000521 case Primitive::kPrimInt:
522 case Primitive::kPrimFloat:
523 return Location::StackSlot(GetStackSlot(load->GetLocal()));
524
525 case Primitive::kPrimLong:
526 case Primitive::kPrimDouble:
527 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
528
Alexandre Rames5319def2014-10-23 10:03:10 +0100529 case Primitive::kPrimBoolean:
530 case Primitive::kPrimByte:
531 case Primitive::kPrimChar:
532 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100533 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100534 LOG(FATAL) << "Unexpected type " << type;
535 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000536
Alexandre Rames5319def2014-10-23 10:03:10 +0100537 LOG(FATAL) << "Unreachable";
538 return Location::NoLocation();
539}
540
541void CodeGeneratorARM64::MarkGCCard(Register object, Register value) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000542 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100543 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000544 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100545 vixl::Label done;
546 __ Cbz(value, &done);
547 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
548 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000549 __ Strb(card, MemOperand(card, temp.X()));
Alexandre Rames5319def2014-10-23 10:03:10 +0100550 __ Bind(&done);
551}
552
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000553void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
554 // Blocked core registers:
555 // lr : Runtime reserved.
556 // tr : Runtime reserved.
557 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
558 // ip1 : VIXL core temp.
559 // ip0 : VIXL core temp.
560 //
561 // Blocked fp registers:
562 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100563 CPURegList reserved_core_registers = vixl_reserved_core_registers;
564 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100565 while (!reserved_core_registers.IsEmpty()) {
566 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
567 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000568
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000569 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800570 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000571 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
572 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000573
574 if (is_baseline) {
575 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
576 while (!reserved_core_baseline_registers.IsEmpty()) {
577 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
578 }
579
580 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
581 while (!reserved_fp_baseline_registers.IsEmpty()) {
582 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
583 }
584 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100585}
586
587Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
588 if (type == Primitive::kPrimVoid) {
589 LOG(FATAL) << "Unreachable type " << type;
590 }
591
Alexandre Rames542361f2015-01-29 16:57:31 +0000592 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000593 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
594 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100595 return Location::FpuRegisterLocation(reg);
596 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000597 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
598 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100599 return Location::RegisterLocation(reg);
600 }
601}
602
Alexandre Rames3e69f162014-12-10 10:36:50 +0000603size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
604 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
605 __ Str(reg, MemOperand(sp, stack_index));
606 return kArm64WordSize;
607}
608
609size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
610 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
611 __ Ldr(reg, MemOperand(sp, stack_index));
612 return kArm64WordSize;
613}
614
615size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
616 FPRegister reg = FPRegister(reg_id, kDRegSize);
617 __ Str(reg, MemOperand(sp, stack_index));
618 return kArm64WordSize;
619}
620
621size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
622 FPRegister reg = FPRegister(reg_id, kDRegSize);
623 __ Ldr(reg, MemOperand(sp, stack_index));
624 return kArm64WordSize;
625}
626
Alexandre Rames5319def2014-10-23 10:03:10 +0100627void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
628 stream << Arm64ManagedRegister::FromXRegister(XRegister(reg));
629}
630
631void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
632 stream << Arm64ManagedRegister::FromDRegister(DRegister(reg));
633}
634
Alexandre Rames67555f72014-11-18 10:55:16 +0000635void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000636 if (constant->IsIntConstant()) {
637 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
638 } else if (constant->IsLongConstant()) {
639 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
640 } else if (constant->IsNullConstant()) {
641 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000642 } else if (constant->IsFloatConstant()) {
643 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
644 } else {
645 DCHECK(constant->IsDoubleConstant());
646 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
647 }
648}
649
Alexandre Rames3e69f162014-12-10 10:36:50 +0000650
651static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
652 DCHECK(constant.IsConstant());
653 HConstant* cst = constant.GetConstant();
654 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000655 // Null is mapped to a core W register, which we associate with kPrimInt.
656 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000657 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
658 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
659 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
660}
661
662void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000663 if (source.Equals(destination)) {
664 return;
665 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000666
667 // A valid move can always be inferred from the destination and source
668 // locations. When moving from and to a register, the argument type can be
669 // used to generate 32bit instead of 64bit moves. In debug mode we also
670 // checks the coherency of the locations and the type.
671 bool unspecified_type = (type == Primitive::kPrimVoid);
672
673 if (destination.IsRegister() || destination.IsFpuRegister()) {
674 if (unspecified_type) {
675 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
676 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000677 (src_cst != nullptr && (src_cst->IsIntConstant()
678 || src_cst->IsFloatConstant()
679 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000680 // For stack slots and 32bit constants, a 64bit type is appropriate.
681 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000682 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000683 // If the source is a double stack slot or a 64bit constant, a 64bit
684 // type is appropriate. Else the source is a register, and since the
685 // type has not been specified, we chose a 64bit type to force a 64bit
686 // move.
687 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000688 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000689 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000690 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
691 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000692 CPURegister dst = CPURegisterFrom(destination, type);
693 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
694 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
695 __ Ldr(dst, StackOperandFrom(source));
696 } else if (source.IsConstant()) {
697 DCHECK(CoherentConstantAndType(source, type));
698 MoveConstant(dst, source.GetConstant());
699 } else {
700 if (destination.IsRegister()) {
701 __ Mov(Register(dst), RegisterFrom(source, type));
702 } else {
703 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
704 }
705 }
706
707 } else { // The destination is not a register. It must be a stack slot.
708 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
709 if (source.IsRegister() || source.IsFpuRegister()) {
710 if (unspecified_type) {
711 if (source.IsRegister()) {
712 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
713 } else {
714 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
715 }
716 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000717 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
718 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000719 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
720 } else if (source.IsConstant()) {
721 DCHECK(unspecified_type || CoherentConstantAndType(source, type));
722 UseScratchRegisterScope temps(GetVIXLAssembler());
723 HConstant* src_cst = source.GetConstant();
724 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000725 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000726 temp = temps.AcquireW();
727 } else if (src_cst->IsLongConstant()) {
728 temp = temps.AcquireX();
729 } else if (src_cst->IsFloatConstant()) {
730 temp = temps.AcquireS();
731 } else {
732 DCHECK(src_cst->IsDoubleConstant());
733 temp = temps.AcquireD();
734 }
735 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000736 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000737 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000738 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000739 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000740 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000741 // There is generally less pressure on FP registers.
742 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000743 __ Ldr(temp, StackOperandFrom(source));
744 __ Str(temp, StackOperandFrom(destination));
745 }
746 }
747}
748
Alexandre Rames3e69f162014-12-10 10:36:50 +0000749void CodeGeneratorARM64::SwapLocations(Location loc1, Location loc2) {
750 DCHECK(!loc1.IsConstant());
751 DCHECK(!loc2.IsConstant());
752
753 if (loc1.Equals(loc2)) {
754 return;
755 }
756
757 UseScratchRegisterScope temps(GetAssembler()->vixl_masm_);
758
759 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
760 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
761 bool is_fp_reg1 = loc1.IsFpuRegister();
762 bool is_fp_reg2 = loc2.IsFpuRegister();
763
764 if (loc2.IsRegister() && loc1.IsRegister()) {
765 Register r1 = XRegisterFrom(loc1);
766 Register r2 = XRegisterFrom(loc2);
767 Register tmp = temps.AcquireSameSizeAs(r1);
768 __ Mov(tmp, r2);
769 __ Mov(r2, r1);
770 __ Mov(r1, tmp);
771 } else if (is_fp_reg2 && is_fp_reg1) {
772 FPRegister r1 = DRegisterFrom(loc1);
773 FPRegister r2 = DRegisterFrom(loc2);
774 FPRegister tmp = temps.AcquireSameSizeAs(r1);
775 __ Fmov(tmp, r2);
776 __ Fmov(r2, r1);
777 __ Fmov(r1, tmp);
778 } else if (is_slot1 != is_slot2) {
779 MemOperand mem = StackOperandFrom(is_slot1 ? loc1 : loc2);
780 Location reg_loc = is_slot1 ? loc2 : loc1;
781 CPURegister reg, tmp;
782 if (reg_loc.IsFpuRegister()) {
783 reg = DRegisterFrom(reg_loc);
784 tmp = temps.AcquireD();
785 } else {
786 reg = XRegisterFrom(reg_loc);
787 tmp = temps.AcquireX();
788 }
789 __ Ldr(tmp, mem);
790 __ Str(reg, mem);
791 if (reg_loc.IsFpuRegister()) {
792 __ Fmov(FPRegister(reg), FPRegister(tmp));
793 } else {
794 __ Mov(Register(reg), Register(tmp));
795 }
796 } else if (is_slot1 && is_slot2) {
797 MemOperand mem1 = StackOperandFrom(loc1);
798 MemOperand mem2 = StackOperandFrom(loc2);
799 Register tmp1 = loc1.IsStackSlot() ? temps.AcquireW() : temps.AcquireX();
800 Register tmp2 = temps.AcquireSameSizeAs(tmp1);
801 __ Ldr(tmp1, mem1);
802 __ Ldr(tmp2, mem2);
803 __ Str(tmp1, mem2);
804 __ Str(tmp2, mem1);
805 } else {
806 LOG(FATAL) << "Unimplemented";
807 }
808}
809
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000810void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000811 CPURegister dst,
812 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000813 switch (type) {
814 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000815 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000816 break;
817 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000818 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000819 break;
820 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000821 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000822 break;
823 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000824 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000825 break;
826 case Primitive::kPrimInt:
827 case Primitive::kPrimNot:
828 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000829 case Primitive::kPrimFloat:
830 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000831 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000832 __ Ldr(dst, src);
833 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000834 case Primitive::kPrimVoid:
835 LOG(FATAL) << "Unreachable type " << type;
836 }
837}
838
Calin Juravle77520bc2015-01-12 18:45:46 +0000839void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000840 CPURegister dst,
841 const MemOperand& src) {
842 UseScratchRegisterScope temps(GetVIXLAssembler());
843 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000844 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000845
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000846 DCHECK(!src.IsPreIndex());
847 DCHECK(!src.IsPostIndex());
848
849 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800850 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000851 MemOperand base = MemOperand(temp_base);
852 switch (type) {
853 case Primitive::kPrimBoolean:
854 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000855 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000856 break;
857 case Primitive::kPrimByte:
858 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000859 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000860 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
861 break;
862 case Primitive::kPrimChar:
863 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000864 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000865 break;
866 case Primitive::kPrimShort:
867 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000868 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000869 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
870 break;
871 case Primitive::kPrimInt:
872 case Primitive::kPrimNot:
873 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000874 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000875 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000876 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000877 break;
878 case Primitive::kPrimFloat:
879 case Primitive::kPrimDouble: {
880 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000881 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000882
883 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
884 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000885 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000886 __ Fmov(FPRegister(dst), temp);
887 break;
888 }
889 case Primitive::kPrimVoid:
890 LOG(FATAL) << "Unreachable type " << type;
891 }
892}
893
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000894void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000895 CPURegister src,
896 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000897 switch (type) {
898 case Primitive::kPrimBoolean:
899 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000900 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000901 break;
902 case Primitive::kPrimChar:
903 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000904 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000905 break;
906 case Primitive::kPrimInt:
907 case Primitive::kPrimNot:
908 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000909 case Primitive::kPrimFloat:
910 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000911 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000912 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000913 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000914 case Primitive::kPrimVoid:
915 LOG(FATAL) << "Unreachable type " << type;
916 }
917}
918
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000919void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
920 CPURegister src,
921 const MemOperand& dst) {
922 UseScratchRegisterScope temps(GetVIXLAssembler());
923 Register temp_base = temps.AcquireX();
924
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000925 DCHECK(!dst.IsPreIndex());
926 DCHECK(!dst.IsPostIndex());
927
928 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800929 Operand op = OperandFromMemOperand(dst);
930 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000931 MemOperand base = MemOperand(temp_base);
932 switch (type) {
933 case Primitive::kPrimBoolean:
934 case Primitive::kPrimByte:
935 __ Stlrb(Register(src), base);
936 break;
937 case Primitive::kPrimChar:
938 case Primitive::kPrimShort:
939 __ Stlrh(Register(src), base);
940 break;
941 case Primitive::kPrimInt:
942 case Primitive::kPrimNot:
943 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000944 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000945 __ Stlr(Register(src), base);
946 break;
947 case Primitive::kPrimFloat:
948 case Primitive::kPrimDouble: {
949 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000950 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000951
952 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
953 __ Fmov(temp, FPRegister(src));
954 __ Stlr(temp, base);
955 break;
956 }
957 case Primitive::kPrimVoid:
958 LOG(FATAL) << "Unreachable type " << type;
959 }
960}
961
Alexandre Rames67555f72014-11-18 10:55:16 +0000962void CodeGeneratorARM64::LoadCurrentMethod(vixl::Register current_method) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000963 DCHECK(RequiresCurrentMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +0000964 DCHECK(current_method.IsW());
965 __ Ldr(current_method, MemOperand(sp, kCurrentMethodStackOffset));
966}
967
968void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
969 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000970 uint32_t dex_pc,
971 SlowPathCode* slow_path) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000972 __ Ldr(lr, MemOperand(tr, entry_point_offset));
973 __ Blr(lr);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000974 if (instruction != nullptr) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000975 RecordPcInfo(instruction, dex_pc, slow_path);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000976 DCHECK(instruction->IsSuspendCheck()
977 || instruction->IsBoundsCheck()
978 || instruction->IsNullCheck()
979 || instruction->IsDivZeroCheck()
980 || !IsLeafMethod());
981 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000982}
983
984void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
985 vixl::Register class_reg) {
986 UseScratchRegisterScope temps(GetVIXLAssembler());
987 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000988 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +0000989 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000990
Serban Constantinescu02164b32014-11-13 14:05:07 +0000991 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +0000992 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000993 // TODO(vixl): Let the MacroAssembler handle MemOperand.
994 __ Add(temp, class_reg, status_offset);
995 __ Ldar(temp, HeapOperand(temp));
996 __ Cmp(temp, mirror::Class::kStatusInitialized);
997 __ B(lt, slow_path->GetEntryLabel());
998 } else {
999 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1000 __ Cmp(temp, mirror::Class::kStatusInitialized);
1001 __ B(lt, slow_path->GetEntryLabel());
1002 __ Dmb(InnerShareable, BarrierReads);
1003 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001004 __ Bind(slow_path->GetExitLabel());
1005}
Alexandre Rames5319def2014-10-23 10:03:10 +01001006
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001007void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1008 BarrierType type = BarrierAll;
1009
1010 switch (kind) {
1011 case MemBarrierKind::kAnyAny:
1012 case MemBarrierKind::kAnyStore: {
1013 type = BarrierAll;
1014 break;
1015 }
1016 case MemBarrierKind::kLoadAny: {
1017 type = BarrierReads;
1018 break;
1019 }
1020 case MemBarrierKind::kStoreStore: {
1021 type = BarrierWrites;
1022 break;
1023 }
1024 default:
1025 LOG(FATAL) << "Unexpected memory barrier " << kind;
1026 }
1027 __ Dmb(InnerShareable, type);
1028}
1029
Serban Constantinescu02164b32014-11-13 14:05:07 +00001030void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1031 HBasicBlock* successor) {
1032 SuspendCheckSlowPathARM64* slow_path =
1033 new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1034 codegen_->AddSlowPath(slow_path);
1035 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1036 Register temp = temps.AcquireW();
1037
1038 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1039 if (successor == nullptr) {
1040 __ Cbnz(temp, slow_path->GetEntryLabel());
1041 __ Bind(slow_path->GetReturnLabel());
1042 } else {
1043 __ Cbz(temp, codegen_->GetLabelOf(successor));
1044 __ B(slow_path->GetEntryLabel());
1045 // slow_path will return to GetLabelOf(successor).
1046 }
1047}
1048
Alexandre Rames5319def2014-10-23 10:03:10 +01001049InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1050 CodeGeneratorARM64* codegen)
1051 : HGraphVisitor(graph),
1052 assembler_(codegen->GetAssembler()),
1053 codegen_(codegen) {}
1054
1055#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001056 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001057
1058#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1059
1060enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001061 // Using a base helps identify when we hit such breakpoints.
1062 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001063#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1064 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1065#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1066};
1067
1068#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1069 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001070 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001071 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1072 } \
1073 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1074 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1075 locations->SetOut(Location::Any()); \
1076 }
1077 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1078#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1079
1080#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001081#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001082
Alexandre Rames67555f72014-11-18 10:55:16 +00001083void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001084 DCHECK_EQ(instr->InputCount(), 2U);
1085 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1086 Primitive::Type type = instr->GetResultType();
1087 switch (type) {
1088 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001089 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001090 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001091 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001092 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001093 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001094
1095 case Primitive::kPrimFloat:
1096 case Primitive::kPrimDouble:
1097 locations->SetInAt(0, Location::RequiresFpuRegister());
1098 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001099 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001100 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001101
Alexandre Rames5319def2014-10-23 10:03:10 +01001102 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001103 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001104 }
1105}
1106
Alexandre Rames67555f72014-11-18 10:55:16 +00001107void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001108 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001109
1110 switch (type) {
1111 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001112 case Primitive::kPrimLong: {
1113 Register dst = OutputRegister(instr);
1114 Register lhs = InputRegisterAt(instr, 0);
1115 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001116 if (instr->IsAdd()) {
1117 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001118 } else if (instr->IsAnd()) {
1119 __ And(dst, lhs, rhs);
1120 } else if (instr->IsOr()) {
1121 __ Orr(dst, lhs, rhs);
1122 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001123 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001124 } else {
1125 DCHECK(instr->IsXor());
1126 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001127 }
1128 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001129 }
1130 case Primitive::kPrimFloat:
1131 case Primitive::kPrimDouble: {
1132 FPRegister dst = OutputFPRegister(instr);
1133 FPRegister lhs = InputFPRegisterAt(instr, 0);
1134 FPRegister rhs = InputFPRegisterAt(instr, 1);
1135 if (instr->IsAdd()) {
1136 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001137 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001138 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001139 } else {
1140 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001141 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001142 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001143 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001144 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001145 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001146 }
1147}
1148
Serban Constantinescu02164b32014-11-13 14:05:07 +00001149void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1150 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1151
1152 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1153 Primitive::Type type = instr->GetResultType();
1154 switch (type) {
1155 case Primitive::kPrimInt:
1156 case Primitive::kPrimLong: {
1157 locations->SetInAt(0, Location::RequiresRegister());
1158 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1159 locations->SetOut(Location::RequiresRegister());
1160 break;
1161 }
1162 default:
1163 LOG(FATAL) << "Unexpected shift type " << type;
1164 }
1165}
1166
1167void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1168 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1169
1170 Primitive::Type type = instr->GetType();
1171 switch (type) {
1172 case Primitive::kPrimInt:
1173 case Primitive::kPrimLong: {
1174 Register dst = OutputRegister(instr);
1175 Register lhs = InputRegisterAt(instr, 0);
1176 Operand rhs = InputOperandAt(instr, 1);
1177 if (rhs.IsImmediate()) {
1178 uint32_t shift_value = (type == Primitive::kPrimInt)
1179 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1180 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1181 if (instr->IsShl()) {
1182 __ Lsl(dst, lhs, shift_value);
1183 } else if (instr->IsShr()) {
1184 __ Asr(dst, lhs, shift_value);
1185 } else {
1186 __ Lsr(dst, lhs, shift_value);
1187 }
1188 } else {
1189 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1190
1191 if (instr->IsShl()) {
1192 __ Lsl(dst, lhs, rhs_reg);
1193 } else if (instr->IsShr()) {
1194 __ Asr(dst, lhs, rhs_reg);
1195 } else {
1196 __ Lsr(dst, lhs, rhs_reg);
1197 }
1198 }
1199 break;
1200 }
1201 default:
1202 LOG(FATAL) << "Unexpected shift operation type " << type;
1203 }
1204}
1205
Alexandre Rames5319def2014-10-23 10:03:10 +01001206void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001207 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001208}
1209
1210void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001211 HandleBinaryOp(instruction);
1212}
1213
1214void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1215 HandleBinaryOp(instruction);
1216}
1217
1218void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1219 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001220}
1221
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001222void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1223 LocationSummary* locations =
1224 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1225 locations->SetInAt(0, Location::RequiresRegister());
1226 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1227 locations->SetOut(Location::RequiresRegister());
1228}
1229
1230void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1231 LocationSummary* locations = instruction->GetLocations();
1232 Primitive::Type type = instruction->GetType();
1233 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001234 Location index = locations->InAt(1);
1235 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001236 MemOperand source = HeapOperand(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00001237 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001238
1239 if (index.IsConstant()) {
1240 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001241 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001242 } else {
1243 Register temp = temps.AcquireSameSizeAs(obj);
1244 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
1245 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001246 source = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001247 }
1248
Alexandre Rames67555f72014-11-18 10:55:16 +00001249 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001250 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001251}
1252
Alexandre Rames5319def2014-10-23 10:03:10 +01001253void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1254 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1255 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001256 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001257}
1258
1259void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
1260 __ Ldr(OutputRegister(instruction),
1261 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001262 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001263}
1264
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001265void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
1266 Primitive::Type value_type = instruction->GetComponentType();
1267 bool is_object = value_type == Primitive::kPrimNot;
1268 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1269 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1270 if (is_object) {
1271 InvokeRuntimeCallingConvention calling_convention;
1272 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1273 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1274 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1275 } else {
1276 locations->SetInAt(0, Location::RequiresRegister());
1277 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1278 locations->SetInAt(2, Location::RequiresRegister());
1279 }
1280}
1281
1282void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1283 Primitive::Type value_type = instruction->GetComponentType();
1284 if (value_type == Primitive::kPrimNot) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001285 codegen_->InvokeRuntime(
1286 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001287 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001288 } else {
1289 LocationSummary* locations = instruction->GetLocations();
1290 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001291 CPURegister value = InputCPURegisterAt(instruction, 2);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001292 Location index = locations->InAt(1);
1293 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001294 MemOperand destination = HeapOperand(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00001295 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001296
1297 if (index.IsConstant()) {
1298 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001299 destination = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001300 } else {
1301 Register temp = temps.AcquireSameSizeAs(obj);
1302 Register index_reg = InputRegisterAt(instruction, 1);
1303 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(value_type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001304 destination = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001305 }
1306
1307 codegen_->Store(value_type, value, destination);
Calin Juravle77520bc2015-01-12 18:45:46 +00001308 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001309 }
1310}
1311
Alexandre Rames67555f72014-11-18 10:55:16 +00001312void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1313 LocationSummary* locations =
1314 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1315 locations->SetInAt(0, Location::RequiresRegister());
1316 locations->SetInAt(1, Location::RequiresRegister());
1317 if (instruction->HasUses()) {
1318 locations->SetOut(Location::SameAsFirstInput());
1319 }
1320}
1321
1322void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001323 LocationSummary* locations = instruction->GetLocations();
1324 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1325 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001326 codegen_->AddSlowPath(slow_path);
1327
1328 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1329 __ B(slow_path->GetEntryLabel(), hs);
1330}
1331
1332void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1333 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1334 instruction, LocationSummary::kCallOnSlowPath);
1335 locations->SetInAt(0, Location::RequiresRegister());
1336 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001337 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001338}
1339
1340void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001341 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001342 Register obj = InputRegisterAt(instruction, 0);;
1343 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001344 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001345
Alexandre Rames3e69f162014-12-10 10:36:50 +00001346 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1347 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001348 codegen_->AddSlowPath(slow_path);
1349
1350 // TODO: avoid this check if we know obj is not null.
1351 __ Cbz(obj, slow_path->GetExitLabel());
1352 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001353 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
1354 __ Cmp(obj_cls, cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00001355 __ B(ne, slow_path->GetEntryLabel());
1356 __ Bind(slow_path->GetExitLabel());
1357}
1358
1359void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1360 LocationSummary* locations =
1361 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1362 locations->SetInAt(0, Location::RequiresRegister());
1363 if (check->HasUses()) {
1364 locations->SetOut(Location::SameAsFirstInput());
1365 }
1366}
1367
1368void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1369 // We assume the class is not null.
1370 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1371 check->GetLoadClass(), check, check->GetDexPc(), true);
1372 codegen_->AddSlowPath(slow_path);
1373 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1374}
1375
Serban Constantinescu02164b32014-11-13 14:05:07 +00001376void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001377 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001378 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1379 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001380 switch (in_type) {
1381 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001382 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001383 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001384 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1385 break;
1386 }
1387 case Primitive::kPrimFloat:
1388 case Primitive::kPrimDouble: {
1389 locations->SetInAt(0, Location::RequiresFpuRegister());
Alexandre Rames93415462015-02-17 15:08:20 +00001390 HInstruction* right = compare->InputAt(1);
1391 if ((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1392 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0))) {
1393 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1394 } else {
1395 locations->SetInAt(1, Location::RequiresFpuRegister());
1396 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001397 locations->SetOut(Location::RequiresRegister());
1398 break;
1399 }
1400 default:
1401 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1402 }
1403}
1404
1405void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1406 Primitive::Type in_type = compare->InputAt(0)->GetType();
1407
1408 // 0 if: left == right
1409 // 1 if: left > right
1410 // -1 if: left < right
1411 switch (in_type) {
1412 case Primitive::kPrimLong: {
1413 Register result = OutputRegister(compare);
1414 Register left = InputRegisterAt(compare, 0);
1415 Operand right = InputOperandAt(compare, 1);
1416
1417 __ Cmp(left, right);
1418 __ Cset(result, ne);
1419 __ Cneg(result, result, lt);
1420 break;
1421 }
1422 case Primitive::kPrimFloat:
1423 case Primitive::kPrimDouble: {
1424 Register result = OutputRegister(compare);
1425 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001426 if (compare->GetLocations()->InAt(1).IsConstant()) {
1427 if (kIsDebugBuild) {
1428 HInstruction* right = compare->GetLocations()->InAt(1).GetConstant();
1429 DCHECK((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1430 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0)));
1431 }
1432 // 0.0 is the only immediate that can be encoded directly in a FCMP instruction.
1433 __ Fcmp(left, 0.0);
1434 } else {
1435 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1436 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001437 if (compare->IsGtBias()) {
1438 __ Cset(result, ne);
1439 } else {
1440 __ Csetm(result, ne);
1441 }
1442 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001443 break;
1444 }
1445 default:
1446 LOG(FATAL) << "Unimplemented compare type " << in_type;
1447 }
1448}
1449
1450void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1451 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1452 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001453 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames5319def2014-10-23 10:03:10 +01001454 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001455 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001456 }
1457}
1458
1459void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1460 if (!instruction->NeedsMaterialization()) {
1461 return;
1462 }
1463
1464 LocationSummary* locations = instruction->GetLocations();
1465 Register lhs = InputRegisterAt(instruction, 0);
1466 Operand rhs = InputOperandAt(instruction, 1);
1467 Register res = RegisterFrom(locations->Out(), instruction->GetType());
1468 Condition cond = ARM64Condition(instruction->GetCondition());
1469
1470 __ Cmp(lhs, rhs);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001471 __ Cset(res, cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001472}
1473
1474#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1475 M(Equal) \
1476 M(NotEqual) \
1477 M(LessThan) \
1478 M(LessThanOrEqual) \
1479 M(GreaterThan) \
1480 M(GreaterThanOrEqual)
1481#define DEFINE_CONDITION_VISITORS(Name) \
1482void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1483void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1484FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001485#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001486#undef FOR_EACH_CONDITION_INSTRUCTION
1487
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001488void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1489 LocationSummary* locations =
1490 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1491 switch (div->GetResultType()) {
1492 case Primitive::kPrimInt:
1493 case Primitive::kPrimLong:
1494 locations->SetInAt(0, Location::RequiresRegister());
1495 locations->SetInAt(1, Location::RequiresRegister());
1496 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1497 break;
1498
1499 case Primitive::kPrimFloat:
1500 case Primitive::kPrimDouble:
1501 locations->SetInAt(0, Location::RequiresFpuRegister());
1502 locations->SetInAt(1, Location::RequiresFpuRegister());
1503 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1504 break;
1505
1506 default:
1507 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1508 }
1509}
1510
1511void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1512 Primitive::Type type = div->GetResultType();
1513 switch (type) {
1514 case Primitive::kPrimInt:
1515 case Primitive::kPrimLong:
1516 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
1517 break;
1518
1519 case Primitive::kPrimFloat:
1520 case Primitive::kPrimDouble:
1521 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1522 break;
1523
1524 default:
1525 LOG(FATAL) << "Unexpected div type " << type;
1526 }
1527}
1528
Alexandre Rames67555f72014-11-18 10:55:16 +00001529void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1530 LocationSummary* locations =
1531 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1532 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1533 if (instruction->HasUses()) {
1534 locations->SetOut(Location::SameAsFirstInput());
1535 }
1536}
1537
1538void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1539 SlowPathCodeARM64* slow_path =
1540 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1541 codegen_->AddSlowPath(slow_path);
1542 Location value = instruction->GetLocations()->InAt(0);
1543
Alexandre Rames3e69f162014-12-10 10:36:50 +00001544 Primitive::Type type = instruction->GetType();
1545
1546 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
1547 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
1548 return;
1549 }
1550
Alexandre Rames67555f72014-11-18 10:55:16 +00001551 if (value.IsConstant()) {
1552 int64_t divisor = Int64ConstantFrom(value);
1553 if (divisor == 0) {
1554 __ B(slow_path->GetEntryLabel());
1555 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001556 // A division by a non-null constant is valid. We don't need to perform
1557 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00001558 }
1559 } else {
1560 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
1561 }
1562}
1563
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001564void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1565 LocationSummary* locations =
1566 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1567 locations->SetOut(Location::ConstantLocation(constant));
1568}
1569
1570void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1571 UNUSED(constant);
1572 // Will be generated at use site.
1573}
1574
Alexandre Rames5319def2014-10-23 10:03:10 +01001575void LocationsBuilderARM64::VisitExit(HExit* exit) {
1576 exit->SetLocations(nullptr);
1577}
1578
1579void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001580 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01001581}
1582
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001583void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
1584 LocationSummary* locations =
1585 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1586 locations->SetOut(Location::ConstantLocation(constant));
1587}
1588
1589void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
1590 UNUSED(constant);
1591 // Will be generated at use site.
1592}
1593
Alexandre Rames5319def2014-10-23 10:03:10 +01001594void LocationsBuilderARM64::VisitGoto(HGoto* got) {
1595 got->SetLocations(nullptr);
1596}
1597
1598void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
1599 HBasicBlock* successor = got->GetSuccessor();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001600 DCHECK(!successor->IsExitBlock());
1601 HBasicBlock* block = got->GetBlock();
1602 HInstruction* previous = got->GetPrevious();
1603 HLoopInformation* info = block->GetLoopInformation();
1604
David Brazdil46e2a392015-03-16 17:31:52 +00001605 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001606 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1607 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1608 return;
1609 }
1610 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1611 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1612 }
1613 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001614 __ B(codegen_->GetLabelOf(successor));
1615 }
1616}
1617
Andreas Gampe0ba62732015-03-24 02:39:46 +00001618void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
1619 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1620 HInstruction* cond = if_instr->InputAt(0);
1621 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1622 locations->SetInAt(0, Location::RequiresRegister());
1623 }
1624}
1625
1626void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
1627 HInstruction* cond = if_instr->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001628 HCondition* condition = cond->AsCondition();
Andreas Gampe0ba62732015-03-24 02:39:46 +00001629 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1630 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01001631
Serban Constantinescu02164b32014-11-13 14:05:07 +00001632 if (cond->IsIntConstant()) {
1633 int32_t cond_value = cond->AsIntConstant()->GetValue();
1634 if (cond_value == 1) {
Andreas Gampe0ba62732015-03-24 02:39:46 +00001635 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfTrueSuccessor())) {
1636 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001637 }
1638 return;
1639 } else {
1640 DCHECK_EQ(cond_value, 0);
1641 }
1642 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001643 // The condition instruction has been materialized, compare the output to 0.
Andreas Gampe0ba62732015-03-24 02:39:46 +00001644 Location cond_val = if_instr->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001645 DCHECK(cond_val.IsRegister());
Andreas Gampe0ba62732015-03-24 02:39:46 +00001646 __ Cbnz(InputRegisterAt(if_instr, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001647 } else {
1648 // The condition instruction has not been materialized, use its inputs as
1649 // the comparison and its condition as the branch condition.
1650 Register lhs = InputRegisterAt(condition, 0);
1651 Operand rhs = InputOperandAt(condition, 1);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001652 Condition arm64_cond = ARM64Condition(condition->GetCondition());
1653 if ((arm64_cond == eq || arm64_cond == ne) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
1654 if (arm64_cond == eq) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001655 __ Cbz(lhs, true_target);
1656 } else {
1657 __ Cbnz(lhs, true_target);
1658 }
1659 } else {
1660 __ Cmp(lhs, rhs);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001661 __ B(arm64_cond, true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001662 }
1663 }
Andreas Gampe0ba62732015-03-24 02:39:46 +00001664 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001665 __ B(false_target);
1666 }
1667}
1668
1669void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001670 LocationSummary* locations =
1671 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames5319def2014-10-23 10:03:10 +01001672 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001673 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001674}
1675
1676void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001677 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), instruction->GetFieldOffset());
Serban Constantinescu579885a2015-02-22 20:51:33 +00001678 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001679
1680 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00001681 if (use_acquire_release) {
Calin Juravle77520bc2015-01-12 18:45:46 +00001682 // NB: LoadAcquire will record the pc info if needed.
1683 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001684 } else {
1685 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
Calin Juravle77520bc2015-01-12 18:45:46 +00001686 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001687 // For IRIW sequential consistency kLoadAny is not sufficient.
1688 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1689 }
1690 } else {
1691 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
Calin Juravle77520bc2015-01-12 18:45:46 +00001692 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001693 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001694}
1695
1696void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001697 LocationSummary* locations =
1698 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames5319def2014-10-23 10:03:10 +01001699 locations->SetInAt(0, Location::RequiresRegister());
1700 locations->SetInAt(1, Location::RequiresRegister());
1701}
1702
1703void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001704 Register obj = InputRegisterAt(instruction, 0);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001705 CPURegister value = InputCPURegisterAt(instruction, 1);
1706 Offset offset = instruction->GetFieldOffset();
1707 Primitive::Type field_type = instruction->GetFieldType();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001708 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001709
1710 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00001711 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001712 codegen_->StoreRelease(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001713 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001714 } else {
1715 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1716 codegen_->Store(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001717 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001718 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1719 }
1720 } else {
1721 codegen_->Store(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001722 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001723 }
1724
1725 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001726 codegen_->MarkGCCard(obj, Register(value));
Alexandre Rames5319def2014-10-23 10:03:10 +01001727 }
1728}
1729
Alexandre Rames67555f72014-11-18 10:55:16 +00001730void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
1731 LocationSummary::CallKind call_kind =
1732 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
1733 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1734 locations->SetInAt(0, Location::RequiresRegister());
1735 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001736 // The output does overlap inputs.
1737 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00001738}
1739
1740void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
1741 LocationSummary* locations = instruction->GetLocations();
1742 Register obj = InputRegisterAt(instruction, 0);;
1743 Register cls = InputRegisterAt(instruction, 1);;
1744 Register out = OutputRegister(instruction);
1745
1746 vixl::Label done;
1747
1748 // Return 0 if `obj` is null.
1749 // TODO: Avoid this check if we know `obj` is not null.
1750 __ Mov(out, 0);
1751 __ Cbz(obj, &done);
1752
1753 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00001754 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Alexandre Rames67555f72014-11-18 10:55:16 +00001755 __ Cmp(out, cls);
1756 if (instruction->IsClassFinal()) {
1757 // Classes must be equal for the instanceof to succeed.
1758 __ Cset(out, eq);
1759 } else {
1760 // If the classes are not equal, we go into a slow path.
1761 DCHECK(locations->OnlyCallsOnSlowPath());
1762 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00001763 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1764 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001765 codegen_->AddSlowPath(slow_path);
1766 __ B(ne, slow_path->GetEntryLabel());
1767 __ Mov(out, 1);
1768 __ Bind(slow_path->GetExitLabel());
1769 }
1770
1771 __ Bind(&done);
1772}
1773
Alexandre Rames5319def2014-10-23 10:03:10 +01001774void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
1775 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
1776 locations->SetOut(Location::ConstantLocation(constant));
1777}
1778
1779void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
1780 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001781 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01001782}
1783
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001784void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
1785 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
1786 locations->SetOut(Location::ConstantLocation(constant));
1787}
1788
1789void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
1790 // Will be generated at use site.
1791 UNUSED(constant);
1792}
1793
Alexandre Rames5319def2014-10-23 10:03:10 +01001794void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
1795 LocationSummary* locations =
1796 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
1797 locations->AddTemp(LocationFrom(x0));
1798
1799 InvokeDexCallingConventionVisitor calling_convention_visitor;
1800 for (size_t i = 0; i < invoke->InputCount(); i++) {
1801 HInstruction* input = invoke->InputAt(i);
1802 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1803 }
1804
1805 Primitive::Type return_type = invoke->GetType();
1806 if (return_type != Primitive::kPrimVoid) {
1807 locations->SetOut(calling_convention_visitor.GetReturnLocation(return_type));
1808 }
1809}
1810
Alexandre Rames67555f72014-11-18 10:55:16 +00001811void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1812 HandleInvoke(invoke);
1813}
1814
1815void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1816 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1817 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
1818 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1819 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1820 Location receiver = invoke->GetLocations()->InAt(0);
1821 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001822 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00001823
1824 // The register ip1 is required to be used for the hidden argument in
1825 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
1826 UseScratchRegisterScope scratch_scope(GetVIXLAssembler());
1827 scratch_scope.Exclude(ip1);
1828 __ Mov(ip1, invoke->GetDexMethodIndex());
1829
1830 // temp = object->GetClass();
1831 if (receiver.IsStackSlot()) {
1832 __ Ldr(temp, StackOperandFrom(receiver));
1833 __ Ldr(temp, HeapOperand(temp, class_offset));
1834 } else {
1835 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
1836 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001837 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00001838 // temp = temp->GetImtEntryAt(method_offset);
1839 __ Ldr(temp, HeapOperand(temp, method_offset));
1840 // lr = temp->GetEntryPoint();
1841 __ Ldr(lr, HeapOperand(temp, entry_point));
1842 // lr();
1843 __ Blr(lr);
1844 DCHECK(!codegen_->IsLeafMethod());
1845 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1846}
1847
1848void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001849 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1850 if (intrinsic.TryDispatch(invoke)) {
1851 return;
1852 }
1853
Alexandre Rames67555f72014-11-18 10:55:16 +00001854 HandleInvoke(invoke);
1855}
1856
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001857void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001858 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1859 if (intrinsic.TryDispatch(invoke)) {
1860 return;
1861 }
1862
Alexandre Rames67555f72014-11-18 10:55:16 +00001863 HandleInvoke(invoke);
1864}
1865
Andreas Gampe878d58c2015-01-15 23:24:00 -08001866static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
1867 if (invoke->GetLocations()->Intrinsified()) {
1868 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
1869 intrinsic.Dispatch(invoke);
1870 return true;
1871 }
1872 return false;
1873}
1874
1875void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Register temp) {
1876 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
1877 DCHECK(temp.Is(kArtMethodRegister));
Alexandre Rames5319def2014-10-23 10:03:10 +01001878 size_t index_in_cache = mirror::Array::DataOffset(kHeapRefSize).SizeValue() +
Andreas Gampe878d58c2015-01-15 23:24:00 -08001879 invoke->GetDexMethodIndex() * kHeapRefSize;
Alexandre Rames5319def2014-10-23 10:03:10 +01001880
1881 // TODO: Implement all kinds of calls:
1882 // 1) boot -> boot
1883 // 2) app -> boot
1884 // 3) app -> app
1885 //
1886 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1887
Nicolas Geoffray0a299b92015-01-29 11:39:44 +00001888 // temp = method;
1889 LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001890 if (!invoke->IsRecursive()) {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001891 // temp = temp->dex_cache_resolved_methods_;
1892 __ Ldr(temp, HeapOperand(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset()));
1893 // temp = temp[index_in_cache];
1894 __ Ldr(temp, HeapOperand(temp, index_in_cache));
1895 // lr = temp->entry_point_from_quick_compiled_code_;
1896 __ Ldr(lr, HeapOperand(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
1897 kArm64WordSize)));
1898 // lr();
1899 __ Blr(lr);
1900 } else {
1901 __ Bl(&frame_entry_label_);
1902 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001903
Andreas Gampe878d58c2015-01-15 23:24:00 -08001904 DCHECK(!IsLeafMethod());
1905}
1906
1907void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
1908 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1909 return;
1910 }
1911
1912 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
1913 codegen_->GenerateStaticOrDirectCall(invoke, temp);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001914 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01001915}
1916
1917void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001918 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1919 return;
1920 }
1921
Alexandre Rames5319def2014-10-23 10:03:10 +01001922 LocationSummary* locations = invoke->GetLocations();
1923 Location receiver = locations->InAt(0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001924 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01001925 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1926 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1927 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001928 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01001929
1930 // temp = object->GetClass();
1931 if (receiver.IsStackSlot()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001932 __ Ldr(temp, MemOperand(sp, receiver.GetStackIndex()));
1933 __ Ldr(temp, HeapOperand(temp, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01001934 } else {
1935 DCHECK(receiver.IsRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001936 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01001937 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001938 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames5319def2014-10-23 10:03:10 +01001939 // temp = temp->GetMethodAt(method_offset);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001940 __ Ldr(temp, HeapOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01001941 // lr = temp->GetEntryPoint();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001942 __ Ldr(lr, HeapOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001943 // lr();
1944 __ Blr(lr);
1945 DCHECK(!codegen_->IsLeafMethod());
1946 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1947}
1948
Alexandre Rames67555f72014-11-18 10:55:16 +00001949void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
1950 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
1951 : LocationSummary::kNoCall;
1952 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
1953 locations->SetOut(Location::RequiresRegister());
1954}
1955
1956void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
1957 Register out = OutputRegister(cls);
1958 if (cls->IsReferrersClass()) {
1959 DCHECK(!cls->CanCallRuntime());
1960 DCHECK(!cls->MustGenerateClinitCheck());
1961 codegen_->LoadCurrentMethod(out);
1962 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
1963 } else {
1964 DCHECK(cls->CanCallRuntime());
1965 codegen_->LoadCurrentMethod(out);
1966 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DexCacheResolvedTypesOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001967 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00001968
1969 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1970 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
1971 codegen_->AddSlowPath(slow_path);
1972 __ Cbz(out, slow_path->GetEntryLabel());
1973 if (cls->MustGenerateClinitCheck()) {
1974 GenerateClassInitializationCheck(slow_path, out);
1975 } else {
1976 __ Bind(slow_path->GetExitLabel());
1977 }
1978 }
1979}
1980
1981void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
1982 LocationSummary* locations =
1983 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
1984 locations->SetOut(Location::RequiresRegister());
1985}
1986
1987void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
1988 MemOperand exception = MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
1989 __ Ldr(OutputRegister(instruction), exception);
1990 __ Str(wzr, exception);
1991}
1992
Alexandre Rames5319def2014-10-23 10:03:10 +01001993void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
1994 load->SetLocations(nullptr);
1995}
1996
1997void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
1998 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001999 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002000}
2001
Alexandre Rames67555f72014-11-18 10:55:16 +00002002void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2003 LocationSummary* locations =
2004 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2005 locations->SetOut(Location::RequiresRegister());
2006}
2007
2008void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2009 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2010 codegen_->AddSlowPath(slow_path);
2011
2012 Register out = OutputRegister(load);
2013 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08002014 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
2015 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002016 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002017 __ Cbz(out, slow_path->GetEntryLabel());
2018 __ Bind(slow_path->GetExitLabel());
2019}
2020
Alexandre Rames5319def2014-10-23 10:03:10 +01002021void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2022 local->SetLocations(nullptr);
2023}
2024
2025void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2026 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2027}
2028
2029void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2030 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2031 locations->SetOut(Location::ConstantLocation(constant));
2032}
2033
2034void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2035 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002036 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002037}
2038
Alexandre Rames67555f72014-11-18 10:55:16 +00002039void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2040 LocationSummary* locations =
2041 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2042 InvokeRuntimeCallingConvention calling_convention;
2043 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2044}
2045
2046void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2047 codegen_->InvokeRuntime(instruction->IsEnter()
2048 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2049 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002050 instruction->GetDexPc(),
2051 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002052 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002053}
2054
Alexandre Rames42d641b2014-10-27 14:00:51 +00002055void LocationsBuilderARM64::VisitMul(HMul* mul) {
2056 LocationSummary* locations =
2057 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2058 switch (mul->GetResultType()) {
2059 case Primitive::kPrimInt:
2060 case Primitive::kPrimLong:
2061 locations->SetInAt(0, Location::RequiresRegister());
2062 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002063 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002064 break;
2065
2066 case Primitive::kPrimFloat:
2067 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002068 locations->SetInAt(0, Location::RequiresFpuRegister());
2069 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002070 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002071 break;
2072
2073 default:
2074 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2075 }
2076}
2077
2078void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2079 switch (mul->GetResultType()) {
2080 case Primitive::kPrimInt:
2081 case Primitive::kPrimLong:
2082 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2083 break;
2084
2085 case Primitive::kPrimFloat:
2086 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002087 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002088 break;
2089
2090 default:
2091 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2092 }
2093}
2094
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002095void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2096 LocationSummary* locations =
2097 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2098 switch (neg->GetResultType()) {
2099 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002100 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002101 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002102 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002103 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002104
2105 case Primitive::kPrimFloat:
2106 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002107 locations->SetInAt(0, Location::RequiresFpuRegister());
2108 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002109 break;
2110
2111 default:
2112 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2113 }
2114}
2115
2116void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2117 switch (neg->GetResultType()) {
2118 case Primitive::kPrimInt:
2119 case Primitive::kPrimLong:
2120 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2121 break;
2122
2123 case Primitive::kPrimFloat:
2124 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002125 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002126 break;
2127
2128 default:
2129 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2130 }
2131}
2132
2133void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2134 LocationSummary* locations =
2135 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2136 InvokeRuntimeCallingConvention calling_convention;
2137 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002138 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002139 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002140 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2141 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2142 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002143}
2144
2145void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2146 LocationSummary* locations = instruction->GetLocations();
2147 InvokeRuntimeCallingConvention calling_convention;
2148 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2149 DCHECK(type_index.Is(w0));
2150 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002151 DCHECK(current_method.Is(w2));
Alexandre Rames67555f72014-11-18 10:55:16 +00002152 codegen_->LoadCurrentMethod(current_method);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002153 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002154 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002155 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2156 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002157 instruction->GetDexPc(),
2158 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002159 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2160 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002161}
2162
Alexandre Rames5319def2014-10-23 10:03:10 +01002163void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2164 LocationSummary* locations =
2165 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2166 InvokeRuntimeCallingConvention calling_convention;
2167 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2168 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2169 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002170 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002171}
2172
2173void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2174 LocationSummary* locations = instruction->GetLocations();
2175 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2176 DCHECK(type_index.Is(w0));
2177 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
2178 DCHECK(current_method.Is(w1));
Alexandre Rames67555f72014-11-18 10:55:16 +00002179 codegen_->LoadCurrentMethod(current_method);
Alexandre Rames5319def2014-10-23 10:03:10 +01002180 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002181 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002182 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2183 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002184 instruction->GetDexPc(),
2185 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002186 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002187}
2188
2189void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2190 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002191 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002192 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002193}
2194
2195void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002196 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002197 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002198 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002199 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002200 break;
2201
2202 default:
2203 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2204 }
2205}
2206
2207void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2208 LocationSummary* locations =
2209 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2210 locations->SetInAt(0, Location::RequiresRegister());
2211 if (instruction->HasUses()) {
2212 locations->SetOut(Location::SameAsFirstInput());
2213 }
2214}
2215
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002216void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002217 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2218 return;
2219 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002220 Location obj = instruction->GetLocations()->InAt(0);
2221
2222 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2223 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2224}
2225
2226void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002227 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2228 codegen_->AddSlowPath(slow_path);
2229
2230 LocationSummary* locations = instruction->GetLocations();
2231 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002232
2233 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002234}
2235
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002236void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2237 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2238 GenerateImplicitNullCheck(instruction);
2239 } else {
2240 GenerateExplicitNullCheck(instruction);
2241 }
2242}
2243
Alexandre Rames67555f72014-11-18 10:55:16 +00002244void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2245 HandleBinaryOp(instruction);
2246}
2247
2248void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2249 HandleBinaryOp(instruction);
2250}
2251
Alexandre Rames3e69f162014-12-10 10:36:50 +00002252void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2253 LOG(FATAL) << "Unreachable";
2254}
2255
2256void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2257 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2258}
2259
Alexandre Rames5319def2014-10-23 10:03:10 +01002260void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2261 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2262 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2263 if (location.IsStackSlot()) {
2264 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2265 } else if (location.IsDoubleStackSlot()) {
2266 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2267 }
2268 locations->SetOut(location);
2269}
2270
2271void InstructionCodeGeneratorARM64::VisitParameterValue(HParameterValue* instruction) {
2272 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002273 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002274}
2275
2276void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2277 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2278 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2279 locations->SetInAt(i, Location::Any());
2280 }
2281 locations->SetOut(Location::Any());
2282}
2283
2284void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002285 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002286 LOG(FATAL) << "Unreachable";
2287}
2288
Serban Constantinescu02164b32014-11-13 14:05:07 +00002289void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002290 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002291 LocationSummary::CallKind call_kind =
2292 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002293 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2294
2295 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002296 case Primitive::kPrimInt:
2297 case Primitive::kPrimLong:
2298 locations->SetInAt(0, Location::RequiresRegister());
2299 locations->SetInAt(1, Location::RequiresRegister());
2300 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2301 break;
2302
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002303 case Primitive::kPrimFloat:
2304 case Primitive::kPrimDouble: {
2305 InvokeRuntimeCallingConvention calling_convention;
2306 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2307 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2308 locations->SetOut(calling_convention.GetReturnLocation(type));
2309
2310 break;
2311 }
2312
Serban Constantinescu02164b32014-11-13 14:05:07 +00002313 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002314 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002315 }
2316}
2317
2318void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2319 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002320
Serban Constantinescu02164b32014-11-13 14:05:07 +00002321 switch (type) {
2322 case Primitive::kPrimInt:
2323 case Primitive::kPrimLong: {
2324 UseScratchRegisterScope temps(GetVIXLAssembler());
2325 Register dividend = InputRegisterAt(rem, 0);
2326 Register divisor = InputRegisterAt(rem, 1);
2327 Register output = OutputRegister(rem);
2328 Register temp = temps.AcquireSameSizeAs(output);
2329
2330 __ Sdiv(temp, dividend, divisor);
2331 __ Msub(output, temp, divisor, dividend);
2332 break;
2333 }
2334
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002335 case Primitive::kPrimFloat:
2336 case Primitive::kPrimDouble: {
2337 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2338 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002339 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002340 break;
2341 }
2342
Serban Constantinescu02164b32014-11-13 14:05:07 +00002343 default:
2344 LOG(FATAL) << "Unexpected rem type " << type;
2345 }
2346}
2347
Alexandre Rames5319def2014-10-23 10:03:10 +01002348void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2349 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2350 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002351 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002352}
2353
2354void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002355 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002356 codegen_->GenerateFrameExit();
Alexandre Rames3e69f162014-12-10 10:36:50 +00002357 __ Ret();
Alexandre Rames5319def2014-10-23 10:03:10 +01002358}
2359
2360void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2361 instruction->SetLocations(nullptr);
2362}
2363
2364void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002365 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002366 codegen_->GenerateFrameExit();
Alexandre Rames3e69f162014-12-10 10:36:50 +00002367 __ Ret();
Alexandre Rames5319def2014-10-23 10:03:10 +01002368}
2369
Serban Constantinescu02164b32014-11-13 14:05:07 +00002370void LocationsBuilderARM64::VisitShl(HShl* shl) {
2371 HandleShift(shl);
2372}
2373
2374void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2375 HandleShift(shl);
2376}
2377
2378void LocationsBuilderARM64::VisitShr(HShr* shr) {
2379 HandleShift(shr);
2380}
2381
2382void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2383 HandleShift(shr);
2384}
2385
Alexandre Rames5319def2014-10-23 10:03:10 +01002386void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2387 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2388 Primitive::Type field_type = store->InputAt(1)->GetType();
2389 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002390 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002391 case Primitive::kPrimBoolean:
2392 case Primitive::kPrimByte:
2393 case Primitive::kPrimChar:
2394 case Primitive::kPrimShort:
2395 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002396 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002397 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2398 break;
2399
2400 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002401 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002402 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2403 break;
2404
2405 default:
2406 LOG(FATAL) << "Unimplemented local type " << field_type;
2407 }
2408}
2409
2410void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002411 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002412}
2413
2414void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002415 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002416}
2417
2418void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002419 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002420}
2421
Alexandre Rames67555f72014-11-18 10:55:16 +00002422void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2423 LocationSummary* locations =
2424 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2425 locations->SetInAt(0, Location::RequiresRegister());
2426 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2427}
2428
2429void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002430 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), instruction->GetFieldOffset());
Serban Constantinescu579885a2015-02-22 20:51:33 +00002431 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002432
2433 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00002434 if (use_acquire_release) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002435 // NB: LoadAcquire will record the pc info if needed.
2436 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002437 } else {
2438 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
2439 // For IRIW sequential consistency kLoadAny is not sufficient.
2440 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2441 }
2442 } else {
2443 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
2444 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002445}
2446
2447void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002448 LocationSummary* locations =
2449 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2450 locations->SetInAt(0, Location::RequiresRegister());
2451 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01002452}
2453
Alexandre Rames67555f72014-11-18 10:55:16 +00002454void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002455 Register cls = InputRegisterAt(instruction, 0);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002456 CPURegister value = InputCPURegisterAt(instruction, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002457 Offset offset = instruction->GetFieldOffset();
Alexandre Rames67555f72014-11-18 10:55:16 +00002458 Primitive::Type field_type = instruction->GetFieldType();
Serban Constantinescu579885a2015-02-22 20:51:33 +00002459 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Alexandre Rames5319def2014-10-23 10:03:10 +01002460
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002461 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00002462 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002463 codegen_->StoreRelease(field_type, value, HeapOperand(cls, offset));
2464 } else {
2465 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2466 codegen_->Store(field_type, value, HeapOperand(cls, offset));
2467 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2468 }
2469 } else {
2470 codegen_->Store(field_type, value, HeapOperand(cls, offset));
2471 }
2472
2473 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002474 codegen_->MarkGCCard(cls, Register(value));
2475 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002476}
2477
2478void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2479 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2480}
2481
2482void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002483 HBasicBlock* block = instruction->GetBlock();
2484 if (block->GetLoopInformation() != nullptr) {
2485 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2486 // The back edge will generate the suspend check.
2487 return;
2488 }
2489 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2490 // The goto will generate the suspend check.
2491 return;
2492 }
2493 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01002494}
2495
2496void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
2497 temp->SetLocations(nullptr);
2498}
2499
2500void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
2501 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002502 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01002503}
2504
Alexandre Rames67555f72014-11-18 10:55:16 +00002505void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
2506 LocationSummary* locations =
2507 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2508 InvokeRuntimeCallingConvention calling_convention;
2509 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2510}
2511
2512void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
2513 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002514 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002515 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002516}
2517
2518void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
2519 LocationSummary* locations =
2520 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2521 Primitive::Type input_type = conversion->GetInputType();
2522 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002523 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00002524 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
2525 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
2526 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
2527 }
2528
Alexandre Rames542361f2015-01-29 16:57:31 +00002529 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002530 locations->SetInAt(0, Location::RequiresFpuRegister());
2531 } else {
2532 locations->SetInAt(0, Location::RequiresRegister());
2533 }
2534
Alexandre Rames542361f2015-01-29 16:57:31 +00002535 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002536 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2537 } else {
2538 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2539 }
2540}
2541
2542void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
2543 Primitive::Type result_type = conversion->GetResultType();
2544 Primitive::Type input_type = conversion->GetInputType();
2545
2546 DCHECK_NE(input_type, result_type);
2547
Alexandre Rames542361f2015-01-29 16:57:31 +00002548 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002549 int result_size = Primitive::ComponentSize(result_type);
2550 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002551 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002552 Register output = OutputRegister(conversion);
2553 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002554 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
2555 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
2556 } else if ((result_type == Primitive::kPrimChar) ||
2557 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
2558 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002559 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002560 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002561 }
Alexandre Rames542361f2015-01-29 16:57:31 +00002562 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002563 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002564 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002565 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
2566 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002567 } else if (Primitive::IsFloatingPointType(result_type) &&
2568 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002569 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
2570 } else {
2571 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
2572 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00002573 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002574}
Alexandre Rames67555f72014-11-18 10:55:16 +00002575
Serban Constantinescu02164b32014-11-13 14:05:07 +00002576void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
2577 HandleShift(ushr);
2578}
2579
2580void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
2581 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00002582}
2583
2584void LocationsBuilderARM64::VisitXor(HXor* instruction) {
2585 HandleBinaryOp(instruction);
2586}
2587
2588void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
2589 HandleBinaryOp(instruction);
2590}
2591
Calin Juravleb1498f62015-02-16 13:13:29 +00002592void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
2593 // Nothing to do, this should be removed during prepare for register allocator.
2594 UNUSED(instruction);
2595 LOG(FATAL) << "Unreachable";
2596}
2597
2598void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
2599 // Nothing to do, this should be removed during prepare for register allocator.
2600 UNUSED(instruction);
2601 LOG(FATAL) << "Unreachable";
2602}
2603
Alexandre Rames67555f72014-11-18 10:55:16 +00002604#undef __
2605#undef QUICK_ENTRY_POINT
2606
Alexandre Rames5319def2014-10-23 10:03:10 +01002607} // namespace arm64
2608} // namespace art