blob: 7588a295245a408d4ae08d984cc90ff38c297a53 [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
Andreas Gampe878d58c2015-01-15 23:24:00 -080019#include "common_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010020#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080021#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010022#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080023#include "intrinsics.h"
24#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "mirror/array-inl.h"
26#include "mirror/art_method.h"
27#include "mirror/class.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000028#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010029#include "thread.h"
30#include "utils/arm64/assembler_arm64.h"
31#include "utils/assembler.h"
32#include "utils/stack_checks.h"
33
34
35using namespace vixl; // NOLINT(build/namespaces)
36
37#ifdef __
38#error "ARM64 Codegen VIXL macro-assembler macro already defined."
39#endif
40
Alexandre Rames5319def2014-10-23 10:03:10 +010041namespace art {
42
43namespace arm64 {
44
Andreas Gampe878d58c2015-01-15 23:24:00 -080045using helpers::CPURegisterFrom;
46using helpers::DRegisterFrom;
47using helpers::FPRegisterFrom;
48using helpers::HeapOperand;
49using helpers::HeapOperandFrom;
50using helpers::InputCPURegisterAt;
51using helpers::InputFPRegisterAt;
52using helpers::InputRegisterAt;
53using helpers::InputOperandAt;
54using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080055using helpers::LocationFrom;
56using helpers::OperandFromMemOperand;
57using helpers::OutputCPURegister;
58using helpers::OutputFPRegister;
59using helpers::OutputRegister;
60using helpers::RegisterFrom;
61using helpers::StackOperandFrom;
62using helpers::VIXLRegCodeFromART;
63using helpers::WRegisterFrom;
64using helpers::XRegisterFrom;
65
Alexandre Rames5319def2014-10-23 10:03:10 +010066static constexpr size_t kHeapRefSize = sizeof(mirror::HeapReference<mirror::Object>);
67static constexpr int kCurrentMethodStackOffset = 0;
68
Alexandre Rames5319def2014-10-23 10:03:10 +010069inline Condition ARM64Condition(IfCondition cond) {
70 switch (cond) {
71 case kCondEQ: return eq;
72 case kCondNE: return ne;
73 case kCondLT: return lt;
74 case kCondLE: return le;
75 case kCondGT: return gt;
76 case kCondGE: return ge;
77 default:
78 LOG(FATAL) << "Unknown if condition";
79 }
80 return nv; // Unreachable.
81}
82
Alexandre Ramesa89086e2014-11-07 17:13:25 +000083Location ARM64ReturnLocation(Primitive::Type return_type) {
84 DCHECK_NE(return_type, Primitive::kPrimVoid);
85 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
86 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
87 // but we use the exact registers for clarity.
88 if (return_type == Primitive::kPrimFloat) {
89 return LocationFrom(s0);
90 } else if (return_type == Primitive::kPrimDouble) {
91 return LocationFrom(d0);
92 } else if (return_type == Primitive::kPrimLong) {
93 return LocationFrom(x0);
94 } else {
95 return LocationFrom(w0);
96 }
97}
98
Alexandre Rames5319def2014-10-23 10:03:10 +010099static const Register kRuntimeParameterCoreRegisters[] = { x0, x1, x2, x3, x4, x5, x6, x7 };
100static constexpr size_t kRuntimeParameterCoreRegistersLength =
101 arraysize(kRuntimeParameterCoreRegisters);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000102static const FPRegister kRuntimeParameterFpuRegisters[] = { d0, d1, d2, d3, d4, d5, d6, d7 };
103static constexpr size_t kRuntimeParameterFpuRegistersLength =
104 arraysize(kRuntimeParameterCoreRegisters);
Alexandre Rames5319def2014-10-23 10:03:10 +0100105
106class InvokeRuntimeCallingConvention : public CallingConvention<Register, FPRegister> {
107 public:
108 static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
109
110 InvokeRuntimeCallingConvention()
111 : CallingConvention(kRuntimeParameterCoreRegisters,
112 kRuntimeParameterCoreRegistersLength,
113 kRuntimeParameterFpuRegisters,
114 kRuntimeParameterFpuRegistersLength) {}
115
116 Location GetReturnLocation(Primitive::Type return_type);
117
118 private:
119 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
120};
121
122Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000123 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100124}
125
Alexandre Rames67555f72014-11-18 10:55:16 +0000126#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
127#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100128
Alexandre Rames5319def2014-10-23 10:03:10 +0100129class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
130 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000131 BoundsCheckSlowPathARM64(HBoundsCheck* instruction,
132 Location index_location,
133 Location length_location)
134 : instruction_(instruction),
135 index_location_(index_location),
136 length_location_(length_location) {}
137
Alexandre Rames5319def2014-10-23 10:03:10 +0100138
Alexandre Rames67555f72014-11-18 10:55:16 +0000139 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000140 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100141 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000142 // We're moving two locations to locations that could overlap, so we need a parallel
143 // move resolver.
144 InvokeRuntimeCallingConvention calling_convention;
145 codegen->EmitParallelMoves(
146 index_location_, LocationFrom(calling_convention.GetRegisterAt(0)),
147 length_location_, LocationFrom(calling_convention.GetRegisterAt(1)));
148 arm64_codegen->InvokeRuntime(
149 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800150 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100151 }
152
153 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000154 HBoundsCheck* const instruction_;
155 const Location index_location_;
156 const Location length_location_;
157
Alexandre Rames5319def2014-10-23 10:03:10 +0100158 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
159};
160
Alexandre Rames67555f72014-11-18 10:55:16 +0000161class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
162 public:
163 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
164
165 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
166 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
167 __ Bind(GetEntryLabel());
168 arm64_codegen->InvokeRuntime(
169 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800170 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000171 }
172
173 private:
174 HDivZeroCheck* const instruction_;
175 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
176};
177
178class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
179 public:
180 LoadClassSlowPathARM64(HLoadClass* cls,
181 HInstruction* at,
182 uint32_t dex_pc,
183 bool do_clinit)
184 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
185 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
186 }
187
188 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
189 LocationSummary* locations = at_->GetLocations();
190 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
191
192 __ Bind(GetEntryLabel());
193 codegen->SaveLiveRegisters(locations);
194
195 InvokeRuntimeCallingConvention calling_convention;
196 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
197 arm64_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1).W());
198 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
199 : QUICK_ENTRY_POINT(pInitializeType);
200 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800201 if (do_clinit_) {
202 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t, mirror::ArtMethod*>();
203 } else {
204 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t, mirror::ArtMethod*>();
205 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000206
207 // Move the class to the desired location.
208 Location out = locations->Out();
209 if (out.IsValid()) {
210 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
211 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000212 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000213 }
214
215 codegen->RestoreLiveRegisters(locations);
216 __ B(GetExitLabel());
217 }
218
219 private:
220 // The class this slow path will load.
221 HLoadClass* const cls_;
222
223 // The instruction where this slow path is happening.
224 // (Might be the load class or an initialization check).
225 HInstruction* const at_;
226
227 // The dex PC of `at_`.
228 const uint32_t dex_pc_;
229
230 // Whether to initialize the class.
231 const bool do_clinit_;
232
233 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
234};
235
236class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
237 public:
238 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
239
240 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
241 LocationSummary* locations = instruction_->GetLocations();
242 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
243 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
244
245 __ Bind(GetEntryLabel());
246 codegen->SaveLiveRegisters(locations);
247
248 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800249 arm64_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1).W());
250 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000251 arm64_codegen->InvokeRuntime(
252 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800253 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000254 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000255 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000256
257 codegen->RestoreLiveRegisters(locations);
258 __ B(GetExitLabel());
259 }
260
261 private:
262 HLoadString* const instruction_;
263
264 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
265};
266
Alexandre Rames5319def2014-10-23 10:03:10 +0100267class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
268 public:
269 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
270
Alexandre Rames67555f72014-11-18 10:55:16 +0000271 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
272 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100273 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000274 arm64_codegen->InvokeRuntime(
275 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800276 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100277 }
278
279 private:
280 HNullCheck* const instruction_;
281
282 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
283};
284
285class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
286 public:
287 explicit SuspendCheckSlowPathARM64(HSuspendCheck* instruction,
288 HBasicBlock* successor)
289 : instruction_(instruction), successor_(successor) {}
290
Alexandre Rames67555f72014-11-18 10:55:16 +0000291 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
292 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100293 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000294 codegen->SaveLiveRegisters(instruction_->GetLocations());
295 arm64_codegen->InvokeRuntime(
296 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800297 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 codegen->RestoreLiveRegisters(instruction_->GetLocations());
299 if (successor_ == nullptr) {
300 __ B(GetReturnLabel());
301 } else {
302 __ B(arm64_codegen->GetLabelOf(successor_));
303 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100304 }
305
306 vixl::Label* GetReturnLabel() {
307 DCHECK(successor_ == nullptr);
308 return &return_label_;
309 }
310
Alexandre Rames5319def2014-10-23 10:03:10 +0100311 private:
312 HSuspendCheck* const instruction_;
313 // If not null, the block to branch to after the suspend check.
314 HBasicBlock* const successor_;
315
316 // If `successor_` is null, the label to branch to after the suspend check.
317 vixl::Label return_label_;
318
319 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
320};
321
Alexandre Rames67555f72014-11-18 10:55:16 +0000322class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
323 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000324 TypeCheckSlowPathARM64(HInstruction* instruction,
325 Location class_to_check,
326 Location object_class,
327 uint32_t dex_pc)
328 : instruction_(instruction),
329 class_to_check_(class_to_check),
330 object_class_(object_class),
331 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000332
333 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000334 LocationSummary* locations = instruction_->GetLocations();
335 DCHECK(instruction_->IsCheckCast()
336 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
337 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
338
Alexandre Rames67555f72014-11-18 10:55:16 +0000339 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000340 codegen->SaveLiveRegisters(locations);
341
342 // We're moving two locations to locations that could overlap, so we need a parallel
343 // move resolver.
344 InvokeRuntimeCallingConvention calling_convention;
345 codegen->EmitParallelMoves(
346 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)),
347 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)));
348
349 if (instruction_->IsInstanceOf()) {
350 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_);
351 Primitive::Type ret_type = instruction_->GetType();
352 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
353 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800354 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
355 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000356 } else {
357 DCHECK(instruction_->IsCheckCast());
358 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800359 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000360 }
361
362 codegen->RestoreLiveRegisters(locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000363 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000364 }
365
366 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000367 HInstruction* const instruction_;
368 const Location class_to_check_;
369 const Location object_class_;
370 uint32_t dex_pc_;
371
Alexandre Rames67555f72014-11-18 10:55:16 +0000372 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
373};
374
Alexandre Rames5319def2014-10-23 10:03:10 +0100375#undef __
376
377Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
378 Location next_location;
379 if (type == Primitive::kPrimVoid) {
380 LOG(FATAL) << "Unreachable type " << type;
381 }
382
Alexandre Rames542361f2015-01-29 16:57:31 +0000383 if (Primitive::IsFloatingPointType(type) &&
384 (fp_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000385 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(fp_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000386 } else if (!Primitive::IsFloatingPointType(type) &&
387 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000388 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
389 } else {
390 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000391 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
392 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100393 }
394
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000395 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000396 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100397 return next_location;
398}
399
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000400CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph, const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100401 : CodeGenerator(graph,
402 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000403 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000404 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000405 callee_saved_core_registers.list(),
406 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000407 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100408 block_labels_(nullptr),
409 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000410 instruction_visitor_(graph, this),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000411 move_resolver_(graph->GetArena(), this) {
412 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000413 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000414}
Alexandre Rames5319def2014-10-23 10:03:10 +0100415
Alexandre Rames67555f72014-11-18 10:55:16 +0000416#undef __
417#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100418
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000419void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
420 // Ensure we emit the literal pool.
421 __ FinalizeCode();
422 CodeGenerator::Finalize(allocator);
423}
424
Alexandre Rames3e69f162014-12-10 10:36:50 +0000425void ParallelMoveResolverARM64::EmitMove(size_t index) {
426 MoveOperands* move = moves_.Get(index);
427 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
428}
429
430void ParallelMoveResolverARM64::EmitSwap(size_t index) {
431 MoveOperands* move = moves_.Get(index);
432 codegen_->SwapLocations(move->GetDestination(), move->GetSource());
433}
434
435void ParallelMoveResolverARM64::RestoreScratch(int reg) {
436 __ Pop(Register(VIXLRegCodeFromART(reg), kXRegSize));
437}
438
439void ParallelMoveResolverARM64::SpillScratch(int reg) {
440 __ Push(Register(VIXLRegCodeFromART(reg), kXRegSize));
441}
442
Alexandre Rames5319def2014-10-23 10:03:10 +0100443void CodeGeneratorARM64::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000444 __ Bind(&frame_entry_label_);
445
Serban Constantinescu02164b32014-11-13 14:05:07 +0000446 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
447 if (do_overflow_check) {
448 UseScratchRegisterScope temps(GetVIXLAssembler());
449 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000450 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000451 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000452 __ Ldr(wzr, MemOperand(temp, 0));
453 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000454 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100455
Alexandre Rames5319def2014-10-23 10:03:10 +0100456 int frame_size = GetFrameSize();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800457 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000458 __ PokeCPURegList(GetFramePreservedCoreRegisters(), frame_size - GetCoreSpillSize());
459 __ PokeCPURegList(GetFramePreservedFPRegisters(), frame_size - FrameEntrySpillSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100460
461 // Stack layout:
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000462 // sp[frame_size - 8] : lr.
463 // ... : other preserved core registers.
464 // ... : other preserved fp registers.
465 // ... : reserved frame space.
466 // sp[0] : current method.
Alexandre Rames5319def2014-10-23 10:03:10 +0100467}
468
469void CodeGeneratorARM64::GenerateFrameExit() {
470 int frame_size = GetFrameSize();
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000471 __ PeekCPURegList(GetFramePreservedFPRegisters(), frame_size - FrameEntrySpillSize());
472 __ PeekCPURegList(GetFramePreservedCoreRegisters(), frame_size - GetCoreSpillSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100473 __ Drop(frame_size);
474}
475
476void CodeGeneratorARM64::Bind(HBasicBlock* block) {
477 __ Bind(GetLabelOf(block));
478}
479
Alexandre Rames5319def2014-10-23 10:03:10 +0100480void CodeGeneratorARM64::Move(HInstruction* instruction,
481 Location location,
482 HInstruction* move_for) {
483 LocationSummary* locations = instruction->GetLocations();
484 if (locations != nullptr && locations->Out().Equals(location)) {
485 return;
486 }
487
488 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000489 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100490
491 if (instruction->IsIntConstant() || instruction->IsLongConstant()) {
492 int64_t value = instruction->IsIntConstant() ? instruction->AsIntConstant()->GetValue()
493 : instruction->AsLongConstant()->GetValue();
494 if (location.IsRegister()) {
495 Register dst = RegisterFrom(location, type);
496 DCHECK((instruction->IsIntConstant() && dst.Is32Bits()) ||
497 (instruction->IsLongConstant() && dst.Is64Bits()));
498 __ Mov(dst, value);
499 } else {
500 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000501 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100502 Register temp = instruction->IsIntConstant() ? temps.AcquireW() : temps.AcquireX();
503 __ Mov(temp, value);
504 __ Str(temp, StackOperandFrom(location));
505 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000506 } else if (instruction->IsTemporary()) {
507 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000508 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100509 } else if (instruction->IsLoadLocal()) {
510 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000511 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000512 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000513 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000514 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100515 }
516
517 } else {
518 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000519 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100520 }
521}
522
Alexandre Rames5319def2014-10-23 10:03:10 +0100523Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
524 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000525
Alexandre Rames5319def2014-10-23 10:03:10 +0100526 switch (type) {
527 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000528 case Primitive::kPrimInt:
529 case Primitive::kPrimFloat:
530 return Location::StackSlot(GetStackSlot(load->GetLocal()));
531
532 case Primitive::kPrimLong:
533 case Primitive::kPrimDouble:
534 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
535
Alexandre Rames5319def2014-10-23 10:03:10 +0100536 case Primitive::kPrimBoolean:
537 case Primitive::kPrimByte:
538 case Primitive::kPrimChar:
539 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100540 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100541 LOG(FATAL) << "Unexpected type " << type;
542 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000543
Alexandre Rames5319def2014-10-23 10:03:10 +0100544 LOG(FATAL) << "Unreachable";
545 return Location::NoLocation();
546}
547
548void CodeGeneratorARM64::MarkGCCard(Register object, Register value) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000549 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100550 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000551 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100552 vixl::Label done;
553 __ Cbz(value, &done);
554 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
555 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000556 __ Strb(card, MemOperand(card, temp.X()));
Alexandre Rames5319def2014-10-23 10:03:10 +0100557 __ Bind(&done);
558}
559
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000560void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
561 // Blocked core registers:
562 // lr : Runtime reserved.
563 // tr : Runtime reserved.
564 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
565 // ip1 : VIXL core temp.
566 // ip0 : VIXL core temp.
567 //
568 // Blocked fp registers:
569 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100570 CPURegList reserved_core_registers = vixl_reserved_core_registers;
571 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100572 while (!reserved_core_registers.IsEmpty()) {
573 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
574 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000575
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000576 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000577 while (!reserved_core_registers.IsEmpty()) {
578 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
579 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000580
581 if (is_baseline) {
582 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
583 while (!reserved_core_baseline_registers.IsEmpty()) {
584 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
585 }
586
587 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
588 while (!reserved_fp_baseline_registers.IsEmpty()) {
589 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
590 }
591 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100592}
593
594Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
595 if (type == Primitive::kPrimVoid) {
596 LOG(FATAL) << "Unreachable type " << type;
597 }
598
Alexandre Rames542361f2015-01-29 16:57:31 +0000599 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000600 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
601 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100602 return Location::FpuRegisterLocation(reg);
603 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000604 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
605 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100606 return Location::RegisterLocation(reg);
607 }
608}
609
Alexandre Rames3e69f162014-12-10 10:36:50 +0000610size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
611 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
612 __ Str(reg, MemOperand(sp, stack_index));
613 return kArm64WordSize;
614}
615
616size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
617 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
618 __ Ldr(reg, MemOperand(sp, stack_index));
619 return kArm64WordSize;
620}
621
622size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
623 FPRegister reg = FPRegister(reg_id, kDRegSize);
624 __ Str(reg, MemOperand(sp, stack_index));
625 return kArm64WordSize;
626}
627
628size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
629 FPRegister reg = FPRegister(reg_id, kDRegSize);
630 __ Ldr(reg, MemOperand(sp, stack_index));
631 return kArm64WordSize;
632}
633
Alexandre Rames5319def2014-10-23 10:03:10 +0100634void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
635 stream << Arm64ManagedRegister::FromXRegister(XRegister(reg));
636}
637
638void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
639 stream << Arm64ManagedRegister::FromDRegister(DRegister(reg));
640}
641
Alexandre Rames67555f72014-11-18 10:55:16 +0000642void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
643 if (constant->IsIntConstant() || constant->IsLongConstant()) {
644 __ Mov(Register(destination),
645 constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
646 : constant->AsLongConstant()->GetValue());
647 } else if (constant->IsFloatConstant()) {
648 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
649 } else {
650 DCHECK(constant->IsDoubleConstant());
651 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
652 }
653}
654
Alexandre Rames3e69f162014-12-10 10:36:50 +0000655
656static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
657 DCHECK(constant.IsConstant());
658 HConstant* cst = constant.GetConstant();
659 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
660 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
661 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
662 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
663}
664
665void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000666 if (source.Equals(destination)) {
667 return;
668 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000669
670 // A valid move can always be inferred from the destination and source
671 // locations. When moving from and to a register, the argument type can be
672 // used to generate 32bit instead of 64bit moves. In debug mode we also
673 // checks the coherency of the locations and the type.
674 bool unspecified_type = (type == Primitive::kPrimVoid);
675
676 if (destination.IsRegister() || destination.IsFpuRegister()) {
677 if (unspecified_type) {
678 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
679 if (source.IsStackSlot() ||
680 (src_cst != nullptr && (src_cst->IsIntConstant() || src_cst->IsFloatConstant()))) {
681 // For stack slots and 32bit constants, a 64bit type is appropriate.
682 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000683 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000684 // If the source is a double stack slot or a 64bit constant, a 64bit
685 // type is appropriate. Else the source is a register, and since the
686 // type has not been specified, we chose a 64bit type to force a 64bit
687 // move.
688 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000689 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000690 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000691 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
692 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000693 CPURegister dst = CPURegisterFrom(destination, type);
694 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
695 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
696 __ Ldr(dst, StackOperandFrom(source));
697 } else if (source.IsConstant()) {
698 DCHECK(CoherentConstantAndType(source, type));
699 MoveConstant(dst, source.GetConstant());
700 } else {
701 if (destination.IsRegister()) {
702 __ Mov(Register(dst), RegisterFrom(source, type));
703 } else {
704 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
705 }
706 }
707
708 } else { // The destination is not a register. It must be a stack slot.
709 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
710 if (source.IsRegister() || source.IsFpuRegister()) {
711 if (unspecified_type) {
712 if (source.IsRegister()) {
713 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
714 } else {
715 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
716 }
717 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000718 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
719 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000720 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
721 } else if (source.IsConstant()) {
722 DCHECK(unspecified_type || CoherentConstantAndType(source, type));
723 UseScratchRegisterScope temps(GetVIXLAssembler());
724 HConstant* src_cst = source.GetConstant();
725 CPURegister temp;
726 if (src_cst->IsIntConstant()) {
727 temp = temps.AcquireW();
728 } else if (src_cst->IsLongConstant()) {
729 temp = temps.AcquireX();
730 } else if (src_cst->IsFloatConstant()) {
731 temp = temps.AcquireS();
732 } else {
733 DCHECK(src_cst->IsDoubleConstant());
734 temp = temps.AcquireD();
735 }
736 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000737 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000738 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000739 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000740 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000741 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000742 // There is generally less pressure on FP registers.
743 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000744 __ Ldr(temp, StackOperandFrom(source));
745 __ Str(temp, StackOperandFrom(destination));
746 }
747 }
748}
749
Alexandre Rames3e69f162014-12-10 10:36:50 +0000750void CodeGeneratorARM64::SwapLocations(Location loc1, Location loc2) {
751 DCHECK(!loc1.IsConstant());
752 DCHECK(!loc2.IsConstant());
753
754 if (loc1.Equals(loc2)) {
755 return;
756 }
757
758 UseScratchRegisterScope temps(GetAssembler()->vixl_masm_);
759
760 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
761 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
762 bool is_fp_reg1 = loc1.IsFpuRegister();
763 bool is_fp_reg2 = loc2.IsFpuRegister();
764
765 if (loc2.IsRegister() && loc1.IsRegister()) {
766 Register r1 = XRegisterFrom(loc1);
767 Register r2 = XRegisterFrom(loc2);
768 Register tmp = temps.AcquireSameSizeAs(r1);
769 __ Mov(tmp, r2);
770 __ Mov(r2, r1);
771 __ Mov(r1, tmp);
772 } else if (is_fp_reg2 && is_fp_reg1) {
773 FPRegister r1 = DRegisterFrom(loc1);
774 FPRegister r2 = DRegisterFrom(loc2);
775 FPRegister tmp = temps.AcquireSameSizeAs(r1);
776 __ Fmov(tmp, r2);
777 __ Fmov(r2, r1);
778 __ Fmov(r1, tmp);
779 } else if (is_slot1 != is_slot2) {
780 MemOperand mem = StackOperandFrom(is_slot1 ? loc1 : loc2);
781 Location reg_loc = is_slot1 ? loc2 : loc1;
782 CPURegister reg, tmp;
783 if (reg_loc.IsFpuRegister()) {
784 reg = DRegisterFrom(reg_loc);
785 tmp = temps.AcquireD();
786 } else {
787 reg = XRegisterFrom(reg_loc);
788 tmp = temps.AcquireX();
789 }
790 __ Ldr(tmp, mem);
791 __ Str(reg, mem);
792 if (reg_loc.IsFpuRegister()) {
793 __ Fmov(FPRegister(reg), FPRegister(tmp));
794 } else {
795 __ Mov(Register(reg), Register(tmp));
796 }
797 } else if (is_slot1 && is_slot2) {
798 MemOperand mem1 = StackOperandFrom(loc1);
799 MemOperand mem2 = StackOperandFrom(loc2);
800 Register tmp1 = loc1.IsStackSlot() ? temps.AcquireW() : temps.AcquireX();
801 Register tmp2 = temps.AcquireSameSizeAs(tmp1);
802 __ Ldr(tmp1, mem1);
803 __ Ldr(tmp2, mem2);
804 __ Str(tmp1, mem2);
805 __ Str(tmp2, mem1);
806 } else {
807 LOG(FATAL) << "Unimplemented";
808 }
809}
810
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000811void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000812 CPURegister dst,
813 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000814 switch (type) {
815 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000816 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000817 break;
818 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000819 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000820 break;
821 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000822 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000823 break;
824 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000825 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000826 break;
827 case Primitive::kPrimInt:
828 case Primitive::kPrimNot:
829 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000830 case Primitive::kPrimFloat:
831 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000832 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000833 __ Ldr(dst, src);
834 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000835 case Primitive::kPrimVoid:
836 LOG(FATAL) << "Unreachable type " << type;
837 }
838}
839
Calin Juravle77520bc2015-01-12 18:45:46 +0000840void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000841 CPURegister dst,
842 const MemOperand& src) {
843 UseScratchRegisterScope temps(GetVIXLAssembler());
844 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000845 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000846
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000847 DCHECK(!src.IsPreIndex());
848 DCHECK(!src.IsPostIndex());
849
850 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800851 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000852 MemOperand base = MemOperand(temp_base);
853 switch (type) {
854 case Primitive::kPrimBoolean:
855 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000856 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000857 break;
858 case Primitive::kPrimByte:
859 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000860 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000861 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
862 break;
863 case Primitive::kPrimChar:
864 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000865 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000866 break;
867 case Primitive::kPrimShort:
868 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000869 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000870 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
871 break;
872 case Primitive::kPrimInt:
873 case Primitive::kPrimNot:
874 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000875 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000876 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000877 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000878 break;
879 case Primitive::kPrimFloat:
880 case Primitive::kPrimDouble: {
881 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000882 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000883
884 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
885 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000886 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000887 __ Fmov(FPRegister(dst), temp);
888 break;
889 }
890 case Primitive::kPrimVoid:
891 LOG(FATAL) << "Unreachable type " << type;
892 }
893}
894
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000895void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000896 CPURegister src,
897 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000898 switch (type) {
899 case Primitive::kPrimBoolean:
900 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000901 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000902 break;
903 case Primitive::kPrimChar:
904 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000905 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000906 break;
907 case Primitive::kPrimInt:
908 case Primitive::kPrimNot:
909 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000910 case Primitive::kPrimFloat:
911 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000912 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000913 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000914 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000915 case Primitive::kPrimVoid:
916 LOG(FATAL) << "Unreachable type " << type;
917 }
918}
919
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000920void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
921 CPURegister src,
922 const MemOperand& dst) {
923 UseScratchRegisterScope temps(GetVIXLAssembler());
924 Register temp_base = temps.AcquireX();
925
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000926 DCHECK(!dst.IsPreIndex());
927 DCHECK(!dst.IsPostIndex());
928
929 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800930 Operand op = OperandFromMemOperand(dst);
931 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000932 MemOperand base = MemOperand(temp_base);
933 switch (type) {
934 case Primitive::kPrimBoolean:
935 case Primitive::kPrimByte:
936 __ Stlrb(Register(src), base);
937 break;
938 case Primitive::kPrimChar:
939 case Primitive::kPrimShort:
940 __ Stlrh(Register(src), base);
941 break;
942 case Primitive::kPrimInt:
943 case Primitive::kPrimNot:
944 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000945 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000946 __ Stlr(Register(src), base);
947 break;
948 case Primitive::kPrimFloat:
949 case Primitive::kPrimDouble: {
950 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000951 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000952
953 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
954 __ Fmov(temp, FPRegister(src));
955 __ Stlr(temp, base);
956 break;
957 }
958 case Primitive::kPrimVoid:
959 LOG(FATAL) << "Unreachable type " << type;
960 }
961}
962
Alexandre Rames67555f72014-11-18 10:55:16 +0000963void CodeGeneratorARM64::LoadCurrentMethod(vixl::Register current_method) {
964 DCHECK(current_method.IsW());
965 __ Ldr(current_method, MemOperand(sp, kCurrentMethodStackOffset));
966}
967
968void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
969 HInstruction* instruction,
970 uint32_t dex_pc) {
971 __ Ldr(lr, MemOperand(tr, entry_point_offset));
972 __ Blr(lr);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000973 if (instruction != nullptr) {
974 RecordPcInfo(instruction, dex_pc);
975 DCHECK(instruction->IsSuspendCheck()
976 || instruction->IsBoundsCheck()
977 || instruction->IsNullCheck()
978 || instruction->IsDivZeroCheck()
979 || !IsLeafMethod());
980 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000981}
982
983void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
984 vixl::Register class_reg) {
985 UseScratchRegisterScope temps(GetVIXLAssembler());
986 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000987 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
988
Serban Constantinescu02164b32014-11-13 14:05:07 +0000989 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000990 if (kUseAcquireRelease) {
991 // TODO(vixl): Let the MacroAssembler handle MemOperand.
992 __ Add(temp, class_reg, status_offset);
993 __ Ldar(temp, HeapOperand(temp));
994 __ Cmp(temp, mirror::Class::kStatusInitialized);
995 __ B(lt, slow_path->GetEntryLabel());
996 } else {
997 __ Ldr(temp, HeapOperand(class_reg, status_offset));
998 __ Cmp(temp, mirror::Class::kStatusInitialized);
999 __ B(lt, slow_path->GetEntryLabel());
1000 __ Dmb(InnerShareable, BarrierReads);
1001 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001002 __ Bind(slow_path->GetExitLabel());
1003}
Alexandre Rames5319def2014-10-23 10:03:10 +01001004
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001005void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1006 BarrierType type = BarrierAll;
1007
1008 switch (kind) {
1009 case MemBarrierKind::kAnyAny:
1010 case MemBarrierKind::kAnyStore: {
1011 type = BarrierAll;
1012 break;
1013 }
1014 case MemBarrierKind::kLoadAny: {
1015 type = BarrierReads;
1016 break;
1017 }
1018 case MemBarrierKind::kStoreStore: {
1019 type = BarrierWrites;
1020 break;
1021 }
1022 default:
1023 LOG(FATAL) << "Unexpected memory barrier " << kind;
1024 }
1025 __ Dmb(InnerShareable, type);
1026}
1027
Serban Constantinescu02164b32014-11-13 14:05:07 +00001028void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1029 HBasicBlock* successor) {
1030 SuspendCheckSlowPathARM64* slow_path =
1031 new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1032 codegen_->AddSlowPath(slow_path);
1033 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1034 Register temp = temps.AcquireW();
1035
1036 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1037 if (successor == nullptr) {
1038 __ Cbnz(temp, slow_path->GetEntryLabel());
1039 __ Bind(slow_path->GetReturnLabel());
1040 } else {
1041 __ Cbz(temp, codegen_->GetLabelOf(successor));
1042 __ B(slow_path->GetEntryLabel());
1043 // slow_path will return to GetLabelOf(successor).
1044 }
1045}
1046
Alexandre Rames5319def2014-10-23 10:03:10 +01001047InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1048 CodeGeneratorARM64* codegen)
1049 : HGraphVisitor(graph),
1050 assembler_(codegen->GetAssembler()),
1051 codegen_(codegen) {}
1052
1053#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001054 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001055
1056#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1057
1058enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001059 // Using a base helps identify when we hit such breakpoints.
1060 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001061#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1062 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1063#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1064};
1065
1066#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1067 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001068 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001069 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1070 } \
1071 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1072 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1073 locations->SetOut(Location::Any()); \
1074 }
1075 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1076#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1077
1078#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001079#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001080
Alexandre Rames67555f72014-11-18 10:55:16 +00001081void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001082 DCHECK_EQ(instr->InputCount(), 2U);
1083 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1084 Primitive::Type type = instr->GetResultType();
1085 switch (type) {
1086 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001087 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001088 locations->SetInAt(0, Location::RequiresRegister());
1089 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001090 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001091 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001092
1093 case Primitive::kPrimFloat:
1094 case Primitive::kPrimDouble:
1095 locations->SetInAt(0, Location::RequiresFpuRegister());
1096 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001097 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001098 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001099
Alexandre Rames5319def2014-10-23 10:03:10 +01001100 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001101 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001102 }
1103}
1104
Alexandre Rames67555f72014-11-18 10:55:16 +00001105void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001106 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001107
1108 switch (type) {
1109 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001110 case Primitive::kPrimLong: {
1111 Register dst = OutputRegister(instr);
1112 Register lhs = InputRegisterAt(instr, 0);
1113 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001114 if (instr->IsAdd()) {
1115 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001116 } else if (instr->IsAnd()) {
1117 __ And(dst, lhs, rhs);
1118 } else if (instr->IsOr()) {
1119 __ Orr(dst, lhs, rhs);
1120 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001121 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001122 } else {
1123 DCHECK(instr->IsXor());
1124 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001125 }
1126 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001127 }
1128 case Primitive::kPrimFloat:
1129 case Primitive::kPrimDouble: {
1130 FPRegister dst = OutputFPRegister(instr);
1131 FPRegister lhs = InputFPRegisterAt(instr, 0);
1132 FPRegister rhs = InputFPRegisterAt(instr, 1);
1133 if (instr->IsAdd()) {
1134 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001135 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001136 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001137 } else {
1138 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001139 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001140 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001141 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001142 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001143 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001144 }
1145}
1146
Serban Constantinescu02164b32014-11-13 14:05:07 +00001147void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1148 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1149
1150 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1151 Primitive::Type type = instr->GetResultType();
1152 switch (type) {
1153 case Primitive::kPrimInt:
1154 case Primitive::kPrimLong: {
1155 locations->SetInAt(0, Location::RequiresRegister());
1156 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1157 locations->SetOut(Location::RequiresRegister());
1158 break;
1159 }
1160 default:
1161 LOG(FATAL) << "Unexpected shift type " << type;
1162 }
1163}
1164
1165void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1166 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1167
1168 Primitive::Type type = instr->GetType();
1169 switch (type) {
1170 case Primitive::kPrimInt:
1171 case Primitive::kPrimLong: {
1172 Register dst = OutputRegister(instr);
1173 Register lhs = InputRegisterAt(instr, 0);
1174 Operand rhs = InputOperandAt(instr, 1);
1175 if (rhs.IsImmediate()) {
1176 uint32_t shift_value = (type == Primitive::kPrimInt)
1177 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1178 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1179 if (instr->IsShl()) {
1180 __ Lsl(dst, lhs, shift_value);
1181 } else if (instr->IsShr()) {
1182 __ Asr(dst, lhs, shift_value);
1183 } else {
1184 __ Lsr(dst, lhs, shift_value);
1185 }
1186 } else {
1187 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1188
1189 if (instr->IsShl()) {
1190 __ Lsl(dst, lhs, rhs_reg);
1191 } else if (instr->IsShr()) {
1192 __ Asr(dst, lhs, rhs_reg);
1193 } else {
1194 __ Lsr(dst, lhs, rhs_reg);
1195 }
1196 }
1197 break;
1198 }
1199 default:
1200 LOG(FATAL) << "Unexpected shift operation type " << type;
1201 }
1202}
1203
Alexandre Rames5319def2014-10-23 10:03:10 +01001204void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001205 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001206}
1207
1208void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001209 HandleBinaryOp(instruction);
1210}
1211
1212void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1213 HandleBinaryOp(instruction);
1214}
1215
1216void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1217 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001218}
1219
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001220void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1221 LocationSummary* locations =
1222 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1223 locations->SetInAt(0, Location::RequiresRegister());
1224 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1225 locations->SetOut(Location::RequiresRegister());
1226}
1227
1228void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1229 LocationSummary* locations = instruction->GetLocations();
1230 Primitive::Type type = instruction->GetType();
1231 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001232 Location index = locations->InAt(1);
1233 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001234 MemOperand source = HeapOperand(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00001235 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001236
1237 if (index.IsConstant()) {
1238 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001239 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001240 } else {
1241 Register temp = temps.AcquireSameSizeAs(obj);
1242 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
1243 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001244 source = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001245 }
1246
Alexandre Rames67555f72014-11-18 10:55:16 +00001247 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001248 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001249}
1250
Alexandre Rames5319def2014-10-23 10:03:10 +01001251void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1252 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1253 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001254 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001255}
1256
1257void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
1258 __ Ldr(OutputRegister(instruction),
1259 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001260 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001261}
1262
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001263void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
1264 Primitive::Type value_type = instruction->GetComponentType();
1265 bool is_object = value_type == Primitive::kPrimNot;
1266 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1267 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1268 if (is_object) {
1269 InvokeRuntimeCallingConvention calling_convention;
1270 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1271 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1272 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1273 } else {
1274 locations->SetInAt(0, Location::RequiresRegister());
1275 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1276 locations->SetInAt(2, Location::RequiresRegister());
1277 }
1278}
1279
1280void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1281 Primitive::Type value_type = instruction->GetComponentType();
1282 if (value_type == Primitive::kPrimNot) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001283 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001284 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001285 } else {
1286 LocationSummary* locations = instruction->GetLocations();
1287 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001288 CPURegister value = InputCPURegisterAt(instruction, 2);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001289 Location index = locations->InAt(1);
1290 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001291 MemOperand destination = HeapOperand(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00001292 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001293
1294 if (index.IsConstant()) {
1295 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001296 destination = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001297 } else {
1298 Register temp = temps.AcquireSameSizeAs(obj);
1299 Register index_reg = InputRegisterAt(instruction, 1);
1300 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(value_type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001301 destination = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001302 }
1303
1304 codegen_->Store(value_type, value, destination);
Calin Juravle77520bc2015-01-12 18:45:46 +00001305 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001306 }
1307}
1308
Alexandre Rames67555f72014-11-18 10:55:16 +00001309void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1310 LocationSummary* locations =
1311 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1312 locations->SetInAt(0, Location::RequiresRegister());
1313 locations->SetInAt(1, Location::RequiresRegister());
1314 if (instruction->HasUses()) {
1315 locations->SetOut(Location::SameAsFirstInput());
1316 }
1317}
1318
1319void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001320 LocationSummary* locations = instruction->GetLocations();
1321 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1322 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001323 codegen_->AddSlowPath(slow_path);
1324
1325 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1326 __ B(slow_path->GetEntryLabel(), hs);
1327}
1328
1329void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1330 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1331 instruction, LocationSummary::kCallOnSlowPath);
1332 locations->SetInAt(0, Location::RequiresRegister());
1333 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001334 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001335}
1336
1337void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001338 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001339 Register obj = InputRegisterAt(instruction, 0);;
1340 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001341 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001342
Alexandre Rames3e69f162014-12-10 10:36:50 +00001343 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1344 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001345 codegen_->AddSlowPath(slow_path);
1346
1347 // TODO: avoid this check if we know obj is not null.
1348 __ Cbz(obj, slow_path->GetExitLabel());
1349 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001350 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
1351 __ Cmp(obj_cls, cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00001352 __ B(ne, slow_path->GetEntryLabel());
1353 __ Bind(slow_path->GetExitLabel());
1354}
1355
1356void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1357 LocationSummary* locations =
1358 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1359 locations->SetInAt(0, Location::RequiresRegister());
1360 if (check->HasUses()) {
1361 locations->SetOut(Location::SameAsFirstInput());
1362 }
1363}
1364
1365void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1366 // We assume the class is not null.
1367 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1368 check->GetLoadClass(), check, check->GetDexPc(), true);
1369 codegen_->AddSlowPath(slow_path);
1370 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1371}
1372
Serban Constantinescu02164b32014-11-13 14:05:07 +00001373void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001374 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001375 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1376 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001377 switch (in_type) {
1378 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001379 locations->SetInAt(0, Location::RequiresRegister());
1380 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
1381 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1382 break;
1383 }
1384 case Primitive::kPrimFloat:
1385 case Primitive::kPrimDouble: {
1386 locations->SetInAt(0, Location::RequiresFpuRegister());
1387 locations->SetInAt(1, Location::RequiresFpuRegister());
1388 locations->SetOut(Location::RequiresRegister());
1389 break;
1390 }
1391 default:
1392 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1393 }
1394}
1395
1396void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1397 Primitive::Type in_type = compare->InputAt(0)->GetType();
1398
1399 // 0 if: left == right
1400 // 1 if: left > right
1401 // -1 if: left < right
1402 switch (in_type) {
1403 case Primitive::kPrimLong: {
1404 Register result = OutputRegister(compare);
1405 Register left = InputRegisterAt(compare, 0);
1406 Operand right = InputOperandAt(compare, 1);
1407
1408 __ Cmp(left, right);
1409 __ Cset(result, ne);
1410 __ Cneg(result, result, lt);
1411 break;
1412 }
1413 case Primitive::kPrimFloat:
1414 case Primitive::kPrimDouble: {
1415 Register result = OutputRegister(compare);
1416 FPRegister left = InputFPRegisterAt(compare, 0);
1417 FPRegister right = InputFPRegisterAt(compare, 1);
1418
1419 __ Fcmp(left, right);
1420 if (compare->IsGtBias()) {
1421 __ Cset(result, ne);
1422 } else {
1423 __ Csetm(result, ne);
1424 }
1425 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001426 break;
1427 }
1428 default:
1429 LOG(FATAL) << "Unimplemented compare type " << in_type;
1430 }
1431}
1432
1433void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1434 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1435 locations->SetInAt(0, Location::RequiresRegister());
1436 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1437 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001438 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001439 }
1440}
1441
1442void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1443 if (!instruction->NeedsMaterialization()) {
1444 return;
1445 }
1446
1447 LocationSummary* locations = instruction->GetLocations();
1448 Register lhs = InputRegisterAt(instruction, 0);
1449 Operand rhs = InputOperandAt(instruction, 1);
1450 Register res = RegisterFrom(locations->Out(), instruction->GetType());
1451 Condition cond = ARM64Condition(instruction->GetCondition());
1452
1453 __ Cmp(lhs, rhs);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001454 __ Cset(res, cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001455}
1456
1457#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1458 M(Equal) \
1459 M(NotEqual) \
1460 M(LessThan) \
1461 M(LessThanOrEqual) \
1462 M(GreaterThan) \
1463 M(GreaterThanOrEqual)
1464#define DEFINE_CONDITION_VISITORS(Name) \
1465void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1466void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1467FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001468#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001469#undef FOR_EACH_CONDITION_INSTRUCTION
1470
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001471void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1472 LocationSummary* locations =
1473 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1474 switch (div->GetResultType()) {
1475 case Primitive::kPrimInt:
1476 case Primitive::kPrimLong:
1477 locations->SetInAt(0, Location::RequiresRegister());
1478 locations->SetInAt(1, Location::RequiresRegister());
1479 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1480 break;
1481
1482 case Primitive::kPrimFloat:
1483 case Primitive::kPrimDouble:
1484 locations->SetInAt(0, Location::RequiresFpuRegister());
1485 locations->SetInAt(1, Location::RequiresFpuRegister());
1486 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1487 break;
1488
1489 default:
1490 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1491 }
1492}
1493
1494void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1495 Primitive::Type type = div->GetResultType();
1496 switch (type) {
1497 case Primitive::kPrimInt:
1498 case Primitive::kPrimLong:
1499 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
1500 break;
1501
1502 case Primitive::kPrimFloat:
1503 case Primitive::kPrimDouble:
1504 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1505 break;
1506
1507 default:
1508 LOG(FATAL) << "Unexpected div type " << type;
1509 }
1510}
1511
Alexandre Rames67555f72014-11-18 10:55:16 +00001512void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1513 LocationSummary* locations =
1514 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1515 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1516 if (instruction->HasUses()) {
1517 locations->SetOut(Location::SameAsFirstInput());
1518 }
1519}
1520
1521void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1522 SlowPathCodeARM64* slow_path =
1523 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1524 codegen_->AddSlowPath(slow_path);
1525 Location value = instruction->GetLocations()->InAt(0);
1526
Alexandre Rames3e69f162014-12-10 10:36:50 +00001527 Primitive::Type type = instruction->GetType();
1528
1529 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
1530 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
1531 return;
1532 }
1533
Alexandre Rames67555f72014-11-18 10:55:16 +00001534 if (value.IsConstant()) {
1535 int64_t divisor = Int64ConstantFrom(value);
1536 if (divisor == 0) {
1537 __ B(slow_path->GetEntryLabel());
1538 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001539 // A division by a non-null constant is valid. We don't need to perform
1540 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00001541 }
1542 } else {
1543 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
1544 }
1545}
1546
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001547void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1548 LocationSummary* locations =
1549 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1550 locations->SetOut(Location::ConstantLocation(constant));
1551}
1552
1553void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1554 UNUSED(constant);
1555 // Will be generated at use site.
1556}
1557
Alexandre Rames5319def2014-10-23 10:03:10 +01001558void LocationsBuilderARM64::VisitExit(HExit* exit) {
1559 exit->SetLocations(nullptr);
1560}
1561
1562void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001563 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01001564 if (kIsDebugBuild) {
1565 down_cast<Arm64Assembler*>(GetAssembler())->Comment("Unreachable");
Alexandre Rames67555f72014-11-18 10:55:16 +00001566 __ Brk(__LINE__); // TODO: Introduce special markers for such code locations.
Alexandre Rames5319def2014-10-23 10:03:10 +01001567 }
1568}
1569
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001570void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
1571 LocationSummary* locations =
1572 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1573 locations->SetOut(Location::ConstantLocation(constant));
1574}
1575
1576void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
1577 UNUSED(constant);
1578 // Will be generated at use site.
1579}
1580
Alexandre Rames5319def2014-10-23 10:03:10 +01001581void LocationsBuilderARM64::VisitGoto(HGoto* got) {
1582 got->SetLocations(nullptr);
1583}
1584
1585void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
1586 HBasicBlock* successor = got->GetSuccessor();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001587 DCHECK(!successor->IsExitBlock());
1588 HBasicBlock* block = got->GetBlock();
1589 HInstruction* previous = got->GetPrevious();
1590 HLoopInformation* info = block->GetLoopInformation();
1591
1592 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
1593 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1594 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1595 return;
1596 }
1597 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1598 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1599 }
1600 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001601 __ B(codegen_->GetLabelOf(successor));
1602 }
1603}
1604
1605void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
1606 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1607 HInstruction* cond = if_instr->InputAt(0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001608 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001609 locations->SetInAt(0, Location::RequiresRegister());
1610 }
1611}
1612
1613void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
1614 HInstruction* cond = if_instr->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001615 HCondition* condition = cond->AsCondition();
1616 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1617 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1618
Serban Constantinescu02164b32014-11-13 14:05:07 +00001619 if (cond->IsIntConstant()) {
1620 int32_t cond_value = cond->AsIntConstant()->GetValue();
1621 if (cond_value == 1) {
1622 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfTrueSuccessor())) {
1623 __ B(true_target);
1624 }
1625 return;
1626 } else {
1627 DCHECK_EQ(cond_value, 0);
1628 }
1629 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001630 // The condition instruction has been materialized, compare the output to 0.
1631 Location cond_val = if_instr->GetLocations()->InAt(0);
1632 DCHECK(cond_val.IsRegister());
1633 __ Cbnz(InputRegisterAt(if_instr, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001634 } else {
1635 // The condition instruction has not been materialized, use its inputs as
1636 // the comparison and its condition as the branch condition.
1637 Register lhs = InputRegisterAt(condition, 0);
1638 Operand rhs = InputOperandAt(condition, 1);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001639 Condition arm64_cond = ARM64Condition(condition->GetCondition());
1640 if ((arm64_cond == eq || arm64_cond == ne) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
1641 if (arm64_cond == eq) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001642 __ Cbz(lhs, true_target);
1643 } else {
1644 __ Cbnz(lhs, true_target);
1645 }
1646 } else {
1647 __ Cmp(lhs, rhs);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001648 __ B(arm64_cond, true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001649 }
1650 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001651 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
1652 __ B(false_target);
1653 }
1654}
1655
1656void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001657 LocationSummary* locations =
1658 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames5319def2014-10-23 10:03:10 +01001659 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001660 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001661}
1662
1663void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001664 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), instruction->GetFieldOffset());
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001665
1666 if (instruction->IsVolatile()) {
1667 if (kUseAcquireRelease) {
Calin Juravle77520bc2015-01-12 18:45:46 +00001668 // NB: LoadAcquire will record the pc info if needed.
1669 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001670 } else {
1671 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
Calin Juravle77520bc2015-01-12 18:45:46 +00001672 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001673 // For IRIW sequential consistency kLoadAny is not sufficient.
1674 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1675 }
1676 } else {
1677 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
Calin Juravle77520bc2015-01-12 18:45:46 +00001678 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001679 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001680}
1681
1682void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001683 LocationSummary* locations =
1684 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames5319def2014-10-23 10:03:10 +01001685 locations->SetInAt(0, Location::RequiresRegister());
1686 locations->SetInAt(1, Location::RequiresRegister());
1687}
1688
1689void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001690 Register obj = InputRegisterAt(instruction, 0);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001691 CPURegister value = InputCPURegisterAt(instruction, 1);
1692 Offset offset = instruction->GetFieldOffset();
1693 Primitive::Type field_type = instruction->GetFieldType();
1694
1695 if (instruction->IsVolatile()) {
1696 if (kUseAcquireRelease) {
1697 codegen_->StoreRelease(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001698 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001699 } else {
1700 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1701 codegen_->Store(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001702 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001703 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1704 }
1705 } else {
1706 codegen_->Store(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001707 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001708 }
1709
1710 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001711 codegen_->MarkGCCard(obj, Register(value));
Alexandre Rames5319def2014-10-23 10:03:10 +01001712 }
1713}
1714
Alexandre Rames67555f72014-11-18 10:55:16 +00001715void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
1716 LocationSummary::CallKind call_kind =
1717 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
1718 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1719 locations->SetInAt(0, Location::RequiresRegister());
1720 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001721 // The output does overlap inputs.
1722 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00001723}
1724
1725void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
1726 LocationSummary* locations = instruction->GetLocations();
1727 Register obj = InputRegisterAt(instruction, 0);;
1728 Register cls = InputRegisterAt(instruction, 1);;
1729 Register out = OutputRegister(instruction);
1730
1731 vixl::Label done;
1732
1733 // Return 0 if `obj` is null.
1734 // TODO: Avoid this check if we know `obj` is not null.
1735 __ Mov(out, 0);
1736 __ Cbz(obj, &done);
1737
1738 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00001739 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Alexandre Rames67555f72014-11-18 10:55:16 +00001740 __ Cmp(out, cls);
1741 if (instruction->IsClassFinal()) {
1742 // Classes must be equal for the instanceof to succeed.
1743 __ Cset(out, eq);
1744 } else {
1745 // If the classes are not equal, we go into a slow path.
1746 DCHECK(locations->OnlyCallsOnSlowPath());
1747 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00001748 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1749 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001750 codegen_->AddSlowPath(slow_path);
1751 __ B(ne, slow_path->GetEntryLabel());
1752 __ Mov(out, 1);
1753 __ Bind(slow_path->GetExitLabel());
1754 }
1755
1756 __ Bind(&done);
1757}
1758
Alexandre Rames5319def2014-10-23 10:03:10 +01001759void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
1760 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
1761 locations->SetOut(Location::ConstantLocation(constant));
1762}
1763
1764void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
1765 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001766 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01001767}
1768
Alexandre Rames5319def2014-10-23 10:03:10 +01001769void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
1770 LocationSummary* locations =
1771 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
1772 locations->AddTemp(LocationFrom(x0));
1773
1774 InvokeDexCallingConventionVisitor calling_convention_visitor;
1775 for (size_t i = 0; i < invoke->InputCount(); i++) {
1776 HInstruction* input = invoke->InputAt(i);
1777 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1778 }
1779
1780 Primitive::Type return_type = invoke->GetType();
1781 if (return_type != Primitive::kPrimVoid) {
1782 locations->SetOut(calling_convention_visitor.GetReturnLocation(return_type));
1783 }
1784}
1785
Alexandre Rames67555f72014-11-18 10:55:16 +00001786void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1787 HandleInvoke(invoke);
1788}
1789
1790void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1791 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1792 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
1793 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1794 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1795 Location receiver = invoke->GetLocations()->InAt(0);
1796 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001797 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00001798
1799 // The register ip1 is required to be used for the hidden argument in
1800 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
1801 UseScratchRegisterScope scratch_scope(GetVIXLAssembler());
1802 scratch_scope.Exclude(ip1);
1803 __ Mov(ip1, invoke->GetDexMethodIndex());
1804
1805 // temp = object->GetClass();
1806 if (receiver.IsStackSlot()) {
1807 __ Ldr(temp, StackOperandFrom(receiver));
1808 __ Ldr(temp, HeapOperand(temp, class_offset));
1809 } else {
1810 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
1811 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001812 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00001813 // temp = temp->GetImtEntryAt(method_offset);
1814 __ Ldr(temp, HeapOperand(temp, method_offset));
1815 // lr = temp->GetEntryPoint();
1816 __ Ldr(lr, HeapOperand(temp, entry_point));
1817 // lr();
1818 __ Blr(lr);
1819 DCHECK(!codegen_->IsLeafMethod());
1820 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1821}
1822
1823void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001824 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1825 if (intrinsic.TryDispatch(invoke)) {
1826 return;
1827 }
1828
Alexandre Rames67555f72014-11-18 10:55:16 +00001829 HandleInvoke(invoke);
1830}
1831
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001832void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001833 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1834 if (intrinsic.TryDispatch(invoke)) {
1835 return;
1836 }
1837
Alexandre Rames67555f72014-11-18 10:55:16 +00001838 HandleInvoke(invoke);
1839}
1840
Andreas Gampe878d58c2015-01-15 23:24:00 -08001841static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
1842 if (invoke->GetLocations()->Intrinsified()) {
1843 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
1844 intrinsic.Dispatch(invoke);
1845 return true;
1846 }
1847 return false;
1848}
1849
1850void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Register temp) {
1851 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
1852 DCHECK(temp.Is(kArtMethodRegister));
Alexandre Rames5319def2014-10-23 10:03:10 +01001853 size_t index_in_cache = mirror::Array::DataOffset(kHeapRefSize).SizeValue() +
Andreas Gampe878d58c2015-01-15 23:24:00 -08001854 invoke->GetDexMethodIndex() * kHeapRefSize;
Alexandre Rames5319def2014-10-23 10:03:10 +01001855
1856 // TODO: Implement all kinds of calls:
1857 // 1) boot -> boot
1858 // 2) app -> boot
1859 // 3) app -> app
1860 //
1861 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1862
Nicolas Geoffray0a299b92015-01-29 11:39:44 +00001863 // temp = method;
1864 LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001865 if (!invoke->IsRecursive()) {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001866 // temp = temp->dex_cache_resolved_methods_;
1867 __ Ldr(temp, HeapOperand(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset()));
1868 // temp = temp[index_in_cache];
1869 __ Ldr(temp, HeapOperand(temp, index_in_cache));
1870 // lr = temp->entry_point_from_quick_compiled_code_;
1871 __ Ldr(lr, HeapOperand(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
1872 kArm64WordSize)));
1873 // lr();
1874 __ Blr(lr);
1875 } else {
1876 __ Bl(&frame_entry_label_);
1877 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001878
Andreas Gampe878d58c2015-01-15 23:24:00 -08001879 RecordPcInfo(invoke, invoke->GetDexPc());
1880 DCHECK(!IsLeafMethod());
1881}
1882
1883void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
1884 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1885 return;
1886 }
1887
1888 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
1889 codegen_->GenerateStaticOrDirectCall(invoke, temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01001890}
1891
1892void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001893 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1894 return;
1895 }
1896
Alexandre Rames5319def2014-10-23 10:03:10 +01001897 LocationSummary* locations = invoke->GetLocations();
1898 Location receiver = locations->InAt(0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001899 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01001900 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1901 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1902 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001903 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01001904
1905 // temp = object->GetClass();
1906 if (receiver.IsStackSlot()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001907 __ Ldr(temp, MemOperand(sp, receiver.GetStackIndex()));
1908 __ Ldr(temp, HeapOperand(temp, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01001909 } else {
1910 DCHECK(receiver.IsRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001911 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01001912 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001913 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames5319def2014-10-23 10:03:10 +01001914 // temp = temp->GetMethodAt(method_offset);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001915 __ Ldr(temp, HeapOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01001916 // lr = temp->GetEntryPoint();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001917 __ Ldr(lr, HeapOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001918 // lr();
1919 __ Blr(lr);
1920 DCHECK(!codegen_->IsLeafMethod());
1921 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1922}
1923
Alexandre Rames67555f72014-11-18 10:55:16 +00001924void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
1925 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
1926 : LocationSummary::kNoCall;
1927 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
1928 locations->SetOut(Location::RequiresRegister());
1929}
1930
1931void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
1932 Register out = OutputRegister(cls);
1933 if (cls->IsReferrersClass()) {
1934 DCHECK(!cls->CanCallRuntime());
1935 DCHECK(!cls->MustGenerateClinitCheck());
1936 codegen_->LoadCurrentMethod(out);
1937 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
1938 } else {
1939 DCHECK(cls->CanCallRuntime());
1940 codegen_->LoadCurrentMethod(out);
1941 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DexCacheResolvedTypesOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001942 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00001943
1944 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1945 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
1946 codegen_->AddSlowPath(slow_path);
1947 __ Cbz(out, slow_path->GetEntryLabel());
1948 if (cls->MustGenerateClinitCheck()) {
1949 GenerateClassInitializationCheck(slow_path, out);
1950 } else {
1951 __ Bind(slow_path->GetExitLabel());
1952 }
1953 }
1954}
1955
1956void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
1957 LocationSummary* locations =
1958 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
1959 locations->SetOut(Location::RequiresRegister());
1960}
1961
1962void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
1963 MemOperand exception = MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
1964 __ Ldr(OutputRegister(instruction), exception);
1965 __ Str(wzr, exception);
1966}
1967
Alexandre Rames5319def2014-10-23 10:03:10 +01001968void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
1969 load->SetLocations(nullptr);
1970}
1971
1972void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
1973 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001974 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01001975}
1976
Alexandre Rames67555f72014-11-18 10:55:16 +00001977void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
1978 LocationSummary* locations =
1979 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
1980 locations->SetOut(Location::RequiresRegister());
1981}
1982
1983void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
1984 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
1985 codegen_->AddSlowPath(slow_path);
1986
1987 Register out = OutputRegister(load);
1988 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08001989 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
1990 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001991 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00001992 __ Cbz(out, slow_path->GetEntryLabel());
1993 __ Bind(slow_path->GetExitLabel());
1994}
1995
Alexandre Rames5319def2014-10-23 10:03:10 +01001996void LocationsBuilderARM64::VisitLocal(HLocal* local) {
1997 local->SetLocations(nullptr);
1998}
1999
2000void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2001 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2002}
2003
2004void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2005 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2006 locations->SetOut(Location::ConstantLocation(constant));
2007}
2008
2009void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2010 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002011 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002012}
2013
Alexandre Rames67555f72014-11-18 10:55:16 +00002014void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2015 LocationSummary* locations =
2016 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2017 InvokeRuntimeCallingConvention calling_convention;
2018 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2019}
2020
2021void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2022 codegen_->InvokeRuntime(instruction->IsEnter()
2023 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2024 instruction,
2025 instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002026 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002027}
2028
Alexandre Rames42d641b2014-10-27 14:00:51 +00002029void LocationsBuilderARM64::VisitMul(HMul* mul) {
2030 LocationSummary* locations =
2031 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2032 switch (mul->GetResultType()) {
2033 case Primitive::kPrimInt:
2034 case Primitive::kPrimLong:
2035 locations->SetInAt(0, Location::RequiresRegister());
2036 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002037 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002038 break;
2039
2040 case Primitive::kPrimFloat:
2041 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002042 locations->SetInAt(0, Location::RequiresFpuRegister());
2043 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002044 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002045 break;
2046
2047 default:
2048 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2049 }
2050}
2051
2052void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2053 switch (mul->GetResultType()) {
2054 case Primitive::kPrimInt:
2055 case Primitive::kPrimLong:
2056 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2057 break;
2058
2059 case Primitive::kPrimFloat:
2060 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002061 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002062 break;
2063
2064 default:
2065 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2066 }
2067}
2068
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002069void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2070 LocationSummary* locations =
2071 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2072 switch (neg->GetResultType()) {
2073 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002074 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002075 locations->SetInAt(0, Location::RegisterOrConstant(neg->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00002076 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002077 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002078
2079 case Primitive::kPrimFloat:
2080 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002081 locations->SetInAt(0, Location::RequiresFpuRegister());
2082 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002083 break;
2084
2085 default:
2086 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2087 }
2088}
2089
2090void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2091 switch (neg->GetResultType()) {
2092 case Primitive::kPrimInt:
2093 case Primitive::kPrimLong:
2094 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2095 break;
2096
2097 case Primitive::kPrimFloat:
2098 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002099 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002100 break;
2101
2102 default:
2103 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2104 }
2105}
2106
2107void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2108 LocationSummary* locations =
2109 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2110 InvokeRuntimeCallingConvention calling_convention;
2111 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002112 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002113 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002114 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2115 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2116 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002117}
2118
2119void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2120 LocationSummary* locations = instruction->GetLocations();
2121 InvokeRuntimeCallingConvention calling_convention;
2122 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2123 DCHECK(type_index.Is(w0));
2124 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002125 DCHECK(current_method.Is(w2));
Alexandre Rames67555f72014-11-18 10:55:16 +00002126 codegen_->LoadCurrentMethod(current_method);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002127 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002128 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002129 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2130 instruction,
2131 instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002132 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2133 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002134}
2135
Alexandre Rames5319def2014-10-23 10:03:10 +01002136void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2137 LocationSummary* locations =
2138 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2139 InvokeRuntimeCallingConvention calling_convention;
2140 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2141 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2142 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002143 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002144}
2145
2146void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2147 LocationSummary* locations = instruction->GetLocations();
2148 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2149 DCHECK(type_index.Is(w0));
2150 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
2151 DCHECK(current_method.Is(w1));
Alexandre Rames67555f72014-11-18 10:55:16 +00002152 codegen_->LoadCurrentMethod(current_method);
Alexandre Rames5319def2014-10-23 10:03:10 +01002153 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002154 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002155 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2156 instruction,
2157 instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002158 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002159}
2160
2161void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2162 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002163 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002164 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002165}
2166
2167void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
2168 switch (instruction->InputAt(0)->GetType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002169 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002170 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002171 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002172 break;
2173
2174 default:
2175 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2176 }
2177}
2178
2179void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2180 LocationSummary* locations =
2181 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2182 locations->SetInAt(0, Location::RequiresRegister());
2183 if (instruction->HasUses()) {
2184 locations->SetOut(Location::SameAsFirstInput());
2185 }
2186}
2187
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002188void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002189 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2190 return;
2191 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002192 Location obj = instruction->GetLocations()->InAt(0);
2193
2194 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2195 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2196}
2197
2198void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002199 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2200 codegen_->AddSlowPath(slow_path);
2201
2202 LocationSummary* locations = instruction->GetLocations();
2203 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002204
2205 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002206}
2207
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002208void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2209 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2210 GenerateImplicitNullCheck(instruction);
2211 } else {
2212 GenerateExplicitNullCheck(instruction);
2213 }
2214}
2215
Alexandre Rames67555f72014-11-18 10:55:16 +00002216void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2217 HandleBinaryOp(instruction);
2218}
2219
2220void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2221 HandleBinaryOp(instruction);
2222}
2223
Alexandre Rames3e69f162014-12-10 10:36:50 +00002224void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2225 LOG(FATAL) << "Unreachable";
2226}
2227
2228void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2229 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2230}
2231
Alexandre Rames5319def2014-10-23 10:03:10 +01002232void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2233 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2234 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2235 if (location.IsStackSlot()) {
2236 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2237 } else if (location.IsDoubleStackSlot()) {
2238 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2239 }
2240 locations->SetOut(location);
2241}
2242
2243void InstructionCodeGeneratorARM64::VisitParameterValue(HParameterValue* instruction) {
2244 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002245 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002246}
2247
2248void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2249 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2250 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2251 locations->SetInAt(i, Location::Any());
2252 }
2253 locations->SetOut(Location::Any());
2254}
2255
2256void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002257 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002258 LOG(FATAL) << "Unreachable";
2259}
2260
Serban Constantinescu02164b32014-11-13 14:05:07 +00002261void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002262 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002263 LocationSummary::CallKind call_kind =
2264 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002265 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2266
2267 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002268 case Primitive::kPrimInt:
2269 case Primitive::kPrimLong:
2270 locations->SetInAt(0, Location::RequiresRegister());
2271 locations->SetInAt(1, Location::RequiresRegister());
2272 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2273 break;
2274
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002275 case Primitive::kPrimFloat:
2276 case Primitive::kPrimDouble: {
2277 InvokeRuntimeCallingConvention calling_convention;
2278 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2279 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2280 locations->SetOut(calling_convention.GetReturnLocation(type));
2281
2282 break;
2283 }
2284
Serban Constantinescu02164b32014-11-13 14:05:07 +00002285 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002286 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002287 }
2288}
2289
2290void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2291 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002292
Serban Constantinescu02164b32014-11-13 14:05:07 +00002293 switch (type) {
2294 case Primitive::kPrimInt:
2295 case Primitive::kPrimLong: {
2296 UseScratchRegisterScope temps(GetVIXLAssembler());
2297 Register dividend = InputRegisterAt(rem, 0);
2298 Register divisor = InputRegisterAt(rem, 1);
2299 Register output = OutputRegister(rem);
2300 Register temp = temps.AcquireSameSizeAs(output);
2301
2302 __ Sdiv(temp, dividend, divisor);
2303 __ Msub(output, temp, divisor, dividend);
2304 break;
2305 }
2306
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002307 case Primitive::kPrimFloat:
2308 case Primitive::kPrimDouble: {
2309 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2310 : QUICK_ENTRY_POINT(pFmod);
2311 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc());
2312 break;
2313 }
2314
Serban Constantinescu02164b32014-11-13 14:05:07 +00002315 default:
2316 LOG(FATAL) << "Unexpected rem type " << type;
2317 }
2318}
2319
Alexandre Rames5319def2014-10-23 10:03:10 +01002320void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2321 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2322 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002323 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002324}
2325
2326void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002327 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002328 codegen_->GenerateFrameExit();
Alexandre Rames3e69f162014-12-10 10:36:50 +00002329 __ Ret();
Alexandre Rames5319def2014-10-23 10:03:10 +01002330}
2331
2332void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2333 instruction->SetLocations(nullptr);
2334}
2335
2336void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002337 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002338 codegen_->GenerateFrameExit();
Alexandre Rames3e69f162014-12-10 10:36:50 +00002339 __ Ret();
Alexandre Rames5319def2014-10-23 10:03:10 +01002340}
2341
Serban Constantinescu02164b32014-11-13 14:05:07 +00002342void LocationsBuilderARM64::VisitShl(HShl* shl) {
2343 HandleShift(shl);
2344}
2345
2346void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2347 HandleShift(shl);
2348}
2349
2350void LocationsBuilderARM64::VisitShr(HShr* shr) {
2351 HandleShift(shr);
2352}
2353
2354void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2355 HandleShift(shr);
2356}
2357
Alexandre Rames5319def2014-10-23 10:03:10 +01002358void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2359 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2360 Primitive::Type field_type = store->InputAt(1)->GetType();
2361 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002362 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002363 case Primitive::kPrimBoolean:
2364 case Primitive::kPrimByte:
2365 case Primitive::kPrimChar:
2366 case Primitive::kPrimShort:
2367 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002368 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002369 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2370 break;
2371
2372 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002373 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002374 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2375 break;
2376
2377 default:
2378 LOG(FATAL) << "Unimplemented local type " << field_type;
2379 }
2380}
2381
2382void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002383 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002384}
2385
2386void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002387 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002388}
2389
2390void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002391 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002392}
2393
Alexandre Rames67555f72014-11-18 10:55:16 +00002394void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2395 LocationSummary* locations =
2396 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2397 locations->SetInAt(0, Location::RequiresRegister());
2398 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2399}
2400
2401void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002402 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), instruction->GetFieldOffset());
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002403
2404 if (instruction->IsVolatile()) {
2405 if (kUseAcquireRelease) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002406 // NB: LoadAcquire will record the pc info if needed.
2407 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002408 } else {
2409 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
2410 // For IRIW sequential consistency kLoadAny is not sufficient.
2411 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2412 }
2413 } else {
2414 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
2415 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002416}
2417
2418void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002419 LocationSummary* locations =
2420 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2421 locations->SetInAt(0, Location::RequiresRegister());
2422 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01002423}
2424
Alexandre Rames67555f72014-11-18 10:55:16 +00002425void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002426 Register cls = InputRegisterAt(instruction, 0);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002427 CPURegister value = InputCPURegisterAt(instruction, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002428 Offset offset = instruction->GetFieldOffset();
Alexandre Rames67555f72014-11-18 10:55:16 +00002429 Primitive::Type field_type = instruction->GetFieldType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002430
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002431 if (instruction->IsVolatile()) {
2432 if (kUseAcquireRelease) {
2433 codegen_->StoreRelease(field_type, value, HeapOperand(cls, offset));
2434 } else {
2435 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2436 codegen_->Store(field_type, value, HeapOperand(cls, offset));
2437 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2438 }
2439 } else {
2440 codegen_->Store(field_type, value, HeapOperand(cls, offset));
2441 }
2442
2443 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002444 codegen_->MarkGCCard(cls, Register(value));
2445 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002446}
2447
2448void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2449 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2450}
2451
2452void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002453 HBasicBlock* block = instruction->GetBlock();
2454 if (block->GetLoopInformation() != nullptr) {
2455 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2456 // The back edge will generate the suspend check.
2457 return;
2458 }
2459 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2460 // The goto will generate the suspend check.
2461 return;
2462 }
2463 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01002464}
2465
2466void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
2467 temp->SetLocations(nullptr);
2468}
2469
2470void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
2471 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002472 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01002473}
2474
Alexandre Rames67555f72014-11-18 10:55:16 +00002475void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
2476 LocationSummary* locations =
2477 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2478 InvokeRuntimeCallingConvention calling_convention;
2479 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2480}
2481
2482void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
2483 codegen_->InvokeRuntime(
2484 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002485 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002486}
2487
2488void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
2489 LocationSummary* locations =
2490 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2491 Primitive::Type input_type = conversion->GetInputType();
2492 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002493 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00002494 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
2495 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
2496 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
2497 }
2498
Alexandre Rames542361f2015-01-29 16:57:31 +00002499 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002500 locations->SetInAt(0, Location::RequiresFpuRegister());
2501 } else {
2502 locations->SetInAt(0, Location::RequiresRegister());
2503 }
2504
Alexandre Rames542361f2015-01-29 16:57:31 +00002505 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002506 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2507 } else {
2508 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2509 }
2510}
2511
2512void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
2513 Primitive::Type result_type = conversion->GetResultType();
2514 Primitive::Type input_type = conversion->GetInputType();
2515
2516 DCHECK_NE(input_type, result_type);
2517
Alexandre Rames542361f2015-01-29 16:57:31 +00002518 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002519 int result_size = Primitive::ComponentSize(result_type);
2520 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002521 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002522 Register output = OutputRegister(conversion);
2523 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002524 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
2525 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
2526 } else if ((result_type == Primitive::kPrimChar) ||
2527 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
2528 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002529 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002530 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002531 }
Alexandre Rames542361f2015-01-29 16:57:31 +00002532 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002533 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002534 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002535 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
2536 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002537 } else if (Primitive::IsFloatingPointType(result_type) &&
2538 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002539 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
2540 } else {
2541 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
2542 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00002543 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002544}
Alexandre Rames67555f72014-11-18 10:55:16 +00002545
Serban Constantinescu02164b32014-11-13 14:05:07 +00002546void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
2547 HandleShift(ushr);
2548}
2549
2550void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
2551 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00002552}
2553
2554void LocationsBuilderARM64::VisitXor(HXor* instruction) {
2555 HandleBinaryOp(instruction);
2556}
2557
2558void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
2559 HandleBinaryOp(instruction);
2560}
2561
2562#undef __
2563#undef QUICK_ENTRY_POINT
2564
Alexandre Rames5319def2014-10-23 10:03:10 +01002565} // namespace arm64
2566} // namespace art