blob: 348c5c8945d8fc34e5dd0d01f6f6d82a5be3f965 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080022#include "common_arm64.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010024#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080025#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010026#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080027#include "intrinsics.h"
28#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010029#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000031#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010032#include "thread.h"
33#include "utils/arm64/assembler_arm64.h"
34#include "utils/assembler.h"
35#include "utils/stack_checks.h"
36
37
38using namespace vixl; // NOLINT(build/namespaces)
39
40#ifdef __
41#error "ARM64 Codegen VIXL macro-assembler macro already defined."
42#endif
43
Alexandre Rames5319def2014-10-23 10:03:10 +010044namespace art {
45
46namespace arm64 {
47
Andreas Gampe878d58c2015-01-15 23:24:00 -080048using helpers::CPURegisterFrom;
49using helpers::DRegisterFrom;
50using helpers::FPRegisterFrom;
51using helpers::HeapOperand;
52using helpers::HeapOperandFrom;
53using helpers::InputCPURegisterAt;
54using helpers::InputFPRegisterAt;
55using helpers::InputRegisterAt;
56using helpers::InputOperandAt;
57using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080058using helpers::LocationFrom;
59using helpers::OperandFromMemOperand;
60using helpers::OutputCPURegister;
61using helpers::OutputFPRegister;
62using helpers::OutputRegister;
63using helpers::RegisterFrom;
64using helpers::StackOperandFrom;
65using helpers::VIXLRegCodeFromART;
66using helpers::WRegisterFrom;
67using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000068using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080069using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080070
Alexandre Rames5319def2014-10-23 10:03:10 +010071static constexpr int kCurrentMethodStackOffset = 0;
72
Alexandre Rames5319def2014-10-23 10:03:10 +010073inline Condition ARM64Condition(IfCondition cond) {
74 switch (cond) {
75 case kCondEQ: return eq;
76 case kCondNE: return ne;
77 case kCondLT: return lt;
78 case kCondLE: return le;
79 case kCondGT: return gt;
80 case kCondGE: return ge;
Alexandre Rames5319def2014-10-23 10:03:10 +010081 }
Roland Levillain7f63c522015-07-13 15:54:55 +000082 LOG(FATAL) << "Unreachable";
83 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010084}
85
Alexandre Ramesa89086e2014-11-07 17:13:25 +000086Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000087 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
88 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
89 // but we use the exact registers for clarity.
90 if (return_type == Primitive::kPrimFloat) {
91 return LocationFrom(s0);
92 } else if (return_type == Primitive::kPrimDouble) {
93 return LocationFrom(d0);
94 } else if (return_type == Primitive::kPrimLong) {
95 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +010096 } else if (return_type == Primitive::kPrimVoid) {
97 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +000098 } else {
99 return LocationFrom(w0);
100 }
101}
102
Alexandre Rames5319def2014-10-23 10:03:10 +0100103Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000104 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100105}
106
Alexandre Rames67555f72014-11-18 10:55:16 +0000107#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
108#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100109
Zheng Xuda403092015-04-24 17:35:39 +0800110// Calculate memory accessing operand for save/restore live registers.
111static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
112 RegisterSet* register_set,
113 int64_t spill_offset,
114 bool is_save) {
115 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
116 codegen->GetNumberOfCoreRegisters(),
117 register_set->GetFloatingPointRegisters(),
118 codegen->GetNumberOfFloatingPointRegisters()));
119
120 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
121 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
122 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
123 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
124
125 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
126 UseScratchRegisterScope temps(masm);
127
128 Register base = masm->StackPointer();
129 int64_t core_spill_size = core_list.TotalSizeInBytes();
130 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
131 int64_t reg_size = kXRegSizeInBytes;
132 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
133 uint32_t ls_access_size = WhichPowerOf2(reg_size);
134 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
135 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
136 // If the offset does not fit in the instruction's immediate field, use an alternate register
137 // to compute the base address(float point registers spill base address).
138 Register new_base = temps.AcquireSameSizeAs(base);
139 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
140 base = new_base;
141 spill_offset = -core_spill_size;
142 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
143 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
144 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
145 }
146
147 if (is_save) {
148 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
149 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
150 } else {
151 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
152 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
153 }
154}
155
156void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
157 RegisterSet* register_set = locations->GetLiveRegisters();
158 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
159 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
160 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
161 // If the register holds an object, update the stack mask.
162 if (locations->RegisterContainsObject(i)) {
163 locations->SetStackBit(stack_offset / kVRegSize);
164 }
165 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
166 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
167 saved_core_stack_offsets_[i] = stack_offset;
168 stack_offset += kXRegSizeInBytes;
169 }
170 }
171
172 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
173 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
174 register_set->ContainsFloatingPointRegister(i)) {
175 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
176 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
177 saved_fpu_stack_offsets_[i] = stack_offset;
178 stack_offset += kDRegSizeInBytes;
179 }
180 }
181
182 SaveRestoreLiveRegistersHelper(codegen, register_set,
183 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
184}
185
186void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
187 RegisterSet* register_set = locations->GetLiveRegisters();
188 SaveRestoreLiveRegistersHelper(codegen, register_set,
189 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
190}
191
Alexandre Rames5319def2014-10-23 10:03:10 +0100192class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
193 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000194 BoundsCheckSlowPathARM64(HBoundsCheck* instruction,
195 Location index_location,
196 Location length_location)
197 : instruction_(instruction),
198 index_location_(index_location),
199 length_location_(length_location) {}
200
Alexandre Rames5319def2014-10-23 10:03:10 +0100201
Alexandre Rames67555f72014-11-18 10:55:16 +0000202 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000203 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100204 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000205 // We're moving two locations to locations that could overlap, so we need a parallel
206 // move resolver.
207 InvokeRuntimeCallingConvention calling_convention;
208 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100209 index_location_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
210 length_location_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000211 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000212 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800213 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100214 }
215
Alexandre Rames8158f282015-08-07 10:26:17 +0100216 bool IsFatal() const OVERRIDE { return true; }
217
Alexandre Rames9931f312015-06-19 14:47:01 +0100218 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
219
Alexandre Rames5319def2014-10-23 10:03:10 +0100220 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000221 HBoundsCheck* const instruction_;
222 const Location index_location_;
223 const Location length_location_;
224
Alexandre Rames5319def2014-10-23 10:03:10 +0100225 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
226};
227
Alexandre Rames67555f72014-11-18 10:55:16 +0000228class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
229 public:
230 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
231
232 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
233 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
234 __ Bind(GetEntryLabel());
235 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000236 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800237 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000238 }
239
Alexandre Rames8158f282015-08-07 10:26:17 +0100240 bool IsFatal() const OVERRIDE { return true; }
241
Alexandre Rames9931f312015-06-19 14:47:01 +0100242 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
243
Alexandre Rames67555f72014-11-18 10:55:16 +0000244 private:
245 HDivZeroCheck* const instruction_;
246 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
247};
248
249class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
250 public:
251 LoadClassSlowPathARM64(HLoadClass* cls,
252 HInstruction* at,
253 uint32_t dex_pc,
254 bool do_clinit)
255 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
256 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
257 }
258
259 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
260 LocationSummary* locations = at_->GetLocations();
261 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
262
263 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000264 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000265
266 InvokeRuntimeCallingConvention calling_convention;
267 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000268 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
269 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000270 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800271 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100272 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800273 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100274 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800275 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000276
277 // Move the class to the desired location.
278 Location out = locations->Out();
279 if (out.IsValid()) {
280 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
281 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000282 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000283 }
284
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000285 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000286 __ B(GetExitLabel());
287 }
288
Alexandre Rames9931f312015-06-19 14:47:01 +0100289 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
290
Alexandre Rames67555f72014-11-18 10:55:16 +0000291 private:
292 // The class this slow path will load.
293 HLoadClass* const cls_;
294
295 // The instruction where this slow path is happening.
296 // (Might be the load class or an initialization check).
297 HInstruction* const at_;
298
299 // The dex PC of `at_`.
300 const uint32_t dex_pc_;
301
302 // Whether to initialize the class.
303 const bool do_clinit_;
304
305 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
306};
307
308class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
309 public:
310 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
311
312 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
313 LocationSummary* locations = instruction_->GetLocations();
314 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
315 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
316
317 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000318 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000319
320 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800321 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000322 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000323 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100324 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000325 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000326 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000327
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000328 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000329 __ B(GetExitLabel());
330 }
331
Alexandre Rames9931f312015-06-19 14:47:01 +0100332 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
333
Alexandre Rames67555f72014-11-18 10:55:16 +0000334 private:
335 HLoadString* const instruction_;
336
337 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
338};
339
Alexandre Rames5319def2014-10-23 10:03:10 +0100340class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
341 public:
342 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
343
Alexandre Rames67555f72014-11-18 10:55:16 +0000344 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
345 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100346 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000347 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000348 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800349 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100350 }
351
Alexandre Rames8158f282015-08-07 10:26:17 +0100352 bool IsFatal() const OVERRIDE { return true; }
353
Alexandre Rames9931f312015-06-19 14:47:01 +0100354 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
355
Alexandre Rames5319def2014-10-23 10:03:10 +0100356 private:
357 HNullCheck* const instruction_;
358
359 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
360};
361
362class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
363 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100364 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100365 : instruction_(instruction), successor_(successor) {}
366
Alexandre Rames67555f72014-11-18 10:55:16 +0000367 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
368 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100369 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000370 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000371 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000372 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800373 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000374 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000375 if (successor_ == nullptr) {
376 __ B(GetReturnLabel());
377 } else {
378 __ B(arm64_codegen->GetLabelOf(successor_));
379 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100380 }
381
382 vixl::Label* GetReturnLabel() {
383 DCHECK(successor_ == nullptr);
384 return &return_label_;
385 }
386
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100387 HBasicBlock* GetSuccessor() const {
388 return successor_;
389 }
390
Alexandre Rames9931f312015-06-19 14:47:01 +0100391 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
392
Alexandre Rames5319def2014-10-23 10:03:10 +0100393 private:
394 HSuspendCheck* const instruction_;
395 // If not null, the block to branch to after the suspend check.
396 HBasicBlock* const successor_;
397
398 // If `successor_` is null, the label to branch to after the suspend check.
399 vixl::Label return_label_;
400
401 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
402};
403
Alexandre Rames67555f72014-11-18 10:55:16 +0000404class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
405 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000406 TypeCheckSlowPathARM64(HInstruction* instruction,
407 Location class_to_check,
408 Location object_class,
409 uint32_t dex_pc)
410 : instruction_(instruction),
411 class_to_check_(class_to_check),
412 object_class_(object_class),
413 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000414
415 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000416 LocationSummary* locations = instruction_->GetLocations();
417 DCHECK(instruction_->IsCheckCast()
418 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
419 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
420
Alexandre Rames67555f72014-11-18 10:55:16 +0000421 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000422 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000423
424 // We're moving two locations to locations that could overlap, so we need a parallel
425 // move resolver.
426 InvokeRuntimeCallingConvention calling_convention;
427 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100428 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
429 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000430
431 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000432 arm64_codegen->InvokeRuntime(
433 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000434 Primitive::Type ret_type = instruction_->GetType();
435 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
436 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800437 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
438 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000439 } else {
440 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000441 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800442 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000443 }
444
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000445 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000446 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000447 }
448
Alexandre Rames9931f312015-06-19 14:47:01 +0100449 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
450
Alexandre Rames67555f72014-11-18 10:55:16 +0000451 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000452 HInstruction* const instruction_;
453 const Location class_to_check_;
454 const Location object_class_;
455 uint32_t dex_pc_;
456
Alexandre Rames67555f72014-11-18 10:55:16 +0000457 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
458};
459
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700460class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
461 public:
462 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
463 : instruction_(instruction) {}
464
465 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
466 __ Bind(GetEntryLabel());
467 SaveLiveRegisters(codegen, instruction_->GetLocations());
468 DCHECK(instruction_->IsDeoptimize());
469 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
470 uint32_t dex_pc = deoptimize->GetDexPc();
471 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
472 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
473 }
474
Alexandre Rames9931f312015-06-19 14:47:01 +0100475 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
476
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700477 private:
478 HInstruction* const instruction_;
479 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
480};
481
Alexandre Rames5319def2014-10-23 10:03:10 +0100482#undef __
483
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100484Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100485 Location next_location;
486 if (type == Primitive::kPrimVoid) {
487 LOG(FATAL) << "Unreachable type " << type;
488 }
489
Alexandre Rames542361f2015-01-29 16:57:31 +0000490 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100491 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
492 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000493 } else if (!Primitive::IsFloatingPointType(type) &&
494 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000495 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
496 } else {
497 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000498 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
499 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100500 }
501
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000502 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000503 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100504 return next_location;
505}
506
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100507Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100508 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100509}
510
Serban Constantinescu579885a2015-02-22 20:51:33 +0000511CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
512 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100513 const CompilerOptions& compiler_options,
514 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100515 : CodeGenerator(graph,
516 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000517 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000518 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000519 callee_saved_core_registers.list(),
520 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100521 compiler_options,
522 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100523 block_labels_(nullptr),
524 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000525 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000526 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000527 isa_features_(isa_features),
528 uint64_literals_(std::less<uint64_t>(), graph->GetArena()->Adapter()),
529 method_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
530 call_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
531 relative_call_patches_(graph->GetArena()->Adapter()),
532 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000533 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000534 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000535}
Alexandre Rames5319def2014-10-23 10:03:10 +0100536
Alexandre Rames67555f72014-11-18 10:55:16 +0000537#undef __
538#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100539
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000540void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
541 // Ensure we emit the literal pool.
542 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000543
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000544 CodeGenerator::Finalize(allocator);
545}
546
Zheng Xuad4450e2015-04-17 18:48:56 +0800547void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
548 // Note: There are 6 kinds of moves:
549 // 1. constant -> GPR/FPR (non-cycle)
550 // 2. constant -> stack (non-cycle)
551 // 3. GPR/FPR -> GPR/FPR
552 // 4. GPR/FPR -> stack
553 // 5. stack -> GPR/FPR
554 // 6. stack -> stack (non-cycle)
555 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
556 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
557 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
558 // dependency.
559 vixl_temps_.Open(GetVIXLAssembler());
560}
561
562void ParallelMoveResolverARM64::FinishEmitNativeCode() {
563 vixl_temps_.Close();
564}
565
566Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
567 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
568 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
569 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
570 Location scratch = GetScratchLocation(kind);
571 if (!scratch.Equals(Location::NoLocation())) {
572 return scratch;
573 }
574 // Allocate from VIXL temp registers.
575 if (kind == Location::kRegister) {
576 scratch = LocationFrom(vixl_temps_.AcquireX());
577 } else {
578 DCHECK(kind == Location::kFpuRegister);
579 scratch = LocationFrom(vixl_temps_.AcquireD());
580 }
581 AddScratchLocation(scratch);
582 return scratch;
583}
584
585void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
586 if (loc.IsRegister()) {
587 vixl_temps_.Release(XRegisterFrom(loc));
588 } else {
589 DCHECK(loc.IsFpuRegister());
590 vixl_temps_.Release(DRegisterFrom(loc));
591 }
592 RemoveScratchLocation(loc);
593}
594
Alexandre Rames3e69f162014-12-10 10:36:50 +0000595void ParallelMoveResolverARM64::EmitMove(size_t index) {
596 MoveOperands* move = moves_.Get(index);
597 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
598}
599
Alexandre Rames5319def2014-10-23 10:03:10 +0100600void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100601 MacroAssembler* masm = GetVIXLAssembler();
602 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000603 __ Bind(&frame_entry_label_);
604
Serban Constantinescu02164b32014-11-13 14:05:07 +0000605 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
606 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100607 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000608 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000609 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000610 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000611 __ Ldr(wzr, MemOperand(temp, 0));
612 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000613 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100614
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000615 if (!HasEmptyFrame()) {
616 int frame_size = GetFrameSize();
617 // Stack layout:
618 // sp[frame_size - 8] : lr.
619 // ... : other preserved core registers.
620 // ... : other preserved fp registers.
621 // ... : reserved frame space.
622 // sp[0] : current method.
623 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100624 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800625 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
626 frame_size - GetCoreSpillSize());
627 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
628 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000629 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100630}
631
632void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100633 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100634 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000635 if (!HasEmptyFrame()) {
636 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800637 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
638 frame_size - FrameEntrySpillSize());
639 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
640 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000641 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100642 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000643 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100644 __ Ret();
645 GetAssembler()->cfi().RestoreState();
646 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100647}
648
Zheng Xuda403092015-04-24 17:35:39 +0800649vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
650 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
651 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
652 core_spill_mask_);
653}
654
655vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
656 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
657 GetNumberOfFloatingPointRegisters()));
658 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
659 fpu_spill_mask_);
660}
661
Alexandre Rames5319def2014-10-23 10:03:10 +0100662void CodeGeneratorARM64::Bind(HBasicBlock* block) {
663 __ Bind(GetLabelOf(block));
664}
665
Alexandre Rames5319def2014-10-23 10:03:10 +0100666void CodeGeneratorARM64::Move(HInstruction* instruction,
667 Location location,
668 HInstruction* move_for) {
669 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100670 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000671 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100672
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100673 if (instruction->IsFakeString()) {
674 // The fake string is an alias for null.
675 DCHECK(IsBaseline());
676 instruction = locations->Out().GetConstant();
677 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
678 }
679
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100680 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700681 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100682 } else if (locations != nullptr && locations->Out().Equals(location)) {
683 return;
684 } else if (instruction->IsIntConstant()
685 || instruction->IsLongConstant()
686 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000687 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100688 if (location.IsRegister()) {
689 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000690 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100691 (instruction->IsLongConstant() && dst.Is64Bits()));
692 __ Mov(dst, value);
693 } else {
694 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000695 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000696 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
697 ? temps.AcquireW()
698 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100699 __ Mov(temp, value);
700 __ Str(temp, StackOperandFrom(location));
701 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000702 } else if (instruction->IsTemporary()) {
703 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000704 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100705 } else if (instruction->IsLoadLocal()) {
706 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000707 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000708 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000709 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000710 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100711 }
712
713 } else {
714 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000715 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100716 }
717}
718
Alexandre Rames5319def2014-10-23 10:03:10 +0100719Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
720 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000721
Alexandre Rames5319def2014-10-23 10:03:10 +0100722 switch (type) {
723 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000724 case Primitive::kPrimInt:
725 case Primitive::kPrimFloat:
726 return Location::StackSlot(GetStackSlot(load->GetLocal()));
727
728 case Primitive::kPrimLong:
729 case Primitive::kPrimDouble:
730 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
731
Alexandre Rames5319def2014-10-23 10:03:10 +0100732 case Primitive::kPrimBoolean:
733 case Primitive::kPrimByte:
734 case Primitive::kPrimChar:
735 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100736 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100737 LOG(FATAL) << "Unexpected type " << type;
738 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000739
Alexandre Rames5319def2014-10-23 10:03:10 +0100740 LOG(FATAL) << "Unreachable";
741 return Location::NoLocation();
742}
743
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100744void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000745 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100746 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000747 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100748 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100749 if (value_can_be_null) {
750 __ Cbz(value, &done);
751 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100752 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
753 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000754 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100755 if (value_can_be_null) {
756 __ Bind(&done);
757 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100758}
759
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000760void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
761 // Blocked core registers:
762 // lr : Runtime reserved.
763 // tr : Runtime reserved.
764 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
765 // ip1 : VIXL core temp.
766 // ip0 : VIXL core temp.
767 //
768 // Blocked fp registers:
769 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100770 CPURegList reserved_core_registers = vixl_reserved_core_registers;
771 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100772 while (!reserved_core_registers.IsEmpty()) {
773 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
774 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000775
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000776 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800777 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000778 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
779 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000780
781 if (is_baseline) {
782 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
783 while (!reserved_core_baseline_registers.IsEmpty()) {
784 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
785 }
786
787 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
788 while (!reserved_fp_baseline_registers.IsEmpty()) {
789 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
790 }
791 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100792}
793
794Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
795 if (type == Primitive::kPrimVoid) {
796 LOG(FATAL) << "Unreachable type " << type;
797 }
798
Alexandre Rames542361f2015-01-29 16:57:31 +0000799 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000800 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
801 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100802 return Location::FpuRegisterLocation(reg);
803 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000804 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
805 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100806 return Location::RegisterLocation(reg);
807 }
808}
809
Alexandre Rames3e69f162014-12-10 10:36:50 +0000810size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
811 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
812 __ Str(reg, MemOperand(sp, stack_index));
813 return kArm64WordSize;
814}
815
816size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
817 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
818 __ Ldr(reg, MemOperand(sp, stack_index));
819 return kArm64WordSize;
820}
821
822size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
823 FPRegister reg = FPRegister(reg_id, kDRegSize);
824 __ Str(reg, MemOperand(sp, stack_index));
825 return kArm64WordSize;
826}
827
828size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
829 FPRegister reg = FPRegister(reg_id, kDRegSize);
830 __ Ldr(reg, MemOperand(sp, stack_index));
831 return kArm64WordSize;
832}
833
Alexandre Rames5319def2014-10-23 10:03:10 +0100834void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100835 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100836}
837
838void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100839 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100840}
841
Alexandre Rames67555f72014-11-18 10:55:16 +0000842void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000843 if (constant->IsIntConstant()) {
844 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
845 } else if (constant->IsLongConstant()) {
846 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
847 } else if (constant->IsNullConstant()) {
848 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000849 } else if (constant->IsFloatConstant()) {
850 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
851 } else {
852 DCHECK(constant->IsDoubleConstant());
853 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
854 }
855}
856
Alexandre Rames3e69f162014-12-10 10:36:50 +0000857
858static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
859 DCHECK(constant.IsConstant());
860 HConstant* cst = constant.GetConstant();
861 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000862 // Null is mapped to a core W register, which we associate with kPrimInt.
863 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000864 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
865 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
866 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
867}
868
869void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000870 if (source.Equals(destination)) {
871 return;
872 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000873
874 // A valid move can always be inferred from the destination and source
875 // locations. When moving from and to a register, the argument type can be
876 // used to generate 32bit instead of 64bit moves. In debug mode we also
877 // checks the coherency of the locations and the type.
878 bool unspecified_type = (type == Primitive::kPrimVoid);
879
880 if (destination.IsRegister() || destination.IsFpuRegister()) {
881 if (unspecified_type) {
882 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
883 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000884 (src_cst != nullptr && (src_cst->IsIntConstant()
885 || src_cst->IsFloatConstant()
886 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000887 // For stack slots and 32bit constants, a 64bit type is appropriate.
888 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000889 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000890 // If the source is a double stack slot or a 64bit constant, a 64bit
891 // type is appropriate. Else the source is a register, and since the
892 // type has not been specified, we chose a 64bit type to force a 64bit
893 // move.
894 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000895 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000896 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000897 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
898 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000899 CPURegister dst = CPURegisterFrom(destination, type);
900 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
901 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
902 __ Ldr(dst, StackOperandFrom(source));
903 } else if (source.IsConstant()) {
904 DCHECK(CoherentConstantAndType(source, type));
905 MoveConstant(dst, source.GetConstant());
906 } else {
907 if (destination.IsRegister()) {
908 __ Mov(Register(dst), RegisterFrom(source, type));
909 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800910 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000911 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
912 }
913 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000914 } else { // The destination is not a register. It must be a stack slot.
915 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
916 if (source.IsRegister() || source.IsFpuRegister()) {
917 if (unspecified_type) {
918 if (source.IsRegister()) {
919 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
920 } else {
921 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
922 }
923 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000924 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
925 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000926 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
927 } else if (source.IsConstant()) {
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100928 DCHECK(unspecified_type || CoherentConstantAndType(source, type)) << source << " " << type;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000929 UseScratchRegisterScope temps(GetVIXLAssembler());
930 HConstant* src_cst = source.GetConstant();
931 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000932 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000933 temp = temps.AcquireW();
934 } else if (src_cst->IsLongConstant()) {
935 temp = temps.AcquireX();
936 } else if (src_cst->IsFloatConstant()) {
937 temp = temps.AcquireS();
938 } else {
939 DCHECK(src_cst->IsDoubleConstant());
940 temp = temps.AcquireD();
941 }
942 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000943 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000944 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000945 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000946 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000947 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000948 // There is generally less pressure on FP registers.
949 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000950 __ Ldr(temp, StackOperandFrom(source));
951 __ Str(temp, StackOperandFrom(destination));
952 }
953 }
954}
955
956void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000957 CPURegister dst,
958 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000959 switch (type) {
960 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000961 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000962 break;
963 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000964 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000965 break;
966 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000967 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000968 break;
969 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000970 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000971 break;
972 case Primitive::kPrimInt:
973 case Primitive::kPrimNot:
974 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000975 case Primitive::kPrimFloat:
976 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000977 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000978 __ Ldr(dst, src);
979 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000980 case Primitive::kPrimVoid:
981 LOG(FATAL) << "Unreachable type " << type;
982 }
983}
984
Calin Juravle77520bc2015-01-12 18:45:46 +0000985void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000986 CPURegister dst,
987 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100988 MacroAssembler* masm = GetVIXLAssembler();
989 BlockPoolsScope block_pools(masm);
990 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000991 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000992 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000993
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000994 DCHECK(!src.IsPreIndex());
995 DCHECK(!src.IsPostIndex());
996
997 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800998 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000999 MemOperand base = MemOperand(temp_base);
1000 switch (type) {
1001 case Primitive::kPrimBoolean:
1002 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001003 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001004 break;
1005 case Primitive::kPrimByte:
1006 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001007 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001008 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1009 break;
1010 case Primitive::kPrimChar:
1011 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001012 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001013 break;
1014 case Primitive::kPrimShort:
1015 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001016 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001017 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1018 break;
1019 case Primitive::kPrimInt:
1020 case Primitive::kPrimNot:
1021 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001022 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001023 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001024 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001025 break;
1026 case Primitive::kPrimFloat:
1027 case Primitive::kPrimDouble: {
1028 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001029 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001030
1031 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1032 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001033 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001034 __ Fmov(FPRegister(dst), temp);
1035 break;
1036 }
1037 case Primitive::kPrimVoid:
1038 LOG(FATAL) << "Unreachable type " << type;
1039 }
1040}
1041
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001042void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001043 CPURegister src,
1044 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001045 switch (type) {
1046 case Primitive::kPrimBoolean:
1047 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001048 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001049 break;
1050 case Primitive::kPrimChar:
1051 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001052 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001053 break;
1054 case Primitive::kPrimInt:
1055 case Primitive::kPrimNot:
1056 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001057 case Primitive::kPrimFloat:
1058 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001059 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001060 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001061 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001062 case Primitive::kPrimVoid:
1063 LOG(FATAL) << "Unreachable type " << type;
1064 }
1065}
1066
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001067void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1068 CPURegister src,
1069 const MemOperand& dst) {
1070 UseScratchRegisterScope temps(GetVIXLAssembler());
1071 Register temp_base = temps.AcquireX();
1072
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001073 DCHECK(!dst.IsPreIndex());
1074 DCHECK(!dst.IsPostIndex());
1075
1076 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001077 Operand op = OperandFromMemOperand(dst);
1078 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001079 MemOperand base = MemOperand(temp_base);
1080 switch (type) {
1081 case Primitive::kPrimBoolean:
1082 case Primitive::kPrimByte:
1083 __ Stlrb(Register(src), base);
1084 break;
1085 case Primitive::kPrimChar:
1086 case Primitive::kPrimShort:
1087 __ Stlrh(Register(src), base);
1088 break;
1089 case Primitive::kPrimInt:
1090 case Primitive::kPrimNot:
1091 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001092 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001093 __ Stlr(Register(src), base);
1094 break;
1095 case Primitive::kPrimFloat:
1096 case Primitive::kPrimDouble: {
1097 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001098 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001099
1100 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1101 __ Fmov(temp, FPRegister(src));
1102 __ Stlr(temp, base);
1103 break;
1104 }
1105 case Primitive::kPrimVoid:
1106 LOG(FATAL) << "Unreachable type " << type;
1107 }
1108}
1109
Alexandre Rames67555f72014-11-18 10:55:16 +00001110void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1111 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001112 uint32_t dex_pc,
1113 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001114 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001115 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001116 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1117 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001118 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001119}
1120
1121void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1122 vixl::Register class_reg) {
1123 UseScratchRegisterScope temps(GetVIXLAssembler());
1124 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001125 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001126 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001127
Serban Constantinescu02164b32014-11-13 14:05:07 +00001128 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001129 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001130 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1131 __ Add(temp, class_reg, status_offset);
1132 __ Ldar(temp, HeapOperand(temp));
1133 __ Cmp(temp, mirror::Class::kStatusInitialized);
1134 __ B(lt, slow_path->GetEntryLabel());
1135 } else {
1136 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1137 __ Cmp(temp, mirror::Class::kStatusInitialized);
1138 __ B(lt, slow_path->GetEntryLabel());
1139 __ Dmb(InnerShareable, BarrierReads);
1140 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001141 __ Bind(slow_path->GetExitLabel());
1142}
Alexandre Rames5319def2014-10-23 10:03:10 +01001143
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001144void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1145 BarrierType type = BarrierAll;
1146
1147 switch (kind) {
1148 case MemBarrierKind::kAnyAny:
1149 case MemBarrierKind::kAnyStore: {
1150 type = BarrierAll;
1151 break;
1152 }
1153 case MemBarrierKind::kLoadAny: {
1154 type = BarrierReads;
1155 break;
1156 }
1157 case MemBarrierKind::kStoreStore: {
1158 type = BarrierWrites;
1159 break;
1160 }
1161 default:
1162 LOG(FATAL) << "Unexpected memory barrier " << kind;
1163 }
1164 __ Dmb(InnerShareable, type);
1165}
1166
Serban Constantinescu02164b32014-11-13 14:05:07 +00001167void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1168 HBasicBlock* successor) {
1169 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001170 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1171 if (slow_path == nullptr) {
1172 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1173 instruction->SetSlowPath(slow_path);
1174 codegen_->AddSlowPath(slow_path);
1175 if (successor != nullptr) {
1176 DCHECK(successor->IsLoopHeader());
1177 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1178 }
1179 } else {
1180 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1181 }
1182
Serban Constantinescu02164b32014-11-13 14:05:07 +00001183 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1184 Register temp = temps.AcquireW();
1185
1186 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1187 if (successor == nullptr) {
1188 __ Cbnz(temp, slow_path->GetEntryLabel());
1189 __ Bind(slow_path->GetReturnLabel());
1190 } else {
1191 __ Cbz(temp, codegen_->GetLabelOf(successor));
1192 __ B(slow_path->GetEntryLabel());
1193 // slow_path will return to GetLabelOf(successor).
1194 }
1195}
1196
Alexandre Rames5319def2014-10-23 10:03:10 +01001197InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1198 CodeGeneratorARM64* codegen)
1199 : HGraphVisitor(graph),
1200 assembler_(codegen->GetAssembler()),
1201 codegen_(codegen) {}
1202
1203#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001204 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001205
1206#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1207
1208enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001209 // Using a base helps identify when we hit such breakpoints.
1210 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001211#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1212 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1213#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1214};
1215
1216#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1217 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001218 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001219 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1220 } \
1221 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1222 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1223 locations->SetOut(Location::Any()); \
1224 }
1225 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1226#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1227
1228#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001229#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001230
Alexandre Rames67555f72014-11-18 10:55:16 +00001231void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001232 DCHECK_EQ(instr->InputCount(), 2U);
1233 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1234 Primitive::Type type = instr->GetResultType();
1235 switch (type) {
1236 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001237 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001238 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001239 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001240 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001241 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001242
1243 case Primitive::kPrimFloat:
1244 case Primitive::kPrimDouble:
1245 locations->SetInAt(0, Location::RequiresFpuRegister());
1246 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001247 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001248 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001249
Alexandre Rames5319def2014-10-23 10:03:10 +01001250 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001251 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001252 }
1253}
1254
Alexandre Rames09a99962015-04-15 11:47:56 +01001255void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1256 LocationSummary* locations =
1257 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1258 locations->SetInAt(0, Location::RequiresRegister());
1259 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1260 locations->SetOut(Location::RequiresFpuRegister());
1261 } else {
1262 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1263 }
1264}
1265
1266void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1267 const FieldInfo& field_info) {
1268 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001269 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001270 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001271
1272 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1273 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1274
1275 if (field_info.IsVolatile()) {
1276 if (use_acquire_release) {
1277 // NB: LoadAcquire will record the pc info if needed.
1278 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1279 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001280 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001281 codegen_->MaybeRecordImplicitNullCheck(instruction);
1282 // For IRIW sequential consistency kLoadAny is not sufficient.
1283 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1284 }
1285 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001286 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001287 codegen_->MaybeRecordImplicitNullCheck(instruction);
1288 }
Roland Levillain4d027112015-07-01 15:41:14 +01001289
1290 if (field_type == Primitive::kPrimNot) {
1291 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1292 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001293}
1294
1295void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1296 LocationSummary* locations =
1297 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1298 locations->SetInAt(0, Location::RequiresRegister());
1299 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1300 locations->SetInAt(1, Location::RequiresFpuRegister());
1301 } else {
1302 locations->SetInAt(1, Location::RequiresRegister());
1303 }
1304}
1305
1306void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001307 const FieldInfo& field_info,
1308 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001309 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001310 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001311
1312 Register obj = InputRegisterAt(instruction, 0);
1313 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001314 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001315 Offset offset = field_info.GetFieldOffset();
1316 Primitive::Type field_type = field_info.GetFieldType();
1317 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1318
Roland Levillain4d027112015-07-01 15:41:14 +01001319 {
1320 // We use a block to end the scratch scope before the write barrier, thus
1321 // freeing the temporary registers so they can be used in `MarkGCCard`.
1322 UseScratchRegisterScope temps(GetVIXLAssembler());
1323
1324 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1325 DCHECK(value.IsW());
1326 Register temp = temps.AcquireW();
1327 __ Mov(temp, value.W());
1328 GetAssembler()->PoisonHeapReference(temp.W());
1329 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001330 }
Roland Levillain4d027112015-07-01 15:41:14 +01001331
1332 if (field_info.IsVolatile()) {
1333 if (use_acquire_release) {
1334 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1335 codegen_->MaybeRecordImplicitNullCheck(instruction);
1336 } else {
1337 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1338 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1339 codegen_->MaybeRecordImplicitNullCheck(instruction);
1340 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1341 }
1342 } else {
1343 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1344 codegen_->MaybeRecordImplicitNullCheck(instruction);
1345 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001346 }
1347
1348 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001349 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001350 }
1351}
1352
Alexandre Rames67555f72014-11-18 10:55:16 +00001353void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001354 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001355
1356 switch (type) {
1357 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001358 case Primitive::kPrimLong: {
1359 Register dst = OutputRegister(instr);
1360 Register lhs = InputRegisterAt(instr, 0);
1361 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001362 if (instr->IsAdd()) {
1363 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001364 } else if (instr->IsAnd()) {
1365 __ And(dst, lhs, rhs);
1366 } else if (instr->IsOr()) {
1367 __ Orr(dst, lhs, rhs);
1368 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001369 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001370 } else {
1371 DCHECK(instr->IsXor());
1372 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001373 }
1374 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001375 }
1376 case Primitive::kPrimFloat:
1377 case Primitive::kPrimDouble: {
1378 FPRegister dst = OutputFPRegister(instr);
1379 FPRegister lhs = InputFPRegisterAt(instr, 0);
1380 FPRegister rhs = InputFPRegisterAt(instr, 1);
1381 if (instr->IsAdd()) {
1382 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001383 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001384 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001385 } else {
1386 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001387 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001388 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001389 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001390 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001391 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001392 }
1393}
1394
Serban Constantinescu02164b32014-11-13 14:05:07 +00001395void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1396 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1397
1398 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1399 Primitive::Type type = instr->GetResultType();
1400 switch (type) {
1401 case Primitive::kPrimInt:
1402 case Primitive::kPrimLong: {
1403 locations->SetInAt(0, Location::RequiresRegister());
1404 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1405 locations->SetOut(Location::RequiresRegister());
1406 break;
1407 }
1408 default:
1409 LOG(FATAL) << "Unexpected shift type " << type;
1410 }
1411}
1412
1413void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1414 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1415
1416 Primitive::Type type = instr->GetType();
1417 switch (type) {
1418 case Primitive::kPrimInt:
1419 case Primitive::kPrimLong: {
1420 Register dst = OutputRegister(instr);
1421 Register lhs = InputRegisterAt(instr, 0);
1422 Operand rhs = InputOperandAt(instr, 1);
1423 if (rhs.IsImmediate()) {
1424 uint32_t shift_value = (type == Primitive::kPrimInt)
1425 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1426 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1427 if (instr->IsShl()) {
1428 __ Lsl(dst, lhs, shift_value);
1429 } else if (instr->IsShr()) {
1430 __ Asr(dst, lhs, shift_value);
1431 } else {
1432 __ Lsr(dst, lhs, shift_value);
1433 }
1434 } else {
1435 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1436
1437 if (instr->IsShl()) {
1438 __ Lsl(dst, lhs, rhs_reg);
1439 } else if (instr->IsShr()) {
1440 __ Asr(dst, lhs, rhs_reg);
1441 } else {
1442 __ Lsr(dst, lhs, rhs_reg);
1443 }
1444 }
1445 break;
1446 }
1447 default:
1448 LOG(FATAL) << "Unexpected shift operation type " << type;
1449 }
1450}
1451
Alexandre Rames5319def2014-10-23 10:03:10 +01001452void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001453 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001454}
1455
1456void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001457 HandleBinaryOp(instruction);
1458}
1459
1460void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1461 HandleBinaryOp(instruction);
1462}
1463
1464void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1465 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001466}
1467
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001468void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1469 LocationSummary* locations =
1470 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1471 locations->SetInAt(0, Location::RequiresRegister());
1472 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001473 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1474 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1475 } else {
1476 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1477 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001478}
1479
1480void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1481 LocationSummary* locations = instruction->GetLocations();
1482 Primitive::Type type = instruction->GetType();
1483 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001484 Location index = locations->InAt(1);
1485 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001486 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001487 MacroAssembler* masm = GetVIXLAssembler();
1488 UseScratchRegisterScope temps(masm);
1489 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001490
1491 if (index.IsConstant()) {
1492 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001493 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001494 } else {
1495 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001496 __ Add(temp, obj, offset);
1497 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001498 }
1499
Alexandre Rames67555f72014-11-18 10:55:16 +00001500 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001501 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001502
1503 if (type == Primitive::kPrimNot) {
1504 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1505 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001506}
1507
Alexandre Rames5319def2014-10-23 10:03:10 +01001508void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1509 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1510 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001511 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001512}
1513
1514void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001515 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001516 __ Ldr(OutputRegister(instruction),
1517 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001518 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001519}
1520
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001521void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001522 if (instruction->NeedsTypeCheck()) {
1523 LocationSummary* locations =
1524 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001525 InvokeRuntimeCallingConvention calling_convention;
1526 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1527 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1528 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1529 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001530 LocationSummary* locations =
1531 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001532 locations->SetInAt(0, Location::RequiresRegister());
1533 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001534 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1535 locations->SetInAt(2, Location::RequiresFpuRegister());
1536 } else {
1537 locations->SetInAt(2, Location::RequiresRegister());
1538 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001539 }
1540}
1541
1542void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1543 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001544 LocationSummary* locations = instruction->GetLocations();
1545 bool needs_runtime_call = locations->WillCall();
1546
1547 if (needs_runtime_call) {
Roland Levillain4d027112015-07-01 15:41:14 +01001548 // Note: if heap poisoning is enabled, pAputObject takes cares
1549 // of poisoning the reference.
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001550 codegen_->InvokeRuntime(
1551 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001552 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001553 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001554 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001555 CPURegister value = InputCPURegisterAt(instruction, 2);
Roland Levillain4d027112015-07-01 15:41:14 +01001556 CPURegister source = value;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001557 Location index = locations->InAt(1);
1558 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001559 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001560 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001561 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001562 {
1563 // We use a block to end the scratch scope before the write barrier, thus
1564 // freeing the temporary registers so they can be used in `MarkGCCard`.
1565 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001566
Roland Levillain4d027112015-07-01 15:41:14 +01001567 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
1568 DCHECK(value.IsW());
1569 Register temp = temps.AcquireW();
1570 __ Mov(temp, value.W());
1571 GetAssembler()->PoisonHeapReference(temp.W());
1572 source = temp;
1573 }
1574
Alexandre Rames97833a02015-04-16 15:07:12 +01001575 if (index.IsConstant()) {
1576 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1577 destination = HeapOperand(obj, offset);
1578 } else {
1579 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001580 __ Add(temp, obj, offset);
1581 destination = HeapOperand(temp,
1582 XRegisterFrom(index),
1583 LSL,
1584 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001585 }
1586
Roland Levillain4d027112015-07-01 15:41:14 +01001587 codegen_->Store(value_type, source, destination);
Alexandre Rames97833a02015-04-16 15:07:12 +01001588 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001589 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001590 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001591 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001592 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001593 }
1594}
1595
Alexandre Rames67555f72014-11-18 10:55:16 +00001596void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1597 LocationSummary* locations =
1598 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1599 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001600 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001601 if (instruction->HasUses()) {
1602 locations->SetOut(Location::SameAsFirstInput());
1603 }
1604}
1605
1606void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001607 LocationSummary* locations = instruction->GetLocations();
1608 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1609 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001610 codegen_->AddSlowPath(slow_path);
1611
1612 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1613 __ B(slow_path->GetEntryLabel(), hs);
1614}
1615
1616void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1617 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1618 instruction, LocationSummary::kCallOnSlowPath);
1619 locations->SetInAt(0, Location::RequiresRegister());
1620 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001621 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001622}
1623
1624void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001625 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001626 Register obj = InputRegisterAt(instruction, 0);;
1627 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001628 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001629
Alexandre Rames3e69f162014-12-10 10:36:50 +00001630 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1631 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001632 codegen_->AddSlowPath(slow_path);
1633
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001634 // Avoid null check if we know obj is not null.
1635 if (instruction->MustDoNullCheck()) {
1636 __ Cbz(obj, slow_path->GetExitLabel());
1637 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001638 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001639 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01001640 GetAssembler()->MaybeUnpoisonHeapReference(obj_cls.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001641 __ Cmp(obj_cls, cls);
Roland Levillain4d027112015-07-01 15:41:14 +01001642 // The checkcast succeeds if the classes are equal (fast path).
1643 // Otherwise, we need to go into the slow path to check the types.
Alexandre Rames67555f72014-11-18 10:55:16 +00001644 __ B(ne, slow_path->GetEntryLabel());
1645 __ Bind(slow_path->GetExitLabel());
1646}
1647
1648void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1649 LocationSummary* locations =
1650 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1651 locations->SetInAt(0, Location::RequiresRegister());
1652 if (check->HasUses()) {
1653 locations->SetOut(Location::SameAsFirstInput());
1654 }
1655}
1656
1657void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1658 // We assume the class is not null.
1659 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1660 check->GetLoadClass(), check, check->GetDexPc(), true);
1661 codegen_->AddSlowPath(slow_path);
1662 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1663}
1664
Roland Levillain7f63c522015-07-13 15:54:55 +00001665static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1666 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1667 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1668}
1669
Serban Constantinescu02164b32014-11-13 14:05:07 +00001670void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001671 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001672 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1673 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001674 switch (in_type) {
1675 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001676 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001677 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001678 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1679 break;
1680 }
1681 case Primitive::kPrimFloat:
1682 case Primitive::kPrimDouble: {
1683 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001684 locations->SetInAt(1,
1685 IsFloatingPointZeroConstant(compare->InputAt(1))
1686 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1687 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001688 locations->SetOut(Location::RequiresRegister());
1689 break;
1690 }
1691 default:
1692 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1693 }
1694}
1695
1696void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1697 Primitive::Type in_type = compare->InputAt(0)->GetType();
1698
1699 // 0 if: left == right
1700 // 1 if: left > right
1701 // -1 if: left < right
1702 switch (in_type) {
1703 case Primitive::kPrimLong: {
1704 Register result = OutputRegister(compare);
1705 Register left = InputRegisterAt(compare, 0);
1706 Operand right = InputOperandAt(compare, 1);
1707
1708 __ Cmp(left, right);
1709 __ Cset(result, ne);
1710 __ Cneg(result, result, lt);
1711 break;
1712 }
1713 case Primitive::kPrimFloat:
1714 case Primitive::kPrimDouble: {
1715 Register result = OutputRegister(compare);
1716 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001717 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001718 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1719 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001720 __ Fcmp(left, 0.0);
1721 } else {
1722 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1723 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001724 if (compare->IsGtBias()) {
1725 __ Cset(result, ne);
1726 } else {
1727 __ Csetm(result, ne);
1728 }
1729 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001730 break;
1731 }
1732 default:
1733 LOG(FATAL) << "Unimplemented compare type " << in_type;
1734 }
1735}
1736
1737void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1738 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001739
1740 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1741 locations->SetInAt(0, Location::RequiresFpuRegister());
1742 locations->SetInAt(1,
1743 IsFloatingPointZeroConstant(instruction->InputAt(1))
1744 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1745 : Location::RequiresFpuRegister());
1746 } else {
1747 // Integer cases.
1748 locations->SetInAt(0, Location::RequiresRegister());
1749 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1750 }
1751
Alexandre Rames5319def2014-10-23 10:03:10 +01001752 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001753 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001754 }
1755}
1756
1757void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1758 if (!instruction->NeedsMaterialization()) {
1759 return;
1760 }
1761
1762 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001763 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001764 IfCondition if_cond = instruction->GetCondition();
1765 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001766
Roland Levillain7f63c522015-07-13 15:54:55 +00001767 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1768 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1769 if (locations->InAt(1).IsConstant()) {
1770 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1771 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1772 __ Fcmp(lhs, 0.0);
1773 } else {
1774 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1775 }
1776 __ Cset(res, arm64_cond);
1777 if (instruction->IsFPConditionTrueIfNaN()) {
1778 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1779 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1780 } else if (instruction->IsFPConditionFalseIfNaN()) {
1781 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1782 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1783 }
1784 } else {
1785 // Integer cases.
1786 Register lhs = InputRegisterAt(instruction, 0);
1787 Operand rhs = InputOperandAt(instruction, 1);
1788 __ Cmp(lhs, rhs);
1789 __ Cset(res, arm64_cond);
1790 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001791}
1792
1793#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1794 M(Equal) \
1795 M(NotEqual) \
1796 M(LessThan) \
1797 M(LessThanOrEqual) \
1798 M(GreaterThan) \
1799 M(GreaterThanOrEqual)
1800#define DEFINE_CONDITION_VISITORS(Name) \
1801void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1802void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1803FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001804#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001805#undef FOR_EACH_CONDITION_INSTRUCTION
1806
Zheng Xuc6667102015-05-15 16:08:45 +08001807void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1808 DCHECK(instruction->IsDiv() || instruction->IsRem());
1809
1810 LocationSummary* locations = instruction->GetLocations();
1811 Location second = locations->InAt(1);
1812 DCHECK(second.IsConstant());
1813
1814 Register out = OutputRegister(instruction);
1815 Register dividend = InputRegisterAt(instruction, 0);
1816 int64_t imm = Int64FromConstant(second.GetConstant());
1817 DCHECK(imm == 1 || imm == -1);
1818
1819 if (instruction->IsRem()) {
1820 __ Mov(out, 0);
1821 } else {
1822 if (imm == 1) {
1823 __ Mov(out, dividend);
1824 } else {
1825 __ Neg(out, dividend);
1826 }
1827 }
1828}
1829
1830void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1831 DCHECK(instruction->IsDiv() || instruction->IsRem());
1832
1833 LocationSummary* locations = instruction->GetLocations();
1834 Location second = locations->InAt(1);
1835 DCHECK(second.IsConstant());
1836
1837 Register out = OutputRegister(instruction);
1838 Register dividend = InputRegisterAt(instruction, 0);
1839 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001840 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001841 DCHECK(IsPowerOfTwo(abs_imm));
1842 int ctz_imm = CTZ(abs_imm);
1843
1844 UseScratchRegisterScope temps(GetVIXLAssembler());
1845 Register temp = temps.AcquireSameSizeAs(out);
1846
1847 if (instruction->IsDiv()) {
1848 __ Add(temp, dividend, abs_imm - 1);
1849 __ Cmp(dividend, 0);
1850 __ Csel(out, temp, dividend, lt);
1851 if (imm > 0) {
1852 __ Asr(out, out, ctz_imm);
1853 } else {
1854 __ Neg(out, Operand(out, ASR, ctz_imm));
1855 }
1856 } else {
1857 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1858 __ Asr(temp, dividend, bits - 1);
1859 __ Lsr(temp, temp, bits - ctz_imm);
1860 __ Add(out, dividend, temp);
1861 __ And(out, out, abs_imm - 1);
1862 __ Sub(out, out, temp);
1863 }
1864}
1865
1866void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1867 DCHECK(instruction->IsDiv() || instruction->IsRem());
1868
1869 LocationSummary* locations = instruction->GetLocations();
1870 Location second = locations->InAt(1);
1871 DCHECK(second.IsConstant());
1872
1873 Register out = OutputRegister(instruction);
1874 Register dividend = InputRegisterAt(instruction, 0);
1875 int64_t imm = Int64FromConstant(second.GetConstant());
1876
1877 Primitive::Type type = instruction->GetResultType();
1878 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1879
1880 int64_t magic;
1881 int shift;
1882 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1883
1884 UseScratchRegisterScope temps(GetVIXLAssembler());
1885 Register temp = temps.AcquireSameSizeAs(out);
1886
1887 // temp = get_high(dividend * magic)
1888 __ Mov(temp, magic);
1889 if (type == Primitive::kPrimLong) {
1890 __ Smulh(temp, dividend, temp);
1891 } else {
1892 __ Smull(temp.X(), dividend, temp);
1893 __ Lsr(temp.X(), temp.X(), 32);
1894 }
1895
1896 if (imm > 0 && magic < 0) {
1897 __ Add(temp, temp, dividend);
1898 } else if (imm < 0 && magic > 0) {
1899 __ Sub(temp, temp, dividend);
1900 }
1901
1902 if (shift != 0) {
1903 __ Asr(temp, temp, shift);
1904 }
1905
1906 if (instruction->IsDiv()) {
1907 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1908 } else {
1909 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1910 // TODO: Strength reduction for msub.
1911 Register temp_imm = temps.AcquireSameSizeAs(out);
1912 __ Mov(temp_imm, imm);
1913 __ Msub(out, temp, temp_imm, dividend);
1914 }
1915}
1916
1917void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1918 DCHECK(instruction->IsDiv() || instruction->IsRem());
1919 Primitive::Type type = instruction->GetResultType();
1920 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1921
1922 LocationSummary* locations = instruction->GetLocations();
1923 Register out = OutputRegister(instruction);
1924 Location second = locations->InAt(1);
1925
1926 if (second.IsConstant()) {
1927 int64_t imm = Int64FromConstant(second.GetConstant());
1928
1929 if (imm == 0) {
1930 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1931 } else if (imm == 1 || imm == -1) {
1932 DivRemOneOrMinusOne(instruction);
1933 } else if (IsPowerOfTwo(std::abs(imm))) {
1934 DivRemByPowerOfTwo(instruction);
1935 } else {
1936 DCHECK(imm <= -2 || imm >= 2);
1937 GenerateDivRemWithAnyConstant(instruction);
1938 }
1939 } else {
1940 Register dividend = InputRegisterAt(instruction, 0);
1941 Register divisor = InputRegisterAt(instruction, 1);
1942 if (instruction->IsDiv()) {
1943 __ Sdiv(out, dividend, divisor);
1944 } else {
1945 UseScratchRegisterScope temps(GetVIXLAssembler());
1946 Register temp = temps.AcquireSameSizeAs(out);
1947 __ Sdiv(temp, dividend, divisor);
1948 __ Msub(out, temp, divisor, dividend);
1949 }
1950 }
1951}
1952
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001953void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1954 LocationSummary* locations =
1955 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1956 switch (div->GetResultType()) {
1957 case Primitive::kPrimInt:
1958 case Primitive::kPrimLong:
1959 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08001960 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001961 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1962 break;
1963
1964 case Primitive::kPrimFloat:
1965 case Primitive::kPrimDouble:
1966 locations->SetInAt(0, Location::RequiresFpuRegister());
1967 locations->SetInAt(1, Location::RequiresFpuRegister());
1968 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1969 break;
1970
1971 default:
1972 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1973 }
1974}
1975
1976void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1977 Primitive::Type type = div->GetResultType();
1978 switch (type) {
1979 case Primitive::kPrimInt:
1980 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08001981 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001982 break;
1983
1984 case Primitive::kPrimFloat:
1985 case Primitive::kPrimDouble:
1986 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1987 break;
1988
1989 default:
1990 LOG(FATAL) << "Unexpected div type " << type;
1991 }
1992}
1993
Alexandre Rames67555f72014-11-18 10:55:16 +00001994void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1995 LocationSummary* locations =
1996 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1997 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1998 if (instruction->HasUses()) {
1999 locations->SetOut(Location::SameAsFirstInput());
2000 }
2001}
2002
2003void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2004 SlowPathCodeARM64* slow_path =
2005 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2006 codegen_->AddSlowPath(slow_path);
2007 Location value = instruction->GetLocations()->InAt(0);
2008
Alexandre Rames3e69f162014-12-10 10:36:50 +00002009 Primitive::Type type = instruction->GetType();
2010
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002011 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2012 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002013 return;
2014 }
2015
Alexandre Rames67555f72014-11-18 10:55:16 +00002016 if (value.IsConstant()) {
2017 int64_t divisor = Int64ConstantFrom(value);
2018 if (divisor == 0) {
2019 __ B(slow_path->GetEntryLabel());
2020 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002021 // A division by a non-null constant is valid. We don't need to perform
2022 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002023 }
2024 } else {
2025 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2026 }
2027}
2028
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002029void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2030 LocationSummary* locations =
2031 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2032 locations->SetOut(Location::ConstantLocation(constant));
2033}
2034
2035void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2036 UNUSED(constant);
2037 // Will be generated at use site.
2038}
2039
Alexandre Rames5319def2014-10-23 10:03:10 +01002040void LocationsBuilderARM64::VisitExit(HExit* exit) {
2041 exit->SetLocations(nullptr);
2042}
2043
2044void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002045 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002046}
2047
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002048void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2049 LocationSummary* locations =
2050 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2051 locations->SetOut(Location::ConstantLocation(constant));
2052}
2053
2054void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2055 UNUSED(constant);
2056 // Will be generated at use site.
2057}
2058
David Brazdilfc6a86a2015-06-26 10:33:45 +00002059void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002060 DCHECK(!successor->IsExitBlock());
2061 HBasicBlock* block = got->GetBlock();
2062 HInstruction* previous = got->GetPrevious();
2063 HLoopInformation* info = block->GetLoopInformation();
2064
David Brazdil46e2a392015-03-16 17:31:52 +00002065 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002066 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2067 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2068 return;
2069 }
2070 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2071 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2072 }
2073 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002074 __ B(codegen_->GetLabelOf(successor));
2075 }
2076}
2077
David Brazdilfc6a86a2015-06-26 10:33:45 +00002078void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2079 got->SetLocations(nullptr);
2080}
2081
2082void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2083 HandleGoto(got, got->GetSuccessor());
2084}
2085
2086void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2087 try_boundary->SetLocations(nullptr);
2088}
2089
2090void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2091 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2092 if (!successor->IsExitBlock()) {
2093 HandleGoto(try_boundary, successor);
2094 }
2095}
2096
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002097void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2098 vixl::Label* true_target,
2099 vixl::Label* false_target,
2100 vixl::Label* always_true_target) {
2101 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002102 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002103
Serban Constantinescu02164b32014-11-13 14:05:07 +00002104 if (cond->IsIntConstant()) {
2105 int32_t cond_value = cond->AsIntConstant()->GetValue();
2106 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002107 if (always_true_target != nullptr) {
2108 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002109 }
2110 return;
2111 } else {
2112 DCHECK_EQ(cond_value, 0);
2113 }
2114 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002115 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002116 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002117 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002118 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002119 } else {
2120 // The condition instruction has not been materialized, use its inputs as
2121 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002122 Primitive::Type type =
2123 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2124
2125 if (Primitive::IsFloatingPointType(type)) {
2126 // FP compares don't like null false_targets.
2127 if (false_target == nullptr) {
2128 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002129 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002130 FPRegister lhs = InputFPRegisterAt(condition, 0);
2131 if (condition->GetLocations()->InAt(1).IsConstant()) {
2132 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2133 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2134 __ Fcmp(lhs, 0.0);
2135 } else {
2136 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2137 }
2138 if (condition->IsFPConditionTrueIfNaN()) {
2139 __ B(vs, true_target); // VS for unordered.
2140 } else if (condition->IsFPConditionFalseIfNaN()) {
2141 __ B(vs, false_target); // VS for unordered.
2142 }
2143 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002144 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002145 // Integer cases.
2146 Register lhs = InputRegisterAt(condition, 0);
2147 Operand rhs = InputOperandAt(condition, 1);
2148 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2149 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2150 switch (arm64_cond) {
2151 case eq:
2152 __ Cbz(lhs, true_target);
2153 break;
2154 case ne:
2155 __ Cbnz(lhs, true_target);
2156 break;
2157 case lt:
2158 // Test the sign bit and branch accordingly.
2159 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2160 break;
2161 case ge:
2162 // Test the sign bit and branch accordingly.
2163 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2164 break;
2165 default:
2166 // Without the `static_cast` the compiler throws an error for
2167 // `-Werror=sign-promo`.
2168 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2169 }
2170 } else {
2171 __ Cmp(lhs, rhs);
2172 __ B(arm64_cond, true_target);
2173 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002174 }
2175 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002176 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002177 __ B(false_target);
2178 }
2179}
2180
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002181void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2182 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2183 HInstruction* cond = if_instr->InputAt(0);
2184 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2185 locations->SetInAt(0, Location::RequiresRegister());
2186 }
2187}
2188
2189void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2190 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2191 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2192 vixl::Label* always_true_target = true_target;
2193 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2194 if_instr->IfTrueSuccessor())) {
2195 always_true_target = nullptr;
2196 }
2197 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2198 if_instr->IfFalseSuccessor())) {
2199 false_target = nullptr;
2200 }
2201 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2202}
2203
2204void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2205 LocationSummary* locations = new (GetGraph()->GetArena())
2206 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2207 HInstruction* cond = deoptimize->InputAt(0);
2208 DCHECK(cond->IsCondition());
2209 if (cond->AsCondition()->NeedsMaterialization()) {
2210 locations->SetInAt(0, Location::RequiresRegister());
2211 }
2212}
2213
2214void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2215 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2216 DeoptimizationSlowPathARM64(deoptimize);
2217 codegen_->AddSlowPath(slow_path);
2218 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2219 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2220}
2221
Alexandre Rames5319def2014-10-23 10:03:10 +01002222void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002223 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002224}
2225
2226void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002227 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002228}
2229
2230void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002231 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002232}
2233
2234void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002235 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002236}
2237
Alexandre Rames67555f72014-11-18 10:55:16 +00002238void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
2239 LocationSummary::CallKind call_kind =
2240 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2241 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2242 locations->SetInAt(0, Location::RequiresRegister());
2243 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002244 // The output does overlap inputs.
2245 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00002246}
2247
2248void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2249 LocationSummary* locations = instruction->GetLocations();
2250 Register obj = InputRegisterAt(instruction, 0);;
2251 Register cls = InputRegisterAt(instruction, 1);;
2252 Register out = OutputRegister(instruction);
2253
2254 vixl::Label done;
2255
2256 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002257 // Avoid null check if we know `obj` is not null.
2258 if (instruction->MustDoNullCheck()) {
2259 __ Mov(out, 0);
2260 __ Cbz(obj, &done);
2261 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002262
2263 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00002264 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002265 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002266 __ Cmp(out, cls);
2267 if (instruction->IsClassFinal()) {
2268 // Classes must be equal for the instanceof to succeed.
2269 __ Cset(out, eq);
2270 } else {
2271 // If the classes are not equal, we go into a slow path.
2272 DCHECK(locations->OnlyCallsOnSlowPath());
2273 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00002274 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2275 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00002276 codegen_->AddSlowPath(slow_path);
2277 __ B(ne, slow_path->GetEntryLabel());
2278 __ Mov(out, 1);
2279 __ Bind(slow_path->GetExitLabel());
2280 }
2281
2282 __ Bind(&done);
2283}
2284
Alexandre Rames5319def2014-10-23 10:03:10 +01002285void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2286 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2287 locations->SetOut(Location::ConstantLocation(constant));
2288}
2289
2290void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2291 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002292 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002293}
2294
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002295void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2296 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2297 locations->SetOut(Location::ConstantLocation(constant));
2298}
2299
2300void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2301 // Will be generated at use site.
2302 UNUSED(constant);
2303}
2304
Alexandre Rames5319def2014-10-23 10:03:10 +01002305void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002306 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002307 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002308}
2309
Alexandre Rames67555f72014-11-18 10:55:16 +00002310void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2311 HandleInvoke(invoke);
2312}
2313
2314void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2315 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002316 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2317 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2318 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002319 Location receiver = invoke->GetLocations()->InAt(0);
2320 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002321 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002322
2323 // The register ip1 is required to be used for the hidden argument in
2324 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002325 MacroAssembler* masm = GetVIXLAssembler();
2326 UseScratchRegisterScope scratch_scope(masm);
2327 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002328 scratch_scope.Exclude(ip1);
2329 __ Mov(ip1, invoke->GetDexMethodIndex());
2330
2331 // temp = object->GetClass();
2332 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002333 __ Ldr(temp.W(), StackOperandFrom(receiver));
2334 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002335 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002336 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002337 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002338 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002339 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002340 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002341 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002342 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002343 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002344 // lr();
2345 __ Blr(lr);
2346 DCHECK(!codegen_->IsLeafMethod());
2347 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2348}
2349
2350void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002351 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2352 if (intrinsic.TryDispatch(invoke)) {
2353 return;
2354 }
2355
Alexandre Rames67555f72014-11-18 10:55:16 +00002356 HandleInvoke(invoke);
2357}
2358
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002359void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002360 // When we do not run baseline, explicit clinit checks triggered by static
2361 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2362 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002363
Andreas Gampe878d58c2015-01-15 23:24:00 -08002364 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2365 if (intrinsic.TryDispatch(invoke)) {
2366 return;
2367 }
2368
Alexandre Rames67555f72014-11-18 10:55:16 +00002369 HandleInvoke(invoke);
2370}
2371
Andreas Gampe878d58c2015-01-15 23:24:00 -08002372static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2373 if (invoke->GetLocations()->Intrinsified()) {
2374 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2375 intrinsic.Dispatch(invoke);
2376 return true;
2377 }
2378 return false;
2379}
2380
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002381void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00002382 // For better instruction scheduling we load the direct code pointer before the method pointer.
2383 bool direct_code_loaded = false;
2384 switch (invoke->GetCodePtrLocation()) {
2385 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2386 // LR = code address from literal pool with link-time patch.
2387 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
2388 direct_code_loaded = true;
2389 break;
2390 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2391 // LR = invoke->GetDirectCodePtr();
2392 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
2393 direct_code_loaded = true;
2394 break;
2395 default:
2396 break;
2397 }
2398
Andreas Gampe878d58c2015-01-15 23:24:00 -08002399 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00002400 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2401 switch (invoke->GetMethodLoadKind()) {
2402 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2403 // temp = thread->string_init_entrypoint
2404 __ Ldr(XRegisterFrom(temp).X(), MemOperand(tr, invoke->GetStringInitOffset()));
2405 break;
2406 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2407 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2408 break;
2409 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2410 // Load method address from literal pool.
2411 __ Ldr(XRegisterFrom(temp).X(), DeduplicateUint64Literal(invoke->GetMethodAddress()));
2412 break;
2413 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2414 // Load method address from literal pool with a link-time patch.
2415 __ Ldr(XRegisterFrom(temp).X(),
2416 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
2417 break;
2418 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
2419 // Add ADRP with its PC-relative DexCache access patch.
2420 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2421 invoke->GetDexCacheArrayOffset());
2422 vixl::Label* pc_insn_label = &pc_rel_dex_cache_patches_.back().label;
2423 {
2424 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
2425 __ adrp(XRegisterFrom(temp).X(), 0);
2426 }
2427 __ Bind(pc_insn_label); // Bind after ADRP.
2428 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2429 // Add LDR with its PC-relative DexCache access patch.
2430 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2431 invoke->GetDexCacheArrayOffset());
2432 __ Ldr(XRegisterFrom(temp).X(), MemOperand(XRegisterFrom(temp).X(), 0));
2433 __ Bind(&pc_rel_dex_cache_patches_.back().label); // Bind after LDR.
2434 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2435 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01002436 }
Vladimir Marko58155012015-08-19 12:49:41 +00002437 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2438 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2439 Register reg = XRegisterFrom(temp);
2440 Register method_reg;
2441 if (current_method.IsRegister()) {
2442 method_reg = XRegisterFrom(current_method);
2443 } else {
2444 DCHECK(invoke->GetLocations()->Intrinsified());
2445 DCHECK(!current_method.IsValid());
2446 method_reg = reg;
2447 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2448 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00002449
Vladimir Marko58155012015-08-19 12:49:41 +00002450 // temp = current_method->dex_cache_resolved_methods_;
2451 __ Ldr(reg.W(), MemOperand(method_reg.X(),
2452 ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
2453 // temp = temp[index_in_cache];
2454 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2455 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
2456 break;
2457 }
2458 }
2459
2460 switch (invoke->GetCodePtrLocation()) {
2461 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2462 __ Bl(&frame_entry_label_);
2463 break;
2464 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
2465 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
2466 vixl::Label* label = &relative_call_patches_.back().label;
2467 __ Bl(label); // Arbitrarily branch to the instruction after BL, override at link time.
2468 __ Bind(label); // Bind after BL.
2469 break;
2470 }
2471 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2472 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2473 // LR prepared above for better instruction scheduling.
2474 DCHECK(direct_code_loaded);
2475 // lr()
2476 __ Blr(lr);
2477 break;
2478 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2479 // LR = callee_method->entry_point_from_quick_compiled_code_;
2480 __ Ldr(lr, MemOperand(
2481 XRegisterFrom(callee_method).X(),
2482 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
2483 // lr()
2484 __ Blr(lr);
2485 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002486 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002487
Andreas Gampe878d58c2015-01-15 23:24:00 -08002488 DCHECK(!IsLeafMethod());
2489}
2490
Vladimir Marko58155012015-08-19 12:49:41 +00002491void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
2492 DCHECK(linker_patches->empty());
2493 size_t size =
2494 method_patches_.size() +
2495 call_patches_.size() +
2496 relative_call_patches_.size() +
2497 pc_rel_dex_cache_patches_.size();
2498 linker_patches->reserve(size);
2499 for (const auto& entry : method_patches_) {
2500 const MethodReference& target_method = entry.first;
2501 vixl::Literal<uint64_t>* literal = entry.second;
2502 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
2503 target_method.dex_file,
2504 target_method.dex_method_index));
2505 }
2506 for (const auto& entry : call_patches_) {
2507 const MethodReference& target_method = entry.first;
2508 vixl::Literal<uint64_t>* literal = entry.second;
2509 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
2510 target_method.dex_file,
2511 target_method.dex_method_index));
2512 }
2513 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
2514 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location() - 4u,
2515 info.target_method.dex_file,
2516 info.target_method.dex_method_index));
2517 }
2518 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
2519 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location() - 4u,
2520 &info.target_dex_file,
2521 info.pc_insn_label->location() - 4u,
2522 info.element_offset));
2523 }
2524}
2525
2526vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
2527 // Look up the literal for value.
2528 auto lb = uint64_literals_.lower_bound(value);
2529 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
2530 return lb->second;
2531 }
2532 // We don't have a literal for this value, insert a new one.
2533 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
2534 uint64_literals_.PutBefore(lb, value, literal);
2535 return literal;
2536}
2537
2538vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
2539 MethodReference target_method,
2540 MethodToLiteralMap* map) {
2541 // Look up the literal for target_method.
2542 auto lb = map->lower_bound(target_method);
2543 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
2544 return lb->second;
2545 }
2546 // We don't have a literal for this method yet, insert a new one.
2547 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
2548 map->PutBefore(lb, target_method, literal);
2549 return literal;
2550}
2551
2552vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
2553 MethodReference target_method) {
2554 return DeduplicateMethodLiteral(target_method, &method_patches_);
2555}
2556
2557vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
2558 MethodReference target_method) {
2559 return DeduplicateMethodLiteral(target_method, &call_patches_);
2560}
2561
2562
Andreas Gampe878d58c2015-01-15 23:24:00 -08002563void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002564 // When we do not run baseline, explicit clinit checks triggered by static
2565 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2566 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002567
Andreas Gampe878d58c2015-01-15 23:24:00 -08002568 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2569 return;
2570 }
2571
Alexandre Ramesd921d642015-04-16 15:07:16 +01002572 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002573 LocationSummary* locations = invoke->GetLocations();
2574 codegen_->GenerateStaticOrDirectCall(
2575 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002576 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002577}
2578
2579void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002580 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2581 return;
2582 }
2583
Alexandre Rames5319def2014-10-23 10:03:10 +01002584 LocationSummary* locations = invoke->GetLocations();
2585 Location receiver = locations->InAt(0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002586 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2587 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2588 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
Alexandre Rames5319def2014-10-23 10:03:10 +01002589 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002590 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002591
Alexandre Ramesd921d642015-04-16 15:07:16 +01002592 BlockPoolsScope block_pools(GetVIXLAssembler());
2593
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002594 DCHECK(receiver.IsRegister());
2595 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002596 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002597 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames5319def2014-10-23 10:03:10 +01002598 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002599 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002600 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002601 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002602 // lr();
2603 __ Blr(lr);
2604 DCHECK(!codegen_->IsLeafMethod());
2605 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2606}
2607
Alexandre Rames67555f72014-11-18 10:55:16 +00002608void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2609 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2610 : LocationSummary::kNoCall;
2611 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002612 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002613 locations->SetOut(Location::RequiresRegister());
2614}
2615
2616void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2617 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002618 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002619 if (cls->IsReferrersClass()) {
2620 DCHECK(!cls->CanCallRuntime());
2621 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002622 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002623 } else {
2624 DCHECK(cls->CanCallRuntime());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002625 __ Ldr(out, MemOperand(current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002626 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002627 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002628
2629 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2630 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2631 codegen_->AddSlowPath(slow_path);
2632 __ Cbz(out, slow_path->GetEntryLabel());
2633 if (cls->MustGenerateClinitCheck()) {
2634 GenerateClassInitializationCheck(slow_path, out);
2635 } else {
2636 __ Bind(slow_path->GetExitLabel());
2637 }
2638 }
2639}
2640
David Brazdilcb1c0552015-08-04 16:22:25 +01002641static MemOperand GetExceptionTlsAddress() {
2642 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2643}
2644
Alexandre Rames67555f72014-11-18 10:55:16 +00002645void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2646 LocationSummary* locations =
2647 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2648 locations->SetOut(Location::RequiresRegister());
2649}
2650
2651void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01002652 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
2653}
2654
2655void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
2656 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
2657}
2658
2659void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
2660 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00002661}
2662
Alexandre Rames5319def2014-10-23 10:03:10 +01002663void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2664 load->SetLocations(nullptr);
2665}
2666
2667void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2668 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002669 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002670}
2671
Alexandre Rames67555f72014-11-18 10:55:16 +00002672void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2673 LocationSummary* locations =
2674 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002675 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002676 locations->SetOut(Location::RequiresRegister());
2677}
2678
2679void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2680 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2681 codegen_->AddSlowPath(slow_path);
2682
2683 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002684 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002685 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08002686 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002687 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002688 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002689 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002690 __ Cbz(out, slow_path->GetEntryLabel());
2691 __ Bind(slow_path->GetExitLabel());
2692}
2693
Alexandre Rames5319def2014-10-23 10:03:10 +01002694void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2695 local->SetLocations(nullptr);
2696}
2697
2698void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2699 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2700}
2701
2702void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2703 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2704 locations->SetOut(Location::ConstantLocation(constant));
2705}
2706
2707void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2708 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002709 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002710}
2711
Alexandre Rames67555f72014-11-18 10:55:16 +00002712void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2713 LocationSummary* locations =
2714 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2715 InvokeRuntimeCallingConvention calling_convention;
2716 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2717}
2718
2719void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2720 codegen_->InvokeRuntime(instruction->IsEnter()
2721 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2722 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002723 instruction->GetDexPc(),
2724 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002725 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002726}
2727
Alexandre Rames42d641b2014-10-27 14:00:51 +00002728void LocationsBuilderARM64::VisitMul(HMul* mul) {
2729 LocationSummary* locations =
2730 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2731 switch (mul->GetResultType()) {
2732 case Primitive::kPrimInt:
2733 case Primitive::kPrimLong:
2734 locations->SetInAt(0, Location::RequiresRegister());
2735 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002736 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002737 break;
2738
2739 case Primitive::kPrimFloat:
2740 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002741 locations->SetInAt(0, Location::RequiresFpuRegister());
2742 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002743 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002744 break;
2745
2746 default:
2747 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2748 }
2749}
2750
2751void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2752 switch (mul->GetResultType()) {
2753 case Primitive::kPrimInt:
2754 case Primitive::kPrimLong:
2755 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2756 break;
2757
2758 case Primitive::kPrimFloat:
2759 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002760 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002761 break;
2762
2763 default:
2764 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2765 }
2766}
2767
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002768void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2769 LocationSummary* locations =
2770 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2771 switch (neg->GetResultType()) {
2772 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002773 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002774 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002775 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002776 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002777
2778 case Primitive::kPrimFloat:
2779 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002780 locations->SetInAt(0, Location::RequiresFpuRegister());
2781 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002782 break;
2783
2784 default:
2785 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2786 }
2787}
2788
2789void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2790 switch (neg->GetResultType()) {
2791 case Primitive::kPrimInt:
2792 case Primitive::kPrimLong:
2793 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2794 break;
2795
2796 case Primitive::kPrimFloat:
2797 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002798 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002799 break;
2800
2801 default:
2802 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2803 }
2804}
2805
2806void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2807 LocationSummary* locations =
2808 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2809 InvokeRuntimeCallingConvention calling_convention;
2810 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002811 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002812 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002813 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002814 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002815 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002816}
2817
2818void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2819 LocationSummary* locations = instruction->GetLocations();
2820 InvokeRuntimeCallingConvention calling_convention;
2821 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2822 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002823 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002824 // Note: if heap poisoning is enabled, the entry point takes cares
2825 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002826 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002827 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2828 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002829 instruction->GetDexPc(),
2830 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002831 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002832}
2833
Alexandre Rames5319def2014-10-23 10:03:10 +01002834void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2835 LocationSummary* locations =
2836 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2837 InvokeRuntimeCallingConvention calling_convention;
2838 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002839 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01002840 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002841 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002842}
2843
2844void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2845 LocationSummary* locations = instruction->GetLocations();
2846 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2847 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002848 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002849 // Note: if heap poisoning is enabled, the entry point takes cares
2850 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002851 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002852 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2853 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002854 instruction->GetDexPc(),
2855 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002856 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002857}
2858
2859void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2860 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002861 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002862 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002863}
2864
2865void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002866 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002867 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002868 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002869 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002870 break;
2871
2872 default:
2873 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2874 }
2875}
2876
David Brazdil66d126e2015-04-03 16:02:44 +01002877void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2878 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2879 locations->SetInAt(0, Location::RequiresRegister());
2880 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2881}
2882
2883void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002884 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2885}
2886
Alexandre Rames5319def2014-10-23 10:03:10 +01002887void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2888 LocationSummary* locations =
2889 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2890 locations->SetInAt(0, Location::RequiresRegister());
2891 if (instruction->HasUses()) {
2892 locations->SetOut(Location::SameAsFirstInput());
2893 }
2894}
2895
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002896void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002897 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2898 return;
2899 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002900
Alexandre Ramesd921d642015-04-16 15:07:16 +01002901 BlockPoolsScope block_pools(GetVIXLAssembler());
2902 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002903 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2904 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2905}
2906
2907void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002908 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2909 codegen_->AddSlowPath(slow_path);
2910
2911 LocationSummary* locations = instruction->GetLocations();
2912 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002913
2914 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002915}
2916
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002917void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2918 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2919 GenerateImplicitNullCheck(instruction);
2920 } else {
2921 GenerateExplicitNullCheck(instruction);
2922 }
2923}
2924
Alexandre Rames67555f72014-11-18 10:55:16 +00002925void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2926 HandleBinaryOp(instruction);
2927}
2928
2929void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2930 HandleBinaryOp(instruction);
2931}
2932
Alexandre Rames3e69f162014-12-10 10:36:50 +00002933void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2934 LOG(FATAL) << "Unreachable";
2935}
2936
2937void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2938 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2939}
2940
Alexandre Rames5319def2014-10-23 10:03:10 +01002941void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2942 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2943 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2944 if (location.IsStackSlot()) {
2945 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2946 } else if (location.IsDoubleStackSlot()) {
2947 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2948 }
2949 locations->SetOut(location);
2950}
2951
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002952void InstructionCodeGeneratorARM64::VisitParameterValue(
2953 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002954 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002955}
2956
2957void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
2958 LocationSummary* locations =
2959 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002960 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002961}
2962
2963void InstructionCodeGeneratorARM64::VisitCurrentMethod(
2964 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
2965 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01002966}
2967
2968void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2969 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2970 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2971 locations->SetInAt(i, Location::Any());
2972 }
2973 locations->SetOut(Location::Any());
2974}
2975
2976void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002977 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002978 LOG(FATAL) << "Unreachable";
2979}
2980
Serban Constantinescu02164b32014-11-13 14:05:07 +00002981void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002982 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002983 LocationSummary::CallKind call_kind =
2984 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002985 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2986
2987 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002988 case Primitive::kPrimInt:
2989 case Primitive::kPrimLong:
2990 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002991 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002992 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2993 break;
2994
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002995 case Primitive::kPrimFloat:
2996 case Primitive::kPrimDouble: {
2997 InvokeRuntimeCallingConvention calling_convention;
2998 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2999 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3000 locations->SetOut(calling_convention.GetReturnLocation(type));
3001
3002 break;
3003 }
3004
Serban Constantinescu02164b32014-11-13 14:05:07 +00003005 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003006 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00003007 }
3008}
3009
3010void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
3011 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003012
Serban Constantinescu02164b32014-11-13 14:05:07 +00003013 switch (type) {
3014 case Primitive::kPrimInt:
3015 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08003016 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003017 break;
3018 }
3019
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003020 case Primitive::kPrimFloat:
3021 case Primitive::kPrimDouble: {
3022 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3023 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003024 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003025 break;
3026 }
3027
Serban Constantinescu02164b32014-11-13 14:05:07 +00003028 default:
3029 LOG(FATAL) << "Unexpected rem type " << type;
3030 }
3031}
3032
Calin Juravle27df7582015-04-17 19:12:31 +01003033void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3034 memory_barrier->SetLocations(nullptr);
3035}
3036
3037void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3038 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3039}
3040
Alexandre Rames5319def2014-10-23 10:03:10 +01003041void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
3042 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3043 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003044 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01003045}
3046
3047void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003048 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003049 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003050}
3051
3052void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
3053 instruction->SetLocations(nullptr);
3054}
3055
3056void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003057 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003058 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003059}
3060
Serban Constantinescu02164b32014-11-13 14:05:07 +00003061void LocationsBuilderARM64::VisitShl(HShl* shl) {
3062 HandleShift(shl);
3063}
3064
3065void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
3066 HandleShift(shl);
3067}
3068
3069void LocationsBuilderARM64::VisitShr(HShr* shr) {
3070 HandleShift(shr);
3071}
3072
3073void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
3074 HandleShift(shr);
3075}
3076
Alexandre Rames5319def2014-10-23 10:03:10 +01003077void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
3078 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3079 Primitive::Type field_type = store->InputAt(1)->GetType();
3080 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003081 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01003082 case Primitive::kPrimBoolean:
3083 case Primitive::kPrimByte:
3084 case Primitive::kPrimChar:
3085 case Primitive::kPrimShort:
3086 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003087 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01003088 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3089 break;
3090
3091 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003092 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01003093 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3094 break;
3095
3096 default:
3097 LOG(FATAL) << "Unimplemented local type " << field_type;
3098 }
3099}
3100
3101void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003102 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01003103}
3104
3105void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003106 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003107}
3108
3109void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003110 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003111}
3112
Alexandre Rames67555f72014-11-18 10:55:16 +00003113void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003114 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003115}
3116
3117void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003118 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00003119}
3120
3121void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003122 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003123}
3124
Alexandre Rames67555f72014-11-18 10:55:16 +00003125void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003126 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003127}
3128
3129void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3130 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3131}
3132
3133void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003134 HBasicBlock* block = instruction->GetBlock();
3135 if (block->GetLoopInformation() != nullptr) {
3136 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3137 // The back edge will generate the suspend check.
3138 return;
3139 }
3140 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3141 // The goto will generate the suspend check.
3142 return;
3143 }
3144 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003145}
3146
3147void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3148 temp->SetLocations(nullptr);
3149}
3150
3151void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3152 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003153 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003154}
3155
Alexandre Rames67555f72014-11-18 10:55:16 +00003156void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3157 LocationSummary* locations =
3158 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3159 InvokeRuntimeCallingConvention calling_convention;
3160 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3161}
3162
3163void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3164 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003165 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003166 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003167}
3168
3169void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3170 LocationSummary* locations =
3171 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3172 Primitive::Type input_type = conversion->GetInputType();
3173 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003174 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003175 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3176 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3177 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3178 }
3179
Alexandre Rames542361f2015-01-29 16:57:31 +00003180 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003181 locations->SetInAt(0, Location::RequiresFpuRegister());
3182 } else {
3183 locations->SetInAt(0, Location::RequiresRegister());
3184 }
3185
Alexandre Rames542361f2015-01-29 16:57:31 +00003186 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003187 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3188 } else {
3189 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3190 }
3191}
3192
3193void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3194 Primitive::Type result_type = conversion->GetResultType();
3195 Primitive::Type input_type = conversion->GetInputType();
3196
3197 DCHECK_NE(input_type, result_type);
3198
Alexandre Rames542361f2015-01-29 16:57:31 +00003199 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003200 int result_size = Primitive::ComponentSize(result_type);
3201 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003202 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003203 Register output = OutputRegister(conversion);
3204 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003205 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3206 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01003207 } else if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
3208 // 'int' values are used directly as W registers, discarding the top
3209 // bits, so we don't need to sign-extend and can just perform a move.
3210 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
3211 // top 32 bits of the target register. We theoretically could leave those
3212 // bits unchanged, but we would have to make sure that no code uses a
3213 // 32bit input value as a 64bit value assuming that the top 32 bits are
3214 // zero.
3215 __ Mov(output.W(), source.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00003216 } else if ((result_type == Primitive::kPrimChar) ||
3217 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3218 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003219 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003220 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003221 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003222 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003223 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003224 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003225 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3226 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003227 } else if (Primitive::IsFloatingPointType(result_type) &&
3228 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003229 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3230 } else {
3231 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3232 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003233 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003234}
Alexandre Rames67555f72014-11-18 10:55:16 +00003235
Serban Constantinescu02164b32014-11-13 14:05:07 +00003236void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3237 HandleShift(ushr);
3238}
3239
3240void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3241 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003242}
3243
3244void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3245 HandleBinaryOp(instruction);
3246}
3247
3248void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3249 HandleBinaryOp(instruction);
3250}
3251
Calin Juravleb1498f62015-02-16 13:13:29 +00003252void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3253 // Nothing to do, this should be removed during prepare for register allocator.
3254 UNUSED(instruction);
3255 LOG(FATAL) << "Unreachable";
3256}
3257
3258void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3259 // Nothing to do, this should be removed during prepare for register allocator.
3260 UNUSED(instruction);
3261 LOG(FATAL) << "Unreachable";
3262}
3263
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003264void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3265 DCHECK(codegen_->IsBaseline());
3266 LocationSummary* locations =
3267 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3268 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3269}
3270
3271void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3272 DCHECK(codegen_->IsBaseline());
3273 // Will be generated at use site.
3274}
3275
Alexandre Rames67555f72014-11-18 10:55:16 +00003276#undef __
3277#undef QUICK_ENTRY_POINT
3278
Alexandre Rames5319def2014-10-23 10:03:10 +01003279} // namespace arm64
3280} // namespace art