blob: c48cab4985b52eabfdf8de6d6cd8475015f65647 [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;
66
Alexandre Rames5319def2014-10-23 10:03:10 +010067static constexpr size_t kHeapRefSize = sizeof(mirror::HeapReference<mirror::Object>);
68static constexpr int kCurrentMethodStackOffset = 0;
69
Alexandre Rames5319def2014-10-23 10:03:10 +010070inline Condition ARM64Condition(IfCondition cond) {
71 switch (cond) {
72 case kCondEQ: return eq;
73 case kCondNE: return ne;
74 case kCondLT: return lt;
75 case kCondLE: return le;
76 case kCondGT: return gt;
77 case kCondGE: return ge;
78 default:
79 LOG(FATAL) << "Unknown if condition";
80 }
81 return nv; // Unreachable.
82}
83
Alexandre Ramesa89086e2014-11-07 17:13:25 +000084Location ARM64ReturnLocation(Primitive::Type return_type) {
85 DCHECK_NE(return_type, Primitive::kPrimVoid);
86 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
87 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
88 // but we use the exact registers for clarity.
89 if (return_type == Primitive::kPrimFloat) {
90 return LocationFrom(s0);
91 } else if (return_type == Primitive::kPrimDouble) {
92 return LocationFrom(d0);
93 } else if (return_type == Primitive::kPrimLong) {
94 return LocationFrom(x0);
95 } else {
96 return LocationFrom(w0);
97 }
98}
99
Alexandre Rames5319def2014-10-23 10:03:10 +0100100static const Register kRuntimeParameterCoreRegisters[] = { x0, x1, x2, x3, x4, x5, x6, x7 };
101static constexpr size_t kRuntimeParameterCoreRegistersLength =
102 arraysize(kRuntimeParameterCoreRegisters);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000103static const FPRegister kRuntimeParameterFpuRegisters[] = { d0, d1, d2, d3, d4, d5, d6, d7 };
104static constexpr size_t kRuntimeParameterFpuRegistersLength =
105 arraysize(kRuntimeParameterCoreRegisters);
Alexandre Rames5319def2014-10-23 10:03:10 +0100106
107class InvokeRuntimeCallingConvention : public CallingConvention<Register, FPRegister> {
108 public:
109 static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
110
111 InvokeRuntimeCallingConvention()
112 : CallingConvention(kRuntimeParameterCoreRegisters,
113 kRuntimeParameterCoreRegistersLength,
114 kRuntimeParameterFpuRegisters,
115 kRuntimeParameterFpuRegistersLength) {}
116
117 Location GetReturnLocation(Primitive::Type return_type);
118
119 private:
120 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
121};
122
123Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000124 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100125}
126
Alexandre Rames67555f72014-11-18 10:55:16 +0000127#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
128#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100129
Alexandre Rames5319def2014-10-23 10:03:10 +0100130class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
131 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000132 BoundsCheckSlowPathARM64(HBoundsCheck* instruction,
133 Location index_location,
134 Location length_location)
135 : instruction_(instruction),
136 index_location_(index_location),
137 length_location_(length_location) {}
138
Alexandre Rames5319def2014-10-23 10:03:10 +0100139
Alexandre Rames67555f72014-11-18 10:55:16 +0000140 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000141 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100142 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000143 // We're moving two locations to locations that could overlap, so we need a parallel
144 // move resolver.
145 InvokeRuntimeCallingConvention calling_convention;
146 codegen->EmitParallelMoves(
147 index_location_, LocationFrom(calling_convention.GetRegisterAt(0)),
148 length_location_, LocationFrom(calling_convention.GetRegisterAt(1)));
149 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000150 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800151 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100152 }
153
154 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000155 HBoundsCheck* const instruction_;
156 const Location index_location_;
157 const Location length_location_;
158
Alexandre Rames5319def2014-10-23 10:03:10 +0100159 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
160};
161
Alexandre Rames67555f72014-11-18 10:55:16 +0000162class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
163 public:
164 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
165
166 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
167 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
168 __ Bind(GetEntryLabel());
169 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000170 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800171 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000172 }
173
174 private:
175 HDivZeroCheck* const instruction_;
176 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
177};
178
179class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
180 public:
181 LoadClassSlowPathARM64(HLoadClass* cls,
182 HInstruction* at,
183 uint32_t dex_pc,
184 bool do_clinit)
185 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
186 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
187 }
188
189 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
190 LocationSummary* locations = at_->GetLocations();
191 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
192
193 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000194 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000195
196 InvokeRuntimeCallingConvention calling_convention;
197 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
198 arm64_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1).W());
199 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
200 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000201 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800202 if (do_clinit_) {
203 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t, mirror::ArtMethod*>();
204 } else {
205 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t, mirror::ArtMethod*>();
206 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000207
208 // Move the class to the desired location.
209 Location out = locations->Out();
210 if (out.IsValid()) {
211 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
212 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000213 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000214 }
215
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000216 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000217 __ B(GetExitLabel());
218 }
219
220 private:
221 // The class this slow path will load.
222 HLoadClass* const cls_;
223
224 // The instruction where this slow path is happening.
225 // (Might be the load class or an initialization check).
226 HInstruction* const at_;
227
228 // The dex PC of `at_`.
229 const uint32_t dex_pc_;
230
231 // Whether to initialize the class.
232 const bool do_clinit_;
233
234 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
235};
236
237class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
238 public:
239 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
240
241 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
242 LocationSummary* locations = instruction_->GetLocations();
243 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
244 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
245
246 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000247 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000248
249 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800250 arm64_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1).W());
251 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000252 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000253 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800254 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000255 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000256 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000257
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000258 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000259 __ B(GetExitLabel());
260 }
261
262 private:
263 HLoadString* const instruction_;
264
265 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
266};
267
Alexandre Rames5319def2014-10-23 10:03:10 +0100268class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
269 public:
270 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
271
Alexandre Rames67555f72014-11-18 10:55:16 +0000272 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
273 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100274 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000275 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000276 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800277 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100278 }
279
280 private:
281 HNullCheck* const instruction_;
282
283 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
284};
285
286class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
287 public:
288 explicit SuspendCheckSlowPathARM64(HSuspendCheck* instruction,
289 HBasicBlock* successor)
290 : instruction_(instruction), successor_(successor) {}
291
Alexandre Rames67555f72014-11-18 10:55:16 +0000292 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
293 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100294 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000295 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000296 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000297 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800298 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000299 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000300 if (successor_ == nullptr) {
301 __ B(GetReturnLabel());
302 } else {
303 __ B(arm64_codegen->GetLabelOf(successor_));
304 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100305 }
306
307 vixl::Label* GetReturnLabel() {
308 DCHECK(successor_ == nullptr);
309 return &return_label_;
310 }
311
Alexandre Rames5319def2014-10-23 10:03:10 +0100312 private:
313 HSuspendCheck* const instruction_;
314 // If not null, the block to branch to after the suspend check.
315 HBasicBlock* const successor_;
316
317 // If `successor_` is null, the label to branch to after the suspend check.
318 vixl::Label return_label_;
319
320 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
321};
322
Alexandre Rames67555f72014-11-18 10:55:16 +0000323class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
324 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000325 TypeCheckSlowPathARM64(HInstruction* instruction,
326 Location class_to_check,
327 Location object_class,
328 uint32_t dex_pc)
329 : instruction_(instruction),
330 class_to_check_(class_to_check),
331 object_class_(object_class),
332 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000333
334 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000335 LocationSummary* locations = instruction_->GetLocations();
336 DCHECK(instruction_->IsCheckCast()
337 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
338 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
339
Alexandre Rames67555f72014-11-18 10:55:16 +0000340 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000341 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000342
343 // We're moving two locations to locations that could overlap, so we need a parallel
344 // move resolver.
345 InvokeRuntimeCallingConvention calling_convention;
346 codegen->EmitParallelMoves(
347 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)),
348 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)));
349
350 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000351 arm64_codegen->InvokeRuntime(
352 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000353 Primitive::Type ret_type = instruction_->GetType();
354 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
355 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800356 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
357 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000358 } else {
359 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000360 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800361 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000362 }
363
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000364 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000365 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000366 }
367
368 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000369 HInstruction* const instruction_;
370 const Location class_to_check_;
371 const Location object_class_;
372 uint32_t dex_pc_;
373
Alexandre Rames67555f72014-11-18 10:55:16 +0000374 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
375};
376
Alexandre Rames5319def2014-10-23 10:03:10 +0100377#undef __
378
379Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
380 Location next_location;
381 if (type == Primitive::kPrimVoid) {
382 LOG(FATAL) << "Unreachable type " << type;
383 }
384
Alexandre Rames542361f2015-01-29 16:57:31 +0000385 if (Primitive::IsFloatingPointType(type) &&
386 (fp_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000387 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(fp_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000388 } else if (!Primitive::IsFloatingPointType(type) &&
389 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000390 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
391 } else {
392 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000393 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
394 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100395 }
396
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000397 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000398 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100399 return next_location;
400}
401
Serban Constantinescu579885a2015-02-22 20:51:33 +0000402CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
403 const Arm64InstructionSetFeatures& isa_features,
404 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100405 : CodeGenerator(graph,
406 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000407 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000408 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000409 callee_saved_core_registers.list(),
410 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000411 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100412 block_labels_(nullptr),
413 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000414 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000415 move_resolver_(graph->GetArena(), this),
416 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000417 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000418 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000419}
Alexandre Rames5319def2014-10-23 10:03:10 +0100420
Alexandre Rames67555f72014-11-18 10:55:16 +0000421#undef __
422#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100423
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000424void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
425 // Ensure we emit the literal pool.
426 __ FinalizeCode();
427 CodeGenerator::Finalize(allocator);
428}
429
Alexandre Rames3e69f162014-12-10 10:36:50 +0000430void ParallelMoveResolverARM64::EmitMove(size_t index) {
431 MoveOperands* move = moves_.Get(index);
432 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
433}
434
435void ParallelMoveResolverARM64::EmitSwap(size_t index) {
436 MoveOperands* move = moves_.Get(index);
437 codegen_->SwapLocations(move->GetDestination(), move->GetSource());
438}
439
440void ParallelMoveResolverARM64::RestoreScratch(int reg) {
441 __ Pop(Register(VIXLRegCodeFromART(reg), kXRegSize));
442}
443
444void ParallelMoveResolverARM64::SpillScratch(int reg) {
445 __ Push(Register(VIXLRegCodeFromART(reg), kXRegSize));
446}
447
Alexandre Rames5319def2014-10-23 10:03:10 +0100448void CodeGeneratorARM64::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000449 __ Bind(&frame_entry_label_);
450
Serban Constantinescu02164b32014-11-13 14:05:07 +0000451 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
452 if (do_overflow_check) {
453 UseScratchRegisterScope temps(GetVIXLAssembler());
454 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000455 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000456 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000457 __ Ldr(wzr, MemOperand(temp, 0));
458 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000459 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100460
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000461 if (!HasEmptyFrame()) {
462 int frame_size = GetFrameSize();
463 // Stack layout:
464 // sp[frame_size - 8] : lr.
465 // ... : other preserved core registers.
466 // ... : other preserved fp registers.
467 // ... : reserved frame space.
468 // sp[0] : current method.
469 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
470 __ PokeCPURegList(GetFramePreservedCoreRegisters(), frame_size - GetCoreSpillSize());
471 __ PokeCPURegList(GetFramePreservedFPRegisters(), frame_size - FrameEntrySpillSize());
472 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100473}
474
475void CodeGeneratorARM64::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000476 if (!HasEmptyFrame()) {
477 int frame_size = GetFrameSize();
478 __ PeekCPURegList(GetFramePreservedFPRegisters(), frame_size - FrameEntrySpillSize());
479 __ PeekCPURegList(GetFramePreservedCoreRegisters(), frame_size - GetCoreSpillSize());
480 __ Drop(frame_size);
481 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100482}
483
484void CodeGeneratorARM64::Bind(HBasicBlock* block) {
485 __ Bind(GetLabelOf(block));
486}
487
Alexandre Rames5319def2014-10-23 10:03:10 +0100488void CodeGeneratorARM64::Move(HInstruction* instruction,
489 Location location,
490 HInstruction* move_for) {
491 LocationSummary* locations = instruction->GetLocations();
492 if (locations != nullptr && locations->Out().Equals(location)) {
493 return;
494 }
495
496 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000497 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100498
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000499 if (instruction->IsIntConstant()
500 || instruction->IsLongConstant()
501 || instruction->IsNullConstant()) {
502 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100503 if (location.IsRegister()) {
504 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000505 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100506 (instruction->IsLongConstant() && dst.Is64Bits()));
507 __ Mov(dst, value);
508 } else {
509 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000510 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000511 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
512 ? temps.AcquireW()
513 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100514 __ Mov(temp, value);
515 __ Str(temp, StackOperandFrom(location));
516 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000517 } else if (instruction->IsTemporary()) {
518 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000519 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100520 } else if (instruction->IsLoadLocal()) {
521 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000522 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000523 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000524 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000525 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100526 }
527
528 } else {
529 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000530 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100531 }
532}
533
Alexandre Rames5319def2014-10-23 10:03:10 +0100534Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
535 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000536
Alexandre Rames5319def2014-10-23 10:03:10 +0100537 switch (type) {
538 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000539 case Primitive::kPrimInt:
540 case Primitive::kPrimFloat:
541 return Location::StackSlot(GetStackSlot(load->GetLocal()));
542
543 case Primitive::kPrimLong:
544 case Primitive::kPrimDouble:
545 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
546
Alexandre Rames5319def2014-10-23 10:03:10 +0100547 case Primitive::kPrimBoolean:
548 case Primitive::kPrimByte:
549 case Primitive::kPrimChar:
550 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100551 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100552 LOG(FATAL) << "Unexpected type " << type;
553 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000554
Alexandre Rames5319def2014-10-23 10:03:10 +0100555 LOG(FATAL) << "Unreachable";
556 return Location::NoLocation();
557}
558
559void CodeGeneratorARM64::MarkGCCard(Register object, Register value) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000560 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100561 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000562 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100563 vixl::Label done;
564 __ Cbz(value, &done);
565 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
566 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000567 __ Strb(card, MemOperand(card, temp.X()));
Alexandre Rames5319def2014-10-23 10:03:10 +0100568 __ Bind(&done);
569}
570
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000571void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
572 // Blocked core registers:
573 // lr : Runtime reserved.
574 // tr : Runtime reserved.
575 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
576 // ip1 : VIXL core temp.
577 // ip0 : VIXL core temp.
578 //
579 // Blocked fp registers:
580 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100581 CPURegList reserved_core_registers = vixl_reserved_core_registers;
582 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100583 while (!reserved_core_registers.IsEmpty()) {
584 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
585 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000586
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000587 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800588 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000589 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
590 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000591
592 if (is_baseline) {
593 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
594 while (!reserved_core_baseline_registers.IsEmpty()) {
595 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
596 }
597
598 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
599 while (!reserved_fp_baseline_registers.IsEmpty()) {
600 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
601 }
602 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100603}
604
605Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
606 if (type == Primitive::kPrimVoid) {
607 LOG(FATAL) << "Unreachable type " << type;
608 }
609
Alexandre Rames542361f2015-01-29 16:57:31 +0000610 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000611 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
612 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100613 return Location::FpuRegisterLocation(reg);
614 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000615 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
616 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100617 return Location::RegisterLocation(reg);
618 }
619}
620
Alexandre Rames3e69f162014-12-10 10:36:50 +0000621size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
622 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
623 __ Str(reg, MemOperand(sp, stack_index));
624 return kArm64WordSize;
625}
626
627size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
628 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
629 __ Ldr(reg, MemOperand(sp, stack_index));
630 return kArm64WordSize;
631}
632
633size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
634 FPRegister reg = FPRegister(reg_id, kDRegSize);
635 __ Str(reg, MemOperand(sp, stack_index));
636 return kArm64WordSize;
637}
638
639size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
640 FPRegister reg = FPRegister(reg_id, kDRegSize);
641 __ Ldr(reg, MemOperand(sp, stack_index));
642 return kArm64WordSize;
643}
644
Alexandre Rames5319def2014-10-23 10:03:10 +0100645void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
646 stream << Arm64ManagedRegister::FromXRegister(XRegister(reg));
647}
648
649void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
650 stream << Arm64ManagedRegister::FromDRegister(DRegister(reg));
651}
652
Alexandre Rames67555f72014-11-18 10:55:16 +0000653void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000654 if (constant->IsIntConstant()) {
655 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
656 } else if (constant->IsLongConstant()) {
657 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
658 } else if (constant->IsNullConstant()) {
659 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000660 } else if (constant->IsFloatConstant()) {
661 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
662 } else {
663 DCHECK(constant->IsDoubleConstant());
664 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
665 }
666}
667
Alexandre Rames3e69f162014-12-10 10:36:50 +0000668
669static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
670 DCHECK(constant.IsConstant());
671 HConstant* cst = constant.GetConstant();
672 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000673 // Null is mapped to a core W register, which we associate with kPrimInt.
674 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000675 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
676 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
677 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
678}
679
680void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000681 if (source.Equals(destination)) {
682 return;
683 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000684
685 // A valid move can always be inferred from the destination and source
686 // locations. When moving from and to a register, the argument type can be
687 // used to generate 32bit instead of 64bit moves. In debug mode we also
688 // checks the coherency of the locations and the type.
689 bool unspecified_type = (type == Primitive::kPrimVoid);
690
691 if (destination.IsRegister() || destination.IsFpuRegister()) {
692 if (unspecified_type) {
693 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
694 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000695 (src_cst != nullptr && (src_cst->IsIntConstant()
696 || src_cst->IsFloatConstant()
697 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000698 // For stack slots and 32bit constants, a 64bit type is appropriate.
699 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000700 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000701 // If the source is a double stack slot or a 64bit constant, a 64bit
702 // type is appropriate. Else the source is a register, and since the
703 // type has not been specified, we chose a 64bit type to force a 64bit
704 // move.
705 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000706 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000707 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000708 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
709 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000710 CPURegister dst = CPURegisterFrom(destination, type);
711 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
712 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
713 __ Ldr(dst, StackOperandFrom(source));
714 } else if (source.IsConstant()) {
715 DCHECK(CoherentConstantAndType(source, type));
716 MoveConstant(dst, source.GetConstant());
717 } else {
718 if (destination.IsRegister()) {
719 __ Mov(Register(dst), RegisterFrom(source, type));
720 } else {
721 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
722 }
723 }
724
725 } else { // The destination is not a register. It must be a stack slot.
726 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
727 if (source.IsRegister() || source.IsFpuRegister()) {
728 if (unspecified_type) {
729 if (source.IsRegister()) {
730 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
731 } else {
732 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
733 }
734 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000735 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
736 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000737 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
738 } else if (source.IsConstant()) {
739 DCHECK(unspecified_type || CoherentConstantAndType(source, type));
740 UseScratchRegisterScope temps(GetVIXLAssembler());
741 HConstant* src_cst = source.GetConstant();
742 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000743 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000744 temp = temps.AcquireW();
745 } else if (src_cst->IsLongConstant()) {
746 temp = temps.AcquireX();
747 } else if (src_cst->IsFloatConstant()) {
748 temp = temps.AcquireS();
749 } else {
750 DCHECK(src_cst->IsDoubleConstant());
751 temp = temps.AcquireD();
752 }
753 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000754 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000755 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000756 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000757 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000758 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000759 // There is generally less pressure on FP registers.
760 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000761 __ Ldr(temp, StackOperandFrom(source));
762 __ Str(temp, StackOperandFrom(destination));
763 }
764 }
765}
766
Alexandre Rames3e69f162014-12-10 10:36:50 +0000767void CodeGeneratorARM64::SwapLocations(Location loc1, Location loc2) {
768 DCHECK(!loc1.IsConstant());
769 DCHECK(!loc2.IsConstant());
770
771 if (loc1.Equals(loc2)) {
772 return;
773 }
774
775 UseScratchRegisterScope temps(GetAssembler()->vixl_masm_);
776
777 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
778 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
779 bool is_fp_reg1 = loc1.IsFpuRegister();
780 bool is_fp_reg2 = loc2.IsFpuRegister();
781
782 if (loc2.IsRegister() && loc1.IsRegister()) {
783 Register r1 = XRegisterFrom(loc1);
784 Register r2 = XRegisterFrom(loc2);
785 Register tmp = temps.AcquireSameSizeAs(r1);
786 __ Mov(tmp, r2);
787 __ Mov(r2, r1);
788 __ Mov(r1, tmp);
789 } else if (is_fp_reg2 && is_fp_reg1) {
790 FPRegister r1 = DRegisterFrom(loc1);
791 FPRegister r2 = DRegisterFrom(loc2);
792 FPRegister tmp = temps.AcquireSameSizeAs(r1);
793 __ Fmov(tmp, r2);
794 __ Fmov(r2, r1);
795 __ Fmov(r1, tmp);
796 } else if (is_slot1 != is_slot2) {
797 MemOperand mem = StackOperandFrom(is_slot1 ? loc1 : loc2);
798 Location reg_loc = is_slot1 ? loc2 : loc1;
799 CPURegister reg, tmp;
800 if (reg_loc.IsFpuRegister()) {
801 reg = DRegisterFrom(reg_loc);
802 tmp = temps.AcquireD();
803 } else {
804 reg = XRegisterFrom(reg_loc);
805 tmp = temps.AcquireX();
806 }
807 __ Ldr(tmp, mem);
808 __ Str(reg, mem);
809 if (reg_loc.IsFpuRegister()) {
810 __ Fmov(FPRegister(reg), FPRegister(tmp));
811 } else {
812 __ Mov(Register(reg), Register(tmp));
813 }
814 } else if (is_slot1 && is_slot2) {
815 MemOperand mem1 = StackOperandFrom(loc1);
816 MemOperand mem2 = StackOperandFrom(loc2);
817 Register tmp1 = loc1.IsStackSlot() ? temps.AcquireW() : temps.AcquireX();
818 Register tmp2 = temps.AcquireSameSizeAs(tmp1);
819 __ Ldr(tmp1, mem1);
820 __ Ldr(tmp2, mem2);
821 __ Str(tmp1, mem2);
822 __ Str(tmp2, mem1);
823 } else {
824 LOG(FATAL) << "Unimplemented";
825 }
826}
827
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000828void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000829 CPURegister dst,
830 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000831 switch (type) {
832 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000833 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000834 break;
835 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000836 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000837 break;
838 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000839 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000840 break;
841 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000842 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000843 break;
844 case Primitive::kPrimInt:
845 case Primitive::kPrimNot:
846 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000847 case Primitive::kPrimFloat:
848 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000849 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000850 __ Ldr(dst, src);
851 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000852 case Primitive::kPrimVoid:
853 LOG(FATAL) << "Unreachable type " << type;
854 }
855}
856
Calin Juravle77520bc2015-01-12 18:45:46 +0000857void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000858 CPURegister dst,
859 const MemOperand& src) {
860 UseScratchRegisterScope temps(GetVIXLAssembler());
861 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000862 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000863
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000864 DCHECK(!src.IsPreIndex());
865 DCHECK(!src.IsPostIndex());
866
867 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800868 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000869 MemOperand base = MemOperand(temp_base);
870 switch (type) {
871 case Primitive::kPrimBoolean:
872 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000873 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000874 break;
875 case Primitive::kPrimByte:
876 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000877 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000878 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
879 break;
880 case Primitive::kPrimChar:
881 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000882 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000883 break;
884 case Primitive::kPrimShort:
885 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000886 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000887 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
888 break;
889 case Primitive::kPrimInt:
890 case Primitive::kPrimNot:
891 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000892 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000893 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000894 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000895 break;
896 case Primitive::kPrimFloat:
897 case Primitive::kPrimDouble: {
898 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000899 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000900
901 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
902 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000903 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000904 __ Fmov(FPRegister(dst), temp);
905 break;
906 }
907 case Primitive::kPrimVoid:
908 LOG(FATAL) << "Unreachable type " << type;
909 }
910}
911
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000912void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000913 CPURegister src,
914 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000915 switch (type) {
916 case Primitive::kPrimBoolean:
917 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000918 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000919 break;
920 case Primitive::kPrimChar:
921 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000922 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000923 break;
924 case Primitive::kPrimInt:
925 case Primitive::kPrimNot:
926 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000927 case Primitive::kPrimFloat:
928 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000929 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000930 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000931 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000932 case Primitive::kPrimVoid:
933 LOG(FATAL) << "Unreachable type " << type;
934 }
935}
936
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000937void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
938 CPURegister src,
939 const MemOperand& dst) {
940 UseScratchRegisterScope temps(GetVIXLAssembler());
941 Register temp_base = temps.AcquireX();
942
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000943 DCHECK(!dst.IsPreIndex());
944 DCHECK(!dst.IsPostIndex());
945
946 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800947 Operand op = OperandFromMemOperand(dst);
948 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000949 MemOperand base = MemOperand(temp_base);
950 switch (type) {
951 case Primitive::kPrimBoolean:
952 case Primitive::kPrimByte:
953 __ Stlrb(Register(src), base);
954 break;
955 case Primitive::kPrimChar:
956 case Primitive::kPrimShort:
957 __ Stlrh(Register(src), base);
958 break;
959 case Primitive::kPrimInt:
960 case Primitive::kPrimNot:
961 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000962 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000963 __ Stlr(Register(src), base);
964 break;
965 case Primitive::kPrimFloat:
966 case Primitive::kPrimDouble: {
967 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000968 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000969
970 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
971 __ Fmov(temp, FPRegister(src));
972 __ Stlr(temp, base);
973 break;
974 }
975 case Primitive::kPrimVoid:
976 LOG(FATAL) << "Unreachable type " << type;
977 }
978}
979
Alexandre Rames67555f72014-11-18 10:55:16 +0000980void CodeGeneratorARM64::LoadCurrentMethod(vixl::Register current_method) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000981 DCHECK(RequiresCurrentMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +0000982 DCHECK(current_method.IsW());
983 __ Ldr(current_method, MemOperand(sp, kCurrentMethodStackOffset));
984}
985
986void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
987 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000988 uint32_t dex_pc,
989 SlowPathCode* slow_path) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000990 __ Ldr(lr, MemOperand(tr, entry_point_offset));
991 __ Blr(lr);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000992 if (instruction != nullptr) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000993 RecordPcInfo(instruction, dex_pc, slow_path);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000994 DCHECK(instruction->IsSuspendCheck()
995 || instruction->IsBoundsCheck()
996 || instruction->IsNullCheck()
997 || instruction->IsDivZeroCheck()
998 || !IsLeafMethod());
999 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001000}
1001
1002void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1003 vixl::Register class_reg) {
1004 UseScratchRegisterScope temps(GetVIXLAssembler());
1005 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001006 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001007 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001008
Serban Constantinescu02164b32014-11-13 14:05:07 +00001009 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001010 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001011 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1012 __ Add(temp, class_reg, status_offset);
1013 __ Ldar(temp, HeapOperand(temp));
1014 __ Cmp(temp, mirror::Class::kStatusInitialized);
1015 __ B(lt, slow_path->GetEntryLabel());
1016 } else {
1017 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1018 __ Cmp(temp, mirror::Class::kStatusInitialized);
1019 __ B(lt, slow_path->GetEntryLabel());
1020 __ Dmb(InnerShareable, BarrierReads);
1021 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001022 __ Bind(slow_path->GetExitLabel());
1023}
Alexandre Rames5319def2014-10-23 10:03:10 +01001024
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001025void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1026 BarrierType type = BarrierAll;
1027
1028 switch (kind) {
1029 case MemBarrierKind::kAnyAny:
1030 case MemBarrierKind::kAnyStore: {
1031 type = BarrierAll;
1032 break;
1033 }
1034 case MemBarrierKind::kLoadAny: {
1035 type = BarrierReads;
1036 break;
1037 }
1038 case MemBarrierKind::kStoreStore: {
1039 type = BarrierWrites;
1040 break;
1041 }
1042 default:
1043 LOG(FATAL) << "Unexpected memory barrier " << kind;
1044 }
1045 __ Dmb(InnerShareable, type);
1046}
1047
Serban Constantinescu02164b32014-11-13 14:05:07 +00001048void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1049 HBasicBlock* successor) {
1050 SuspendCheckSlowPathARM64* slow_path =
1051 new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1052 codegen_->AddSlowPath(slow_path);
1053 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1054 Register temp = temps.AcquireW();
1055
1056 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1057 if (successor == nullptr) {
1058 __ Cbnz(temp, slow_path->GetEntryLabel());
1059 __ Bind(slow_path->GetReturnLabel());
1060 } else {
1061 __ Cbz(temp, codegen_->GetLabelOf(successor));
1062 __ B(slow_path->GetEntryLabel());
1063 // slow_path will return to GetLabelOf(successor).
1064 }
1065}
1066
Alexandre Rames5319def2014-10-23 10:03:10 +01001067InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1068 CodeGeneratorARM64* codegen)
1069 : HGraphVisitor(graph),
1070 assembler_(codegen->GetAssembler()),
1071 codegen_(codegen) {}
1072
1073#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001074 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001075
1076#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1077
1078enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001079 // Using a base helps identify when we hit such breakpoints.
1080 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001081#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1082 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1083#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1084};
1085
1086#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1087 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001088 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001089 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1090 } \
1091 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1092 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1093 locations->SetOut(Location::Any()); \
1094 }
1095 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1096#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1097
1098#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001099#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001100
Alexandre Rames67555f72014-11-18 10:55:16 +00001101void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001102 DCHECK_EQ(instr->InputCount(), 2U);
1103 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1104 Primitive::Type type = instr->GetResultType();
1105 switch (type) {
1106 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001107 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001108 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray3ce57ab2015-03-12 11:06:03 +00001109 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001110 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001111 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001112
1113 case Primitive::kPrimFloat:
1114 case Primitive::kPrimDouble:
1115 locations->SetInAt(0, Location::RequiresFpuRegister());
1116 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001117 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001118 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001119
Alexandre Rames5319def2014-10-23 10:03:10 +01001120 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001121 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001122 }
1123}
1124
Alexandre Rames67555f72014-11-18 10:55:16 +00001125void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001126 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001127
1128 switch (type) {
1129 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001130 case Primitive::kPrimLong: {
1131 Register dst = OutputRegister(instr);
1132 Register lhs = InputRegisterAt(instr, 0);
1133 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001134 if (instr->IsAdd()) {
1135 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001136 } else if (instr->IsAnd()) {
1137 __ And(dst, lhs, rhs);
1138 } else if (instr->IsOr()) {
1139 __ Orr(dst, lhs, rhs);
1140 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001141 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001142 } else {
1143 DCHECK(instr->IsXor());
1144 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001145 }
1146 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001147 }
1148 case Primitive::kPrimFloat:
1149 case Primitive::kPrimDouble: {
1150 FPRegister dst = OutputFPRegister(instr);
1151 FPRegister lhs = InputFPRegisterAt(instr, 0);
1152 FPRegister rhs = InputFPRegisterAt(instr, 1);
1153 if (instr->IsAdd()) {
1154 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001155 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001156 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001157 } else {
1158 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001159 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001160 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001161 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001162 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001163 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001164 }
1165}
1166
Serban Constantinescu02164b32014-11-13 14:05:07 +00001167void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1168 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1169
1170 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1171 Primitive::Type type = instr->GetResultType();
1172 switch (type) {
1173 case Primitive::kPrimInt:
1174 case Primitive::kPrimLong: {
1175 locations->SetInAt(0, Location::RequiresRegister());
1176 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1177 locations->SetOut(Location::RequiresRegister());
1178 break;
1179 }
1180 default:
1181 LOG(FATAL) << "Unexpected shift type " << type;
1182 }
1183}
1184
1185void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1186 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1187
1188 Primitive::Type type = instr->GetType();
1189 switch (type) {
1190 case Primitive::kPrimInt:
1191 case Primitive::kPrimLong: {
1192 Register dst = OutputRegister(instr);
1193 Register lhs = InputRegisterAt(instr, 0);
1194 Operand rhs = InputOperandAt(instr, 1);
1195 if (rhs.IsImmediate()) {
1196 uint32_t shift_value = (type == Primitive::kPrimInt)
1197 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1198 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1199 if (instr->IsShl()) {
1200 __ Lsl(dst, lhs, shift_value);
1201 } else if (instr->IsShr()) {
1202 __ Asr(dst, lhs, shift_value);
1203 } else {
1204 __ Lsr(dst, lhs, shift_value);
1205 }
1206 } else {
1207 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1208
1209 if (instr->IsShl()) {
1210 __ Lsl(dst, lhs, rhs_reg);
1211 } else if (instr->IsShr()) {
1212 __ Asr(dst, lhs, rhs_reg);
1213 } else {
1214 __ Lsr(dst, lhs, rhs_reg);
1215 }
1216 }
1217 break;
1218 }
1219 default:
1220 LOG(FATAL) << "Unexpected shift operation type " << type;
1221 }
1222}
1223
Alexandre Rames5319def2014-10-23 10:03:10 +01001224void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001225 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001226}
1227
1228void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001229 HandleBinaryOp(instruction);
1230}
1231
1232void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1233 HandleBinaryOp(instruction);
1234}
1235
1236void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1237 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001238}
1239
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001240void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1241 LocationSummary* locations =
1242 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1243 locations->SetInAt(0, Location::RequiresRegister());
1244 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1245 locations->SetOut(Location::RequiresRegister());
1246}
1247
1248void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1249 LocationSummary* locations = instruction->GetLocations();
1250 Primitive::Type type = instruction->GetType();
1251 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001252 Location index = locations->InAt(1);
1253 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001254 MemOperand source = HeapOperand(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00001255 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001256
1257 if (index.IsConstant()) {
1258 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001259 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001260 } else {
1261 Register temp = temps.AcquireSameSizeAs(obj);
1262 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
1263 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001264 source = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001265 }
1266
Alexandre Rames67555f72014-11-18 10:55:16 +00001267 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001268 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001269}
1270
Alexandre Rames5319def2014-10-23 10:03:10 +01001271void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1272 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1273 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001274 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001275}
1276
1277void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
1278 __ Ldr(OutputRegister(instruction),
1279 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001280 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001281}
1282
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001283void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
1284 Primitive::Type value_type = instruction->GetComponentType();
1285 bool is_object = value_type == Primitive::kPrimNot;
1286 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1287 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1288 if (is_object) {
1289 InvokeRuntimeCallingConvention calling_convention;
1290 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1291 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1292 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1293 } else {
1294 locations->SetInAt(0, Location::RequiresRegister());
1295 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1296 locations->SetInAt(2, Location::RequiresRegister());
1297 }
1298}
1299
1300void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1301 Primitive::Type value_type = instruction->GetComponentType();
1302 if (value_type == Primitive::kPrimNot) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001303 codegen_->InvokeRuntime(
1304 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001305 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001306 } else {
1307 LocationSummary* locations = instruction->GetLocations();
1308 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001309 CPURegister value = InputCPURegisterAt(instruction, 2);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001310 Location index = locations->InAt(1);
1311 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001312 MemOperand destination = HeapOperand(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00001313 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001314
1315 if (index.IsConstant()) {
1316 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001317 destination = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001318 } else {
1319 Register temp = temps.AcquireSameSizeAs(obj);
1320 Register index_reg = InputRegisterAt(instruction, 1);
1321 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(value_type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001322 destination = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001323 }
1324
1325 codegen_->Store(value_type, value, destination);
Calin Juravle77520bc2015-01-12 18:45:46 +00001326 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001327 }
1328}
1329
Alexandre Rames67555f72014-11-18 10:55:16 +00001330void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1331 LocationSummary* locations =
1332 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1333 locations->SetInAt(0, Location::RequiresRegister());
1334 locations->SetInAt(1, Location::RequiresRegister());
1335 if (instruction->HasUses()) {
1336 locations->SetOut(Location::SameAsFirstInput());
1337 }
1338}
1339
1340void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001341 LocationSummary* locations = instruction->GetLocations();
1342 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1343 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001344 codegen_->AddSlowPath(slow_path);
1345
1346 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1347 __ B(slow_path->GetEntryLabel(), hs);
1348}
1349
1350void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1351 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1352 instruction, LocationSummary::kCallOnSlowPath);
1353 locations->SetInAt(0, Location::RequiresRegister());
1354 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001355 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001356}
1357
1358void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001359 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001360 Register obj = InputRegisterAt(instruction, 0);;
1361 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001362 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001363
Alexandre Rames3e69f162014-12-10 10:36:50 +00001364 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1365 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001366 codegen_->AddSlowPath(slow_path);
1367
1368 // TODO: avoid this check if we know obj is not null.
1369 __ Cbz(obj, slow_path->GetExitLabel());
1370 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001371 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
1372 __ Cmp(obj_cls, cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00001373 __ B(ne, slow_path->GetEntryLabel());
1374 __ Bind(slow_path->GetExitLabel());
1375}
1376
1377void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1378 LocationSummary* locations =
1379 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1380 locations->SetInAt(0, Location::RequiresRegister());
1381 if (check->HasUses()) {
1382 locations->SetOut(Location::SameAsFirstInput());
1383 }
1384}
1385
1386void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1387 // We assume the class is not null.
1388 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1389 check->GetLoadClass(), check, check->GetDexPc(), true);
1390 codegen_->AddSlowPath(slow_path);
1391 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1392}
1393
Serban Constantinescu02164b32014-11-13 14:05:07 +00001394void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001395 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001396 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1397 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001398 switch (in_type) {
1399 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001400 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray3ce57ab2015-03-12 11:06:03 +00001401 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001402 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1403 break;
1404 }
1405 case Primitive::kPrimFloat:
1406 case Primitive::kPrimDouble: {
1407 locations->SetInAt(0, Location::RequiresFpuRegister());
Alexandre Rames93415462015-02-17 15:08:20 +00001408 HInstruction* right = compare->InputAt(1);
1409 if ((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1410 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0))) {
1411 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1412 } else {
1413 locations->SetInAt(1, Location::RequiresFpuRegister());
1414 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001415 locations->SetOut(Location::RequiresRegister());
1416 break;
1417 }
1418 default:
1419 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1420 }
1421}
1422
1423void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1424 Primitive::Type in_type = compare->InputAt(0)->GetType();
1425
1426 // 0 if: left == right
1427 // 1 if: left > right
1428 // -1 if: left < right
1429 switch (in_type) {
1430 case Primitive::kPrimLong: {
1431 Register result = OutputRegister(compare);
1432 Register left = InputRegisterAt(compare, 0);
1433 Operand right = InputOperandAt(compare, 1);
1434
1435 __ Cmp(left, right);
1436 __ Cset(result, ne);
1437 __ Cneg(result, result, lt);
1438 break;
1439 }
1440 case Primitive::kPrimFloat:
1441 case Primitive::kPrimDouble: {
1442 Register result = OutputRegister(compare);
1443 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001444 if (compare->GetLocations()->InAt(1).IsConstant()) {
1445 if (kIsDebugBuild) {
1446 HInstruction* right = compare->GetLocations()->InAt(1).GetConstant();
1447 DCHECK((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1448 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0)));
1449 }
1450 // 0.0 is the only immediate that can be encoded directly in a FCMP instruction.
1451 __ Fcmp(left, 0.0);
1452 } else {
1453 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1454 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001455 if (compare->IsGtBias()) {
1456 __ Cset(result, ne);
1457 } else {
1458 __ Csetm(result, ne);
1459 }
1460 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001461 break;
1462 }
1463 default:
1464 LOG(FATAL) << "Unimplemented compare type " << in_type;
1465 }
1466}
1467
1468void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1469 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1470 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray3ce57ab2015-03-12 11:06:03 +00001471 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01001472 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001473 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001474 }
1475}
1476
1477void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1478 if (!instruction->NeedsMaterialization()) {
1479 return;
1480 }
1481
1482 LocationSummary* locations = instruction->GetLocations();
1483 Register lhs = InputRegisterAt(instruction, 0);
1484 Operand rhs = InputOperandAt(instruction, 1);
1485 Register res = RegisterFrom(locations->Out(), instruction->GetType());
1486 Condition cond = ARM64Condition(instruction->GetCondition());
1487
1488 __ Cmp(lhs, rhs);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001489 __ Cset(res, cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001490}
1491
1492#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1493 M(Equal) \
1494 M(NotEqual) \
1495 M(LessThan) \
1496 M(LessThanOrEqual) \
1497 M(GreaterThan) \
1498 M(GreaterThanOrEqual)
1499#define DEFINE_CONDITION_VISITORS(Name) \
1500void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1501void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1502FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001503#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001504#undef FOR_EACH_CONDITION_INSTRUCTION
1505
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001506void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1507 LocationSummary* locations =
1508 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1509 switch (div->GetResultType()) {
1510 case Primitive::kPrimInt:
1511 case Primitive::kPrimLong:
1512 locations->SetInAt(0, Location::RequiresRegister());
1513 locations->SetInAt(1, Location::RequiresRegister());
1514 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1515 break;
1516
1517 case Primitive::kPrimFloat:
1518 case Primitive::kPrimDouble:
1519 locations->SetInAt(0, Location::RequiresFpuRegister());
1520 locations->SetInAt(1, Location::RequiresFpuRegister());
1521 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1522 break;
1523
1524 default:
1525 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1526 }
1527}
1528
1529void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1530 Primitive::Type type = div->GetResultType();
1531 switch (type) {
1532 case Primitive::kPrimInt:
1533 case Primitive::kPrimLong:
1534 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
1535 break;
1536
1537 case Primitive::kPrimFloat:
1538 case Primitive::kPrimDouble:
1539 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1540 break;
1541
1542 default:
1543 LOG(FATAL) << "Unexpected div type " << type;
1544 }
1545}
1546
Alexandre Rames67555f72014-11-18 10:55:16 +00001547void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1548 LocationSummary* locations =
1549 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1550 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1551 if (instruction->HasUses()) {
1552 locations->SetOut(Location::SameAsFirstInput());
1553 }
1554}
1555
1556void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1557 SlowPathCodeARM64* slow_path =
1558 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1559 codegen_->AddSlowPath(slow_path);
1560 Location value = instruction->GetLocations()->InAt(0);
1561
Alexandre Rames3e69f162014-12-10 10:36:50 +00001562 Primitive::Type type = instruction->GetType();
1563
1564 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
1565 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
1566 return;
1567 }
1568
Alexandre Rames67555f72014-11-18 10:55:16 +00001569 if (value.IsConstant()) {
1570 int64_t divisor = Int64ConstantFrom(value);
1571 if (divisor == 0) {
1572 __ B(slow_path->GetEntryLabel());
1573 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001574 // A division by a non-null constant is valid. We don't need to perform
1575 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00001576 }
1577 } else {
1578 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
1579 }
1580}
1581
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001582void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1583 LocationSummary* locations =
1584 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1585 locations->SetOut(Location::ConstantLocation(constant));
1586}
1587
1588void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1589 UNUSED(constant);
1590 // Will be generated at use site.
1591}
1592
Alexandre Rames5319def2014-10-23 10:03:10 +01001593void LocationsBuilderARM64::VisitExit(HExit* exit) {
1594 exit->SetLocations(nullptr);
1595}
1596
1597void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001598 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01001599 if (kIsDebugBuild) {
1600 down_cast<Arm64Assembler*>(GetAssembler())->Comment("Unreachable");
Alexandre Rames67555f72014-11-18 10:55:16 +00001601 __ Brk(__LINE__); // TODO: Introduce special markers for such code locations.
Alexandre Rames5319def2014-10-23 10:03:10 +01001602 }
1603}
1604
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001605void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
1606 LocationSummary* locations =
1607 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1608 locations->SetOut(Location::ConstantLocation(constant));
1609}
1610
1611void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
1612 UNUSED(constant);
1613 // Will be generated at use site.
1614}
1615
Alexandre Rames5319def2014-10-23 10:03:10 +01001616void LocationsBuilderARM64::VisitGoto(HGoto* got) {
1617 got->SetLocations(nullptr);
1618}
1619
1620void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
1621 HBasicBlock* successor = got->GetSuccessor();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001622 DCHECK(!successor->IsExitBlock());
1623 HBasicBlock* block = got->GetBlock();
1624 HInstruction* previous = got->GetPrevious();
1625 HLoopInformation* info = block->GetLoopInformation();
1626
1627 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
1628 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1629 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1630 return;
1631 }
1632 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1633 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1634 }
1635 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001636 __ B(codegen_->GetLabelOf(successor));
1637 }
1638}
1639
1640void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
1641 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1642 HInstruction* cond = if_instr->InputAt(0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001643 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001644 locations->SetInAt(0, Location::RequiresRegister());
1645 }
1646}
1647
1648void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
1649 HInstruction* cond = if_instr->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001650 HCondition* condition = cond->AsCondition();
1651 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1652 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1653
Serban Constantinescu02164b32014-11-13 14:05:07 +00001654 if (cond->IsIntConstant()) {
1655 int32_t cond_value = cond->AsIntConstant()->GetValue();
1656 if (cond_value == 1) {
1657 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfTrueSuccessor())) {
1658 __ B(true_target);
1659 }
1660 return;
1661 } else {
1662 DCHECK_EQ(cond_value, 0);
1663 }
1664 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001665 // The condition instruction has been materialized, compare the output to 0.
1666 Location cond_val = if_instr->GetLocations()->InAt(0);
1667 DCHECK(cond_val.IsRegister());
1668 __ Cbnz(InputRegisterAt(if_instr, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001669 } else {
1670 // The condition instruction has not been materialized, use its inputs as
1671 // the comparison and its condition as the branch condition.
1672 Register lhs = InputRegisterAt(condition, 0);
1673 Operand rhs = InputOperandAt(condition, 1);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001674 Condition arm64_cond = ARM64Condition(condition->GetCondition());
1675 if ((arm64_cond == eq || arm64_cond == ne) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
1676 if (arm64_cond == eq) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001677 __ Cbz(lhs, true_target);
1678 } else {
1679 __ Cbnz(lhs, true_target);
1680 }
1681 } else {
1682 __ Cmp(lhs, rhs);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001683 __ B(arm64_cond, true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001684 }
1685 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001686 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
1687 __ B(false_target);
1688 }
1689}
1690
1691void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001692 LocationSummary* locations =
1693 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames5319def2014-10-23 10:03:10 +01001694 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001695 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001696}
1697
1698void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001699 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), instruction->GetFieldOffset());
Serban Constantinescu579885a2015-02-22 20:51:33 +00001700 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001701
1702 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00001703 if (use_acquire_release) {
Calin Juravle77520bc2015-01-12 18:45:46 +00001704 // NB: LoadAcquire will record the pc info if needed.
1705 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001706 } else {
1707 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
Calin Juravle77520bc2015-01-12 18:45:46 +00001708 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001709 // For IRIW sequential consistency kLoadAny is not sufficient.
1710 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1711 }
1712 } else {
1713 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
Calin Juravle77520bc2015-01-12 18:45:46 +00001714 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001715 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001716}
1717
1718void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001719 LocationSummary* locations =
1720 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames5319def2014-10-23 10:03:10 +01001721 locations->SetInAt(0, Location::RequiresRegister());
1722 locations->SetInAt(1, Location::RequiresRegister());
1723}
1724
1725void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001726 Register obj = InputRegisterAt(instruction, 0);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001727 CPURegister value = InputCPURegisterAt(instruction, 1);
1728 Offset offset = instruction->GetFieldOffset();
1729 Primitive::Type field_type = instruction->GetFieldType();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001730 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001731
1732 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00001733 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001734 codegen_->StoreRelease(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001735 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001736 } else {
1737 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1738 codegen_->Store(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001739 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001740 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1741 }
1742 } else {
1743 codegen_->Store(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001744 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001745 }
1746
1747 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001748 codegen_->MarkGCCard(obj, Register(value));
Alexandre Rames5319def2014-10-23 10:03:10 +01001749 }
1750}
1751
Alexandre Rames67555f72014-11-18 10:55:16 +00001752void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
1753 LocationSummary::CallKind call_kind =
1754 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
1755 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1756 locations->SetInAt(0, Location::RequiresRegister());
1757 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001758 // The output does overlap inputs.
1759 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00001760}
1761
1762void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
1763 LocationSummary* locations = instruction->GetLocations();
1764 Register obj = InputRegisterAt(instruction, 0);;
1765 Register cls = InputRegisterAt(instruction, 1);;
1766 Register out = OutputRegister(instruction);
1767
1768 vixl::Label done;
1769
1770 // Return 0 if `obj` is null.
1771 // TODO: Avoid this check if we know `obj` is not null.
1772 __ Mov(out, 0);
1773 __ Cbz(obj, &done);
1774
1775 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00001776 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Alexandre Rames67555f72014-11-18 10:55:16 +00001777 __ Cmp(out, cls);
1778 if (instruction->IsClassFinal()) {
1779 // Classes must be equal for the instanceof to succeed.
1780 __ Cset(out, eq);
1781 } else {
1782 // If the classes are not equal, we go into a slow path.
1783 DCHECK(locations->OnlyCallsOnSlowPath());
1784 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00001785 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1786 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001787 codegen_->AddSlowPath(slow_path);
1788 __ B(ne, slow_path->GetEntryLabel());
1789 __ Mov(out, 1);
1790 __ Bind(slow_path->GetExitLabel());
1791 }
1792
1793 __ Bind(&done);
1794}
1795
Alexandre Rames5319def2014-10-23 10:03:10 +01001796void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
1797 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
1798 locations->SetOut(Location::ConstantLocation(constant));
1799}
1800
1801void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
1802 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001803 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01001804}
1805
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001806void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
1807 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
1808 locations->SetOut(Location::ConstantLocation(constant));
1809}
1810
1811void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
1812 // Will be generated at use site.
1813 UNUSED(constant);
1814}
1815
Alexandre Rames5319def2014-10-23 10:03:10 +01001816void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
1817 LocationSummary* locations =
1818 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
1819 locations->AddTemp(LocationFrom(x0));
1820
1821 InvokeDexCallingConventionVisitor calling_convention_visitor;
1822 for (size_t i = 0; i < invoke->InputCount(); i++) {
1823 HInstruction* input = invoke->InputAt(i);
1824 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1825 }
1826
1827 Primitive::Type return_type = invoke->GetType();
1828 if (return_type != Primitive::kPrimVoid) {
1829 locations->SetOut(calling_convention_visitor.GetReturnLocation(return_type));
1830 }
1831}
1832
Alexandre Rames67555f72014-11-18 10:55:16 +00001833void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1834 HandleInvoke(invoke);
1835}
1836
1837void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1838 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1839 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
1840 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1841 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1842 Location receiver = invoke->GetLocations()->InAt(0);
1843 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001844 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00001845
1846 // The register ip1 is required to be used for the hidden argument in
1847 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
1848 UseScratchRegisterScope scratch_scope(GetVIXLAssembler());
1849 scratch_scope.Exclude(ip1);
1850 __ Mov(ip1, invoke->GetDexMethodIndex());
1851
1852 // temp = object->GetClass();
1853 if (receiver.IsStackSlot()) {
1854 __ Ldr(temp, StackOperandFrom(receiver));
1855 __ Ldr(temp, HeapOperand(temp, class_offset));
1856 } else {
1857 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
1858 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001859 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00001860 // temp = temp->GetImtEntryAt(method_offset);
1861 __ Ldr(temp, HeapOperand(temp, method_offset));
1862 // lr = temp->GetEntryPoint();
1863 __ Ldr(lr, HeapOperand(temp, entry_point));
1864 // lr();
1865 __ Blr(lr);
1866 DCHECK(!codegen_->IsLeafMethod());
1867 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1868}
1869
1870void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001871 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1872 if (intrinsic.TryDispatch(invoke)) {
1873 return;
1874 }
1875
Alexandre Rames67555f72014-11-18 10:55:16 +00001876 HandleInvoke(invoke);
1877}
1878
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001879void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001880 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1881 if (intrinsic.TryDispatch(invoke)) {
1882 return;
1883 }
1884
Alexandre Rames67555f72014-11-18 10:55:16 +00001885 HandleInvoke(invoke);
1886}
1887
Andreas Gampe878d58c2015-01-15 23:24:00 -08001888static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
1889 if (invoke->GetLocations()->Intrinsified()) {
1890 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
1891 intrinsic.Dispatch(invoke);
1892 return true;
1893 }
1894 return false;
1895}
1896
1897void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Register temp) {
1898 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
1899 DCHECK(temp.Is(kArtMethodRegister));
Alexandre Rames5319def2014-10-23 10:03:10 +01001900 size_t index_in_cache = mirror::Array::DataOffset(kHeapRefSize).SizeValue() +
Andreas Gampe878d58c2015-01-15 23:24:00 -08001901 invoke->GetDexMethodIndex() * kHeapRefSize;
Alexandre Rames5319def2014-10-23 10:03:10 +01001902
1903 // TODO: Implement all kinds of calls:
1904 // 1) boot -> boot
1905 // 2) app -> boot
1906 // 3) app -> app
1907 //
1908 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1909
Nicolas Geoffray0a299b92015-01-29 11:39:44 +00001910 // temp = method;
1911 LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001912 if (!invoke->IsRecursive()) {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001913 // temp = temp->dex_cache_resolved_methods_;
1914 __ Ldr(temp, HeapOperand(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset()));
1915 // temp = temp[index_in_cache];
1916 __ Ldr(temp, HeapOperand(temp, index_in_cache));
1917 // lr = temp->entry_point_from_quick_compiled_code_;
1918 __ Ldr(lr, HeapOperand(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
1919 kArm64WordSize)));
1920 // lr();
1921 __ Blr(lr);
1922 } else {
1923 __ Bl(&frame_entry_label_);
1924 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001925
Andreas Gampe878d58c2015-01-15 23:24:00 -08001926 DCHECK(!IsLeafMethod());
1927}
1928
1929void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
1930 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1931 return;
1932 }
1933
1934 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
1935 codegen_->GenerateStaticOrDirectCall(invoke, temp);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001936 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01001937}
1938
1939void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001940 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1941 return;
1942 }
1943
Alexandre Rames5319def2014-10-23 10:03:10 +01001944 LocationSummary* locations = invoke->GetLocations();
1945 Location receiver = locations->InAt(0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001946 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01001947 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1948 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1949 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001950 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01001951
1952 // temp = object->GetClass();
1953 if (receiver.IsStackSlot()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001954 __ Ldr(temp, MemOperand(sp, receiver.GetStackIndex()));
1955 __ Ldr(temp, HeapOperand(temp, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01001956 } else {
1957 DCHECK(receiver.IsRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001958 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01001959 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001960 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames5319def2014-10-23 10:03:10 +01001961 // temp = temp->GetMethodAt(method_offset);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001962 __ Ldr(temp, HeapOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01001963 // lr = temp->GetEntryPoint();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001964 __ Ldr(lr, HeapOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001965 // lr();
1966 __ Blr(lr);
1967 DCHECK(!codegen_->IsLeafMethod());
1968 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1969}
1970
Alexandre Rames67555f72014-11-18 10:55:16 +00001971void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
1972 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
1973 : LocationSummary::kNoCall;
1974 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
1975 locations->SetOut(Location::RequiresRegister());
1976}
1977
1978void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
1979 Register out = OutputRegister(cls);
1980 if (cls->IsReferrersClass()) {
1981 DCHECK(!cls->CanCallRuntime());
1982 DCHECK(!cls->MustGenerateClinitCheck());
1983 codegen_->LoadCurrentMethod(out);
1984 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
1985 } else {
1986 DCHECK(cls->CanCallRuntime());
1987 codegen_->LoadCurrentMethod(out);
1988 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DexCacheResolvedTypesOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001989 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00001990
1991 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1992 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
1993 codegen_->AddSlowPath(slow_path);
1994 __ Cbz(out, slow_path->GetEntryLabel());
1995 if (cls->MustGenerateClinitCheck()) {
1996 GenerateClassInitializationCheck(slow_path, out);
1997 } else {
1998 __ Bind(slow_path->GetExitLabel());
1999 }
2000 }
2001}
2002
2003void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2004 LocationSummary* locations =
2005 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2006 locations->SetOut(Location::RequiresRegister());
2007}
2008
2009void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
2010 MemOperand exception = MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2011 __ Ldr(OutputRegister(instruction), exception);
2012 __ Str(wzr, exception);
2013}
2014
Alexandre Rames5319def2014-10-23 10:03:10 +01002015void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2016 load->SetLocations(nullptr);
2017}
2018
2019void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2020 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002021 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002022}
2023
Alexandre Rames67555f72014-11-18 10:55:16 +00002024void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2025 LocationSummary* locations =
2026 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2027 locations->SetOut(Location::RequiresRegister());
2028}
2029
2030void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2031 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2032 codegen_->AddSlowPath(slow_path);
2033
2034 Register out = OutputRegister(load);
2035 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08002036 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
2037 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002038 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002039 __ Cbz(out, slow_path->GetEntryLabel());
2040 __ Bind(slow_path->GetExitLabel());
2041}
2042
Alexandre Rames5319def2014-10-23 10:03:10 +01002043void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2044 local->SetLocations(nullptr);
2045}
2046
2047void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2048 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2049}
2050
2051void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2052 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2053 locations->SetOut(Location::ConstantLocation(constant));
2054}
2055
2056void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2057 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002058 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002059}
2060
Alexandre Rames67555f72014-11-18 10:55:16 +00002061void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2062 LocationSummary* locations =
2063 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2064 InvokeRuntimeCallingConvention calling_convention;
2065 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2066}
2067
2068void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2069 codegen_->InvokeRuntime(instruction->IsEnter()
2070 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2071 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002072 instruction->GetDexPc(),
2073 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002074 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002075}
2076
Alexandre Rames42d641b2014-10-27 14:00:51 +00002077void LocationsBuilderARM64::VisitMul(HMul* mul) {
2078 LocationSummary* locations =
2079 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2080 switch (mul->GetResultType()) {
2081 case Primitive::kPrimInt:
2082 case Primitive::kPrimLong:
2083 locations->SetInAt(0, Location::RequiresRegister());
2084 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002085 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002086 break;
2087
2088 case Primitive::kPrimFloat:
2089 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002090 locations->SetInAt(0, Location::RequiresFpuRegister());
2091 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002092 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002093 break;
2094
2095 default:
2096 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2097 }
2098}
2099
2100void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2101 switch (mul->GetResultType()) {
2102 case Primitive::kPrimInt:
2103 case Primitive::kPrimLong:
2104 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2105 break;
2106
2107 case Primitive::kPrimFloat:
2108 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002109 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002110 break;
2111
2112 default:
2113 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2114 }
2115}
2116
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002117void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2118 LocationSummary* locations =
2119 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2120 switch (neg->GetResultType()) {
2121 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002122 case Primitive::kPrimLong:
Nicolas Geoffray3ce57ab2015-03-12 11:06:03 +00002123 locations->SetInAt(0, Location::RegisterOrConstant(neg->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00002124 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002125 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002126
2127 case Primitive::kPrimFloat:
2128 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002129 locations->SetInAt(0, Location::RequiresFpuRegister());
2130 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002131 break;
2132
2133 default:
2134 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2135 }
2136}
2137
2138void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2139 switch (neg->GetResultType()) {
2140 case Primitive::kPrimInt:
2141 case Primitive::kPrimLong:
2142 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2143 break;
2144
2145 case Primitive::kPrimFloat:
2146 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002147 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002148 break;
2149
2150 default:
2151 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2152 }
2153}
2154
2155void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2156 LocationSummary* locations =
2157 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2158 InvokeRuntimeCallingConvention calling_convention;
2159 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002160 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002161 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002162 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2163 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2164 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002165}
2166
2167void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2168 LocationSummary* locations = instruction->GetLocations();
2169 InvokeRuntimeCallingConvention calling_convention;
2170 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2171 DCHECK(type_index.Is(w0));
2172 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002173 DCHECK(current_method.Is(w2));
Alexandre Rames67555f72014-11-18 10:55:16 +00002174 codegen_->LoadCurrentMethod(current_method);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002175 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002176 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002177 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2178 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002179 instruction->GetDexPc(),
2180 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002181 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2182 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002183}
2184
Alexandre Rames5319def2014-10-23 10:03:10 +01002185void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2186 LocationSummary* locations =
2187 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2188 InvokeRuntimeCallingConvention calling_convention;
2189 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2190 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2191 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002192 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002193}
2194
2195void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2196 LocationSummary* locations = instruction->GetLocations();
2197 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2198 DCHECK(type_index.Is(w0));
2199 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
2200 DCHECK(current_method.Is(w1));
Alexandre Rames67555f72014-11-18 10:55:16 +00002201 codegen_->LoadCurrentMethod(current_method);
Alexandre Rames5319def2014-10-23 10:03:10 +01002202 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002203 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002204 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2205 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002206 instruction->GetDexPc(),
2207 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002208 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002209}
2210
2211void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2212 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002213 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002214 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002215}
2216
2217void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002218 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002219 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002220 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002221 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002222 break;
2223
2224 default:
2225 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2226 }
2227}
2228
2229void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2230 LocationSummary* locations =
2231 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2232 locations->SetInAt(0, Location::RequiresRegister());
2233 if (instruction->HasUses()) {
2234 locations->SetOut(Location::SameAsFirstInput());
2235 }
2236}
2237
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002238void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002239 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2240 return;
2241 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002242 Location obj = instruction->GetLocations()->InAt(0);
2243
2244 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2245 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2246}
2247
2248void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002249 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2250 codegen_->AddSlowPath(slow_path);
2251
2252 LocationSummary* locations = instruction->GetLocations();
2253 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002254
2255 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002256}
2257
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002258void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2259 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2260 GenerateImplicitNullCheck(instruction);
2261 } else {
2262 GenerateExplicitNullCheck(instruction);
2263 }
2264}
2265
Alexandre Rames67555f72014-11-18 10:55:16 +00002266void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2267 HandleBinaryOp(instruction);
2268}
2269
2270void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2271 HandleBinaryOp(instruction);
2272}
2273
Alexandre Rames3e69f162014-12-10 10:36:50 +00002274void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2275 LOG(FATAL) << "Unreachable";
2276}
2277
2278void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2279 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2280}
2281
Alexandre Rames5319def2014-10-23 10:03:10 +01002282void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2283 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2284 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2285 if (location.IsStackSlot()) {
2286 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2287 } else if (location.IsDoubleStackSlot()) {
2288 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2289 }
2290 locations->SetOut(location);
2291}
2292
2293void InstructionCodeGeneratorARM64::VisitParameterValue(HParameterValue* instruction) {
2294 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002295 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002296}
2297
2298void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2299 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2300 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2301 locations->SetInAt(i, Location::Any());
2302 }
2303 locations->SetOut(Location::Any());
2304}
2305
2306void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002307 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002308 LOG(FATAL) << "Unreachable";
2309}
2310
Serban Constantinescu02164b32014-11-13 14:05:07 +00002311void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002312 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002313 LocationSummary::CallKind call_kind =
2314 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002315 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2316
2317 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002318 case Primitive::kPrimInt:
2319 case Primitive::kPrimLong:
2320 locations->SetInAt(0, Location::RequiresRegister());
2321 locations->SetInAt(1, Location::RequiresRegister());
2322 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2323 break;
2324
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002325 case Primitive::kPrimFloat:
2326 case Primitive::kPrimDouble: {
2327 InvokeRuntimeCallingConvention calling_convention;
2328 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2329 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2330 locations->SetOut(calling_convention.GetReturnLocation(type));
2331
2332 break;
2333 }
2334
Serban Constantinescu02164b32014-11-13 14:05:07 +00002335 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002336 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002337 }
2338}
2339
2340void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2341 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002342
Serban Constantinescu02164b32014-11-13 14:05:07 +00002343 switch (type) {
2344 case Primitive::kPrimInt:
2345 case Primitive::kPrimLong: {
2346 UseScratchRegisterScope temps(GetVIXLAssembler());
2347 Register dividend = InputRegisterAt(rem, 0);
2348 Register divisor = InputRegisterAt(rem, 1);
2349 Register output = OutputRegister(rem);
2350 Register temp = temps.AcquireSameSizeAs(output);
2351
2352 __ Sdiv(temp, dividend, divisor);
2353 __ Msub(output, temp, divisor, dividend);
2354 break;
2355 }
2356
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002357 case Primitive::kPrimFloat:
2358 case Primitive::kPrimDouble: {
2359 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2360 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002361 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002362 break;
2363 }
2364
Serban Constantinescu02164b32014-11-13 14:05:07 +00002365 default:
2366 LOG(FATAL) << "Unexpected rem type " << type;
2367 }
2368}
2369
Alexandre Rames5319def2014-10-23 10:03:10 +01002370void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2371 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2372 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002373 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002374}
2375
2376void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002377 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002378 codegen_->GenerateFrameExit();
Alexandre Rames3e69f162014-12-10 10:36:50 +00002379 __ Ret();
Alexandre Rames5319def2014-10-23 10:03:10 +01002380}
2381
2382void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2383 instruction->SetLocations(nullptr);
2384}
2385
2386void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002387 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002388 codegen_->GenerateFrameExit();
Alexandre Rames3e69f162014-12-10 10:36:50 +00002389 __ Ret();
Alexandre Rames5319def2014-10-23 10:03:10 +01002390}
2391
Serban Constantinescu02164b32014-11-13 14:05:07 +00002392void LocationsBuilderARM64::VisitShl(HShl* shl) {
2393 HandleShift(shl);
2394}
2395
2396void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2397 HandleShift(shl);
2398}
2399
2400void LocationsBuilderARM64::VisitShr(HShr* shr) {
2401 HandleShift(shr);
2402}
2403
2404void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2405 HandleShift(shr);
2406}
2407
Alexandre Rames5319def2014-10-23 10:03:10 +01002408void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2409 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2410 Primitive::Type field_type = store->InputAt(1)->GetType();
2411 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002412 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002413 case Primitive::kPrimBoolean:
2414 case Primitive::kPrimByte:
2415 case Primitive::kPrimChar:
2416 case Primitive::kPrimShort:
2417 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002418 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002419 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2420 break;
2421
2422 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002423 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002424 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2425 break;
2426
2427 default:
2428 LOG(FATAL) << "Unimplemented local type " << field_type;
2429 }
2430}
2431
2432void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002433 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002434}
2435
2436void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002437 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002438}
2439
2440void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002441 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002442}
2443
Alexandre Rames67555f72014-11-18 10:55:16 +00002444void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2445 LocationSummary* locations =
2446 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2447 locations->SetInAt(0, Location::RequiresRegister());
2448 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2449}
2450
2451void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002452 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), instruction->GetFieldOffset());
Serban Constantinescu579885a2015-02-22 20:51:33 +00002453 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002454
2455 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00002456 if (use_acquire_release) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002457 // NB: LoadAcquire will record the pc info if needed.
2458 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002459 } else {
2460 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
2461 // For IRIW sequential consistency kLoadAny is not sufficient.
2462 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2463 }
2464 } else {
2465 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
2466 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002467}
2468
2469void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002470 LocationSummary* locations =
2471 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2472 locations->SetInAt(0, Location::RequiresRegister());
2473 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01002474}
2475
Alexandre Rames67555f72014-11-18 10:55:16 +00002476void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002477 Register cls = InputRegisterAt(instruction, 0);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002478 CPURegister value = InputCPURegisterAt(instruction, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002479 Offset offset = instruction->GetFieldOffset();
Alexandre Rames67555f72014-11-18 10:55:16 +00002480 Primitive::Type field_type = instruction->GetFieldType();
Serban Constantinescu579885a2015-02-22 20:51:33 +00002481 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Alexandre Rames5319def2014-10-23 10:03:10 +01002482
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002483 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00002484 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002485 codegen_->StoreRelease(field_type, value, HeapOperand(cls, offset));
2486 } else {
2487 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2488 codegen_->Store(field_type, value, HeapOperand(cls, offset));
2489 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2490 }
2491 } else {
2492 codegen_->Store(field_type, value, HeapOperand(cls, offset));
2493 }
2494
2495 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002496 codegen_->MarkGCCard(cls, Register(value));
2497 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002498}
2499
2500void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2501 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2502}
2503
2504void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002505 HBasicBlock* block = instruction->GetBlock();
2506 if (block->GetLoopInformation() != nullptr) {
2507 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2508 // The back edge will generate the suspend check.
2509 return;
2510 }
2511 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2512 // The goto will generate the suspend check.
2513 return;
2514 }
2515 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01002516}
2517
2518void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
2519 temp->SetLocations(nullptr);
2520}
2521
2522void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
2523 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002524 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01002525}
2526
Alexandre Rames67555f72014-11-18 10:55:16 +00002527void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
2528 LocationSummary* locations =
2529 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2530 InvokeRuntimeCallingConvention calling_convention;
2531 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2532}
2533
2534void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
2535 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002536 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002537 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002538}
2539
2540void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
2541 LocationSummary* locations =
2542 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2543 Primitive::Type input_type = conversion->GetInputType();
2544 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002545 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00002546 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
2547 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
2548 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
2549 }
2550
Alexandre Rames542361f2015-01-29 16:57:31 +00002551 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002552 locations->SetInAt(0, Location::RequiresFpuRegister());
2553 } else {
2554 locations->SetInAt(0, Location::RequiresRegister());
2555 }
2556
Alexandre Rames542361f2015-01-29 16:57:31 +00002557 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002558 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2559 } else {
2560 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2561 }
2562}
2563
2564void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
2565 Primitive::Type result_type = conversion->GetResultType();
2566 Primitive::Type input_type = conversion->GetInputType();
2567
2568 DCHECK_NE(input_type, result_type);
2569
Alexandre Rames542361f2015-01-29 16:57:31 +00002570 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002571 int result_size = Primitive::ComponentSize(result_type);
2572 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002573 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002574 Register output = OutputRegister(conversion);
2575 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002576 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
2577 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
2578 } else if ((result_type == Primitive::kPrimChar) ||
2579 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
2580 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002581 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002582 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002583 }
Alexandre Rames542361f2015-01-29 16:57:31 +00002584 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002585 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002586 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002587 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
2588 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002589 } else if (Primitive::IsFloatingPointType(result_type) &&
2590 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002591 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
2592 } else {
2593 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
2594 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00002595 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002596}
Alexandre Rames67555f72014-11-18 10:55:16 +00002597
Serban Constantinescu02164b32014-11-13 14:05:07 +00002598void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
2599 HandleShift(ushr);
2600}
2601
2602void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
2603 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00002604}
2605
2606void LocationsBuilderARM64::VisitXor(HXor* instruction) {
2607 HandleBinaryOp(instruction);
2608}
2609
2610void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
2611 HandleBinaryOp(instruction);
2612}
2613
Calin Juravleb1498f62015-02-16 13:13:29 +00002614void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
2615 // Nothing to do, this should be removed during prepare for register allocator.
2616 UNUSED(instruction);
2617 LOG(FATAL) << "Unreachable";
2618}
2619
2620void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
2621 // Nothing to do, this should be removed during prepare for register allocator.
2622 UNUSED(instruction);
2623 LOG(FATAL) << "Unreachable";
2624}
2625
Alexandre Rames67555f72014-11-18 10:55:16 +00002626#undef __
2627#undef QUICK_ENTRY_POINT
2628
Alexandre Rames5319def2014-10-23 10:03:10 +01002629} // namespace arm64
2630} // namespace art